diff --git a/plugins/USBqemu/PS2Edefs.h b/plugins/USBqemu/PS2Edefs.h deleted file mode 100644 index fbb0c2eb06..0000000000 --- a/plugins/USBqemu/PS2Edefs.h +++ /dev/null @@ -1,809 +0,0 @@ -#ifndef __PS2EDEFS_H__ -#define __PS2EDEFS_H__ - -/* - * PS2E Definitions v0.6.2 (beta) - * - * Author: linuzappz@hotmail.com - * shadowpcsx2@yahoo.gr - * florinsasu@hotmail.com - */ - -/* - Notes: - * Since this is still beta things may change. - - * OSflags: - __LINUX__ (linux OS) - __WIN32__ (win32 OS) - - * common return values (for ie. GSinit): - 0 - success - -1 - error - - * reserved keys: - F1 to F10 are reserved for the emulator - - * plugins should NOT change the current - working directory. - (on win32, add flag OFN_NOCHANGEDIR for - GetOpenFileName) - -*/ - -#include "PS2Etypes.h" - -#ifdef __LINUX__ -#define CALLBACK -#else -#include -#endif - -/* common defines */ - -#if defined(GSdefs) || defined(PADdefs) || defined(SIOdefs) || \ - defined(SPU2defs) || defined(CDVDdefs) || defined(DEV9defs) || \ - defined(USBdefs) || defined(FWdefs) -#define COMMONdefs -#endif - -// PS2EgetLibType returns (may be OR'd) -#define PS2E_LT_GS 0x01 -#define PS2E_LT_PAD 0x02 // -=[ OBSOLETE ]=- -#define PS2E_LT_SPU2 0x04 -#define PS2E_LT_CDVD 0x08 -#define PS2E_LT_DEV9 0x10 -#define PS2E_LT_USB 0x20 -#define PS2E_LT_FW 0x40 -#define PS2E_LT_SIO 0x80 - -// PS2EgetLibVersion2 (high 16 bits) -#define PS2E_GS_VERSION 0x0006 -#define PS2E_PAD_VERSION 0x0002 // -=[ OBSOLETE ]=- -#define PS2E_SPU2_VERSION 0x0005 -#define PS2E_CDVD_VERSION 0x0005 -#define PS2E_DEV9_VERSION 0x0003 -#define PS2E_USB_VERSION 0x0003 -#define PS2E_FW_VERSION 0x0002 -#define PS2E_SIO_VERSION 0x0001 -#ifdef COMMONdefs - -u32 CALLBACK PS2EgetLibType(void); -u32 CALLBACK PS2EgetLibVersion2(u32 type); -char* CALLBACK PS2EgetLibName(void); - -#endif - -// key values: -/* key values must be OS dependant: - win32: the VK_XXX will be used (WinUser) - linux: the XK_XXX will be used (XFree86) -*/ - -// event values: -#define KEYPRESS 1 -#define KEYRELEASE 2 - -typedef struct { - u32 key; - u32 event; -} keyEvent; - -// plugin types -#define SIO_TYPE_PAD 0x00000001 -#define SIO_TYPE_MTAP 0x00000004 -#define SIO_TYPE_RM 0x00000040 -#define SIO_TYPE_MC 0x00000100 - -typedef int (CALLBACK * SIOchangeSlotCB)(int slot); - -typedef struct { - u8 ctrl:4; // control and mode bits - u8 mode:4; // control and mode bits - u8 trackNum; // current track number (1 to 99) - u8 trackIndex; // current index within track (0 to 99) - u8 trackM; // current minute location on the disc (BCD encoded) - u8 trackS; // current sector location on the disc (BCD encoded) - u8 trackF; // current frame location on the disc (BCD encoded) - u8 pad; // unused - u8 discM; // current minute offset from first track (BCD encoded) - u8 discS; // current sector offset from first track (BCD encoded) - u8 discF; // current frame offset from first track (BCD encoded) -} cdvdSubQ; - -typedef struct { // NOT bcd coded - u32 lsn; - u8 type; -} cdvdTD; - -typedef struct { - u8 strack; //number of the first track (usually 1) - u8 etrack; //number of the last track -} cdvdTN; - -// CDVDreadTrack mode values: -#define CDVD_MODE_2352 0 // full 2352 bytes -#define CDVD_MODE_2340 1 // skip sync (12) bytes -#define CDVD_MODE_2328 2 // skip sync+head+sub (24) bytes -#define CDVD_MODE_2048 3 // skip sync+head+sub (24) bytes -#define CDVD_MODE_2368 4 // full 2352 bytes + 16 subq - -// CDVDgetDiskType returns: -#define CDVD_TYPE_ILLEGAL 0xff // Illegal Disc -#define CDVD_TYPE_DVDV 0xfe // DVD Video -#define CDVD_TYPE_CDDA 0xfd // Audio CD -#define CDVD_TYPE_PS2DVD 0x14 // PS2 DVD -#define CDVD_TYPE_PS2CDDA 0x13 // PS2 CD (with audio) -#define CDVD_TYPE_PS2CD 0x12 // PS2 CD -#define CDVD_TYPE_PSCDDA 0x11 // PS CD (with audio) -#define CDVD_TYPE_PSCD 0x10 // PS CD -#define CDVD_TYPE_UNKNOWN 0x05 // Unknown -#define CDVD_TYPE_DETCTDVDD 0x04 // Detecting Dvd Dual Sided -#define CDVD_TYPE_DETCTDVDS 0x03 // Detecting Dvd Single Sided -#define CDVD_TYPE_DETCTCD 0x02 // Detecting Cd -#define CDVD_TYPE_DETCT 0x01 // Detecting -#define CDVD_TYPE_NODISC 0x00 // No Disc - -// CDVDgetTrayStatus returns: -#define CDVD_TRAY_CLOSE 0x00 -#define CDVD_TRAY_OPEN 0x01 - -// cdvdTD.type (track types for cds) -#define CDVD_AUDIO_TRACK 0x01 -#define CDVD_MODE1_TRACK 0x41 -#define CDVD_MODE2_TRACK 0x61 - -#define CDVD_AUDIO_MASK 0x00 -#define CDVD_DATA_MASK 0x40 -// CDROM_DATA_TRACK 0x04 //do not enable this! (from linux kernel) - -typedef void (*DEV9callback)(int cycles); -typedef int (*DEV9handler)(void); - -typedef void (*USBcallback)(int cycles); -typedef int (*USBhandler)(void); - -// freeze modes: -#define FREEZE_LOAD 0 -#define FREEZE_SAVE 1 -#define FREEZE_SIZE 2 - -typedef struct { - char name[8]; - void *common; -} GSdriverInfo; - -#ifdef __WIN32__ -typedef struct { // unsupported values must be set to zero - HWND hWnd; - HMENU hMenu; - HWND hStatusWnd; -} winInfo; -#endif - -/* GS plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef GSdefs - -// basic funcs - -s32 CALLBACK GSinit(); -s32 CALLBACK GSopen(void *pDsp, char *Title, int multithread); -void CALLBACK GSclose(); -void CALLBACK GSshutdown(); -void CALLBACK GSvsync(int field); -void CALLBACK GSgifTransfer1(u32 *pMem, u32 addr); -void CALLBACK GSgifTransfer2(u32 *pMem, u32 size); -void CALLBACK GSgifTransfer3(u32 *pMem, u32 size); -void CALLBACK GSgifSoftReset(u32 mask); -void CALLBACK GSreadFIFO(u64 *mem); -void CALLBACK GSreadFIFO2(u64 *mem, int qwc); - -// extended funcs - -// GSkeyEvent gets called when there is a keyEvent from the PAD plugin -void CALLBACK GSkeyEvent(keyEvent *ev); -void CALLBACK GSchangeSaveState(int, const char* filename); -void CALLBACK GSmakeSnapshot(char *path); -void CALLBACK GSmakeSnapshot2(char *pathname, int* snapdone, int savejpg); -void CALLBACK GSirqCallback(void (*callback)()); -void CALLBACK GSprintf(int timeout, char *fmt, ...); -void CALLBACK GSsetBaseMem(void*); -void CALLBACK GSsetGameCRC(int); - -// controls frame skipping in the GS, if this routine isn't present, frame skipping won't be done -void CALLBACK GSsetFrameSkip(int frameskip); - -void CALLBACK GSreset(); -void CALLBACK GSwriteCSR(u32 value); -void CALLBACK GSgetDriverInfo(GSdriverInfo *info); -#ifdef __WIN32__ -s32 CALLBACK GSsetWindowInfo(winInfo *info); -#endif -s32 CALLBACK GSfreeze(int mode, freezeData *data); -void CALLBACK GSconfigure(); -void CALLBACK GSabout(); -s32 CALLBACK GStest(); - -#endif - -/* PAD plugin API -=[ OBSOLETE ]=- */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef PADdefs - -// basic funcs - -s32 CALLBACK PADinit(u32 flags); -s32 CALLBACK PADopen(void *pDsp); -void CALLBACK PADclose(); -void CALLBACK PADshutdown(); -// PADkeyEvent is called every vsync (return NULL if no event) -keyEvent* CALLBACK PADkeyEvent(); -u8 CALLBACK PADstartPoll(int pad); -u8 CALLBACK PADpoll(u8 value); -// returns: 1 if supported pad1 -// 2 if supported pad2 -// 3 if both are supported -u32 CALLBACK PADquery(); - -// extended funcs - -void CALLBACK PADgsDriverInfo(GSdriverInfo *info); -void CALLBACK PADconfigure(); -void CALLBACK PADabout(); -s32 CALLBACK PADtest(); - -#endif - -/* SIO plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef SIOdefs - -// basic funcs - -s32 CALLBACK SIOinit(u32 port, u32 slot, SIOchangeSlotCB f); -s32 CALLBACK SIOopen(void *pDsp); -void CALLBACK SIOclose(); -void CALLBACK SIOshutdown(); -u8 CALLBACK SIOstartPoll(u8 value); -u8 CALLBACK SIOpoll(u8 value); -// returns: SIO_TYPE_{PAD,MTAP,RM,MC} -u32 CALLBACK SIOquery(); - -// extended funcs - -void CALLBACK SIOconfigure(); -void CALLBACK SIOabout(); -s32 CALLBACK SIOtest(); - -#endif - -/* SPU2 plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef SPU2defs - -// basic funcs - -s32 CALLBACK SPU2init(); -s32 CALLBACK SPU2open(void *pDsp); -void CALLBACK SPU2close(); -void CALLBACK SPU2shutdown(); -void CALLBACK SPU2write(u32 mem, u16 value); -u16 CALLBACK SPU2read(u32 mem); -void CALLBACK SPU2readDMA4Mem(u16 *pMem, int size); -void CALLBACK SPU2writeDMA4Mem(u16 *pMem, int size); -void CALLBACK SPU2interruptDMA4(); -void CALLBACK SPU2readDMA7Mem(u16* pMem, int size); -void CALLBACK SPU2writeDMA7Mem(u16 *pMem, int size); -void CALLBACK SPU2interruptDMA7(); -u32 CALLBACK SPU2ReadMemAddr(int core); -void CALLBACK SPU2WriteMemAddr(int core,u32 value); -void CALLBACK SPU2irqCallback(void (*SPU2callback)(),void (*DMA4callback)(),void (*DMA7callback)()); -// extended funcs - -void CALLBACK SPU2async(u32 cycles); -s32 CALLBACK SPU2freeze(int mode, freezeData *data); -void CALLBACK SPU2configure(); -void CALLBACK SPU2about(); -s32 CALLBACK SPU2test(); - -#endif - -/* CDVD plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef CDVDdefs - -// basic funcs - -s32 CALLBACK CDVDinit(); -s32 CALLBACK CDVDopen(); -void CALLBACK CDVDclose(); -void CALLBACK CDVDshutdown(); -s32 CALLBACK CDVDreadTrack(u32 lsn, int mode); - -// return can be NULL (for async modes) -u8* CALLBACK CDVDgetBuffer(); - -s32 CALLBACK CDVDreadSubQ(u32 lsn, cdvdSubQ* subq);//read subq from disc (only cds have subq data) -s32 CALLBACK CDVDgetTN(cdvdTN *Buffer); //disk information -s32 CALLBACK CDVDgetTD(u8 Track, cdvdTD *Buffer); //track info: min,sec,frame,type -s32 CALLBACK CDVDgetTOC(void* toc); //gets ps2 style toc from disc -s32 CALLBACK CDVDgetDiskType(); //CDVD_TYPE_xxxx -s32 CALLBACK CDVDgetTrayStatus(); //CDVD_TRAY_xxxx -s32 CALLBACK CDVDctrlTrayOpen(); //open disc tray -s32 CALLBACK CDVDctrlTrayClose(); //close disc tray - -// extended funcs - -void CALLBACK CDVDconfigure(); -void CALLBACK CDVDabout(); -s32 CALLBACK CDVDtest(); -void CALLBACK CDVDnewDiskCB(void (*callback)()); - -#endif - -/* DEV9 plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef DEV9defs - -// basic funcs - -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -s32 CALLBACK DEV9init(); -s32 CALLBACK DEV9open(void *pDsp); -void CALLBACK DEV9close(); -void CALLBACK DEV9shutdown(); -u8 CALLBACK DEV9read8(u32 addr); -u16 CALLBACK DEV9read16(u32 addr); -u32 CALLBACK DEV9read32(u32 addr); -void CALLBACK DEV9write8(u32 addr, u8 value); -void CALLBACK DEV9write16(u32 addr, u16 value); -void CALLBACK DEV9write32(u32 addr, u32 value); -void CALLBACK DEV9readDMA8Mem(u32 *pMem, int size); -void CALLBACK DEV9writeDMA8Mem(u32 *pMem, int size); -// cycles = IOP cycles before calling callback, -// if callback returns 1 the irq is triggered, else not -void CALLBACK DEV9irqCallback(DEV9callback callback); -DEV9handler CALLBACK DEV9irqHandler(void); - -// extended funcs - -s32 CALLBACK DEV9freeze(int mode, freezeData *data); -void CALLBACK DEV9configure(); -void CALLBACK DEV9about(); -s32 CALLBACK DEV9test(); - -#endif - -/* USB plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef USBdefs - -// basic funcs - -s32 CALLBACK USBinit(); -s32 CALLBACK USBopen(void *pDsp); -void CALLBACK USBclose(); -void CALLBACK USBshutdown(); -u8 CALLBACK USBread8(u32 addr); -u16 CALLBACK USBread16(u32 addr); -u32 CALLBACK USBread32(u32 addr); -void CALLBACK USBwrite8(u32 addr, u8 value); -void CALLBACK USBwrite16(u32 addr, u16 value); -void CALLBACK USBwrite32(u32 addr, u32 value); -void CALLBACK USBasync(u32 cycles); - -// cycles = IOP cycles before calling callback, -// if callback returns 1 the irq is triggered, else not -void CALLBACK USBirqCallback(USBcallback callback); -USBhandler CALLBACK USBirqHandler(void); -void CALLBACK USBsetRAM(void *mem); - -// extended funcs - -s32 CALLBACK USBfreeze(int mode, freezeData *data); -void CALLBACK USBconfigure(); -void CALLBACK USBabout(); -s32 CALLBACK USBtest(); - -#endif - -/* FW plugin API */ - -// if this file is included with this define -// the next api will not be skipped by the compiler -#ifdef FWdefs -// basic funcs - -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -s32 CALLBACK FWinit(); -s32 CALLBACK FWopen(void *pDsp); -void CALLBACK FWclose(); -void CALLBACK FWshutdown(); -u32 CALLBACK FWread32(u32 addr); -void CALLBACK FWwrite32(u32 addr, u32 value); -void CALLBACK FWirqCallback(void (*callback)()); - -// extended funcs - -s32 CALLBACK FWfreeze(int mode, freezeData *data); -void CALLBACK FWconfigure(); -void CALLBACK FWabout(); -s32 CALLBACK FWtest(); -#endif - -// might be useful for emulators -#ifdef PLUGINtypedefs - -typedef u32 (CALLBACK* _PS2EgetLibType)(void); -typedef u32 (CALLBACK* _PS2EgetLibVersion2)(u32 type); -typedef char*(CALLBACK* _PS2EgetLibName)(void); - -// GS -// NOTE: GSreadFIFOX/GSwriteCSR functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -typedef s32 (CALLBACK* _GSinit)(); -typedef s32 (CALLBACK* _GSopen)(void *pDsp, char *Title, int multithread); -typedef void (CALLBACK* _GSclose)(); -typedef void (CALLBACK* _GSshutdown)(); -typedef void (CALLBACK* _GSvsync)(int field); -typedef void (CALLBACK* _GSgifTransfer1)(u32 *pMem, u32 addr); -typedef void (CALLBACK* _GSgifTransfer2)(u32 *pMem, u32 size); -typedef void (CALLBACK* _GSgifTransfer3)(u32 *pMem, u32 size); -typedef void (CALLBACK* _GSgifSoftReset)(u32 mask); -typedef void (CALLBACK* _GSreadFIFO)(u64 *pMem); -typedef void (CALLBACK* _GSreadFIFO2)(u64 *pMem, int qwc); - -typedef void (CALLBACK* _GSkeyEvent)(keyEvent* ev); -typedef void (CALLBACK* _GSchangeSaveState)(int, const char* filename); -typedef void (CALLBACK* _GSirqCallback)(void (*callback)()); -typedef void (CALLBACK* _GSprintf)(int timeout, char *fmt, ...); -typedef void (CALLBACK* _GSsetBaseMem)(void*); -typedef void (CALLBACK* _GSsetGameCRC)(int); -typedef void (CALLBACK* _GSsetFrameSkip)(int frameskip); -typedef void (CALLBACK* _GSreset)(); -typedef void (CALLBACK* _GSwriteCSR)(u32 value); -typedef void (CALLBACK* _GSgetDriverInfo)(GSdriverInfo *info); -#ifdef __WIN32__ -typedef s32 (CALLBACK* _GSsetWindowInfo)(winInfo *info); -#endif -typedef void (CALLBACK* _GSmakeSnapshot)(char *path); -typedef void (CALLBACK* _GSmakeSnapshot2)(char *path, int*, int); -typedef s32 (CALLBACK* _GSfreeze)(int mode, freezeData *data); -typedef void (CALLBACK* _GSconfigure)(); -typedef s32 (CALLBACK* _GStest)(); -typedef void (CALLBACK* _GSabout)(); - -// PAD -typedef s32 (CALLBACK* _PADinit)(u32 flags); -typedef s32 (CALLBACK* _PADopen)(void *pDsp); -typedef void (CALLBACK* _PADclose)(); -typedef void (CALLBACK* _PADshutdown)(); -typedef keyEvent* (CALLBACK* _PADkeyEvent)(); -typedef u8 (CALLBACK* _PADstartPoll)(int pad); -typedef u8 (CALLBACK* _PADpoll)(u8 value); -typedef u32 (CALLBACK* _PADquery)(); - -typedef void (CALLBACK* _PADgsDriverInfo)(GSdriverInfo *info); -typedef void (CALLBACK* _PADconfigure)(); -typedef s32 (CALLBACK* _PADtest)(); -typedef void (CALLBACK* _PADabout)(); - -// SIO -typedef s32 (CALLBACK* _SIOinit)(u32 port, u32 slot, SIOchangeSlotCB f); -typedef s32 (CALLBACK* _SIOopen)(void *pDsp); -typedef void (CALLBACK* _SIOclose)(); -typedef void (CALLBACK* _SIOshutdown)(); -typedef u8 (CALLBACK* _SIOstartPoll)(u8 value); -typedef u8 (CALLBACK* _SIOpoll)(u8 value); -typedef u32 (CALLBACK* _SIOquery)(); - -typedef void (CALLBACK* _SIOconfigure)(); -typedef s32 (CALLBACK* _SIOtest)(); -typedef void (CALLBACK* _SIOabout)(); - -// SPU2 -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -typedef s32 (CALLBACK* _SPU2init)(); -typedef s32 (CALLBACK* _SPU2open)(void *pDsp); -typedef void (CALLBACK* _SPU2close)(); -typedef void (CALLBACK* _SPU2shutdown)(); -typedef void (CALLBACK* _SPU2write)(u32 mem, u16 value); -typedef u16 (CALLBACK* _SPU2read)(u32 mem); -typedef void (CALLBACK* _SPU2readDMA4Mem)(u16 *pMem, int size); -typedef void (CALLBACK* _SPU2writeDMA4Mem)(u16 *pMem, int size); -typedef void (CALLBACK* _SPU2interruptDMA4)(); -typedef void (CALLBACK* _SPU2readDMA7Mem)(u16 *pMem, int size); -typedef void (CALLBACK* _SPU2writeDMA7Mem)(u16 *pMem, int size); -typedef void (CALLBACK* _SPU2interruptDMA7)(); -typedef void (CALLBACK* _SPU2irqCallback)(void (*SPU2callback)(),void (*DMA4callback)(),void (*DMA7callback)()); -typedef u32 (CALLBACK* _SPU2ReadMemAddr)(int core); -typedef void (CALLBACK* _SPU2WriteMemAddr)(int core,u32 value); -typedef void (CALLBACK* _SPU2async)(u32 cycles); -typedef s32 (CALLBACK* _SPU2freeze)(int mode, freezeData *data); -typedef void (CALLBACK* _SPU2configure)(); -typedef s32 (CALLBACK* _SPU2test)(); -typedef void (CALLBACK* _SPU2about)(); - -// CDVD -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -typedef s32 (CALLBACK* _CDVDinit)(); -typedef s32 (CALLBACK* _CDVDopen)(); -typedef void (CALLBACK* _CDVDclose)(); -typedef void (CALLBACK* _CDVDshutdown)(); -typedef s32 (CALLBACK* _CDVDreadTrack)(u32 lsn, int mode); -typedef u8* (CALLBACK* _CDVDgetBuffer)(); -typedef s32 (CALLBACK* _CDVDreadSubQ)(u32 lsn, cdvdSubQ* subq); -typedef s32 (CALLBACK* _CDVDgetTN)(cdvdTN *Buffer); -typedef s32 (CALLBACK* _CDVDgetTD)(u8 Track, cdvdTD *Buffer); -typedef s32 (CALLBACK* _CDVDgetTOC)(void* toc); -typedef s32 (CALLBACK* _CDVDgetDiskType)(); -typedef s32 (CALLBACK* _CDVDgetTrayStatus)(); -typedef s32 (CALLBACK* _CDVDctrlTrayOpen)(); -typedef s32 (CALLBACK* _CDVDctrlTrayClose)(); - -typedef void (CALLBACK* _CDVDconfigure)(); -typedef s32 (CALLBACK* _CDVDtest)(); -typedef void (CALLBACK* _CDVDabout)(); -typedef void (CALLBACK* _CDVDnewDiskCB)(void (*callback)()); - -// DEV9 -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -typedef s32 (CALLBACK* _DEV9init)(); -typedef s32 (CALLBACK* _DEV9open)(void *pDsp); -typedef void (CALLBACK* _DEV9close)(); -typedef void (CALLBACK* _DEV9shutdown)(); -typedef u8 (CALLBACK* _DEV9read8)(u32 mem); -typedef u16 (CALLBACK* _DEV9read16)(u32 mem); -typedef u32 (CALLBACK* _DEV9read32)(u32 mem); -typedef void (CALLBACK* _DEV9write8)(u32 mem, u8 value); -typedef void (CALLBACK* _DEV9write16)(u32 mem, u16 value); -typedef void (CALLBACK* _DEV9write32)(u32 mem, u32 value); -typedef void (CALLBACK* _DEV9readDMA8Mem)(u32 *pMem, int size); -typedef void (CALLBACK* _DEV9writeDMA8Mem)(u32 *pMem, int size); -typedef void (CALLBACK* _DEV9irqCallback)(DEV9callback callback); -typedef DEV9handler (CALLBACK* _DEV9irqHandler)(void); - -typedef s32 (CALLBACK* _DEV9freeze)(int mode, freezeData *data); -typedef void (CALLBACK* _DEV9configure)(); -typedef s32 (CALLBACK* _DEV9test)(); -typedef void (CALLBACK* _DEV9about)(); - -// USB -// NOTE: The read/write functions CANNOT use XMM/MMX regs -// If you want to use them, need to save and restore current ones -typedef s32 (CALLBACK* _USBinit)(); -typedef s32 (CALLBACK* _USBopen)(void *pDsp); -typedef void (CALLBACK* _USBclose)(); -typedef void (CALLBACK* _USBshutdown)(); -typedef u8 (CALLBACK* _USBread8)(u32 mem); -typedef u16 (CALLBACK* _USBread16)(u32 mem); -typedef u32 (CALLBACK* _USBread32)(u32 mem); -typedef void (CALLBACK* _USBwrite8)(u32 mem, u8 value); -typedef void (CALLBACK* _USBwrite16)(u32 mem, u16 value); -typedef void (CALLBACK* _USBwrite32)(u32 mem, u32 value); -typedef void (CALLBACK* _USBirqCallback)(USBcallback callback); -typedef USBhandler (CALLBACK* _USBirqHandler)(void); -typedef void (CALLBACK* _USBsetRAM)(void *mem); -typedef void (CALLBACK* _USBasync)(u32 cycles); - -typedef s32 (CALLBACK* _USBfreeze)(int mode, freezeData *data); -typedef void (CALLBACK* _USBconfigure)(); -typedef s32 (CALLBACK* _USBtest)(); -typedef void (CALLBACK* _USBabout)(); - -//FW -typedef s32 (CALLBACK* _FWinit)(); -typedef s32 (CALLBACK* _FWopen)(void *pDsp); -typedef void (CALLBACK* _FWclose)(); -typedef void (CALLBACK* _FWshutdown)(); -typedef u32 (CALLBACK* _FWread32)(u32 mem); -typedef void (CALLBACK* _FWwrite32)(u32 mem, u32 value); -typedef void (CALLBACK* _FWirqCallback)(void (*callback)()); - -typedef s32 (CALLBACK* _FWfreeze)(int mode, freezeData *data); -typedef void (CALLBACK* _FWconfigure)(); -typedef s32 (CALLBACK* _FWtest)(); -typedef void (CALLBACK* _FWabout)(); - -#endif - -#ifdef PLUGINfuncs - -// GS -_GSinit GSinit; -_GSopen GSopen; -_GSclose GSclose; -_GSshutdown GSshutdown; -_GSvsync GSvsync; -_GSgifTransfer1 GSgifTransfer1; -_GSgifTransfer2 GSgifTransfer2; -_GSgifTransfer3 GSgifTransfer3; -_GSgifSoftReset GSgifSoftReset; -_GSreadFIFO GSreadFIFO; -_GSreadFIFO2 GSreadFIFO2; - -_GSkeyEvent GSkeyEvent; -_GSchangeSaveState GSchangeSaveState; -_GSmakeSnapshot GSmakeSnapshot; -_GSmakeSnapshot2 GSmakeSnapshot2; -_GSirqCallback GSirqCallback; -_GSprintf GSprintf; -_GSsetBaseMem GSsetBaseMem; -_GSsetGameCRC GSsetGameCRC; -_GSsetFrameSkip GSsetFrameSkip; -_GSreset GSreset; -_GSwriteCSR GSwriteCSR; -_GSgetDriverInfo GSgetDriverInfo; -#ifdef __WIN32__ -_GSsetWindowInfo GSsetWindowInfo; -#endif -_GSfreeze GSfreeze; -_GSconfigure GSconfigure; -_GStest GStest; -_GSabout GSabout; - -// PAD1 -_PADinit PAD1init; -_PADopen PAD1open; -_PADclose PAD1close; -_PADshutdown PAD1shutdown; -_PADkeyEvent PAD1keyEvent; -_PADstartPoll PAD1startPoll; -_PADpoll PAD1poll; -_PADquery PAD1query; - -_PADgsDriverInfo PAD1gsDriverInfo; -_PADconfigure PAD1configure; -_PADtest PAD1test; -_PADabout PAD1about; - -// PAD2 -_PADinit PAD2init; -_PADopen PAD2open; -_PADclose PAD2close; -_PADshutdown PAD2shutdown; -_PADkeyEvent PAD2keyEvent; -_PADstartPoll PAD2startPoll; -_PADpoll PAD2poll; -_PADquery PAD2query; - -_PADgsDriverInfo PAD2gsDriverInfo; -_PADconfigure PAD2configure; -_PADtest PAD2test; -_PADabout PAD2about; - -// SIO[2] -_SIOinit SIOinit[2][9]; -_SIOopen SIOopen[2][9]; -_SIOclose SIOclose[2][9]; -_SIOshutdown SIOshutdown[2][9]; -_SIOstartPoll SIOstartPoll[2][9]; -_SIOpoll SIOpoll[2][9]; -_SIOquery SIOquery[2][9]; - -_SIOconfigure SIOconfigure[2][9]; -_SIOtest SIOtest[2][9]; -_SIOabout SIOabout[2][9]; - -// SPU2 -_SPU2init SPU2init; -_SPU2open SPU2open; -_SPU2close SPU2close; -_SPU2shutdown SPU2shutdown; -_SPU2write SPU2write; -_SPU2read SPU2read; -_SPU2readDMA4Mem SPU2readDMA4Mem; -_SPU2writeDMA4Mem SPU2writeDMA4Mem; -_SPU2interruptDMA4 SPU2interruptDMA4; -_SPU2readDMA7Mem SPU2readDMA7Mem; -_SPU2writeDMA7Mem SPU2writeDMA7Mem; -_SPU2interruptDMA7 SPU2interruptDMA7; -_SPU2ReadMemAddr SPU2ReadMemAddr; -_SPU2WriteMemAddr SPU2WriteMemAddr; -_SPU2irqCallback SPU2irqCallback; - -_SPU2async SPU2async; -_SPU2freeze SPU2freeze; -_SPU2configure SPU2configure; -_SPU2test SPU2test; -_SPU2about SPU2about; - -// CDVD -_CDVDinit CDVDinit; -_CDVDopen CDVDopen; -_CDVDclose CDVDclose; -_CDVDshutdown CDVDshutdown; -_CDVDreadTrack CDVDreadTrack; -_CDVDgetBuffer CDVDgetBuffer; -_CDVDreadSubQ CDVDreadSubQ; -_CDVDgetTN CDVDgetTN; -_CDVDgetTD CDVDgetTD; -_CDVDgetTOC CDVDgetTOC; -_CDVDgetDiskType CDVDgetDiskType; -_CDVDgetTrayStatus CDVDgetTrayStatus; -_CDVDctrlTrayOpen CDVDctrlTrayOpen; -_CDVDctrlTrayClose CDVDctrlTrayClose; - -_CDVDconfigure CDVDconfigure; -_CDVDtest CDVDtest; -_CDVDabout CDVDabout; -_CDVDnewDiskCB CDVDnewDiskCB; - -// DEV9 -_DEV9init DEV9init; -_DEV9open DEV9open; -_DEV9close DEV9close; -_DEV9shutdown DEV9shutdown; -_DEV9read8 DEV9read8; -_DEV9read16 DEV9read16; -_DEV9read32 DEV9read32; -_DEV9write8 DEV9write8; -_DEV9write16 DEV9write16; -_DEV9write32 DEV9write32; -_DEV9readDMA8Mem DEV9readDMA8Mem; -_DEV9writeDMA8Mem DEV9writeDMA8Mem; -_DEV9irqCallback DEV9irqCallback; -_DEV9irqHandler DEV9irqHandler; - -_DEV9configure DEV9configure; -_DEV9freeze DEV9freeze; -_DEV9test DEV9test; -_DEV9about DEV9about; - -// USB -_USBinit USBinit; -_USBopen USBopen; -_USBclose USBclose; -_USBshutdown USBshutdown; -_USBread8 USBread8; -_USBread16 USBread16; -_USBread32 USBread32; -_USBwrite8 USBwrite8; -_USBwrite16 USBwrite16; -_USBwrite32 USBwrite32; -_USBirqCallback USBirqCallback; -_USBirqHandler USBirqHandler; -_USBsetRAM USBsetRAM; -_USBasync USBasync; - -_USBconfigure USBconfigure; -_USBfreeze USBfreeze; -_USBtest USBtest; -_USBabout USBabout; - -// FW -_FWinit FWinit; -_FWopen FWopen; -_FWclose FWclose; -_FWshutdown FWshutdown; -_FWread32 FWread32; -_FWwrite32 FWwrite32; -_FWirqCallback FWirqCallback; - -_FWconfigure FWconfigure; -_FWfreeze FWfreeze; -_FWtest FWtest; -_FWabout FWabout; -#endif - -#endif /* __PS2EDEFS_H__ */ diff --git a/plugins/USBqemu/PS2Etypes.h b/plugins/USBqemu/PS2Etypes.h deleted file mode 100644 index 194f6558f1..0000000000 --- a/plugins/USBqemu/PS2Etypes.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef __PS2ETYPES_H__ -#define __PS2ETYPES_H__ - -#ifndef ARRAYSIZE -#define ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0])) -#endif - -#if defined (__linux__) // some distributions are lower case -#define __LINUX__ -#endif - -// Basic types -#if defined(_WIN32) - -typedef __int8 s8; -typedef __int16 s16; -typedef __int32 s32; -typedef __int64 s64; - -typedef unsigned __int8 u8; -typedef unsigned __int16 u16; -typedef unsigned __int32 u32; -typedef unsigned __int64 u64; - -#if defined(__x86_64__) -typedef u64 uptr; -#else -typedef u32 uptr; -#endif - -#define PCSX2_ALIGNED16(x) __declspec(align(16)) x - -#else - -typedef char s8; -typedef short s16; -typedef int s32; -typedef long long s64; - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned long long u64; - -#if defined(__x86_64__) -typedef u64 uptr; -#else -typedef u32 uptr; -#endif - -#ifdef __LINUX__ -typedef union _LARGE_INTEGER -{ - long long QuadPart; -} LARGE_INTEGER; -#endif - -#if defined(__MINGW32__) -#define PCSX2_ALIGNED16(x) __declspec(align(16)) x -#else -#define PCSX2_ALIGNED16(x) x __attribute((aligned(16))) -#endif - -#ifndef __forceinline -#define __forceinline inline -#endif - -#endif - -typedef struct { - int size; - s8 *data; -} freezeData; - -#endif /* __PS2ETYPES_H__ */ diff --git a/plugins/USBqemu/USB.cpp b/plugins/USBqemu/USB.cpp index b02285939d..8a03032be4 100644 --- a/plugins/USBqemu/USB.cpp +++ b/plugins/USBqemu/USB.cpp @@ -44,10 +44,26 @@ u8 *ram; usbStruct usb; USBcallback _USBirq; FILE *usbLog; -int64_t usb_frame_time; -int64_t usb_bit_time; +int64_t usb_frame_time=0; +int64_t usb_bit_time=0; -s64 clocks; +s64 clocks=0; +s64 remaining=0; + +int64_t get_ticks_per_sec() +{ + return PSXCLK; +} + +void __Log(char *fmt, ...) { + va_list list; + + if (!conf.Log) return; + + va_start(list, fmt); + vfprintf(usbLog, fmt, list); + va_end(list); +} u32 CALLBACK PS2EgetLibType() { return PS2E_LT_USB; @@ -61,16 +77,6 @@ u32 CALLBACK PS2EgetLibVersion2(u32 type) { return (version<<16) | (revision<<8) | build; } -void __Log(char *fmt, ...) { - va_list list; - - if (!conf.Log) return; - - va_start(list, fmt); - vfprintf(usbLog, fmt, list); - va_end(list); -} - void USBirq(int cycles) { USB_LOG("USBirq.\n"); @@ -84,7 +90,7 @@ s32 CALLBACK USBinit() { { usbLog = fopen("logs/usbLog.txt", "w"); setvbuf(usbLog, NULL, _IONBF, 0); - USB_LOG("usblinuz plugin version %d,%d\n",revision,build); + USB_LOG("USBqemu plugin version %d,%d\n",revision,build); USB_LOG("USBinit\n"); } @@ -96,12 +102,13 @@ s32 CALLBACK USBinit() { void CALLBACK USBshutdown() { - qemu_ohci->rhport[0].port.dev->handle_destroy(qemu_ohci->rhport[0].port.dev); + qemu_ohci->rhport[0].port.dev->info->handle_destroy(qemu_ohci->rhport[0].port.dev); free(qemu_ohci); #ifdef _DEBUG - fclose(usbLog); + if(usbLog) + fclose(usbLog); #endif } @@ -140,7 +147,7 @@ u16 CALLBACK USBread16(u32 addr) { u32 CALLBACK USBread32(u32 addr) { u32 hard; - hard=ohci_mem_read(qemu_ohci,addr); + hard=ohci_mem_read(qemu_ohci,addr - qemu_ohci->mem); USB_LOG("* Known 32bit read at address %lx: %lx\n", addr, hard); @@ -157,7 +164,7 @@ void CALLBACK USBwrite16(u32 addr, u16 value) { void CALLBACK USBwrite32(u32 addr, u32 value) { USB_LOG("* Known 32bit write at address %lx value %lx\n", addr, value); - ohci_mem_write(qemu_ohci,addr,value); + ohci_mem_write(qemu_ohci,addr - qemu_ohci->mem, value); } void CALLBACK USBirqCallback(USBcallback callback) { @@ -209,20 +216,31 @@ s32 CALLBACK USBfreeze(int mode, freezeData *data) { return 0; } -void CALLBACK USBasync(u32 cycles) +void CALLBACK USBasync(u32 _cycles) { - clocks+=cycles; + remaining += _cycles; + clocks += remaining; if(qemu_ohci->eof_timer>0) { - while(cycles>=qemu_ohci->eof_timer) + while(remaining>=qemu_ohci->eof_timer) { - cycles-=qemu_ohci->eof_timer; + remaining-=qemu_ohci->eof_timer; qemu_ohci->eof_timer=0; ohci_frame_boundary(qemu_ohci); } - if((cycles>0)&&(qemu_ohci->eof_timer>0)) - qemu_ohci->eof_timer-=cycles; + 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; + } } + //if(qemu_ohci->eof_timer <= 0) + //{ + // ohci_frame_boundary(qemu_ohci); + //} } s32 CALLBACK USBtest() { diff --git a/plugins/USBqemu/USB.h b/plugins/USBqemu/USB.h index a83f6f3e08..ba2fc287ce 100644 --- a/plugins/USBqemu/USB.h +++ b/plugins/USBqemu/USB.h @@ -100,11 +100,15 @@ typedef struct OHCIPort { typedef uint32_t target_phys_addr_t; typedef struct { - target_phys_addr_t mem_base; + //USBBus bus; + //qemu_irq irq; + enum ohci_type type; int mem; int num_ports; + const char *name; - uint64_t eof_timer; + //QEMUTimer *eof_timer; + int64_t eof_timer; int64_t sof_time; /* OHCI state */ @@ -135,6 +139,23 @@ typedef struct { uint32_t rhdesc_a, rhdesc_b; uint32_t rhstatus; OHCIPort rhport[OHCI_MAX_PORTS]; + + /* PXA27x Non-OHCI events */ + uint32_t hstatus; + uint32_t hmask; + uint32_t hreset; + uint32_t htest; + + /* SM501 local memory offset */ + target_phys_addr_t localmem_base; + + /* Active packets. */ + uint32_t old_ctl; + USBPacket usb_packet; + uint8_t usb_buf[8192]; + uint32_t async_td; + int async_complete; + } OHCIState; /* Host Controller Communications Area */ @@ -144,6 +165,20 @@ struct ohci_hcca { uint32_t done; }; + +extern int64_t usb_frame_time; +extern int64_t usb_bit_time; + +enum ohci_type { + OHCI_TYPE_PCI, + OHCI_TYPE_PXA, + OHCI_TYPE_SM501, +}; + +int64_t get_ticks_per_sec(); + +static void ohci_bus_stop(OHCIState *ohci); + /* Bitfields for the first word of an Endpoint Desciptor. */ #define OHCI_ED_FA_SHIFT 0 #define OHCI_ED_FA_MASK (0x7f< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.sln b/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.sln deleted file mode 100644 index 41cf887a29..0000000000 --- a/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.sln +++ /dev/null @@ -1,26 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 9.00 -# Visual Studio 2005 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "USBlinuz", "USBlinuz_vc2005_x64.vcproj", "{BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.ActiveCfg = Debug|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.Build.0 = Debug|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.ActiveCfg = Debug|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.Build.0 = Debug|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.ActiveCfg = Release|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.Build.0 = Release|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.ActiveCfg = Release|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.vcproj b/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.vcproj deleted file mode 100644 index c89a2f65c0..0000000000 --- a/plugins/USBqemu/Win32/USBlinuz_vc2005_x64.vcproj +++ /dev/null @@ -1,669 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/USBqemu/Win32/USBlinuz_vc2008b2.sln b/plugins/USBqemu/Win32/USBlinuz_vc2008b2.sln deleted file mode 100644 index e13082a8b5..0000000000 --- a/plugins/USBqemu/Win32/USBlinuz_vc2008b2.sln +++ /dev/null @@ -1,25 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "USBlinuz", "USBlinuz_vc2008b2.vcproj", "{BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.ActiveCfg = Debug|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.Build.0 = Debug|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.ActiveCfg = Debug|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.Build.0 = Debug|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.ActiveCfg = Release|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.Build.0 = Release|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.ActiveCfg = Release|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/plugins/USBqemu/Win32/USBlinuz_vc2008b2.vcproj b/plugins/USBqemu/Win32/USBlinuz_vc2008b2.vcproj deleted file mode 100644 index 09bd52e717..0000000000 --- a/plugins/USBqemu/Win32/USBlinuz_vc2008b2.vcproj +++ /dev/null @@ -1,669 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/USBqemu/Win32/USBlinuz.aps b/plugins/USBqemu/Win32/USBqemu.aps similarity index 100% rename from plugins/USBqemu/Win32/USBlinuz.aps rename to plugins/USBqemu/Win32/USBqemu.aps diff --git a/plugins/USBqemu/Win32/USBlinuz.def b/plugins/USBqemu/Win32/USBqemu.def similarity index 73% rename from plugins/USBqemu/Win32/USBlinuz.def rename to plugins/USBqemu/Win32/USBqemu.def index 187d81a501..13ae53fa30 100644 --- a/plugins/USBqemu/Win32/USBlinuz.def +++ b/plugins/USBqemu/Win32/USBqemu.def @@ -1,7 +1,3 @@ -; USBlinuz.def : Declares the module parameters for the DLL. - -LIBRARY "USBqemu" -DESCRIPTION 'USBqemu Driver' EXPORTS ; Explicit exports can go here diff --git a/plugins/USBqemu/Win32/USBlinuz.rc b/plugins/USBqemu/Win32/USBqemu.rc similarity index 100% rename from plugins/USBqemu/Win32/USBlinuz.rc rename to plugins/USBqemu/Win32/USBqemu.rc diff --git a/plugins/USBqemu/Win32/USBlinuz_vc2005.sln b/plugins/USBqemu/Win32/USBqemu.sln similarity index 52% rename from plugins/USBqemu/Win32/USBlinuz_vc2005.sln rename to plugins/USBqemu/Win32/USBqemu.sln index 9fb2025f86..0ebe0428f4 100644 --- a/plugins/USBqemu/Win32/USBlinuz_vc2005.sln +++ b/plugins/USBqemu/Win32/USBqemu.sln @@ -1,23 +1,17 @@ -Microsoft Visual Studio Solution File, Format Version 9.00 -# Visual Studio 2005 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "USBlinuz", "USBlinuz_vc2005.vcproj", "{BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}" +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "USBqemu", "USBqemu.vcxproj", "{BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 Release|Win32 = Release|Win32 - Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.ActiveCfg = Debug|Win32 {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|Win32.Build.0 = Debug|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.ActiveCfg = Debug|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Debug|x64.Build.0 = Debug|x64 {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.ActiveCfg = Release|Win32 {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|Win32.Build.0 = Release|Win32 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.ActiveCfg = Release|x64 - {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/plugins/USBqemu/Win32/USBqemu.vcxproj b/plugins/USBqemu/Win32/USBqemu.vcxproj new file mode 100644 index 0000000000..d0e1561c6e --- /dev/null +++ b/plugins/USBqemu/Win32/USBqemu.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {BF7B81A5-E348-4F7C-A69F-F74C8EEEAD70} + USBlinuz + Win32Proj + + + + DynamicLibrary + MultiByte + true + + + DynamicLibrary + MultiByte + false + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.40219.1 + H:\pcsx2-trunk\bin\plugins\ + Debug\ + true + $(SolutionDir)bin\ + true + AllRules.ruleset + + + AllRules.ruleset + + + $(ProjectName)-dbg + + + + Disabled + false + ..\;h:\pcsx2-trunk\common\include\;%(AdditionalIncludeDirectories) + __WIN32__;_DEBUG;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + NotUsing + Level4 + EditAndContinue + + + USBqemu.def + true + Windows + + + MachineX86 + + + + + MaxSpeed + AnySuitable + true + Speed + true + false + ..\;h:\pcsx2-trunk\common\include\;%(AdditionalIncludeDirectories) + __WIN32__;NDEBUG;%(PreprocessorDefinitions) + false + Default + MultiThreadedDebug + NotSet + + + Level4 + + + + + USBqemu.def + true + Windows + false + + + $(OutDir)USBlinuz.lib + MachineX86 + + + + + + + + + true + true + + + true + true + + + + + true + true + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/USBqemu/Win32/USBqemu.vcxproj.filters b/plugins/USBqemu/Win32/USBqemu.vcxproj.filters new file mode 100644 index 0000000000..adaa0bfa2f --- /dev/null +++ b/plugins/USBqemu/Win32/USBqemu.vcxproj.filters @@ -0,0 +1,85 @@ + + + + + {dfc571b5-5df6-405b-9920-d273ad48d332} + cpp;c;cxx;def;odl;idl;hpj;bat;asm + + + {fe40e0a3-a73a-44b8-a8ee-3d9591f91f0b} + + + {2f1d0e2b-ba2b-4bd2-8bbd-63b3e4909ff3} + h;hpp;hxx;hm;inl;inc + + + {d1b11b29-e230-4fb1-84e1-795a3aafe8dd} + + + {5468eb4e-6580-45d9-9432-a8ac8083abf9} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + Source Files\qemu-usb + + + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files\qemu-usb + + + Header Files\qemu-usb + + + Header Files\qemu-usb + + + + + Resource Files + + + \ No newline at end of file diff --git a/plugins/USBqemu/qemu-usb/usb-base.cpp b/plugins/USBqemu/qemu-usb/usb-base.cpp index 34aac5fa9b..03d8a38300 100644 --- a/plugins/USBqemu/qemu-usb/usb-base.cpp +++ b/plugins/USBqemu/qemu-usb/usb-base.cpp @@ -2,7 +2,9 @@ * QEMU USB emulation * * Copyright (c) 2005 Fabrice Bellard - * + * + * 2008 Generic packet handler rewrite by Max Krasnyansky + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -22,6 +24,7 @@ * THE SOFTWARE. */ #include "vl.h" +//#include "usb.h" void usb_attach(USBPort *port, USBDevice *dev) { @@ -29,150 +32,174 @@ void usb_attach(USBPort *port, USBDevice *dev) } /**********************/ + /* generic USB device helpers (you are not forced to use them when writing your USB device driver, but they help handling the - protocol) + protocol) */ #define SETUP_STATE_IDLE 0 #define SETUP_STATE_DATA 1 #define SETUP_STATE_ACK 2 -int usb_generic_handle_packet(USBDevice *s, int pid, - uint8_t devaddr, uint8_t devep, - uint8_t *data, int len) +static int do_token_setup(USBDevice *s, USBPacket *p) { - int l, ret = 0; + int request, value, index; + int ret = 0; - switch(pid) { + if (p->len != 8) + return USB_RET_STALL; + + memcpy(s->setup_buf, p->data, 8); + s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; + s->setup_index = 0; + + request = (s->setup_buf[0] << 8) | s->setup_buf[1]; + value = (s->setup_buf[3] << 8) | s->setup_buf[2]; + index = (s->setup_buf[5] << 8) | s->setup_buf[4]; + + if (s->setup_buf[0] & USB_DIR_IN) { + ret = s->info->handle_control(s, request, value, index, + s->setup_len, s->data_buf); + if (ret < 0) + return ret; + + if (ret < s->setup_len) + s->setup_len = ret; + s->setup_state = SETUP_STATE_DATA; + } else { + if (s->setup_len == 0) + s->setup_state = SETUP_STATE_ACK; + else + s->setup_state = SETUP_STATE_DATA; + } + + return ret; +} + +static int do_token_in(USBDevice *s, USBPacket *p) +{ + int request, value, index; + int ret = 0; + + if (p->devep != 0) + return s->info->handle_data(s, p); + + request = (s->setup_buf[0] << 8) | s->setup_buf[1]; + value = (s->setup_buf[3] << 8) | s->setup_buf[2]; + index = (s->setup_buf[5] << 8) | s->setup_buf[4]; + + switch(s->setup_state) { + case SETUP_STATE_ACK: + if (!(s->setup_buf[0] & USB_DIR_IN)) { + s->setup_state = SETUP_STATE_IDLE; + ret = s->info->handle_control(s, request, value, index, + s->setup_len, s->data_buf); + if (ret > 0) + return 0; + return ret; + } + + /* return 0 byte */ + return 0; + + case SETUP_STATE_DATA: + if (s->setup_buf[0] & USB_DIR_IN) { + int len = s->setup_len - s->setup_index; + if (len > p->len) + len = p->len; + memcpy(p->data, s->data_buf + s->setup_index, len); + s->setup_index += len; + if (s->setup_index >= s->setup_len) + s->setup_state = SETUP_STATE_ACK; + return len; + } + + s->setup_state = SETUP_STATE_IDLE; + return USB_RET_STALL; + + default: + return USB_RET_STALL; + } +} + +static int do_token_out(USBDevice *s, USBPacket *p) +{ + if (p->devep != 0) + return s->info->handle_data(s, p); + + switch(s->setup_state) { + case SETUP_STATE_ACK: + if (s->setup_buf[0] & USB_DIR_IN) { + s->setup_state = SETUP_STATE_IDLE; + /* transfer OK */ + } else { + /* ignore additional output */ + } + return 0; + + case SETUP_STATE_DATA: + if (!(s->setup_buf[0] & USB_DIR_IN)) { + int len = s->setup_len - s->setup_index; + if (len > p->len) + len = p->len; + memcpy(s->data_buf + s->setup_index, p->data, len); + s->setup_index += len; + if (s->setup_index >= s->setup_len) + s->setup_state = SETUP_STATE_ACK; + return len; + } + + s->setup_state = SETUP_STATE_IDLE; + return USB_RET_STALL; + + default: + return USB_RET_STALL; + } +} + +/* + * Generic packet handler. + * Called by the HC (host controller). + * + * Returns length of the transaction or one of the USB_RET_XXX codes. + */ +int usb_generic_handle_packet(USBDevice *s, USBPacket *p) +{ + switch(p->pid) { case USB_MSG_ATTACH: s->state = USB_STATE_ATTACHED; - break; + return 0; + case USB_MSG_DETACH: s->state = USB_STATE_NOTATTACHED; - break; + return 0; + case USB_MSG_RESET: s->remote_wakeup = 0; s->addr = 0; s->state = USB_STATE_DEFAULT; - s->handle_reset(s); - break; - case USB_TOKEN_SETUP: - if (s->state < USB_STATE_DEFAULT || devaddr != s->addr) - return USB_RET_NODEV; - if (len != 8) - goto fail; - memcpy(s->setup_buf, data, 8); - s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; - s->setup_index = 0; - if (s->setup_buf[0] & USB_DIR_IN) { - ret = s->handle_control(s, - (s->setup_buf[0] << 8) | s->setup_buf[1], - (s->setup_buf[3] << 8) | s->setup_buf[2], - (s->setup_buf[5] << 8) | s->setup_buf[4], - s->setup_len, - s->data_buf); - if (ret < 0) - return ret; - if (ret < s->setup_len) - s->setup_len = ret; - s->setup_state = SETUP_STATE_DATA; - } else { - if (s->setup_len == 0) - s->setup_state = SETUP_STATE_ACK; - else - s->setup_state = SETUP_STATE_DATA; - } - break; - case USB_TOKEN_IN: - if (s->state < USB_STATE_DEFAULT || devaddr != s->addr) - return USB_RET_NODEV; - switch(devep) { - case 0: - switch(s->setup_state) { - case SETUP_STATE_ACK: - if (!(s->setup_buf[0] & USB_DIR_IN)) { - s->setup_state = SETUP_STATE_IDLE; - ret = s->handle_control(s, - (s->setup_buf[0] << 8) | s->setup_buf[1], - (s->setup_buf[3] << 8) | s->setup_buf[2], - (s->setup_buf[5] << 8) | s->setup_buf[4], - s->setup_len, - s->data_buf); - if (ret > 0) - ret = 0; - } else { - /* return 0 byte */ - } - break; - case SETUP_STATE_DATA: - if (s->setup_buf[0] & USB_DIR_IN) { - l = s->setup_len - s->setup_index; - if (l > len) - l = len; - memcpy(data, s->data_buf + s->setup_index, l); - s->setup_index += l; - if (s->setup_index >= s->setup_len) - s->setup_state = SETUP_STATE_ACK; - ret = l; - } else { - s->setup_state = SETUP_STATE_IDLE; - goto fail; - } - break; - default: - goto fail; - } - break; - default: - ret = s->handle_data(s, pid, devep, data, len); - break; - } - break; - case USB_TOKEN_OUT: - if (s->state < USB_STATE_DEFAULT || devaddr != s->addr) - return USB_RET_NODEV; - switch(devep) { - case 0: - switch(s->setup_state) { - case SETUP_STATE_ACK: - if (s->setup_buf[0] & USB_DIR_IN) { - s->setup_state = SETUP_STATE_IDLE; - /* transfer OK */ - } else { - /* ignore additionnal output */ - } - break; - case SETUP_STATE_DATA: - if (!(s->setup_buf[0] & USB_DIR_IN)) { - l = s->setup_len - s->setup_index; - if (l > len) - l = len; - memcpy(s->data_buf + s->setup_index, data, l); - s->setup_index += l; - if (s->setup_index >= s->setup_len) - s->setup_state = SETUP_STATE_ACK; - ret = l; - } else { - s->setup_state = SETUP_STATE_IDLE; - goto fail; - } - break; - default: - goto fail; - } - break; - default: - ret = s->handle_data(s, pid, devep, data, len); - break; - } - break; - default: - fail: - ret = USB_RET_STALL; - break; + s->info->handle_reset(s); + return 0; + } + + /* Rest of the PIDs must match our address */ + if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) + return USB_RET_NODEV; + + switch (p->pid) { + case USB_TOKEN_SETUP: + return do_token_setup(s, p); + + case USB_TOKEN_IN: + return do_token_in(s, p); + + case USB_TOKEN_OUT: + return do_token_out(s, p); + + default: + return USB_RET_STALL; } - return ret; } /* XXX: fix overflow */ @@ -191,3 +218,14 @@ int set_usb_string(uint8_t *buf, const char *str) } return q - buf; } + +/* Send an internal message to a USB device. */ +void usb_send_msg(USBDevice *dev, int msg) +{ + USBPacket p; + memset(&p, 0, sizeof(p)); + p.pid = msg; + dev->info->handle_packet(dev, &p); + + /* This _must_ be synchronous */ +} diff --git a/plugins/USBqemu/qemu-usb/usb-hid.cpp b/plugins/USBqemu/qemu-usb/usb-hid.cpp index 04b78fc0f0..6abb629937 100644 --- a/plugins/USBqemu/qemu-usb/usb-hid.cpp +++ b/plugins/USBqemu/qemu-usb/usb-hid.cpp @@ -1,8 +1,9 @@ /* * QEMU USB HID devices - * + * * Copyright (c) 2005 Fabrice Bellard - * + * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com) + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -21,31 +22,60 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "vl.h" +#include "hw.h" +#include "console.h" +#include "usb.h" /* HID interface requests */ #define GET_REPORT 0xa101 #define GET_IDLE 0xa102 #define GET_PROTOCOL 0xa103 +#define SET_REPORT 0x2109 #define SET_IDLE 0x210a #define SET_PROTOCOL 0x210b -#define USB_MOUSE 1 -#define USB_TABLET 2 +/* HID descriptor types */ +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHY 0x23 + +#define USB_MOUSE 1 +#define USB_TABLET 2 +#define USB_KEYBOARD 3 typedef struct USBMouseState { - USBDevice dev; int dx, dy, dz, buttons_state; int x, y; - int kind; int mouse_grabbed; + QEMUPutMouseEntry *eh_entry; } USBMouseState; +typedef struct USBKeyboardState { + uint16_t modifiers; + uint8_t leds; + uint8_t key[16]; + int keys; +} USBKeyboardState; + +typedef struct USBHIDState { + USBDevice dev; + union { + USBMouseState ptr; + USBKeyboardState kbd; + }; + int kind; + int protocol; + uint8_t idle; + int changed; + void *datain_opaque; + void (*datain)(void *); +} USBHIDState; + /* mostly the same values as the Bochs USB Mouse device */ static const uint8_t qemu_mouse_dev_descriptor[] = { 0x12, /* u8 bLength; */ 0x01, /* u8 bDescriptorType; Device */ - 0x10, 0x00, /* u16 bcdUSB; v1.0 */ + 0x00, 0x01, /* u16 bcdUSB; v1.0 */ 0x00, /* u8 bDeviceClass; */ 0x00, /* u8 bDeviceSubClass; */ @@ -70,13 +100,13 @@ static const uint8_t qemu_mouse_config_descriptor[] = { 0x01, /* u8 bNumInterfaces; (1) */ 0x01, /* u8 bConfigurationValue; */ 0x04, /* u8 iConfiguration; */ - 0xa0, /* u8 bmAttributes; + 0xa0, /* u8 bmAttributes; Bit 7: must be set, 6: Self-powered, 5: Remote wakeup, 4..0: resvd */ 50, /* u8 MaxPower; */ - + /* USB 1.1: * USB 2.0, single TT organization (mandatory): * one interface, protocol 0 @@ -97,8 +127,8 @@ static const uint8_t qemu_mouse_config_descriptor[] = { 0x03, /* u8 if_bInterfaceClass; */ 0x01, /* u8 if_bInterfaceSubClass; */ 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ - 0x05, /* u8 if_iInterface; */ - + 0x07, /* u8 if_iInterface; */ + /* HID descriptor */ 0x09, /* u8 bLength; */ 0x21, /* u8 bDescriptorType; */ @@ -106,14 +136,14 @@ static const uint8_t qemu_mouse_config_descriptor[] = { 0x00, /* u8 country_code */ 0x01, /* u8 num_descriptors */ 0x22, /* u8 type; Report */ - 50, 0, /* u16 len */ + 52, 0, /* u16 len */ /* one endpoint (status change endpoint) */ 0x07, /* u8 ep_bLength; */ 0x05, /* u8 ep_bDescriptorType; Endpoint */ 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */ 0x03, /* u8 ep_bmAttributes; Interrupt */ - 0x03, 0x00, /* u16 ep_wMaxPacketSize; */ + 0x04, 0x00, /* u16 ep_wMaxPacketSize; */ 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */ }; @@ -124,14 +154,14 @@ static const uint8_t qemu_tablet_config_descriptor[] = { 0x22, 0x00, /* u16 wTotalLength; */ 0x01, /* u8 bNumInterfaces; (1) */ 0x01, /* u8 bConfigurationValue; */ - 0x04, /* u8 iConfiguration; */ - 0xa0, /* u8 bmAttributes; + 0x05, /* u8 iConfiguration; */ + 0xa0, /* u8 bmAttributes; Bit 7: must be set, 6: Self-powered, 5: Remote wakeup, 4..0: resvd */ 50, /* u8 MaxPower; */ - + /* USB 1.1: * USB 2.0, single TT organization (mandatory): * one interface, protocol 0 @@ -152,7 +182,7 @@ static const uint8_t qemu_tablet_config_descriptor[] = { 0x03, /* u8 if_bInterfaceClass; */ 0x01, /* u8 if_bInterfaceSubClass; */ 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ - 0x05, /* u8 if_iInterface; */ + 0x07, /* u8 if_iInterface; */ /* HID descriptor */ 0x09, /* u8 bLength; */ @@ -169,79 +199,299 @@ static const uint8_t qemu_tablet_config_descriptor[] = { 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */ 0x03, /* u8 ep_bmAttributes; Interrupt */ 0x08, 0x00, /* u16 ep_wMaxPacketSize; */ - 0x03, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */ + 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */ +}; + +static const uint8_t qemu_keyboard_config_descriptor[] = { + /* one configuration */ + 0x09, /* u8 bLength; */ + USB_DT_CONFIG, /* u8 bDescriptorType; Configuration */ + 0x22, 0x00, /* u16 wTotalLength; */ + 0x01, /* u8 bNumInterfaces; (1) */ + 0x01, /* u8 bConfigurationValue; */ + 0x06, /* u8 iConfiguration; */ + 0xa0, /* u8 bmAttributes; + Bit 7: must be set, + 6: Self-powered, + 5: Remote wakeup, + 4..0: resvd */ + 0x32, /* u8 MaxPower; */ + + /* USB 1.1: + * USB 2.0, single TT organization (mandatory): + * one interface, protocol 0 + * + * USB 2.0, multiple TT organization (optional): + * two interfaces, protocols 1 (like single TT) + * and 2 (multiple TT mode) ... config is + * sometimes settable + * NOT IMPLEMENTED + */ + + /* one interface */ + 0x09, /* u8 if_bLength; */ + USB_DT_INTERFACE, /* u8 if_bDescriptorType; Interface */ + 0x00, /* u8 if_bInterfaceNumber; */ + 0x00, /* u8 if_bAlternateSetting; */ + 0x01, /* u8 if_bNumEndpoints; */ + 0x03, /* u8 if_bInterfaceClass; HID */ + 0x01, /* u8 if_bInterfaceSubClass; Boot */ + 0x01, /* u8 if_bInterfaceProtocol; Keyboard */ + 0x07, /* u8 if_iInterface; */ + + /* HID descriptor */ + 0x09, /* u8 bLength; */ + USB_DT_HID, /* u8 bDescriptorType; */ + 0x11, 0x01, /* u16 HID_class */ + 0x00, /* u8 country_code */ + 0x01, /* u8 num_descriptors */ + USB_DT_REPORT, /* u8 type; Report */ + 0x3f, 0x00, /* u16 len */ + + /* one endpoint (status change endpoint) */ + 0x07, /* u8 ep_bLength; */ + USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; Endpoint */ + USB_DIR_IN | 0x01, /* u8 ep_bEndpointAddress; IN Endpoint 1 */ + 0x03, /* u8 ep_bmAttributes; Interrupt */ + 0x08, 0x00, /* u16 ep_wMaxPacketSize; */ + 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */ }; static const uint8_t qemu_mouse_hid_report_descriptor[] = { - 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, - 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, - 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, - 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, - 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x15, 0x81, - 0x25, 0x7F, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, - 0xC0, 0xC0, + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x02, /* Usage (Mouse) */ + 0xa1, 0x01, /* Collection (Application) */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xa1, 0x00, /* Collection (Physical) */ + 0x05, 0x09, /* Usage Page (Button) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x03, /* Usage Maximum (3) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x95, 0x03, /* Report Count (3) */ + 0x75, 0x01, /* Report Size (1) */ + 0x81, 0x02, /* Input (Data, Variable, Absolute) */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x05, /* Report Size (5) */ + 0x81, 0x01, /* Input (Constant) */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x30, /* Usage (X) */ + 0x09, 0x31, /* Usage (Y) */ + 0x09, 0x38, /* Usage (Wheel) */ + 0x15, 0x81, /* Logical Minimum (-0x7f) */ + 0x25, 0x7f, /* Logical Maximum (0x7f) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x03, /* Report Count (3) */ + 0x81, 0x06, /* Input (Data, Variable, Relative) */ + 0xc0, /* End Collection */ + 0xc0, /* End Collection */ }; static const uint8_t qemu_tablet_hid_report_descriptor[] = { - 0x05, 0x01, /* Usage Page Generic Desktop */ - 0x09, 0x01, /* Usage Mouse */ - 0xA1, 0x01, /* Collection Application */ - 0x09, 0x01, /* Usage Pointer */ - 0xA1, 0x00, /* Collection Physical */ - 0x05, 0x09, /* Usage Page Button */ - 0x19, 0x01, /* Usage Minimum Button 1 */ - 0x29, 0x03, /* Usage Maximum Button 3 */ - 0x15, 0x00, /* Logical Minimum 0 */ - 0x25, 0x01, /* Logical Maximum 1 */ - 0x95, 0x03, /* Report Count 3 */ - 0x75, 0x01, /* Report Size 1 */ - 0x81, 0x02, /* Input (Data, Var, Abs) */ - 0x95, 0x01, /* Report Count 1 */ - 0x75, 0x05, /* Report Size 5 */ - 0x81, 0x01, /* Input (Cnst, Var, Abs) */ - 0x05, 0x01, /* Usage Page Generic Desktop */ - 0x09, 0x30, /* Usage X */ - 0x09, 0x31, /* Usage Y */ - 0x15, 0x00, /* Logical Minimum 0 */ - 0x26, 0xFF, 0x7F, /* Logical Maximum 0x7fff */ - 0x35, 0x00, /* Physical Minimum 0 */ - 0x46, 0xFE, 0x7F, /* Physical Maximum 0x7fff */ - 0x75, 0x10, /* Report Size 16 */ - 0x95, 0x02, /* Report Count 2 */ - 0x81, 0x02, /* Input (Data, Var, Abs) */ - 0x05, 0x01, /* Usage Page Generic Desktop */ - 0x09, 0x38, /* Usage Wheel */ - 0x15, 0x81, /* Logical Minimum -127 */ - 0x25, 0x7F, /* Logical Maximum 127 */ - 0x35, 0x00, /* Physical Minimum 0 (same as logical) */ - 0x45, 0x00, /* Physical Maximum 0 (same as logical) */ - 0x75, 0x08, /* Report Size 8 */ - 0x95, 0x01, /* Report Count 1 */ - 0x81, 0x02, /* Input (Data, Var, Rel) */ - 0xC0, /* End Collection */ - 0xC0, /* End Collection */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xa1, 0x01, /* Collection (Application) */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xa1, 0x00, /* Collection (Physical) */ + 0x05, 0x09, /* Usage Page (Button) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x03, /* Usage Maximum (3) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x95, 0x03, /* Report Count (3) */ + 0x75, 0x01, /* Report Size (1) */ + 0x81, 0x02, /* Input (Data, Variable, Absolute) */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x05, /* Report Size (5) */ + 0x81, 0x01, /* Input (Constant) */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x30, /* Usage (X) */ + 0x09, 0x31, /* Usage (Y) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */ + 0x35, 0x00, /* Physical Minimum (0) */ + 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */ + 0x75, 0x10, /* Report Size (16) */ + 0x95, 0x02, /* Report Count (2) */ + 0x81, 0x02, /* Input (Data, Variable, Absolute) */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x38, /* Usage (Wheel) */ + 0x15, 0x81, /* Logical Minimum (-0x7f) */ + 0x25, 0x7f, /* Logical Maximum (0x7f) */ + 0x35, 0x00, /* Physical Minimum (same as logical) */ + 0x45, 0x00, /* Physical Maximum (same as logical) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x01, /* Report Count (1) */ + 0x81, 0x06, /* Input (Data, Variable, Relative) */ + 0xc0, /* End Collection */ + 0xc0, /* End Collection */ }; +static const uint8_t qemu_keyboard_hid_report_descriptor[] = { + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xa1, 0x01, /* Collection (Application) */ + 0x75, 0x01, /* Report Size (1) */ + 0x95, 0x08, /* Report Count (8) */ + 0x05, 0x07, /* Usage Page (Key Codes) */ + 0x19, 0xe0, /* Usage Minimum (224) */ + 0x29, 0xe7, /* Usage Maximum (231) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x81, 0x02, /* Input (Data, Variable, Absolute) */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x08, /* Report Size (8) */ + 0x81, 0x01, /* Input (Constant) */ + 0x95, 0x05, /* Report Count (5) */ + 0x75, 0x01, /* Report Size (1) */ + 0x05, 0x08, /* Usage Page (LEDs) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x05, /* Usage Maximum (5) */ + 0x91, 0x02, /* Output (Data, Variable, Absolute) */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x03, /* Report Size (3) */ + 0x91, 0x01, /* Output (Constant) */ + 0x95, 0x06, /* Report Count (6) */ + 0x75, 0x08, /* Report Size (8) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0xff, /* Logical Maximum (255) */ + 0x05, 0x07, /* Usage Page (Key Codes) */ + 0x19, 0x00, /* Usage Minimum (0) */ + 0x29, 0xff, /* Usage Maximum (255) */ + 0x81, 0x00, /* Input (Data, Array) */ + 0xc0, /* End Collection */ +}; + +#define USB_HID_USAGE_ERROR_ROLLOVER 0x01 +#define USB_HID_USAGE_POSTFAIL 0x02 +#define USB_HID_USAGE_ERROR_UNDEFINED 0x03 + +/* Indices are QEMU keycodes, values are from HID Usage Table. Indices + * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */ +static const uint8_t usb_hid_usage_keys[0x100] = { + 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b, + 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c, + 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16, + 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33, + 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19, + 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55, + 0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, + 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f, + 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59, + 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44, + 0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46, + 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a, + 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d, + 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static void usb_hid_changed(USBHIDState *hs) +{ + hs->changed = 1; + + if (hs->datain) + hs->datain(hs->datain_opaque); +} + static void usb_mouse_event(void *opaque, int dx1, int dy1, int dz1, int buttons_state) { - USBMouseState *s = opaque; + USBHIDState *hs = opaque; + USBMouseState *s = &hs->ptr; s->dx += dx1; s->dy += dy1; s->dz += dz1; s->buttons_state = buttons_state; + + usb_hid_changed(hs); } static void usb_tablet_event(void *opaque, int x, int y, int dz, int buttons_state) { - USBMouseState *s = opaque; + USBHIDState *hs = opaque; + USBMouseState *s = &hs->ptr; s->x = x; s->y = y; s->dz += dz; s->buttons_state = buttons_state; + + usb_hid_changed(hs); +} + +static void usb_keyboard_event(void *opaque, int keycode) +{ + USBHIDState *hs = opaque; + USBKeyboardState *s = &hs->kbd; + uint8_t hid_code, key; + int i; + + key = keycode & 0x7f; + hid_code = usb_hid_usage_keys[key | ((s->modifiers >> 1) & (1 << 7))]; + s->modifiers &= ~(1 << 8); + + switch (hid_code) { + case 0x00: + return; + + case 0xe0: + if (s->modifiers & (1 << 9)) { + s->modifiers ^= 3 << 8; + return; + } + case 0xe1 ... 0xe7: + if (keycode & (1 << 7)) { + s->modifiers &= ~(1 << (hid_code & 0x0f)); + return; + } + case 0xe8 ... 0xef: + s->modifiers |= 1 << (hid_code & 0x0f); + return; + } + + if (keycode & (1 << 7)) { + for (i = s->keys - 1; i >= 0; i --) + if (s->key[i] == hid_code) { + s->key[i] = s->key[-- s->keys]; + s->key[s->keys] = 0x00; + usb_hid_changed(hs); + break; + } + if (i < 0) + return; + } else { + for (i = s->keys - 1; i >= 0; i --) + if (s->key[i] == hid_code) + break; + if (i < 0) { + if (s->keys < sizeof(s->key)) + s->key[s->keys ++] = hid_code; + } else + return; + } + + usb_hid_changed(hs); } static inline int int_clamp(int val, int vmin, int vmax) @@ -254,23 +504,28 @@ static inline int int_clamp(int val, int vmin, int vmax) return val; } -static int usb_mouse_poll(USBMouseState *s, uint8_t *buf, int len) +static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len) { int dx, dy, dz, b, l; + USBMouseState *s = &hs->ptr; if (!s->mouse_grabbed) { - qemu_add_mouse_event_handler(usb_mouse_event, s, 0); + s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, hs, + 0, "QEMU USB Mouse"); s->mouse_grabbed = 1; } - - dx = int_clamp(s->dx, -128, 127); - dy = int_clamp(s->dy, -128, 127); - dz = int_clamp(s->dz, -128, 127); + + dx = int_clamp(s->dx, -127, 127); + dy = int_clamp(s->dy, -127, 127); + dz = int_clamp(s->dz, -127, 127); s->dx -= dx; s->dy -= dy; s->dz -= dz; - + + /* Appears we have to invert the wheel direction */ + dz = 0 - dz; + b = 0; if (s->buttons_state & MOUSE_EVENT_LBUTTON) b |= 0x01; @@ -278,28 +533,31 @@ static int usb_mouse_poll(USBMouseState *s, uint8_t *buf, int len) b |= 0x02; if (s->buttons_state & MOUSE_EVENT_MBUTTON) b |= 0x04; - - buf[0] = b; - buf[1] = dx; - buf[2] = dy; - l = 3; - if (len >= 4) { - buf[3] = dz; - l = 4; - } + + l = 0; + if (len > l) + buf[l ++] = b; + if (len > l) + buf[l ++] = dx; + if (len > l) + buf[l ++] = dy; + if (len > l) + buf[l ++] = dz; return l; } -static int usb_tablet_poll(USBMouseState *s, uint8_t *buf, int len) +static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len) { int dz, b, l; + USBMouseState *s = &hs->ptr; if (!s->mouse_grabbed) { - qemu_add_mouse_event_handler(usb_tablet_event, s, 1); + s->eh_entry = qemu_add_mouse_event_handler(usb_tablet_event, hs, + 1, "QEMU USB Tablet"); s->mouse_grabbed = 1; } - - dz = int_clamp(s->dz, -128, 127); + + dz = int_clamp(s->dz, -127, 127); s->dz -= dz; /* Appears we have to invert the wheel direction */ @@ -323,22 +581,59 @@ static int usb_tablet_poll(USBMouseState *s, uint8_t *buf, int len) return l; } -static void usb_mouse_handle_reset(USBDevice *dev) +static int usb_keyboard_poll(USBKeyboardState *s, uint8_t *buf, int len) { - USBMouseState *s = (USBMouseState *)dev; + if (len < 2) + return 0; - s->dx = 0; - s->dy = 0; - s->dz = 0; - s->x = 0; - s->y = 0; - s->buttons_state = 0; + buf[0] = s->modifiers & 0xff; + buf[1] = 0; + if (s->keys > 6) + memset(buf + 2, USB_HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2); + else + memcpy(buf + 2, s->key, MIN(8, len) - 2); + + return MIN(8, len); } -static int usb_mouse_handle_control(USBDevice *dev, int request, int value, +static int usb_keyboard_write(USBKeyboardState *s, uint8_t *buf, int len) +{ + if (len > 0) { + /* 0x01: Num Lock LED + * 0x02: Caps Lock LED + * 0x04: Scroll Lock LED + * 0x08: Compose LED + * 0x10: Kana LED */ + s->leds = buf[0]; + } + return 0; +} + +static void usb_mouse_handle_reset(USBDevice *dev) +{ + USBHIDState *s = (USBHIDState *)dev; + + s->ptr.dx = 0; + s->ptr.dy = 0; + s->ptr.dz = 0; + s->ptr.x = 0; + s->ptr.y = 0; + s->ptr.buttons_state = 0; + s->protocol = 1; +} + +static void usb_keyboard_handle_reset(USBDevice *dev) +{ + USBHIDState *s = (USBHIDState *)dev; + + qemu_add_kbd_event_handler(usb_keyboard_event, s); + s->protocol = 1; +} + +static int usb_hid_handle_control(USBDevice *dev, int request, int value, int index, int length, uint8_t *data) { - USBMouseState *s = (USBMouseState *)dev; + USBHIDState *s = (USBHIDState *)dev; int ret = 0; switch(request) { @@ -371,20 +666,24 @@ static int usb_mouse_handle_control(USBDevice *dev, int request, int value, case DeviceRequest | USB_REQ_GET_DESCRIPTOR: switch(value >> 8) { case USB_DT_DEVICE: - memcpy(data, qemu_mouse_dev_descriptor, + memcpy(data, qemu_mouse_dev_descriptor, sizeof(qemu_mouse_dev_descriptor)); ret = sizeof(qemu_mouse_dev_descriptor); break; case USB_DT_CONFIG: if (s->kind == USB_MOUSE) { - memcpy(data, qemu_mouse_config_descriptor, + memcpy(data, qemu_mouse_config_descriptor, sizeof(qemu_mouse_config_descriptor)); ret = sizeof(qemu_mouse_config_descriptor); } else if (s->kind == USB_TABLET) { - memcpy(data, qemu_tablet_config_descriptor, + memcpy(data, qemu_tablet_config_descriptor, sizeof(qemu_tablet_config_descriptor)); ret = sizeof(qemu_tablet_config_descriptor); - } + } else if (s->kind == USB_KEYBOARD) { + memcpy(data, qemu_keyboard_config_descriptor, + sizeof(qemu_keyboard_config_descriptor)); + ret = sizeof(qemu_keyboard_config_descriptor); + } break; case USB_DT_STRING: switch(value & 0xff) { @@ -402,19 +701,22 @@ static int usb_mouse_handle_control(USBDevice *dev, int request, int value, break; case 2: /* product description */ - if (s->kind == USB_MOUSE) - ret = set_usb_string(data, "QEMU USB Mouse"); - else if (s->kind == USB_TABLET) - ret = set_usb_string(data, "QEMU USB Tablet"); + ret = set_usb_string(data, s->dev.product_desc); break; case 3: /* vendor description */ - ret = set_usb_string(data, "PCSX2/QEMU"); + ret = set_usb_string(data, "QEMU " QEMU_VERSION); break; case 4: ret = set_usb_string(data, "HID Mouse"); break; case 5: + ret = set_usb_string(data, "HID Tablet"); + break; + case 6: + ret = set_usb_string(data, "HID Keyboard"); + break; + case 7: ret = set_usb_string(data, "Endpoint1 Interrupt Pipe"); break; default: @@ -444,26 +746,55 @@ static int usb_mouse_handle_control(USBDevice *dev, int request, int value, switch(value >> 8) { case 0x22: if (s->kind == USB_MOUSE) { - memcpy(data, qemu_mouse_hid_report_descriptor, + memcpy(data, qemu_mouse_hid_report_descriptor, sizeof(qemu_mouse_hid_report_descriptor)); ret = sizeof(qemu_mouse_hid_report_descriptor); } else if (s->kind == USB_TABLET) { - memcpy(data, qemu_tablet_hid_report_descriptor, + memcpy(data, qemu_tablet_hid_report_descriptor, sizeof(qemu_tablet_hid_report_descriptor)); ret = sizeof(qemu_tablet_hid_report_descriptor); - } - break; + } else if (s->kind == USB_KEYBOARD) { + memcpy(data, qemu_keyboard_hid_report_descriptor, + sizeof(qemu_keyboard_hid_report_descriptor)); + ret = sizeof(qemu_keyboard_hid_report_descriptor); + } + break; default: goto fail; } break; case GET_REPORT: if (s->kind == USB_MOUSE) - ret = usb_mouse_poll(s, data, length); + ret = usb_mouse_poll(s, data, length); else if (s->kind == USB_TABLET) - ret = usb_tablet_poll(s, data, length); + ret = usb_tablet_poll(s, data, length); + else if (s->kind == USB_KEYBOARD) + ret = usb_keyboard_poll(&s->kbd, data, length); + break; + case SET_REPORT: + if (s->kind == USB_KEYBOARD) + ret = usb_keyboard_write(&s->kbd, data, length); + else + goto fail; + break; + case GET_PROTOCOL: + if (s->kind != USB_KEYBOARD) + goto fail; + ret = 1; + data[0] = s->protocol; + break; + case SET_PROTOCOL: + if (s->kind != USB_KEYBOARD) + goto fail; + ret = 0; + s->protocol = value; + break; + case GET_IDLE: + ret = 1; + data[0] = s->idle; break; case SET_IDLE: + s->idle = (uint8_t) (value >> 8); ret = 0; break; default: @@ -474,19 +805,24 @@ static int usb_mouse_handle_control(USBDevice *dev, int request, int value, return ret; } -static int usb_mouse_handle_data(USBDevice *dev, int pid, - uint8_t devep, uint8_t *data, int len) +static int usb_hid_handle_data(USBDevice *dev, USBPacket *p) { - USBMouseState *s = (USBMouseState *)dev; + USBHIDState *s = (USBHIDState *)dev; int ret = 0; - switch(pid) { + switch(p->pid) { case USB_TOKEN_IN: - if (devep == 1) { - if (s->kind == USB_MOUSE) - ret = usb_mouse_poll(s, data, len); - else if (s->kind == USB_TABLET) - ret = usb_tablet_poll(s, data, len); + if (p->devep == 1) { + /* TODO: Implement finite idle delays. */ + if (!(s->changed || s->idle)) + return USB_RET_NAK; + s->changed = 0; + if (s->kind == USB_MOUSE) + ret = usb_mouse_poll(s, p->data, p->len); + else if (s->kind == USB_TABLET) + ret = usb_tablet_poll(s, p->data, p->len); + else if (s->kind == USB_KEYBOARD) + ret = usb_keyboard_poll(&s->kbd, p->data, p->len); } else { goto fail; } @@ -500,52 +836,89 @@ static int usb_mouse_handle_data(USBDevice *dev, int pid, return ret; } -static void usb_mouse_handle_destroy(USBDevice *dev) +static void usb_hid_handle_destroy(USBDevice *dev) { - USBMouseState *s = (USBMouseState *)dev; + USBHIDState *s = (USBHIDState *)dev; - qemu_add_mouse_event_handler(NULL, NULL, 0); - free(s); + if (s->kind != USB_KEYBOARD) + qemu_remove_mouse_event_handler(s->ptr.eh_entry); + /* TODO: else */ } -USBDevice *usb_tablet_init(void) +static int usb_hid_initfn(USBDevice *dev, int kind) { - USBMouseState *s; - - s = qemu_mallocz(sizeof(USBMouseState)); - if (!s) - return NULL; + USBHIDState *s = DO_UPCAST(USBHIDState, dev, dev); s->dev.speed = USB_SPEED_FULL; - s->dev.handle_packet = usb_generic_handle_packet; - - s->dev.handle_reset = usb_mouse_handle_reset; - s->dev.handle_control = usb_mouse_handle_control; - s->dev.handle_data = usb_mouse_handle_data; - s->dev.handle_destroy = usb_mouse_handle_destroy; - s->kind = USB_TABLET; - - pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Tablet"); - - return (USBDevice *)s; + s->kind = kind; + /* Force poll routine to be run and grab input the first time. */ + s->changed = 1; + return 0; } -USBDevice *usb_mouse_init(void) +static int usb_tablet_initfn(USBDevice *dev) { - USBMouseState *s; - - s = qemu_mallocz(sizeof(USBMouseState)); - if (!s) - return NULL; - s->dev.speed = USB_SPEED_FULL; - s->dev.handle_packet = usb_generic_handle_packet; - - s->dev.handle_reset = usb_mouse_handle_reset; - s->dev.handle_control = usb_mouse_handle_control; - s->dev.handle_data = usb_mouse_handle_data; - s->dev.handle_destroy = usb_mouse_handle_destroy; - s->kind = USB_MOUSE; - - pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Mouse"); - - return (USBDevice *)s; + return usb_hid_initfn(dev, USB_TABLET); } + +static int usb_mouse_initfn(USBDevice *dev) +{ + return usb_hid_initfn(dev, USB_MOUSE); +} + +static int usb_keyboard_initfn(USBDevice *dev) +{ + return usb_hid_initfn(dev, USB_KEYBOARD); +} + +void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *)) +{ + USBHIDState *s = (USBHIDState *)dev; + + s->datain_opaque = opaque; + s->datain = datain; +} + +static struct USBDeviceInfo hid_info[] = { + { + .product_desc = "QEMU USB Tablet", + .qdev.name = "usb-tablet", + .usbdevice_name = "tablet", + .qdev.size = sizeof(USBHIDState), + .init = usb_tablet_initfn, + .handle_packet = usb_generic_handle_packet, + .handle_reset = usb_mouse_handle_reset, + .handle_control = usb_hid_handle_control, + .handle_data = usb_hid_handle_data, + .handle_destroy = usb_hid_handle_destroy, + },{ + .product_desc = "QEMU USB Mouse", + .qdev.name = "usb-mouse", + .usbdevice_name = "mouse", + .qdev.size = sizeof(USBHIDState), + .init = usb_mouse_initfn, + .handle_packet = usb_generic_handle_packet, + .handle_reset = usb_mouse_handle_reset, + .handle_control = usb_hid_handle_control, + .handle_data = usb_hid_handle_data, + .handle_destroy = usb_hid_handle_destroy, + },{ + .product_desc = "QEMU USB Keyboard", + .qdev.name = "usb-kbd", + .usbdevice_name = "keyboard", + .qdev.size = sizeof(USBHIDState), + .init = usb_keyboard_initfn, + .handle_packet = usb_generic_handle_packet, + .handle_reset = usb_keyboard_handle_reset, + .handle_control = usb_hid_handle_control, + .handle_data = usb_hid_handle_data, + .handle_destroy = usb_hid_handle_destroy, + },{ + /* end of list */ + } +}; + +static void usb_hid_register_devices(void) +{ + usb_qdev_register_many(hid_info); +} +device_init(usb_hid_register_devices) diff --git a/plugins/USBqemu/qemu-usb/usb-hub.cpp b/plugins/USBqemu/qemu-usb/usb-hub.cpp index 4654863870..4d62c9ef92 100644 --- a/plugins/USBqemu/qemu-usb/usb-hub.cpp +++ b/plugins/USBqemu/qemu-usb/usb-hub.cpp @@ -2,7 +2,7 @@ * QEMU USB HUB emulation * * Copyright (c) 2005 Fabrice Bellard - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -112,13 +112,13 @@ static const uint8_t qemu_hub_config_descriptor[] = { 0x01, /* u8 bNumInterfaces; (1) */ 0x01, /* u8 bConfigurationValue; */ 0x00, /* u8 iConfiguration; */ - 0xc0, /* u8 bmAttributes; + 0xc0, /* u8 bmAttributes; Bit 7: must be set, 6: Self-powered, 5: Remote wakeup, 4..0: resvd */ 0x00, /* u8 MaxPower; */ - + /* USB 1.1: * USB 2.0, single TT organization (mandatory): * one interface, protocol 0 @@ -140,7 +140,7 @@ static const uint8_t qemu_hub_config_descriptor[] = { 0x00, /* u8 if_bInterfaceSubClass; */ 0x00, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ 0x00, /* u8 if_iInterface; */ - + /* one endpoint (status change endpoint) */ 0x07, /* u8 ep_bLength; */ 0x05, /* u8 ep_bDescriptorType; Endpoint */ @@ -165,13 +165,13 @@ static const uint8_t qemu_hub_hub_descriptor[] = static void usb_hub_attach(USBPort *port1, USBDevice *dev) { - USBHubState *s =(USBHubState *) port1->opaque; - USBHubPort *port = (USBHubPort *)&s->ports[port1->index]; - + USBHubState *s = (USBHubState *)port1->opaque; + USBHubPort *port = &s->ports[port1->index]; + if (dev) { if (port->port.dev) usb_attach(port1, NULL); - + port->wPortStatus |= PORT_STAT_CONNECTION; port->wPortChange |= PORT_STAT_C_CONNECTION; if (dev->speed == USB_SPEED_LOW) @@ -180,8 +180,7 @@ static void usb_hub_attach(USBPort *port1, USBDevice *dev) port->wPortStatus &= ~PORT_STAT_LOW_SPEED; port->port.dev = dev; /* send the attach message */ - dev->handle_packet(dev, - USB_MSG_ATTACH, 0, 0, NULL, 0); + usb_send_msg(dev, USB_MSG_ATTACH); } else { dev = port->port.dev; if (dev) { @@ -192,8 +191,7 @@ static void usb_hub_attach(USBPort *port1, USBDevice *dev) port->wPortChange |= PORT_STAT_C_ENABLE; } /* send the detach message */ - dev->handle_packet(dev, - USB_MSG_DETACH, 0, 0, NULL, 0); + usb_send_msg(dev, USB_MSG_DETACH); port->port.dev = NULL; } } @@ -246,12 +244,12 @@ static int usb_hub_handle_control(USBDevice *dev, int request, int value, case DeviceRequest | USB_REQ_GET_DESCRIPTOR: switch(value >> 8) { case USB_DT_DEVICE: - memcpy(data, qemu_hub_dev_descriptor, + memcpy(data, qemu_hub_dev_descriptor, sizeof(qemu_hub_dev_descriptor)); ret = sizeof(qemu_hub_dev_descriptor); break; case USB_DT_CONFIG: - memcpy(data, qemu_hub_config_descriptor, + memcpy(data, qemu_hub_config_descriptor, sizeof(qemu_hub_config_descriptor)); /* status change endpoint size based on number @@ -280,7 +278,7 @@ static int usb_hub_handle_control(USBDevice *dev, int request, int value, break; case 3: /* vendor description */ - ret = set_usb_string(data, "PCSX2/QEMU"); + ret = set_usb_string(data, "QEMU v0.12.5"); break; default: goto fail; @@ -349,8 +347,7 @@ static int usb_hub_handle_control(USBDevice *dev, int request, int value, break; case PORT_RESET: if (dev) { - dev->handle_packet(dev, - USB_MSG_RESET, 0, 0, NULL, 0); + usb_send_msg(dev, USB_MSG_RESET); port->wPortChange |= PORT_STAT_C_RESET; /* set enable bit */ port->wPortStatus |= PORT_STAT_ENABLE; @@ -404,7 +401,7 @@ static int usb_hub_handle_control(USBDevice *dev, int request, int value, case GetHubDescriptor: { unsigned int n, limit, var_hub_size = 0; - memcpy(data, qemu_hub_hub_descriptor, + memcpy(data, qemu_hub_hub_descriptor, sizeof(qemu_hub_hub_descriptor)); data[2] = s->nb_ports; @@ -434,22 +431,21 @@ static int usb_hub_handle_control(USBDevice *dev, int request, int value, return ret; } -static int usb_hub_handle_data(USBDevice *dev, int pid, - uint8_t devep, uint8_t *data, int len) +static int usb_hub_handle_data(USBDevice *dev, USBPacket *p) { USBHubState *s = (USBHubState *)dev; int ret; - switch(pid) { + switch(p->pid) { case USB_TOKEN_IN: - if (devep == 1) { + if (p->devep == 1) { USBHubPort *port; unsigned int status; int i, n; n = (s->nb_ports + 1 + 7) / 8; - if (len == 1) { /* FreeBSD workaround */ + if (p->len == 1) { /* FreeBSD workaround */ n = 1; - } else if (n > len) { + } else if (n > p->len) { return USB_RET_BABBLE; } status = 0; @@ -460,7 +456,7 @@ static int usb_hub_handle_data(USBDevice *dev, int pid, } if (status != 0) { for(i = 0; i < n; i++) { - data[i] = status >> (8 * i); + p->data[i] = status >> (8 * i); } ret = n; } else { @@ -479,9 +475,7 @@ static int usb_hub_handle_data(USBDevice *dev, int pid, return ret; } -static int usb_hub_broadcast_packet(USBHubState *s, int pid, - uint8_t devaddr, uint8_t devep, - uint8_t *data, int len) +static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p) { USBHubPort *port; USBDevice *dev; @@ -491,9 +485,7 @@ static int usb_hub_broadcast_packet(USBHubState *s, int pid, port = &s->ports[i]; dev = port->port.dev; if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) { - ret = dev->handle_packet(dev, pid, - devaddr, devep, - data, len); + ret = dev->info->handle_packet(dev, p); if (ret != USB_RET_NODEV) { return ret; } @@ -502,9 +494,7 @@ static int usb_hub_broadcast_packet(USBHubState *s, int pid, return USB_RET_NODEV; } -static int usb_hub_handle_packet(USBDevice *dev, int pid, - uint8_t devaddr, uint8_t devep, - uint8_t *data, int len) +static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p) { USBHubState *s = (USBHubState *)dev; @@ -513,53 +503,59 @@ static int usb_hub_handle_packet(USBDevice *dev, int pid, #endif if (dev->state == USB_STATE_DEFAULT && dev->addr != 0 && - devaddr != dev->addr && - (pid == USB_TOKEN_SETUP || - pid == USB_TOKEN_OUT || - pid == USB_TOKEN_IN)) { + p->devaddr != dev->addr && + (p->pid == USB_TOKEN_SETUP || + p->pid == USB_TOKEN_OUT || + p->pid == USB_TOKEN_IN)) { /* broadcast the packet to the devices */ - return usb_hub_broadcast_packet(s, pid, devaddr, devep, data, len); + return usb_hub_broadcast_packet(s, p); } - return usb_generic_handle_packet(dev, pid, devaddr, devep, data, len); + return usb_generic_handle_packet(dev, p); } static void usb_hub_handle_destroy(USBDevice *dev) { USBHubState *s = (USBHubState *)dev; + int i; - free(s); + //for (i = 0; i < s->nb_ports; i++) { + // usb_unregister_port(usb_bus_from_device(dev), + // &s->ports[i].port); + //} } -USBDevice *usb_hub_init(int nb_ports) +static int usb_hub_initfn(USBDevice *dev) { - USBHubState *s; + USBHubState *s = (USBHubState*)dev; USBHubPort *port; int i; - if (nb_ports > MAX_PORTS) - return NULL; - s = (USBHubState *)qemu_mallocz(sizeof(USBHubState)); - if (!s) - return NULL; - s->dev.speed = USB_SPEED_FULL; - s->dev.handle_packet = usb_hub_handle_packet; - - /* generic USB device init */ - s->dev.handle_reset = usb_hub_handle_reset; - s->dev.handle_control = usb_hub_handle_control; - s->dev.handle_data = usb_hub_handle_data; - s->dev.handle_destroy = usb_hub_handle_destroy; - - strncpy(s->dev.devname, "QEMU USB Hub", sizeof(s->dev.devname)); - - s->nb_ports = nb_ports; - for(i = 0; i < s->nb_ports; i++) { + s->dev.speed = USB_SPEED_FULL, + s->nb_ports = MAX_PORTS; /* FIXME: make configurable */ + for (i = 0; i < s->nb_ports; i++) { port = &s->ports[i]; - port->port.opaque = s; - port->port.index = i; - port->port.attach = usb_hub_attach; + //usb_register_port(usb_bus_from_device(dev), + // &port->port, s, i, usb_hub_attach); port->wPortStatus = PORT_STAT_POWER; port->wPortChange = 0; } - return (USBDevice *)s; + return 0; } + +static struct USBDeviceInfo hub_info = { + usb_hub_initfn, + usb_hub_handle_packet, + usb_hub_handle_destroy, + usb_hub_handle_reset, + usb_hub_handle_control, + usb_hub_handle_data, + + "QEMU USB Hub", + +}; + +static void usb_hub_register_devices(void) +{ + //usb_qdev_register(&hub_info); +} +//device_init(usb_hub_register_devices) diff --git a/plugins/USBqemu/qemu-usb/usb-kbd.cpp b/plugins/USBqemu/qemu-usb/usb-kbd.cpp index e4aa6b5f46..6c4d427b24 100644 --- a/plugins/USBqemu/qemu-usb/usb-kbd.cpp +++ b/plugins/USBqemu/qemu-usb/usb-kbd.cpp @@ -38,6 +38,8 @@ typedef struct USBKeyboardState { int keyboard_grabbed; } USBKeyboardState; +USBDeviceInfo devinfo; + extern HWND gsWnd; #define VK_BASED @@ -817,16 +819,15 @@ static int usb_keyboard_handle_control(USBDevice *dev, int request, int value, return ret; } -static int usb_keyboard_handle_data(USBDevice *dev, int pid, - uint8_t devep, uint8_t *data, int len) +static int usb_keyboard_handle_data(USBDevice *dev, USBPacket* packet) { USBKeyboardState *s = (USBKeyboardState *)dev; int ret = 0; - switch(pid) { + switch(packet->pid) { case USB_TOKEN_IN: - if (devep == 1) { - ret = usb_keyboard_poll(s, data, len); + if (packet->devep == 1) { + ret = usb_keyboard_poll(s, packet->data, packet->len); } else { goto fail; } @@ -857,14 +858,13 @@ USBDevice *usb_keyboard_init(void) return NULL; memset(s,0,sizeof(USBKeyboardState)); s->dev.speed = USB_SPEED_FULL; - s->dev.handle_packet = usb_generic_handle_packet; - - s->dev.handle_reset = usb_keyboard_handle_reset; - s->dev.handle_control = usb_keyboard_handle_control; - s->dev.handle_data = usb_keyboard_handle_data; - s->dev.handle_destroy = usb_keyboard_handle_destroy; - - strncpy(s->dev.devname, "Generic USB Keyboard", sizeof(s->dev.devname)); + s->dev.info = &devinfo; + s->dev.info->handle_packet = usb_generic_handle_packet; + s->dev.info->handle_reset = usb_keyboard_handle_reset; + s->dev.info->handle_control = usb_keyboard_handle_control; + s->dev.info->handle_data = usb_keyboard_handle_data; + s->dev.info->handle_destroy = usb_keyboard_handle_destroy; + s->dev.info->product_desc = "Generic USB Keyboard"; return (USBDevice *)s; } diff --git a/plugins/USBqemu/qemu-usb/usb-msd.c b/plugins/USBqemu/qemu-usb/usb-msd.c index ff2047d4b2..9d8d0443d9 100644 --- a/plugins/USBqemu/qemu-usb/usb-msd.c +++ b/plugins/USBqemu/qemu-usb/usb-msd.c @@ -1,4 +1,4 @@ -/* +/* * USB Mass Storage Device emulation * * Copyright (c) 2006 CodeSourcery. @@ -7,15 +7,22 @@ * This code is licenced under the LGPL. */ -#include "vl.h" +#include "qemu-common.h" +#include "qemu-option.h" +#include "qemu-config.h" +#include "usb.h" +#include "block.h" +#include "scsi.h" +#include "console.h" +#include "monitor.h" //#define DEBUG_MSD #ifdef DEBUG_MSD -#define DPRINTF(fmt, args...) \ -do { printf("usb-msd: " fmt , ##args); } while (0) +#define DPRINTF(fmt, ...) \ +do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0) #else -#define DPRINTF(fmt, args...) do {} while(0) +#define DPRINTF(fmt, ...) do {} while(0) #endif /* USB requests. */ @@ -32,16 +39,42 @@ enum USBMSDMode { typedef struct { USBDevice dev; enum USBMSDMode mode; + uint32_t scsi_len; + uint8_t *scsi_buf; + uint32_t usb_len; + uint8_t *usb_buf; uint32_t data_len; + uint32_t residue; uint32_t tag; + SCSIBus bus; + DriveInfo *dinfo; SCSIDevice *scsi_dev; int result; + /* For async completion. */ + USBPacket *packet; } MSDState; +struct usb_msd_cbw { + uint32_t sig; + uint32_t tag; + uint32_t data_len; + uint8_t flags; + uint8_t lun; + uint8_t cmd_len; + uint8_t cmd[16]; +}; + +struct usb_msd_csw { + uint32_t sig; + uint32_t tag; + uint32_t residue; + uint8_t status; +}; + static const uint8_t qemu_msd_dev_descriptor[] = { 0x12, /* u8 bLength; */ 0x01, /* u8 bDescriptorType; Device */ - 0x10, 0x00, /* u16 bcdUSB; v1.0 */ + 0x00, 0x01, /* u16 bcdUSB; v1.0 */ 0x00, /* u8 bDeviceClass; */ 0x00, /* u8 bDeviceSubClass; */ @@ -68,13 +101,13 @@ static const uint8_t qemu_msd_config_descriptor[] = { 0x01, /* u8 bNumInterfaces; (1) */ 0x01, /* u8 bConfigurationValue; */ 0x00, /* u8 iConfiguration; */ - 0xc0, /* u8 bmAttributes; + 0xc0, /* u8 bmAttributes; Bit 7: must be set, 6: Self-powered, 5: Remote wakeup, 4..0: resvd */ 0x00, /* u8 MaxPower; */ - + /* one interface */ 0x09, /* u8 if_bLength; */ 0x04, /* u8 if_bDescriptorType; Interface */ @@ -85,7 +118,7 @@ static const uint8_t qemu_msd_config_descriptor[] = { 0x06, /* u8 if_bInterfaceSubClass; SCSI */ 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */ 0x00, /* u8 if_iInterface; */ - + /* Bulk-In endpoint */ 0x07, /* u8 ep_bLength; */ 0x05, /* u8 ep_bDescriptorType; Endpoint */ @@ -103,13 +136,91 @@ static const uint8_t qemu_msd_config_descriptor[] = { 0x00 /* u8 ep_bInterval; */ }; -static void usb_msd_command_complete(void *opaque, uint32_t tag, int fail) +static void usb_msd_copy_data(MSDState *s) { - MSDState *s = (MSDState *)opaque; + uint32_t len; + len = s->usb_len; + if (len > s->scsi_len) + len = s->scsi_len; + if (s->mode == USB_MSDM_DATAIN) { + memcpy(s->usb_buf, s->scsi_buf, len); + } else { + memcpy(s->scsi_buf, s->usb_buf, len); + } + s->usb_len -= len; + s->scsi_len -= len; + s->usb_buf += len; + s->scsi_buf += len; + s->data_len -= len; + if (s->scsi_len == 0) { + if (s->mode == USB_MSDM_DATAIN) { + s->scsi_dev->info->read_data(s->scsi_dev, s->tag); + } else if (s->mode == USB_MSDM_DATAOUT) { + s->scsi_dev->info->write_data(s->scsi_dev, s->tag); + } + } +} - DPRINTF("Command complete\n"); - s->result = fail; - s->mode = USB_MSDM_CSW; +static void usb_msd_send_status(MSDState *s) +{ + struct usb_msd_csw csw; + + csw.sig = cpu_to_le32(0x53425355); + csw.tag = cpu_to_le32(s->tag); + csw.residue = s->residue; + csw.status = s->result; + memcpy(s->usb_buf, &csw, 13); +} + +static void usb_msd_command_complete(SCSIBus *bus, int reason, uint32_t tag, + uint32_t arg) +{ + MSDState *s = DO_UPCAST(MSDState, dev.qdev, bus->qbus.parent); + USBPacket *p = s->packet; + + if (tag != s->tag) { + fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag); + } + if (reason == SCSI_REASON_DONE) { + DPRINTF("Command complete %d\n", arg); + s->residue = s->data_len; + s->result = arg != 0; + if (s->packet) { + if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { + /* A deferred packet with no write data remaining must be + the status read packet. */ + usb_msd_send_status(s); + s->mode = USB_MSDM_CBW; + } else { + if (s->data_len) { + s->data_len -= s->usb_len; + if (s->mode == USB_MSDM_DATAIN) + memset(s->usb_buf, 0, s->usb_len); + s->usb_len = 0; + } + if (s->data_len == 0) + s->mode = USB_MSDM_CSW; + } + s->packet = NULL; + usb_packet_complete(p); + } else if (s->data_len == 0) { + s->mode = USB_MSDM_CSW; + } + return; + } + s->scsi_len = arg; + s->scsi_buf = s->scsi_dev->info->get_buf(s->scsi_dev, tag); + if (p) { + usb_msd_copy_data(s); + if (s->usb_len == 0) { + /* Set s->packet to NULL before calling usb_packet_complete + because annother request may be issued before + usb_packet_complete returns. */ + DPRINTF("Packet complete %p\n", p); + s->packet = NULL; + usb_packet_complete(p); + } + } } static void usb_msd_handle_reset(USBDevice *dev) @@ -156,12 +267,12 @@ static int usb_msd_handle_control(USBDevice *dev, int request, int value, case DeviceRequest | USB_REQ_GET_DESCRIPTOR: switch(value >> 8) { case USB_DT_DEVICE: - memcpy(data, qemu_msd_dev_descriptor, + memcpy(data, qemu_msd_dev_descriptor, sizeof(qemu_msd_dev_descriptor)); ret = sizeof(qemu_msd_dev_descriptor); break; case USB_DT_CONFIG: - memcpy(data, qemu_msd_config_descriptor, + memcpy(data, qemu_msd_config_descriptor, sizeof(qemu_msd_config_descriptor)); ret = sizeof(qemu_msd_config_descriptor); break; @@ -233,32 +344,24 @@ static int usb_msd_handle_control(USBDevice *dev, int request, int value, return ret; } -struct usb_msd_cbw { - uint32_t sig; - uint32_t tag; - uint32_t data_len; - uint8_t flags; - uint8_t lun; - uint8_t cmd_len; - uint8_t cmd[16]; -}; +static void usb_msd_cancel_io(USBPacket *p, void *opaque) +{ + MSDState *s = opaque; + s->scsi_dev->info->cancel_io(s->scsi_dev, s->tag); + s->packet = NULL; + s->scsi_len = 0; +} -struct usb_msd_csw { - uint32_t sig; - uint32_t tag; - uint32_t residue; - uint8_t status; -}; - -static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep, - uint8_t *data, int len) +static int usb_msd_handle_data(USBDevice *dev, USBPacket *p) { MSDState *s = (MSDState *)dev; int ret = 0; struct usb_msd_cbw cbw; - struct usb_msd_csw csw; + uint8_t devep = p->devep; + uint8_t *data = p->data; + int len = p->len; - switch (pid) { + switch (p->pid) { case USB_TOKEN_OUT: if (devep != 2) goto fail; @@ -291,7 +394,17 @@ static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep, } DPRINTF("Command tag 0x%x flags %08x len %d data %d\n", s->tag, cbw.flags, cbw.cmd_len, s->data_len); - scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0); + s->residue = 0; + s->scsi_dev->info->send_command(s->scsi_dev, s->tag, cbw.cmd, 0); + /* ??? Should check that USB and SCSI data transfer + directions match. */ + if (s->residue == 0) { + if (s->mode == USB_MSDM_DATAIN) { + s->scsi_dev->info->read_data(s->scsi_dev, s->tag); + } else if (s->mode == USB_MSDM_DATAOUT) { + s->scsi_dev->info->write_data(s->scsi_dev, s->tag); + } + } ret = len; break; @@ -300,13 +413,25 @@ static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep, if (len > s->data_len) goto fail; - if (scsi_write_data(s->scsi_dev, data, len)) - goto fail; - - s->data_len -= len; - if (s->data_len == 0) - s->mode = USB_MSDM_CSW; - ret = len; + s->usb_buf = data; + s->usb_len = len; + if (s->scsi_len) { + usb_msd_copy_data(s); + } + if (s->residue && s->usb_len) { + s->data_len -= s->usb_len; + if (s->data_len == 0) + s->mode = USB_MSDM_CSW; + s->usb_len = 0; + } + if (s->usb_len) { + DPRINTF("Deferring packet %p\n", p); + usb_defer_packet(p, usb_msd_cancel_io, s); + s->packet = p; + ret = USB_RET_ASYNC; + } else { + ret = len; + } break; default: @@ -320,33 +445,52 @@ static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep, goto fail; switch (s->mode) { + case USB_MSDM_DATAOUT: + if (s->data_len != 0 || len < 13) + goto fail; + /* Waiting for SCSI write to complete. */ + usb_defer_packet(p, usb_msd_cancel_io, s); + s->packet = p; + ret = USB_RET_ASYNC; + break; + case USB_MSDM_CSW: DPRINTF("Command status %d tag 0x%x, len %d\n", s->result, s->tag, len); if (len < 13) goto fail; - csw.sig = cpu_to_le32(0x53425355); - csw.tag = cpu_to_le32(s->tag); - csw.residue = 0; - csw.status = s->result; - memcpy(data, &csw, 13); - ret = 13; + s->usb_len = len; + s->usb_buf = data; + usb_msd_send_status(s); s->mode = USB_MSDM_CBW; + ret = 13; break; case USB_MSDM_DATAIN: DPRINTF("Data in %d/%d\n", len, s->data_len); if (len > s->data_len) len = s->data_len; - - if (scsi_read_data(s->scsi_dev, data, len)) - goto fail; - - s->data_len -= len; - if (s->data_len == 0) - s->mode = USB_MSDM_CSW; - ret = len; + s->usb_buf = data; + s->usb_len = len; + if (s->scsi_len) { + usb_msd_copy_data(s); + } + if (s->residue && s->usb_len) { + s->data_len -= s->usb_len; + memset(s->usb_buf, 0, s->usb_len); + if (s->data_len == 0) + s->mode = USB_MSDM_CSW; + s->usb_len = 0; + } + if (s->usb_len) { + DPRINTF("Deferring packet %p\n", p); + usb_defer_packet(p, usb_msd_cancel_io, s); + s->packet = p; + ret = USB_RET_ASYNC; + } else { + ret = len; + } break; default: @@ -365,38 +509,118 @@ static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep, return ret; } -static void usb_msd_handle_destroy(USBDevice *dev) +static void usb_msd_password_cb(void *opaque, int err) { - MSDState *s = (MSDState *)dev; + MSDState *s = opaque; - scsi_disk_destroy(s->scsi_dev); - qemu_free(s); + if (!err) + usb_device_attach(&s->dev); + else + qdev_unplug(&s->dev.qdev); } -USBDevice *usb_msd_init(const char *filename) +static int usb_msd_initfn(USBDevice *dev) { - MSDState *s; - BlockDriverState *bdrv; + MSDState *s = DO_UPCAST(MSDState, dev, dev); - s = qemu_mallocz(sizeof(MSDState)); - if (!s) - return NULL; - - bdrv = bdrv_new("usb"); - bdrv_open(bdrv, filename, 0); + if (!s->dinfo || !s->dinfo->bdrv) { + qemu_error("usb-msd: drive property not set\n"); + return -1; + } s->dev.speed = USB_SPEED_FULL; - s->dev.handle_packet = usb_generic_handle_packet; + scsi_bus_new(&s->bus, &s->dev.qdev, 0, 1, usb_msd_command_complete); + s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, s->dinfo, 0); + s->bus.qbus.allow_hotplug = 0; + usb_msd_handle_reset(dev); - s->dev.handle_reset = usb_msd_handle_reset; - s->dev.handle_control = usb_msd_handle_control; - s->dev.handle_data = usb_msd_handle_data; - s->dev.handle_destroy = usb_msd_handle_destroy; + if (bdrv_key_required(s->dinfo->bdrv)) { + if (s->dev.qdev.hotplugged) { + monitor_read_bdrv_key_start(cur_mon, s->dinfo->bdrv, + usb_msd_password_cb, s); + s->dev.auto_attach = 0; + } else { + autostart = 0; + } + } - snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)", - filename); - - s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s); - usb_msd_handle_reset((USBDevice *)s); - return (USBDevice *)s; + return 0; } + +static USBDevice *usb_msd_init(const char *filename) +{ + static int nr=0; + char id[8]; + QemuOpts *opts; + DriveInfo *dinfo; + USBDevice *dev; + int fatal_error; + const char *p1; + char fmt[32]; + + /* parse -usbdevice disk: syntax into drive opts */ + snprintf(id, sizeof(id), "usb%d", nr++); + opts = qemu_opts_create(&qemu_drive_opts, id, 0); + + p1 = strchr(filename, ':'); + if (p1++) { + const char *p2; + + if (strstart(filename, "format=", &p2)) { + int len = MIN(p1 - p2, sizeof(fmt)); + pstrcpy(fmt, len, p2); + qemu_opt_set(opts, "format", fmt); + } else if (*filename != ':') { + printf("unrecognized USB mass-storage option %s\n", filename); + return NULL; + } + filename = p1; + } + if (!*filename) { + printf("block device specification needed\n"); + return NULL; + } + qemu_opt_set(opts, "file", filename); + qemu_opt_set(opts, "if", "none"); + + /* create host drive */ + dinfo = drive_init(opts, NULL, &fatal_error); + if (!dinfo) { + qemu_opts_del(opts); + return NULL; + } + + /* create guest device */ + dev = usb_create(NULL /* FIXME */, "usb-storage"); + if (!dev) { + return NULL; + } + qdev_prop_set_drive(&dev->qdev, "drive", dinfo); + if (qdev_init(&dev->qdev) < 0) + return NULL; + + return dev; +} + +static struct USBDeviceInfo msd_info = { + .product_desc = "QEMU USB MSD", + .qdev.name = "usb-storage", + .qdev.size = sizeof(MSDState), + .init = usb_msd_initfn, + .handle_packet = usb_generic_handle_packet, + .handle_reset = usb_msd_handle_reset, + .handle_control = usb_msd_handle_control, + .handle_data = usb_msd_handle_data, + .usbdevice_name = "disk", + .usbdevice_init = usb_msd_init, + .qdev.props = (Property[]) { + DEFINE_PROP_DRIVE("drive", MSDState, dinfo), + DEFINE_PROP_END_OF_LIST(), + }, +}; + +static void usb_msd_register_devices(void) +{ + usb_qdev_register(&msd_info); +} +device_init(usb_msd_register_devices) diff --git a/plugins/USBqemu/qemu-usb/usb-ohci.cpp b/plugins/USBqemu/qemu-usb/usb-ohci.cpp index 0432149a35..ce5cc90817 100644 --- a/plugins/USBqemu/qemu-usb/usb-ohci.cpp +++ b/plugins/USBqemu/qemu-usb/usb-ohci.cpp @@ -1,985 +1,1456 @@ -/* - * QEMU USB OHCI Emulation - * Copyright (c) 2004 Gianni Tedesco - * Copyright (c) 2006 CodeSourcery - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * TODO: - * o Isochronous transfers - * o Allocate bandwidth in frames properly - * o Disable timers when nothing needs to be done, or remove timer usage - * all together. - * o Handle unrecoverable errors properly - * o BIOS work to boot from USB storage -*/ - -//typedef CPUReadMemoryFunc - -#include "vl.h" -#include "../USB.h" - -uint32_t bits = 0; -uint32_t need_interrupt = 0; - -extern FILE* usbLog; - -int dprintf(const char *fmt,...) -{ -#ifdef DEBUG_OHCI - int t; - va_list list; - - va_start(list, fmt); - t=vfprintf(stderr, fmt, list); - va_end(list); - - if(usbLog) - { - va_start(list, fmt); - vfprintf(usbLog, fmt, list); - va_end(list); - } - - return t; -#else - return 0; -#endif -} - -/* Update IRQ levels */ -static inline void ohci_intr_update(OHCIState *ohci) -{ - bits = (ohci->intr_status & ohci->intr) & 0x7fffffff; - - if ((ohci->intr & OHCI_INTR_MIE) && (bits!=0)) // && (ohci->ctl & OHCI_CTL_HCFS)) - { - /* - static char reasons[1024]; - int first=1; - - reasons[0]=0; - -#define reason_add(p,t) if(bits&(p)) { if(!first) strcat_s(reasons,1024,", "); first=0; strcat_s(reasons,1024,t); } - reason_add(OHCI_INTR_SO,"Scheduling overrun"); - reason_add(OHCI_INTR_WD,"HcDoneHead writeback"); - reason_add(OHCI_INTR_SF,"Start of frame"); - reason_add(OHCI_INTR_RD,"Resume detect"); - reason_add(OHCI_INTR_UE,"Unrecoverable error"); - reason_add(OHCI_INTR_FNO,"Frame number overflow"); - reason_add(OHCI_INTR_RHSC,"Root hub status change"); - reason_add(OHCI_INTR_OC,"Ownership change"); - */ - if((ohci->ctl & OHCI_CTL_HCFS)==OHCI_USB_OPERATIONAL) - { - USBirq(1); - //dprintf("usb-ohci: Interrupt Called. Reason(s): %s\n",reasons); - } - } -} - -/* Set an interrupt */ -static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr) -{ - ohci->intr_status |= intr; - ohci_intr_update(ohci); -} - -//static void usb_attach(USBPort *port, USBDevice *dev) -//{ -// port->attach(port,dev); -//} - -/* Attach or detach a device on a root hub port. */ -static void ohci_attach(USBPort *port1, USBDevice *dev) -{ - OHCIState *s = (OHCIState *)port1->opaque; - OHCIPort *port = (OHCIPort *)&s->rhport[port1->index]; - uint32_t old_state = port->ctrl; - - if (dev) { - if (port->port.dev) { - usb_attach(port1, NULL); - } - /* set connect status */ - port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC; - - /* update speed */ - if (dev->speed == USB_SPEED_LOW) - port->ctrl |= OHCI_PORT_LSDA; - else - port->ctrl &= ~OHCI_PORT_LSDA; - port->port.dev = dev; - /* send the attach message */ - dev->handle_packet(dev, - USB_MSG_ATTACH, 0, 0, NULL, 0); - dprintf("usb-ohci: Attached port %d\n", port1->index); - } else { - /* set connect status */ - if (port->ctrl & OHCI_PORT_CCS) { - port->ctrl &= ~OHCI_PORT_CCS; - port->ctrl |= OHCI_PORT_CSC; - } - /* disable port */ - if (port->ctrl & OHCI_PORT_PES) { - port->ctrl &= ~OHCI_PORT_PES; - port->ctrl |= OHCI_PORT_PESC; - } - dev = port->port.dev; - if (dev) { - /* send the detach message */ - dev->handle_packet(dev, - USB_MSG_DETACH, 0, 0, NULL, 0); - } - port->port.dev = NULL; - dprintf("usb-ohci: Detached port %d\n", port1->index); - } - - if (old_state != port->ctrl) - ohci_set_interrupt(s, OHCI_INTR_RHSC); -} - -/* Reset the controller */ -static void ohci_reset(OHCIState *ohci) -{ - OHCIPort *port; - int i; - - ohci->ctl = 0; - ohci->status = 0; - ohci->intr_status = 0; - ohci->intr = OHCI_INTR_MIE; - - - - ohci->hcca = 0; - ohci->ctrl_head = ohci->ctrl_cur = 0; - ohci->bulk_head = ohci->bulk_cur = 0; - ohci->per_cur = 0; - ohci->done = 0; - ohci->done_count = 7; - - /* FSMPS is marked TBD in OCHI 1.0, what gives ffs? - * I took the value linux sets ... - */ - ohci->fsmps = 0x2778; - ohci->fi = 0x2edf; - ohci->fit = 0; - ohci->frt = 0; - ohci->frame_number = 0; - ohci->pstart = 0; - ohci->lst = OHCI_LS_THRESH; - - ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports; - ohci->rhdesc_b = 0x0; /* Impl. specific */ - ohci->rhstatus = 0; - - for (i = 0; i < ohci->num_ports; i++) - { - port = &ohci->rhport[i]; - port->ctrl = 0; - if (port->port.dev) - ohci_attach(&port->port, port->port.dev); - } - dprintf("usb-ohci: Reset.\n"); -} - -#define le32_to_cpu(x) (x) -#define cpu_to_le32(x) (x) - -/* Get an array of dwords from main memory */ -static inline int get_dwords(uint32_t addr, uint32_t *buf, int num) -{ - int i; - - for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { - cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0); - *buf = le32_to_cpu(*buf); - } - - return 1; -} - -/* Put an array of dwords in to main memory */ -static inline int put_dwords(uint32_t addr, uint32_t *buf, int num) -{ - int i; - - for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { - uint32_t tmp = cpu_to_le32(*buf); - cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1); - } - - return 1; -} - -static inline int ohci_read_ed(uint32_t addr, struct ohci_ed *ed) -{ - return get_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2); -} - -static inline int ohci_read_td(uint32_t addr, struct ohci_td *td) -{ - return get_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2); -} - -static inline int ohci_put_ed(uint32_t addr, struct ohci_ed *ed) -{ - return put_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2); -} - -static inline int ohci_put_td(uint32_t addr, struct ohci_td *td) -{ - return put_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2); -} - -/* Read/Write the contents of a TD from/to main memory. */ -static void ohci_copy_td(struct ohci_td *td, uint8_t *buf, int len, int write) -{ - uint32_t ptr; - uint32_t n; - - ptr = td->cbp; - n = 0x1000 - (ptr & 0xfff); - if (n > len) - n = len; - cpu_physical_memory_rw(ptr, buf, n, write); - if (n == len) - return; - ptr = td->be & ~0xfffu; - buf += n; - cpu_physical_memory_rw(ptr, buf, len - n, write); -} - -/* Service a transport descriptor. - Returns nonzero to terminate processing of this endpoint. */ - -static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) -{ - int dir; - size_t len = 0; - uint8_t buf[8192]; - char *str = NULL; - int pid; - int ret; - int i; - USBDevice *dev; - struct ohci_td td; - uint32_t addr; - int flag_r; - - addr = ed->head & OHCI_DPTR_MASK; - if (!ohci_read_td(addr, &td)) { - fprintf(stderr, "usb-ohci: TD read error at %x\n", addr); - return 0; - } - - dir = OHCI_BM(ed->flags, ED_D); - switch (dir) { - case OHCI_TD_DIR_OUT: - case OHCI_TD_DIR_IN: - /* Same value. */ - break; - default: - dir = OHCI_BM(td.flags, TD_DP); - break; - } - - switch (dir) { - case OHCI_TD_DIR_IN: - str = "in"; - pid = USB_TOKEN_IN; - break; - case OHCI_TD_DIR_OUT: - str = "out"; - pid = USB_TOKEN_OUT; - break; - case OHCI_TD_DIR_SETUP: - str = "setup"; - pid = USB_TOKEN_SETUP; - break; - default: - fprintf(stderr, "usb-ohci: Bad direction\n"); - return 1; - } - if (td.cbp && td.be) { - if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) { - len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff); - } else { - len = (td.be - td.cbp) + 1; - } - - if (len && dir != OHCI_TD_DIR_IN) { - ohci_copy_td(&td, buf, len, 0); - } - } - - flag_r = (td.flags & OHCI_TD_R) != 0; -#ifdef DEBUG_PACKET - dprintf(" TD @ 0x%.8x %u bytes %s r=%d cbp=0x%.8x be=0x%.8x\n", - addr, len, str, flag_r, td.cbp, td.be); - - if (len >= 0 && dir != OHCI_TD_DIR_IN) { - dprintf(" data:"); - for (i = 0; i < len; i++) - printf(" %.2x", buf[i]); - dprintf("\n"); - } -#endif - ret = USB_RET_NODEV; - for (i = 0; i < ohci->num_ports; i++) { - dev = ohci->rhport[i].port.dev; - if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) - continue; - - ret = dev->handle_packet(dev, pid, OHCI_BM(ed->flags, ED_FA), - OHCI_BM(ed->flags, ED_EN), buf, len); - if (ret != USB_RET_NODEV) - break; - } -#ifdef DEBUG_PACKET - dprintf("ret=%d\n", ret); -#endif - if (ret >= 0) { - if (dir == OHCI_TD_DIR_IN) { - ohci_copy_td(&td, buf, ret, 1); -#ifdef DEBUG_PACKET - dprintf(" data:"); - for (i = 0; i < ret; i++) - printf(" %.2x", buf[i]); - dprintf("\n"); -#endif - } else { - ret = len; - } - } - - /* Writeback */ - if (ret == len || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) { - /* Transmission succeeded. */ - if (ret == len) { - td.cbp = 0; - } else { - td.cbp += ret; - if ((td.cbp & 0xfff) + ret > 0xfff) { - td.cbp &= 0xfff; - td.cbp |= td.be & ~0xfff; - } - } - td.flags |= OHCI_TD_T1; - td.flags ^= OHCI_TD_T0; - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR); - OHCI_SET_BM(td.flags, TD_EC, 0); - - ed->head &= ~OHCI_ED_C; - if (td.flags & OHCI_TD_T0) - ed->head |= OHCI_ED_C; - } else { - if (ret >= 0) { - dprintf("usb-ohci: Underrun\n"); - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN); - } else { - switch (ret) { - case USB_RET_NODEV: - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING); - case USB_RET_NAK: - dprintf("usb-ohci: got NAK\n"); - return 1; - case USB_RET_STALL: - dprintf("usb-ohci: got STALL\n"); - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL); - break; - case USB_RET_BABBLE: - dprintf("usb-ohci: got BABBLE\n"); - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN); - break; - default: - fprintf(stderr, "usb-ohci: Bad device response %d\n", ret); - OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID); - OHCI_SET_BM(td.flags, TD_EC, 3); - break; - } - } - ed->head |= OHCI_ED_H; - } - - /* Retire this TD */ - ed->head &= ~OHCI_DPTR_MASK; - ed->head |= td.next & OHCI_DPTR_MASK; - td.next = ohci->done; - ohci->done = addr; - i = OHCI_BM(td.flags, TD_DI); - if (i < ohci->done_count) - ohci->done_count = i; - ohci_put_td(addr, &td); - return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR; -} - -/* Service an endpoint list. Returns nonzero if active TD were found. */ -static int ohci_service_ed_list(OHCIState *ohci, uint32_t head) -{ - struct ohci_ed ed; - uint32_t next_ed; - uint32_t cur; - int active; - - active = 0; - - if (head == 0) - return 0; - - for (cur = head; cur; cur = next_ed) { - if (!ohci_read_ed(cur, &ed)) { - fprintf(stderr, "usb-ohci: ED read error at %x\n", cur); - return 0; - } - - next_ed = ed.next & OHCI_DPTR_MASK; - - if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) - continue; - - /* Skip isochronous endpoints. */ - if (ed.flags & OHCI_ED_F) - continue; - - while ((ed.head & OHCI_DPTR_MASK) != ed.tail) { -#ifdef DEBUG_PACKET - dprintf("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u " - "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur, - OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN), - OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0, - (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0, - OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0, - (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK, - ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK); -#endif - active = 1; - - if (ohci_service_td(ohci, &ed)) - break; - } - - ohci_put_ed(cur, &ed); - } - - return active; -} - -/* Generate a SOF event, and set a timer for EOF */ -static void ohci_sof(OHCIState *ohci) -{ - ohci->sof_time = get_clock(); - ohci->eof_timer = usb_frame_time; - ohci_set_interrupt(ohci, OHCI_INTR_SF); -} - -/* Do frame processing on frame boundary */ -void ohci_frame_boundary(void *opaque) -{ - OHCIState *ohci = (OHCIState *)opaque; - struct ohci_hcca hcca; - - cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 0); - - /* Process all the lists at the end of the frame */ - if (ohci->ctl & OHCI_CTL_PLE) { - int n; - - n = ohci->frame_number & 0x1f; - ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n])); - } - if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) { - if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) - dprintf("usb-ohci: head %x, cur %x\n", ohci->ctrl_head, ohci->ctrl_cur); - if (!ohci_service_ed_list(ohci, ohci->ctrl_head)) { - ohci->ctrl_cur = 0; - ohci->status &= ~OHCI_STATUS_CLF; - } - } - - if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) { - if (!ohci_service_ed_list(ohci, ohci->bulk_head)) { - ohci->bulk_cur = 0; - ohci->status &= ~OHCI_STATUS_BLF; - } - } - - /* Frame boundary, so do EOF stuf here */ - ohci->frt = ohci->fit; - - /* XXX: endianness */ - ohci->frame_number = (ohci->frame_number + 1) & 0xffff; - hcca.frame = cpu_to_le32(ohci->frame_number); - - if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) { - if (!ohci->done) - abort(); - if (ohci->intr & ohci->intr_status) - ohci->done |= 1; - hcca.done = cpu_to_le32(ohci->done); - ohci->done = 0; - ohci->done_count = 7; - ohci_set_interrupt(ohci, OHCI_INTR_WD); - } - - if (ohci->done_count != 7 && ohci->done_count != 0) - ohci->done_count--; - - /* Do SOF stuff here */ - ohci_sof(ohci); - - /* Writeback HCCA */ - cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 1); -} - -/* Start sending SOF tokens across the USB bus, lists are processed in - * next frame - */ -int ohci_bus_start(OHCIState *ohci) -{ - ohci->eof_timer = 0; - - dprintf("usb-ohci: USB Operational\n"); - - ohci_sof(ohci); - - return 1; -} - -/* Stop sending SOF tokens on the bus */ -void ohci_bus_stop(OHCIState *ohci) -{ - if (ohci->eof_timer) - ohci->eof_timer=0; -} - -/* Sets a flag in a port status register but only set it if the port is - * connected, if not set ConnectStatusChange flag. If flag is enabled - * return 1. - */ -static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val) -{ - int ret = 1; - - /* writing a 0 has no effect */ - if (val == 0) - return 0; - - /* If CurrentConnectStatus is cleared we set - * ConnectStatusChange - */ - if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) { - ohci->rhport[i].ctrl |= OHCI_PORT_CSC; - if (ohci->rhstatus & OHCI_RHS_DRWE) { - /* TODO: CSC is a wakeup event */ - } - return 0; - } - - if (ohci->rhport[i].ctrl & val) - ret = 0; - - /* set the bit */ - ohci->rhport[i].ctrl |= val; - - return ret; -} - -/* Set the frame interval - frame interval toggle is manipulated by the hcd only */ -static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val) -{ - val &= OHCI_FMI_FI; - - if (val != ohci->fi) { - dprintf("usb-ohci: FrameInterval = 0x%x (%u)\n", ohci->fi, ohci->fi); - } - - ohci->fi = val; -} - -static void ohci_port_power(OHCIState *ohci, int i, int p) -{ - if (p) { - ohci->rhport[i].ctrl |= OHCI_PORT_PPS; - } else { - ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS| - OHCI_PORT_CCS| - OHCI_PORT_PSS| - OHCI_PORT_PRS); - } -} - -/* Set HcControlRegister */ -static void ohci_set_ctl(OHCIState *ohci, uint32_t val) -{ - uint32_t old_state; - uint32_t new_state; - - old_state = ohci->ctl & OHCI_CTL_HCFS; - ohci->ctl = val; - new_state = ohci->ctl & OHCI_CTL_HCFS; - - /* no state change */ - if (old_state == new_state) - return; - - switch (new_state) { - case OHCI_USB_OPERATIONAL: - ohci_bus_start(ohci); - break; - case OHCI_USB_SUSPEND: - ohci_bus_stop(ohci); - dprintf("usb-ohci: USB Suspended\n"); - break; - case OHCI_USB_RESUME: - dprintf("usb-ohci: USB Resume\n"); - break; - case OHCI_USB_RESET: - dprintf("usb-ohci: USB Reset\n"); - break; - } - ohci_intr_update(ohci); -} - -static uint32_t ohci_get_frame_remaining(OHCIState *ohci) -{ - uint16_t fr; - int64_t tks; - - if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL) - return (ohci->frt << 31); - - /* Being in USB operational state guarnatees sof_time was - * set already. - */ - tks = get_clock() - ohci->sof_time; - - /* avoid muldiv if possible */ - if (tks >= usb_frame_time) - return (ohci->frt << 31); - - tks = muldiv64(1, tks, usb_bit_time); - fr = (uint16_t)(ohci->fi - tks); - - return (ohci->frt << 31) | fr; -} - - -/* Set root hub status */ -static void ohci_set_hub_status(OHCIState *ohci, uint32_t val) -{ - uint32_t old_state; - - old_state = ohci->rhstatus; - - /* write 1 to clear OCIC */ - if (val & OHCI_RHS_OCIC) - ohci->rhstatus &= ~OHCI_RHS_OCIC; - - if (val & OHCI_RHS_LPS) { - int i; - - for (i = 0; i < ohci->num_ports; i++) - ohci_port_power(ohci, i, 0); - dprintf("usb-ohci: powered down all ports\n"); - } - - if (val & OHCI_RHS_LPSC) { - int i; - - for (i = 0; i < ohci->num_ports; i++) - ohci_port_power(ohci, i, 1); - dprintf("usb-ohci: powered up all ports\n"); - } - - if (val & OHCI_RHS_DRWE) - ohci->rhstatus |= OHCI_RHS_DRWE; - - if (val & OHCI_RHS_CRWE) - ohci->rhstatus &= ~OHCI_RHS_DRWE; - - if (old_state != ohci->rhstatus) - ohci_set_interrupt(ohci, OHCI_INTR_RHSC); -} - -/* Set root hub port status */ -static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val) -{ - uint32_t old_state; - OHCIPort *port; - - port = &ohci->rhport[portnum]; - old_state = port->ctrl; - - /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */ - if (val & OHCI_PORT_WTC) - port->ctrl &= ~(val & OHCI_PORT_WTC); - - if (val & OHCI_PORT_CCS) - port->ctrl &= ~OHCI_PORT_PES; - - ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES); - - if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) - dprintf("usb-ohci: port %d: SUSPEND\n", portnum); - - if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) { - dprintf("usb-ohci: port %d: RESET\n", portnum); - port->port.dev->handle_packet(port->port.dev, USB_MSG_RESET, - 0, 0, NULL, 0); - port->ctrl &= ~OHCI_PORT_PRS; - /* ??? Should this also set OHCI_PORT_PESC. */ - port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC; - } - - /* Invert order here to ensure in ambiguous case, device is - * powered up... - */ - if (val & OHCI_PORT_LSDA) - ohci_port_power(ohci, portnum, 0); - if (val & OHCI_PORT_PPS) - ohci_port_power(ohci, portnum, 1); - - if (old_state != port->ctrl) - ohci_set_interrupt(ohci, OHCI_INTR_RHSC); - - return; -} - -uint32_t ohci_mem_read(OHCIState *ptr, uint32_t addr) -{ - OHCIState *ohci = ptr; - - addr -= ohci->mem_base; - - /* Only aligned reads are allowed on OHCI */ - if (addr & 3) { - fprintf(stderr, "usb-ohci: Mis-aligned read\n"); - return 0xffffffff; - } - - if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) { - /* HcRhPortStatus */ - return ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS; - } - - switch (addr >> 2) { - case 0: /* HcRevision */ - return 0x10; - - case 1: /* HcControl */ - return ohci->ctl; - - case 2: /* HcCommandStatus */ - return ohci->status; - - case 3: /* HcInterruptStatus */ - return ohci->intr_status; - - case 4: /* HcInterruptEnable */ - case 5: /* HcInterruptDisable */ - return ohci->intr; - - case 6: /* HcHCCA */ - return ohci->hcca; - - case 7: /* HcPeriodCurrentED */ - return ohci->per_cur; - - case 8: /* HcControlHeadED */ - return ohci->ctrl_head; - - case 9: /* HcControlCurrentED */ - return ohci->ctrl_cur; - - case 10: /* HcBulkHeadED */ - return ohci->bulk_head; - - case 11: /* HcBulkCurrentED */ - return ohci->bulk_cur; - - case 12: /* HcDoneHead */ - return ohci->done; - - case 13: /* HcFmInterval */ - return (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi); - - case 14: /* HcFmRemaining */ - return ohci_get_frame_remaining(ohci); - - case 15: /* HcFmNumber */ - return ohci->frame_number; - - case 16: /* HcPeriodicStart */ - return ohci->pstart; - - case 17: /* HcLSThreshold */ - return ohci->lst; - - case 18: /* HcRhDescriptorA */ - return ohci->rhdesc_a; - - case 19: /* HcRhDescriptorB */ - return ohci->rhdesc_b; - - case 20: /* HcRhStatus */ - return ohci->rhstatus; - - default: - fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr); - return 0xffffffff; - } -} - -void ohci_mem_write(OHCIState *ptr,uint32_t addr, uint32_t val) -{ - OHCIState *ohci = ptr; - - addr -= ohci->mem_base; - - /* Only aligned reads are allowed on OHCI */ - if (addr & 3) { - fprintf(stderr, "usb-ohci: Mis-aligned write\n"); - return; - } - - if ((addr >= 0x54) && (addr < (0x54 + ohci->num_ports * 4))) { - /* HcRhPortStatus */ - ohci_port_set_status(ohci, (addr - 0x54) >> 2, val); - return; - } - - switch (addr >> 2) { - case 1: /* HcControl */ - ohci_set_ctl(ohci, val); - break; - - case 2: /* HcCommandStatus */ - /* SOC is read-only */ - val = (val & ~OHCI_STATUS_SOC); - - /* Bits written as '0' remain unchanged in the register */ - ohci->status |= val; - - if (ohci->status & OHCI_STATUS_HCR) - ohci_reset(ohci); - break; - - case 3: /* HcInterruptStatus */ - ohci->intr_status &= ~val; - ohci_intr_update(ohci); - break; - - case 4: /* HcInterruptEnable */ - ohci->intr |= val; - ohci_intr_update(ohci); - break; - - case 5: /* HcInterruptDisable */ - ohci->intr &= ~val; - ohci_intr_update(ohci); - break; - - case 6: /* HcHCCA */ - ohci->hcca = val & OHCI_HCCA_MASK; - break; - - case 8: /* HcControlHeadED */ - ohci->ctrl_head = val & OHCI_EDPTR_MASK; - break; - - case 9: /* HcControlCurrentED */ - ohci->ctrl_cur = val & OHCI_EDPTR_MASK; - break; - - case 10: /* HcBulkHeadED */ - ohci->bulk_head = val & OHCI_EDPTR_MASK; - break; - - case 11: /* HcBulkCurrentED */ - ohci->bulk_cur = val & OHCI_EDPTR_MASK; - break; - - case 13: /* HcFmInterval */ - ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16; - ohci->fit = (val & OHCI_FMI_FIT) >> 31; - ohci_set_frame_interval(ohci, val); - break; - - case 16: /* HcPeriodicStart */ - ohci->pstart = val & 0xffff; - break; - - case 17: /* HcLSThreshold */ - ohci->lst = val & 0xffff; - break; - - case 18: /* HcRhDescriptorA */ - ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK; - ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK; - break; - - case 19: /* HcRhDescriptorB */ - break; - - case 20: /* HcRhStatus */ - ohci_set_hub_status(ohci, val); - break; - - default: - fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr); - break; - } -} - -OHCIState *ohci_create(uint32_t base, int ports) -{ - OHCIState *ohci=(OHCIState*)malloc(sizeof(OHCIState)); - int i; - - const int ticks_per_sec = PSXCLK; - - memset(ohci,0,sizeof(OHCIState)); - - ohci->mem_base=base; - - if (usb_frame_time == 0) { -#if OHCI_TIME_WARP - usb_frame_time = ticks_per_sec; - usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ/1000); -#else - usb_frame_time = muldiv64(1, ticks_per_sec, 1000); - if (ticks_per_sec >= USB_HZ) { - usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ); - } else { - usb_bit_time = 1; - } -#endif - dprintf("usb-ohci: usb_bit_time=%lli usb_frame_time=%lli\n", - usb_frame_time, usb_bit_time); - } - - ohci->num_ports = ports; - for (i = 0; i < ports; i++) { - ohci->rhport[i].port.opaque = ohci; - ohci->rhport[i].port.index = i; - ohci->rhport[i].port.attach = ohci_attach; - } - - ohci_reset(ohci); - - return ohci; -} +/* + * QEMU USB OHCI Emulation + * Copyright (c) 2004 Gianni Tedesco + * Copyright (c) 2006 CodeSourcery + * Copyright (c) 2006 Openedhand Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + * TODO: + * o Isochronous transfers + * o Allocate bandwidth in frames properly + * o Disable timers when nothing needs to be done, or remove timer usage + * all together. + * o Handle unrecoverable errors properly + * o BIOS work to boot from USB storage +*/ + +//typedef CPUReadMemoryFunc + +#include "vl.h" +#include "../USB.h" + +uint32_t bits = 0; +uint32_t need_interrupt = 0; + +extern FILE* usbLog; + +int dprintf(const char *fmt,...) +{ +#ifdef DEBUG_OHCI + int t; + va_list list; + + va_start(list, fmt); + t=vfprintf(stderr, fmt, list); + va_end(list); + + if(usbLog) + { + va_start(list, fmt); + vfprintf(usbLog, fmt, list); + va_end(list); + } + + return t; +#else + return 0; +#endif +} + +/* Update IRQ levels */ +static inline void ohci_intr_update(OHCIState *ohci) +{ + bits = (ohci->intr_status & ohci->intr) & 0x7fffffff; + + if ((ohci->intr & OHCI_INTR_MIE) && (bits!=0)) // && (ohci->ctl & OHCI_CTL_HCFS)) + { + + static char reasons[1024]; + int first=1; + + reasons[0]=0; + +#define reason_add(p,t) if(bits&(p)) { if(!first) strcat_s(reasons,1024,", "); first=0; strcat_s(reasons,1024,t); } + reason_add(OHCI_INTR_SO,"Scheduling overrun"); + reason_add(OHCI_INTR_WD,"HcDoneHead writeback"); + reason_add(OHCI_INTR_SF,"Start of frame"); + reason_add(OHCI_INTR_RD,"Resume detect"); + reason_add(OHCI_INTR_UE,"Unrecoverable error"); + reason_add(OHCI_INTR_FNO,"Frame number overflow"); + reason_add(OHCI_INTR_RHSC,"Root hub status change"); + reason_add(OHCI_INTR_OC,"Ownership change"); + + if((ohci->ctl & OHCI_CTL_HCFS)==OHCI_USB_OPERATIONAL) + { + USBirq(1); + //dprintf("usb-ohci: Interrupt Called. Reason(s): %s\n",reasons); + } + } +} + +/* Set an interrupt */ +static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr) +{ + ohci->intr_status |= intr; + ohci_intr_update(ohci); +} + +/* Attach or detach a device on a root hub port. */ +static void ohci_attach(USBPort *port1, USBDevice *dev) +{ + OHCIState *s = (OHCIState *)port1->opaque; + OHCIPort *port = &s->rhport[port1->index]; + uint32_t old_state = port->ctrl; + + if (dev) { + if (port->port.dev) { + usb_attach(port1, NULL); + } + /* set connect status */ + port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC; + + /* update speed */ + if (dev->speed == USB_SPEED_LOW) + port->ctrl |= OHCI_PORT_LSDA; + else + port->ctrl &= ~OHCI_PORT_LSDA; + port->port.dev = dev; + + /* notify of remote-wakeup */ + if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) + ohci_set_interrupt(s, OHCI_INTR_RD); + + /* send the attach message */ + usb_send_msg(dev, USB_MSG_ATTACH); + dprintf("usb-ohci: Attached port %d\n", port1->index); + } else { + /* set connect status */ + if (port->ctrl & OHCI_PORT_CCS) { + port->ctrl &= ~OHCI_PORT_CCS; + port->ctrl |= OHCI_PORT_CSC; + } + /* disable port */ + if (port->ctrl & OHCI_PORT_PES) { + port->ctrl &= ~OHCI_PORT_PES; + port->ctrl |= OHCI_PORT_PESC; + } + dev = port->port.dev; + if (dev) { + /* send the detach message */ + usb_send_msg(dev, USB_MSG_DETACH); + } + port->port.dev = NULL; + dprintf("usb-ohci: Detached port %d\n", port1->index); + } + + if (old_state != port->ctrl) + ohci_set_interrupt(s, OHCI_INTR_RHSC); +} + +/* Reset the controller */ +static void ohci_reset(void *opaque) +{ + OHCIState *ohci = (OHCIState *)opaque; + OHCIPort *port; + int i; + + ohci_bus_stop(ohci); + ohci->ctl = 0; + ohci->old_ctl = 0; + ohci->status = 0; + ohci->intr_status = 0; + ohci->intr = OHCI_INTR_MIE; + + ohci->hcca = 0; + ohci->ctrl_head = ohci->ctrl_cur = 0; + ohci->bulk_head = ohci->bulk_cur = 0; + ohci->per_cur = 0; + ohci->done = 0; + ohci->done_count = 7; + + /* FSMPS is marked TBD in OCHI 1.0, what gives ffs? + * I took the value linux sets ... + */ + ohci->fsmps = 0x2778; + ohci->fi = 0x2edf; + ohci->fit = 0; + ohci->frt = 0; + ohci->frame_number = 0; + ohci->pstart = 0; + ohci->lst = OHCI_LS_THRESH; + + ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports; + ohci->rhdesc_b = 0x0; /* Impl. specific */ + ohci->rhstatus = 0; + + for (i = 0; i < ohci->num_ports; i++) + { + port = &ohci->rhport[i]; + port->ctrl = 0; + if (port->port.dev) + ohci_attach(&port->port, port->port.dev); + } + if (ohci->async_td) { + usb_cancel_packet(&ohci->usb_packet); + ohci->async_td = 0; + } + dprintf("usb-ohci: Reset %s\n", ohci->name); +} + +/* Get an array of dwords from main memory */ +static inline int get_dwords(OHCIState *ohci, + uint32_t addr, uint32_t *buf, int num) +{ + int i; + + addr += ohci->localmem_base; + + for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { + cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0); + *buf = (*buf); + } + + return 1; +} + +/* Put an array of dwords in to main memory */ +static inline int put_dwords(OHCIState *ohci, + uint32_t addr, uint32_t *buf, int num) +{ + int i; + + addr += ohci->localmem_base; + + for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { + uint32_t tmp = (*buf); + cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1); + } + + return 1; +} + +/* Get an array of words from main memory */ +static inline int get_words(OHCIState *ohci, + uint32_t addr, uint16_t *buf, int num) +{ + int i; + + addr += ohci->localmem_base; + + for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { + cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0); + *buf = (*buf); + } + + return 1; +} + +/* Put an array of words in to main memory */ +static inline int put_words(OHCIState *ohci, + uint32_t addr, uint16_t *buf, int num) +{ + int i; + + addr += ohci->localmem_base; + + for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { + uint16_t tmp = (*buf); + cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1); + } + + return 1; +} + +static inline int ohci_read_ed(OHCIState *ohci, + uint32_t addr, struct ohci_ed *ed) +{ + return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2); +} + +static inline int ohci_read_td(OHCIState *ohci, + uint32_t addr, struct ohci_td *td) +{ + return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2); +} + +static inline int ohci_read_iso_td(OHCIState *ohci, + uint32_t addr, struct ohci_iso_td *td) +{ + return (get_dwords(ohci, addr, (uint32_t *)td, 4) && + get_words(ohci, addr + 16, td->offset, 8)); +} + +static inline int ohci_read_hcca(OHCIState *ohci, + uint32_t addr, struct ohci_hcca *hcca) +{ + cpu_physical_memory_rw(addr + ohci->localmem_base, + (uint8_t *)hcca, sizeof(*hcca), 0); + return 1; +} + +static inline int ohci_put_ed(OHCIState *ohci, + uint32_t addr, struct ohci_ed *ed) +{ + return put_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2); +} + +static inline int ohci_put_td(OHCIState *ohci, + uint32_t addr, struct ohci_td *td) +{ + return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2); +} + +static inline int ohci_put_iso_td(OHCIState *ohci, + uint32_t addr, struct ohci_iso_td *td) +{ + return (put_dwords(ohci, addr, (uint32_t *)td, 4) && + put_words(ohci, addr + 16, td->offset, 8)); +} + +static inline int ohci_put_hcca(OHCIState *ohci, + uint32_t addr, struct ohci_hcca *hcca) +{ + cpu_physical_memory_rw(addr + ohci->localmem_base, + (uint8_t *)hcca, sizeof(*hcca), 1); + return 1; +} + +/* Read/Write the contents of a TD from/to main memory. */ +static void ohci_copy_td(OHCIState *ohci, struct ohci_td *td, + uint8_t *buf, int len, int write) +{ + uint32_t ptr; + uint32_t n; + + ptr = td->cbp; + n = 0x1000 - (ptr & 0xfff); + if (n > len) + n = len; + cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write); + if (n == len) + return; + ptr = td->be & ~0xfffu; + buf += n; + cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write); +} + +/* Read/Write the contents of an ISO TD from/to main memory. */ +static void ohci_copy_iso_td(OHCIState *ohci, + uint32_t start_addr, uint32_t end_addr, + uint8_t *buf, int len, int write) +{ + uint32_t ptr; + uint32_t n; + + ptr = start_addr; + n = 0x1000 - (ptr & 0xfff); + if (n > len) + n = len; + cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write); + if (n == len) + return; + ptr = end_addr & ~0xfffu; + buf += n; + cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write); +} + +static void ohci_process_lists(OHCIState *ohci, int completion); + +static void ohci_async_complete_packet(USBPacket *packet, void *opaque) +{ + OHCIState *ohci = (OHCIState *)opaque; +#ifdef DEBUG_PACKET + dprintf("Async packet complete\n"); +#endif + ohci->async_complete = 1; + ohci_process_lists(ohci, 1); +} + +#define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b))) + +static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed, + int completion) +{ + int dir; + size_t len = 0; + const char *str = NULL; + int pid; + int ret; + int i; + USBDevice *dev; + struct ohci_iso_td iso_td; + uint32_t addr; + uint16_t starting_frame; + int16_t relative_frame_number; + int frame_count; + uint32_t start_offset, next_offset, end_offset = 0; + uint32_t start_addr, end_addr; + + addr = ed->head & OHCI_DPTR_MASK; + + if (!ohci_read_iso_td(ohci, addr, &iso_td)) { + printf("usb-ohci: ISO_TD read error at %x\n", addr); + return 0; + } + + starting_frame = OHCI_BM(iso_td.flags, TD_SF); + frame_count = OHCI_BM(iso_td.flags, TD_FC); + relative_frame_number = USUB(ohci->frame_number, starting_frame); + +#ifdef DEBUG_ISOCH + printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n" + "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n" + "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n" + "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n" + "frame_number 0x%.8x starting_frame 0x%.8x\n" + "frame_count 0x%.8x relative %d\n" + "di 0x%.8x cc 0x%.8x\n", + ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK, + iso_td.flags, iso_td.bp, iso_td.next, iso_td.be, + iso_td.offset[0], iso_td.offset[1], iso_td.offset[2], iso_td.offset[3], + iso_td.offset[4], iso_td.offset[5], iso_td.offset[6], iso_td.offset[7], + ohci->frame_number, starting_frame, + frame_count, relative_frame_number, + OHCI_BM(iso_td.flags, TD_DI), OHCI_BM(iso_td.flags, TD_CC)); +#endif + + if (relative_frame_number < 0) { + dprintf("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number); + return 1; + } else if (relative_frame_number > frame_count) { + /* ISO TD expired - retire the TD to the Done Queue and continue with + the next ISO TD of the same ED */ + dprintf("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number, + frame_count); + OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN); + ed->head &= ~OHCI_DPTR_MASK; + ed->head |= (iso_td.next & OHCI_DPTR_MASK); + iso_td.next = ohci->done; + ohci->done = addr; + i = OHCI_BM(iso_td.flags, TD_DI); + if (i < ohci->done_count) + ohci->done_count = i; + ohci_put_iso_td(ohci, addr, &iso_td); + return 0; + } + + dir = OHCI_BM(ed->flags, ED_D); + switch (dir) { + case OHCI_TD_DIR_IN: + str = "in"; + pid = USB_TOKEN_IN; + break; + case OHCI_TD_DIR_OUT: + str = "out"; + pid = USB_TOKEN_OUT; + break; + case OHCI_TD_DIR_SETUP: + str = "setup"; + pid = USB_TOKEN_SETUP; + break; + default: + printf("usb-ohci: Bad direction %d\n", dir); + return 1; + } + + if (!iso_td.bp || !iso_td.be) { + printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td.bp, iso_td.be); + return 1; + } + + start_offset = iso_td.offset[relative_frame_number]; + next_offset = iso_td.offset[relative_frame_number + 1]; + + if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) || + ((relative_frame_number < frame_count) && + !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) { + printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n", + start_offset, next_offset); + return 1; + } + + if ((relative_frame_number < frame_count) && (start_offset > next_offset)) { + printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n", + start_offset, next_offset); + return 1; + } + + if ((start_offset & 0x1000) == 0) { + start_addr = (iso_td.bp & OHCI_PAGE_MASK) | + (start_offset & OHCI_OFFSET_MASK); + } else { + start_addr = (iso_td.be & OHCI_PAGE_MASK) | + (start_offset & OHCI_OFFSET_MASK); + } + + if (relative_frame_number < frame_count) { + end_offset = next_offset - 1; + if ((end_offset & 0x1000) == 0) { + end_addr = (iso_td.bp & OHCI_PAGE_MASK) | + (end_offset & OHCI_OFFSET_MASK); + } else { + end_addr = (iso_td.be & OHCI_PAGE_MASK) | + (end_offset & OHCI_OFFSET_MASK); + } + } else { + /* Last packet in the ISO TD */ + end_addr = iso_td.be; + } + + if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) { + len = (end_addr & OHCI_OFFSET_MASK) + 0x1001 + - (start_addr & OHCI_OFFSET_MASK); + } else { + len = end_addr - start_addr + 1; + } + + if (len && dir != OHCI_TD_DIR_IN) { + ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len, 0); + } + + if (completion) { + ret = ohci->usb_packet.len; + } else { + ret = USB_RET_NODEV; + for (i = 0; i < ohci->num_ports; i++) { + dev = ohci->rhport[i].port.dev; + if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) + continue; + ohci->usb_packet.pid = pid; + ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA); + ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN); + ohci->usb_packet.data = ohci->usb_buf; + ohci->usb_packet.len = len; + ohci->usb_packet.complete_cb = ohci_async_complete_packet; + ohci->usb_packet.complete_opaque = ohci; + ret = dev->info->handle_packet(dev, &ohci->usb_packet); + if (ret != USB_RET_NODEV) + break; + } + + if (ret == USB_RET_ASYNC) { + return 1; + } + } + +#ifdef DEBUG_ISOCH + printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n", + start_offset, end_offset, start_addr, end_addr, str, len, ret); +#endif + + /* Writeback */ + if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) { + /* IN transfer succeeded */ + ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret, 1); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_NOERROR); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret); + } else if (dir == OHCI_TD_DIR_OUT && ret == len) { + /* OUT transfer succeeded */ + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_NOERROR); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0); + } else { + if (ret > (signed) len) { + printf("usb-ohci: DataOverrun %d > %zu\n", ret, len); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_DATAOVERRUN); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, + len); + } else if (ret >= 0) { + printf("usb-ohci: DataUnderrun %d\n", ret); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_DATAUNDERRUN); + } else { + switch (ret) { + case USB_RET_NODEV: + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_DEVICENOTRESPONDING); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, + 0); + break; + case USB_RET_NAK: + case USB_RET_STALL: + printf("usb-ohci: got NAK/STALL %d\n", ret); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_STALL); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, + 0); + break; + default: + printf("usb-ohci: Bad device response %d\n", ret); + OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, + OHCI_CC_UNDEXPETEDPID); + break; + } + } + } + + if (relative_frame_number == frame_count) { + /* Last data packet of ISO TD - retire the TD to the Done Queue */ + OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR); + ed->head &= ~OHCI_DPTR_MASK; + ed->head |= (iso_td.next & OHCI_DPTR_MASK); + iso_td.next = ohci->done; + ohci->done = addr; + i = OHCI_BM(iso_td.flags, TD_DI); + if (i < ohci->done_count) + ohci->done_count = i; + } + ohci_put_iso_td(ohci, addr, &iso_td); + return 1; +} + +/* Service a transport descriptor. + Returns nonzero to terminate processing of this endpoint. */ + +static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) +{ + int dir; + size_t len = 0; + const char *str = NULL; + int pid; + int ret; + int i; + USBDevice *dev; + struct ohci_td td; + uint32_t addr; + int flag_r; + int completion; + + addr = ed->head & OHCI_DPTR_MASK; + /* See if this TD has already been submitted to the device. */ + completion = (addr == ohci->async_td); + if (completion && !ohci->async_complete) { +#ifdef DEBUG_PACKET + dprintf("Skipping async TD\n"); +#endif + return 1; + } + if (!ohci_read_td(ohci, addr, &td)) { + fprintf(stderr, "usb-ohci: TD read error at %x\n", addr); + return 0; + } + + dir = OHCI_BM(ed->flags, ED_D); + switch (dir) { + case OHCI_TD_DIR_OUT: + case OHCI_TD_DIR_IN: + /* Same value. */ + break; + default: + dir = OHCI_BM(td.flags, TD_DP); + break; + } + + switch (dir) { + case OHCI_TD_DIR_IN: + str = "in"; + pid = USB_TOKEN_IN; + break; + case OHCI_TD_DIR_OUT: + str = "out"; + pid = USB_TOKEN_OUT; + break; + case OHCI_TD_DIR_SETUP: + str = "setup"; + pid = USB_TOKEN_SETUP; + break; + default: + fprintf(stderr, "usb-ohci: Bad direction\n"); + return 1; + } + if (td.cbp && td.be) { + if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) { + len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff); + } else { + len = (td.be - td.cbp) + 1; + } + + if (len && dir != OHCI_TD_DIR_IN && !completion) { + ohci_copy_td(ohci, &td, ohci->usb_buf, len, 0); + } + } + + flag_r = (td.flags & OHCI_TD_R) != 0; +#ifdef DEBUG_PACKET + dprintf(" TD @ 0x%.8x %" PRId64 " bytes %s r=%d cbp=0x%.8x be=0x%.8x\n", + addr, len, str, flag_r, td.cbp, td.be); + + if (len > 0 && dir != OHCI_TD_DIR_IN) { + dprintf(" data:"); + for (i = 0; i < len; i++) + printf(" %.2x", ohci->usb_buf[i]); + dprintf("\n"); + } +#endif + if (completion) { + ret = ohci->usb_packet.len; + ohci->async_td = 0; + ohci->async_complete = 0; + } else { + ret = USB_RET_NODEV; + for (i = 0; i < ohci->num_ports; i++) { + dev = ohci->rhport[i].port.dev; + if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) + continue; + + if (ohci->async_td) { + /* ??? The hardware should allow one active packet per + endpoint. We only allow one active packet per controller. + This should be sufficient as long as devices respond in a + timely manner. + */ +#ifdef DEBUG_PACKET + dprintf("Too many pending packets\n"); +#endif + return 1; + } + ohci->usb_packet.pid = pid; + ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA); + ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN); + ohci->usb_packet.data = ohci->usb_buf; + ohci->usb_packet.len = len; + ohci->usb_packet.complete_cb = ohci_async_complete_packet; + ohci->usb_packet.complete_opaque = ohci; + ret = dev->info->handle_packet(dev, &ohci->usb_packet); + if (ret != USB_RET_NODEV) + break; + } +#ifdef DEBUG_PACKET + dprintf("ret=%d\n", ret); +#endif + if (ret == USB_RET_ASYNC) { + ohci->async_td = addr; + return 1; + } + } + if (ret >= 0) { + if (dir == OHCI_TD_DIR_IN) { + ohci_copy_td(ohci, &td, ohci->usb_buf, ret, 1); +#ifdef DEBUG_PACKET + dprintf(" data:"); + for (i = 0; i < ret; i++) + printf(" %.2x", ohci->usb_buf[i]); + dprintf("\n"); +#endif + } else { + ret = len; + } + } + + /* Writeback */ + if (ret == len || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) { + /* Transmission succeeded. */ + if (ret == len) { + td.cbp = 0; + } else { + td.cbp += ret; + if ((td.cbp & 0xfff) + ret > 0xfff) { + td.cbp &= 0xfff; + td.cbp |= td.be & ~0xfff; + } + } + td.flags |= OHCI_TD_T1; + td.flags ^= OHCI_TD_T0; + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR); + OHCI_SET_BM(td.flags, TD_EC, 0); + + ed->head &= ~OHCI_ED_C; + if (td.flags & OHCI_TD_T0) + ed->head |= OHCI_ED_C; + } else { + if (ret >= 0) { + dprintf("usb-ohci: Underrun\n"); + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN); + } else { + switch (ret) { + case USB_RET_NODEV: + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING); + return 1; + case USB_RET_NAK: + dprintf("usb-ohci: got NAK\n"); + return 1; + case USB_RET_STALL: + dprintf("usb-ohci: got STALL\n"); + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL); + break; + case USB_RET_BABBLE: + dprintf("usb-ohci: got BABBLE\n"); + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN); + break; + default: + fprintf(stderr, "usb-ohci: Bad device response %d\n", ret); + OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID); + OHCI_SET_BM(td.flags, TD_EC, 3); + break; + } + } + ed->head |= OHCI_ED_H; + } + + /* Retire this TD */ + ed->head &= ~OHCI_DPTR_MASK; + ed->head |= td.next & OHCI_DPTR_MASK; + td.next = ohci->done; + ohci->done = addr; + i = OHCI_BM(td.flags, TD_DI); + if (i < ohci->done_count) + ohci->done_count = i; + ohci_put_td(ohci, addr, &td); + return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR; +} + +/* Service an endpoint list. Returns nonzero if active TD were found. */ +static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion) +{ + struct ohci_ed ed; + uint32_t next_ed; + uint32_t cur; + int active; + + active = 0; + + if (head == 0) + return 0; + + for (cur = head; cur; cur = next_ed) { + if (!ohci_read_ed(ohci, cur, &ed)) { + fprintf(stderr, "usb-ohci: ED read error at %x\n", cur); + return 0; + } + + next_ed = ed.next & OHCI_DPTR_MASK; + + if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) { + uint32_t addr; + /* Cancel pending packets for ED that have been paused. */ + addr = ed.head & OHCI_DPTR_MASK; + if (ohci->async_td && addr == ohci->async_td) { + usb_cancel_packet(&ohci->usb_packet); + ohci->async_td = 0; + } + continue; + } + + while ((ed.head & OHCI_DPTR_MASK) != ed.tail) { +#ifdef DEBUG_PACKET + dprintf("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u " + "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur, + OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN), + OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0, + (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0, + OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0, + (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK, + ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK); +#endif + active = 1; + + if ((ed.flags & OHCI_ED_F) == 0) { + if (ohci_service_td(ohci, &ed)) + break; + } else { + /* Handle isochronous endpoints */ + if (ohci_service_iso_td(ohci, &ed, completion)) + break; + } + } + + ohci_put_ed(ohci, cur, &ed); + } + + return active; +} + +/* Generate a SOF event, and set a timer for EOF */ +static void ohci_sof(OHCIState *ohci) +{ + ohci->sof_time = get_clock(); + ohci->eof_timer += usb_frame_time; + //qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time); + ohci_set_interrupt(ohci, OHCI_INTR_SF); +} + +/* Process Control and Bulk lists. */ +static void ohci_process_lists(OHCIState *ohci, int completion) +{ + if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) { + if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) + dprintf("usb-ohci: head %x, cur %x\n", + ohci->ctrl_head, ohci->ctrl_cur); + if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) { + ohci->ctrl_cur = 0; + ohci->status &= ~OHCI_STATUS_CLF; + } + } + + if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) { + if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) { + ohci->bulk_cur = 0; + ohci->status &= ~OHCI_STATUS_BLF; + } + } +} + +/* Do frame processing on frame boundary */ +void ohci_frame_boundary(void *opaque) +{ + OHCIState *ohci = (OHCIState *)opaque; + struct ohci_hcca hcca; + + ohci_read_hcca(ohci, ohci->hcca, &hcca); + + /* Process all the lists at the end of the frame */ + if (ohci->ctl & OHCI_CTL_PLE) { + int n; + + n = ohci->frame_number & 0x1f; + ohci_service_ed_list(ohci, (hcca.intr[n]), 0); + } + + /* Cancel all pending packets if either of the lists has been disabled. */ + if (ohci->async_td && + ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) { + usb_cancel_packet(&ohci->usb_packet); + ohci->async_td = 0; + } + ohci->old_ctl = ohci->ctl; + ohci_process_lists(ohci, 0); + + /* Frame boundary, so do EOF stuf here */ + ohci->frt = ohci->fit; + + /* Increment frame number and take care of endianness. */ + ohci->frame_number = (ohci->frame_number + 1) & 0xffff; + hcca.frame = (ohci->frame_number); + + if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) { + if (!ohci->done) + abort(); + if (ohci->intr & ohci->intr_status) + ohci->done |= 1; + hcca.done = (ohci->done); + ohci->done = 0; + ohci->done_count = 7; + ohci_set_interrupt(ohci, OHCI_INTR_WD); + } + + if (ohci->done_count != 7 && ohci->done_count != 0) + ohci->done_count--; + + /* Do SOF stuff here */ + ohci_sof(ohci); + + /* Writeback HCCA */ + ohci_put_hcca(ohci, ohci->hcca, &hcca); +} + +/* Start sending SOF tokens across the USB bus, lists are processed in + * next frame + */ +int ohci_bus_start(OHCIState *ohci) +{ + ohci->eof_timer = 0; + + dprintf("usb-ohci: %s: USB Operational\n", ohci->name); + + ohci_sof(ohci); + + return 1; +} + +/* Stop sending SOF tokens on the bus */ +void ohci_bus_stop(OHCIState *ohci) +{ + if (ohci->eof_timer) + ohci->eof_timer=0; +} + +/* Sets a flag in a port status register but only set it if the port is + * connected, if not set ConnectStatusChange flag. If flag is enabled + * return 1. + */ +static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val) +{ + int ret = 1; + + /* writing a 0 has no effect */ + if (val == 0) + return 0; + + /* If CurrentConnectStatus is cleared we set + * ConnectStatusChange + */ + if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) { + ohci->rhport[i].ctrl |= OHCI_PORT_CSC; + if (ohci->rhstatus & OHCI_RHS_DRWE) { + /* TODO: CSC is a wakeup event */ + } + return 0; + } + + if (ohci->rhport[i].ctrl & val) + ret = 0; + + /* set the bit */ + ohci->rhport[i].ctrl |= val; + + return ret; +} + +/* Set the frame interval - frame interval toggle is manipulated by the hcd only */ +static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val) +{ + val &= OHCI_FMI_FI; + + if (val != ohci->fi) { + dprintf("usb-ohci: %s: FrameInterval = 0x%x (%u)\n", + ohci->name, ohci->fi, ohci->fi); + } + + ohci->fi = val; +} + +static void ohci_port_power(OHCIState *ohci, int i, int p) +{ + if (p) { + ohci->rhport[i].ctrl |= OHCI_PORT_PPS; + } else { + ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS| + OHCI_PORT_CCS| + OHCI_PORT_PSS| + OHCI_PORT_PRS); + } +} + +/* Set HcControlRegister */ +static void ohci_set_ctl(OHCIState *ohci, uint32_t val) +{ + uint32_t old_state; + uint32_t new_state; + + old_state = ohci->ctl & OHCI_CTL_HCFS; + ohci->ctl = val; + new_state = ohci->ctl & OHCI_CTL_HCFS; + + /* no state change */ + if (old_state == new_state) + return; + + switch (new_state) { + case OHCI_USB_OPERATIONAL: + ohci_bus_start(ohci); + break; + case OHCI_USB_SUSPEND: + ohci_bus_stop(ohci); + dprintf("usb-ohci: %s: USB Suspended\n", ohci->name); + break; + case OHCI_USB_RESUME: + dprintf("usb-ohci: %s: USB Resume\n", ohci->name); + break; + case OHCI_USB_RESET: + ohci_reset(ohci); + dprintf("usb-ohci: %s: USB Reset\n", ohci->name); + break; + } +} + +static uint32_t ohci_get_frame_remaining(OHCIState *ohci) +{ + uint16_t fr; + int64_t tks; + + if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL) + return (ohci->frt << 31); + + /* Being in USB operational state guarnatees sof_time was + * set already. + */ + tks = get_clock() - ohci->sof_time; + + /* avoid muldiv if possible */ + if (tks >= usb_frame_time) + return (ohci->frt << 31); + + tks = muldiv64(1, tks, usb_bit_time); + fr = (uint16_t)(ohci->fi - tks); + + return (ohci->frt << 31) | fr; +} + + +/* Set root hub status */ +static void ohci_set_hub_status(OHCIState *ohci, uint32_t val) +{ + uint32_t old_state; + + old_state = ohci->rhstatus; + + /* write 1 to clear OCIC */ + if (val & OHCI_RHS_OCIC) + ohci->rhstatus &= ~OHCI_RHS_OCIC; + + if (val & OHCI_RHS_LPS) { + int i; + + for (i = 0; i < ohci->num_ports; i++) + ohci_port_power(ohci, i, 0); + dprintf("usb-ohci: powered down all ports\n"); + } + + if (val & OHCI_RHS_LPSC) { + int i; + + for (i = 0; i < ohci->num_ports; i++) + ohci_port_power(ohci, i, 1); + dprintf("usb-ohci: powered up all ports\n"); + } + + if (val & OHCI_RHS_DRWE) + ohci->rhstatus |= OHCI_RHS_DRWE; + + if (val & OHCI_RHS_CRWE) + ohci->rhstatus &= ~OHCI_RHS_DRWE; + + if (old_state != ohci->rhstatus) + ohci_set_interrupt(ohci, OHCI_INTR_RHSC); +} + +/* Set root hub port status */ +static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val) +{ + uint32_t old_state; + OHCIPort *port; + + port = &ohci->rhport[portnum]; + old_state = port->ctrl; + + /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */ + if (val & OHCI_PORT_WTC) + port->ctrl &= ~(val & OHCI_PORT_WTC); + + if (val & OHCI_PORT_CCS) + port->ctrl &= ~OHCI_PORT_PES; + + ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES); + + if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) + dprintf("usb-ohci: port %d: SUSPEND\n", portnum); + + if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) { + dprintf("usb-ohci: port %d: RESET\n", portnum); + usb_send_msg(port->port.dev, USB_MSG_RESET); + port->ctrl &= ~OHCI_PORT_PRS; + /* ??? Should this also set OHCI_PORT_PESC. */ + port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC; + } + + /* Invert order here to ensure in ambiguous case, device is + * powered up... + */ + if (val & OHCI_PORT_LSDA) + ohci_port_power(ohci, portnum, 0); + if (val & OHCI_PORT_PPS) + ohci_port_power(ohci, portnum, 1); + + if (old_state != port->ctrl) + ohci_set_interrupt(ohci, OHCI_INTR_RHSC); + + return; +} + +uint32_t ohci_mem_read(void *ptr, target_phys_addr_t addr) +{ + OHCIState *ohci = (OHCIState *)ptr; + uint32_t retval; + + /* Only aligned reads are allowed on OHCI */ + if (addr & 3) { + fprintf(stderr, "usb-ohci: Mis-aligned read\n"); + return 0xffffffff; + } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) { + /* HcRhPortStatus */ + retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS; + } else { + switch (addr >> 2) { + case 0: /* HcRevision */ + retval = 0x10; + break; + + case 1: /* HcControl */ + retval = ohci->ctl; + break; + + case 2: /* HcCommandStatus */ + retval = ohci->status; + break; + + case 3: /* HcInterruptStatus */ + retval = ohci->intr_status; + break; + + case 4: /* HcInterruptEnable */ + case 5: /* HcInterruptDisable */ + retval = ohci->intr; + break; + + case 6: /* HcHCCA */ + retval = ohci->hcca; + break; + + case 7: /* HcPeriodCurrentED */ + retval = ohci->per_cur; + break; + + case 8: /* HcControlHeadED */ + retval = ohci->ctrl_head; + break; + + case 9: /* HcControlCurrentED */ + retval = ohci->ctrl_cur; + break; + + case 10: /* HcBulkHeadED */ + retval = ohci->bulk_head; + break; + + case 11: /* HcBulkCurrentED */ + retval = ohci->bulk_cur; + break; + + case 12: /* HcDoneHead */ + retval = ohci->done; + break; + + case 13: /* HcFmInterretval */ + retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi); + break; + + case 14: /* HcFmRemaining */ + retval = ohci_get_frame_remaining(ohci); + break; + + case 15: /* HcFmNumber */ + retval = ohci->frame_number; + break; + + case 16: /* HcPeriodicStart */ + retval = ohci->pstart; + break; + + case 17: /* HcLSThreshold */ + retval = ohci->lst; + break; + + case 18: /* HcRhDescriptorA */ + retval = ohci->rhdesc_a; + break; + + case 19: /* HcRhDescriptorB */ + retval = ohci->rhdesc_b; + break; + + case 20: /* HcRhStatus */ + retval = ohci->rhstatus; + break; + + /* PXA27x specific registers */ + case 24: /* HcStatus */ + retval = ohci->hstatus & ohci->hmask; + break; + + case 25: /* HcHReset */ + retval = ohci->hreset; + break; + + case 26: /* HcHInterruptEnable */ + retval = ohci->hmask; + break; + + case 27: /* HcHInterruptTest */ + retval = ohci->htest; + break; + + default: + fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr); + retval = 0xffffffff; + } + } + +#ifdef TARGET_WORDS_BIGENDIAN + retval = bswap32(retval); +#endif + return retval; +} + +void ohci_mem_write(void *ptr, target_phys_addr_t addr, uint32_t val) +{ + OHCIState *ohci = (OHCIState *)ptr; + +#ifdef TARGET_WORDS_BIGENDIAN + val = bswap32(val); +#endif + + /* Only aligned reads are allowed on OHCI */ + if (addr & 3) { + fprintf(stderr, "usb-ohci: Mis-aligned write\n"); + return; + } + + if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) { + /* HcRhPortStatus */ + ohci_port_set_status(ohci, (addr - 0x54) >> 2, val); + return; + } + + switch (addr >> 2) { + case 1: /* HcControl */ + ohci_set_ctl(ohci, val); + break; + + case 2: /* HcCommandStatus */ + /* SOC is read-only */ + val = (val & ~OHCI_STATUS_SOC); + + /* Bits written as '0' remain unchanged in the register */ + ohci->status |= val; + + if (ohci->status & OHCI_STATUS_HCR) + ohci_reset(ohci); + break; + + case 3: /* HcInterruptStatus */ + ohci->intr_status &= ~val; + ohci_intr_update(ohci); + break; + + case 4: /* HcInterruptEnable */ + ohci->intr |= val; + ohci_intr_update(ohci); + break; + + case 5: /* HcInterruptDisable */ + ohci->intr &= ~val; + ohci_intr_update(ohci); + break; + + case 6: /* HcHCCA */ + ohci->hcca = val & OHCI_HCCA_MASK; + break; + + case 8: /* HcControlHeadED */ + ohci->ctrl_head = val & OHCI_EDPTR_MASK; + break; + + case 9: /* HcControlCurrentED */ + ohci->ctrl_cur = val & OHCI_EDPTR_MASK; + break; + + case 10: /* HcBulkHeadED */ + ohci->bulk_head = val & OHCI_EDPTR_MASK; + break; + + case 11: /* HcBulkCurrentED */ + ohci->bulk_cur = val & OHCI_EDPTR_MASK; + break; + + case 13: /* HcFmInterval */ + ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16; + ohci->fit = (val & OHCI_FMI_FIT) >> 31; + ohci_set_frame_interval(ohci, val); + break; + + case 15: /* HcFmNumber */ + break; + + case 16: /* HcPeriodicStart */ + ohci->pstart = val & 0xffff; + break; + + case 17: /* HcLSThreshold */ + ohci->lst = val & 0xffff; + break; + + case 18: /* HcRhDescriptorA */ + ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK; + ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK; + break; + + case 19: /* HcRhDescriptorB */ + break; + + case 20: /* HcRhStatus */ + ohci_set_hub_status(ohci, val); + break; + + /* PXA27x specific registers */ + case 24: /* HcStatus */ + ohci->hstatus &= ~(val & ohci->hmask); + + case 25: /* HcHReset */ + ohci->hreset = val & ~OHCI_HRESET_FSBIR; + if (val & OHCI_HRESET_FSBIR) + ohci_reset(ohci); + break; + + case 26: /* HcHInterruptEnable */ + ohci->hmask = val; + break; + + case 27: /* HcHInterruptTest */ + ohci->htest = val; + break; + + default: + fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr); + break; + } +} + +OHCIState *ohci_create(uint32_t base, int ports) +{ + OHCIState *ohci=(OHCIState*)malloc(sizeof(OHCIState)); + int i; + + memset(ohci,0,sizeof(OHCIState)); + + //ohci->localmem_base=base; + + ohci->mem = base; + //ohci->mem = cpu_register_io_memory(ohci_readfn, ohci_writefn, ohci); + //ohci->localmem_base = localmem_base; + ohci->name = "USBemu"; + + //ohci->irq = irq; + //ohci->type = type; + + + if (usb_frame_time == 0) { +#ifdef OHCI_TIME_WARP + usb_frame_time = get_ticks_per_sec(); + usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ/1000); +#else + usb_frame_time = muldiv64(1, get_ticks_per_sec(), 1000); + if (get_ticks_per_sec() >= USB_HZ) { + usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ); + } else { + usb_bit_time = 1; + } +#endif + dprintf("usb-ohci: usb_bit_time=%" PRId64 " usb_frame_time=%" PRId64 "\n", + usb_frame_time, usb_bit_time); + } + + ohci->num_ports = ports; + for (i = 0; i < ports; i++) { + ohci->rhport[i].port.opaque = ohci; + ohci->rhport[i].port.index = i; + ohci->rhport[i].port.attach = ohci_attach; + } + + ohci->async_td = 0; + + return ohci; +} diff --git a/plugins/USBqemu/qemu-usb/usb.h b/plugins/USBqemu/qemu-usb/usb.h index bfbd5d3824..b9cea534a5 100644 --- a/plugins/USBqemu/qemu-usb/usb.h +++ b/plugins/USBqemu/qemu-usb/usb.h @@ -1,175 +1,259 @@ -/* - * QEMU USB API - * - * Copyright (c) 2005 Fabrice Bellard - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#define USB_TOKEN_SETUP 0x2d -#define USB_TOKEN_IN 0x69 /* device -> host */ -#define USB_TOKEN_OUT 0xe1 /* host -> device */ - -/* specific usb messages, also sent in the 'pid' parameter */ -#define USB_MSG_ATTACH 0x100 -#define USB_MSG_DETACH 0x101 -#define USB_MSG_RESET 0x102 - -#define USB_RET_NODEV (-1) -#define USB_RET_NAK (-2) -#define USB_RET_STALL (-3) -#define USB_RET_BABBLE (-4) - -#define USB_SPEED_LOW 0 -#define USB_SPEED_FULL 1 -#define USB_SPEED_HIGH 2 - -#define USB_STATE_NOTATTACHED 0 -#define USB_STATE_ATTACHED 1 -//#define USB_STATE_POWERED 2 -#define USB_STATE_DEFAULT 3 -//#define USB_STATE_ADDRESS 4 -//#define USB_STATE_CONFIGURED 5 -#define USB_STATE_SUSPENDED 6 - -#define USB_CLASS_AUDIO 1 -#define USB_CLASS_COMM 2 -#define USB_CLASS_HID 3 -#define USB_CLASS_PHYSICAL 5 -#define USB_CLASS_STILL_IMAGE 6 -#define USB_CLASS_PRINTER 7 -#define USB_CLASS_MASS_STORAGE 8 -#define USB_CLASS_HUB 9 -#define USB_CLASS_CDC_DATA 0x0a -#define USB_CLASS_CSCID 0x0b -#define USB_CLASS_CONTENT_SEC 0x0d -#define USB_CLASS_APP_SPEC 0xfe -#define USB_CLASS_VENDOR_SPEC 0xff - -#define USB_DIR_OUT 0 -#define USB_DIR_IN 0x80 - -#define USB_TYPE_MASK (0x03 << 5) -#define USB_TYPE_STANDARD (0x00 << 5) -#define USB_TYPE_CLASS (0x01 << 5) -#define USB_TYPE_VENDOR (0x02 << 5) -#define USB_TYPE_RESERVED (0x03 << 5) - -#define USB_RECIP_MASK 0x1f -#define USB_RECIP_DEVICE 0x00 -#define USB_RECIP_INTERFACE 0x01 -#define USB_RECIP_ENDPOINT 0x02 -#define USB_RECIP_OTHER 0x03 - -#define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) -#define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) -#define InterfaceRequest \ - ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) -#define InterfaceOutRequest \ - ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) -#define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) -#define EndpointOutRequest \ - ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) - -#define USB_REQ_GET_STATUS 0x00 -#define USB_REQ_CLEAR_FEATURE 0x01 -#define USB_REQ_SET_FEATURE 0x03 -#define USB_REQ_SET_ADDRESS 0x05 -#define USB_REQ_GET_DESCRIPTOR 0x06 -#define USB_REQ_SET_DESCRIPTOR 0x07 -#define USB_REQ_GET_CONFIGURATION 0x08 -#define USB_REQ_SET_CONFIGURATION 0x09 -#define USB_REQ_GET_INTERFACE 0x0A -#define USB_REQ_SET_INTERFACE 0x0B -#define USB_REQ_SYNCH_FRAME 0x0C - -#define USB_DEVICE_SELF_POWERED 0 -#define USB_DEVICE_REMOTE_WAKEUP 1 - -#define USB_DT_DEVICE 0x01 -#define USB_DT_CONFIG 0x02 -#define USB_DT_STRING 0x03 -#define USB_DT_INTERFACE 0x04 -#define USB_DT_ENDPOINT 0x05 -#define USB_DT_CLASS 0x24 - -typedef struct USBPort USBPort; -typedef struct USBDevice USBDevice; - -/* definition of a USB device */ -struct USBDevice { - void *opaque; - int (*handle_packet)(USBDevice *dev, int pid, - uint8_t devaddr, uint8_t devep, - uint8_t *data, int len); - void (*handle_destroy)(USBDevice *dev); - - int speed; - - /* The following fields are used by the generic USB device - layer. They are here just to avoid creating a new structure for - them. */ - void (*handle_reset)(USBDevice *dev); - int (*handle_control)(USBDevice *dev, int request, int value, - int index, int length, uint8_t *data); - int (*handle_data)(USBDevice *dev, int pid, uint8_t devep, - uint8_t *data, int len); - uint8_t addr; - char devname[32]; - - int state; - uint8_t setup_buf[8]; - uint8_t data_buf[1024]; - int remote_wakeup; - int setup_state; - int setup_len; - int setup_index; -}; - -typedef void (*usb_attachfn)(USBPort *port, USBDevice *dev); - -/* USB port on which a device can be connected */ -struct USBPort { - USBDevice *dev; - usb_attachfn attach; - void *opaque; - int index; /* internal port index, may be used with the opaque */ - struct USBPort *next; /* Used internally by qemu. */ -}; - -void usb_attach(USBPort *port, USBDevice *dev); -int usb_generic_handle_packet(USBDevice *s, int pid, - uint8_t devaddr, uint8_t devep, - uint8_t *data, int len); -int set_usb_string(uint8_t *buf, const char *str); - -/* usb hub */ -USBDevice *usb_hub_init(int nb_ports); - -/* usb-ohci.c */ -void usb_ohci_init(void *bus, int num_ports, int devfn); - -/* usb-hid.c */ -USBDevice *usb_mouse_init(void); - -/* usb-kbd.c */ -USBDevice *usb_keyboard_init(void); - -/* usb-msd.c */ -USBDevice *usb_msd_init(const char *filename); +/* + * QEMU USB API + * + * Copyright (c) 2005 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "qemu-queue.h" + +#define USB_TOKEN_SETUP 0x2d +#define USB_TOKEN_IN 0x69 /* device -> host */ +#define USB_TOKEN_OUT 0xe1 /* host -> device */ + +/* specific usb messages, also sent in the 'pid' parameter */ +#define USB_MSG_ATTACH 0x100 +#define USB_MSG_DETACH 0x101 +#define USB_MSG_RESET 0x102 + +#define USB_RET_NODEV (-1) +#define USB_RET_NAK (-2) +#define USB_RET_STALL (-3) +#define USB_RET_BABBLE (-4) +#define USB_RET_ASYNC (-5) + +#define USB_SPEED_LOW 0 +#define USB_SPEED_FULL 1 +#define USB_SPEED_HIGH 2 + +#define USB_STATE_NOTATTACHED 0 +#define USB_STATE_ATTACHED 1 +//#define USB_STATE_POWERED 2 +#define USB_STATE_DEFAULT 3 +//#define USB_STATE_ADDRESS 4 +//#define USB_STATE_CONFIGURED 5 +#define USB_STATE_SUSPENDED 6 + +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PHYSICAL 5 +#define USB_CLASS_STILL_IMAGE 6 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_CDC_DATA 0x0a +#define USB_CLASS_CSCID 0x0b +#define USB_CLASS_CONTENT_SEC 0x0d +#define USB_CLASS_APP_SPEC 0xfe +#define USB_CLASS_VENDOR_SPEC 0xff + +#define USB_DIR_OUT 0 +#define USB_DIR_IN 0x80 + +#define USB_TYPE_MASK (0x03 << 5) +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +#define USB_RECIP_MASK 0x1f +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 + +#define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) +#define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) +#define InterfaceRequest \ + ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) +#define InterfaceOutRequest \ + ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) +#define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) +#define EndpointOutRequest \ + ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) + +#define USB_REQ_GET_STATUS 0x00 +#define USB_REQ_CLEAR_FEATURE 0x01 +#define USB_REQ_SET_FEATURE 0x03 +#define USB_REQ_SET_ADDRESS 0x05 +#define USB_REQ_GET_DESCRIPTOR 0x06 +#define USB_REQ_SET_DESCRIPTOR 0x07 +#define USB_REQ_GET_CONFIGURATION 0x08 +#define USB_REQ_SET_CONFIGURATION 0x09 +#define USB_REQ_GET_INTERFACE 0x0A +#define USB_REQ_SET_INTERFACE 0x0B +#define USB_REQ_SYNCH_FRAME 0x0C + +#define USB_DEVICE_SELF_POWERED 0 +#define USB_DEVICE_REMOTE_WAKEUP 1 + +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 + +#define USB_ENDPOINT_XFER_CONTROL 0 +#define USB_ENDPOINT_XFER_ISOC 1 +#define USB_ENDPOINT_XFER_BULK 2 +#define USB_ENDPOINT_XFER_INT 3 + +typedef struct USBBus USBBus; +typedef struct USBPort USBPort; +typedef struct USBDevice USBDevice; +typedef struct USBDeviceInfo USBDeviceInfo; +typedef struct USBPacket USBPacket; + +/* definition of a USB device */ +struct USBDevice { + //DeviceState qdev; + USBDeviceInfo *info; + void *opaque; + + int speed; + uint8_t addr; + char product_desc[32]; + int auto_attach; + int attached; + + int state; + uint8_t setup_buf[8]; + uint8_t data_buf[1024]; + int remote_wakeup; + int setup_state; + int setup_len; + int setup_index; +}; + +struct USBDeviceInfo { + //DeviceInfo qdev; + int (*init)(USBDevice *dev); + + /* + * Process USB packet. + * Called by the HC (Host Controller). + * + * Returns length of the transaction + * or one of the USB_RET_XXX codes. + */ + int (*handle_packet)(USBDevice *dev, USBPacket *p); + + /* + * Called when device is destroyed. + */ + void (*handle_destroy)(USBDevice *dev); + + /* + * Reset the device + */ + void (*handle_reset)(USBDevice *dev); + + /* + * Process control request. + * Called from handle_packet(). + * + * Returns length or one of the USB_RET_ codes. + */ + int (*handle_control)(USBDevice *dev, int request, int value, + int index, int length, uint8_t *data); + + /* + * Process data transfers (both BULK and ISOC). + * Called from handle_packet(). + * + * Returns length or one of the USB_RET_ codes. + */ + int (*handle_data)(USBDevice *dev, USBPacket *p); + + const char *product_desc; + + /* handle legacy -usbdevice command line options */ + const char *usbdevice_name; + USBDevice *(*usbdevice_init)(const char *params); +}; + +typedef void (*usb_attachfn)(USBPort *port, USBDevice *dev); + +/* USB port on which a device can be connected */ +struct USBPort { + USBDevice *dev; + usb_attachfn attach; + void *opaque; + int index; /* internal port index, may be used with the opaque */ + QTAILQ_ENTRY(USBPort) next; +}; + +typedef void USBCallback(USBPacket * packet, void *opaque); + +/* Structure used to hold information about an active USB packet. */ +struct USBPacket { + /* Data fields for use by the driver. */ + int pid; + uint8_t devaddr; + uint8_t devep; + uint8_t *data; + int len; + /* Internal use by the USB layer. */ + USBCallback *complete_cb; + void *complete_opaque; + USBCallback *cancel_cb; + void *cancel_opaque; +}; + +/* Defer completion of a USB packet. The hadle_packet routine should then + return USB_RET_ASYNC. Packets that complete immediately (before + handle_packet returns) should not call this method. */ +static inline void usb_defer_packet(USBPacket *p, USBCallback *cancel, + void * opaque) +{ + p->cancel_cb = cancel; + p->cancel_opaque = opaque; +} + +/* Notify the controller that an async packet is complete. This should only + be called for packets previously deferred with usb_defer_packet, and + should never be called from within handle_packet. */ +static inline void usb_packet_complete(USBPacket *p) +{ + p->complete_cb(p, p->complete_opaque); +} + +/* Cancel an active packet. The packed must have been deferred with + usb_defer_packet, and not yet completed. */ +static inline void usb_cancel_packet(USBPacket * p) +{ + p->cancel_cb(p, p->cancel_opaque); +} + +void usb_attach(USBPort *port, USBDevice *dev); +int usb_generic_handle_packet(USBDevice *s, USBPacket *p); +int set_usb_string(uint8_t *buf, const char *str); +void usb_send_msg(USBDevice *dev, int msg); + +/* usb-hid.c */ +void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *)); + +/* usb ports of the VM */ + +#define VM_USB_HUB_SIZE 2 + +/* usb-kbd.cpp */ +USBDevice *usb_keyboard_init(void); \ No newline at end of file diff --git a/plugins/USBqemu/qemu-usb/vl.cpp b/plugins/USBqemu/qemu-usb/vl.cpp index 965fd6ebdb..21e4ec349b 100644 --- a/plugins/USBqemu/qemu-usb/vl.cpp +++ b/plugins/USBqemu/qemu-usb/vl.cpp @@ -1,21 +1,21 @@ -#include -#include -#include +#include +#include +#include #include "vl.h" - -void cpu_physical_memory_rw(uint32_t addr, uint8_t *buf, - int len, int is_write); - -static inline void cpu_physical_memory_read(uint32_t addr, - uint8_t *buf, int len) -{ - cpu_physical_memory_rw(addr, buf, len, 0); -} -static inline void cpu_physical_memory_write(uint32_t addr, - const uint8_t *buf, int len) -{ - cpu_physical_memory_rw(addr, (uint8_t *)buf, len, 1); -} + +void cpu_physical_memory_rw(uint32_t addr, uint8_t *buf, + int len, int is_write); + +static inline void cpu_physical_memory_read(uint32_t addr, + uint8_t *buf, int len) +{ + cpu_physical_memory_rw(addr, buf, len, 0); +} +static inline void cpu_physical_memory_write(uint32_t addr, + const uint8_t *buf, int len) +{ + cpu_physical_memory_rw(addr, (uint8_t *)buf, len, 1); +} /* compute with 96 bit intermediate result: (a*b)/c */ uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) diff --git a/plugins/USBqemu/qemu-usb/vl.h b/plugins/USBqemu/qemu-usb/vl.h index a4b0a51e30..59ebea45c7 100644 --- a/plugins/USBqemu/qemu-usb/vl.h +++ b/plugins/USBqemu/qemu-usb/vl.h @@ -59,7 +59,7 @@ typedef unsigned __int64 uint64_t; #include #define fsync _commit #define lseek _lseeki64 -#define ENOTSUP 4096 +//#define ENOTSUP 4096 extern int qemu_ftruncate64(int, int64_t); #define ftruncate qemu_ftruncate64