usbPipe0      = 0000h
usbPipe1      = 0100h
usbPipe2      = 0200h
usbPipe3      = 0300h

usbBulk       = 20h
usbInt        = 30h

usbPacket8    = 0100h
usbPacket16   = 0200h
usbPacket32   = 0400h
usbPacket64   = 0800h

pipeIn        = 1
pipeOut       = 2

pipeBitIn     = 0
pipeBitOut    = 1

cbNewData = 0
cbStall   = 1
cbNak     = 2

GetVersion		= 0
DriverInit		= 1
IsDeviceConnected	= 2
HostInit		= 3
HostKill		= 4
ReadDescriptor		= 5
GetIDs			= 6
GetClass		= 7
ConfigureDevice	= 8
SetupOutPipe		= 9
SetupInPipe		= 10
FindDescriptor		= 11
SendData		= 12
AutoSetup		= 13
SendControlData	= 14
ReqData             = 15
DriverKill          = 16
MouseInit           = 17
MouseGetKey         = 18 
WaitTimerB		= 19
PipeInfo		= 20
SetupLog		= 21
SendDataPart		= 22
FindPipe		= 23
StopLog		= 24
LogCustom		= 25
KBDVersion		= 26
MSDVersion		= 27
MSD_Initialize		= 28
UFI_Initialize		= 29
UFI_Read		= 30
UFI_Write		= 31
FAT_Initialize		= 32
FAT_lookupPath		= 33
DOS_openFile		= 34
DOS_fileSeek		= 35
DOS_fileRead		= 36
DOS_fileWrite		= 37
DOS_createFile		= 38
DOS_createDirectory	= 39
DOS_deleteFileEntry	= 40
DOS_countFilesInDir	= 41
DOS_getNextFile	= 42
FAT_getStartingCluster	= 43
MSD_ImportVariable	= 44
SD_ExportVariable	= 45
DOS_renameFileEntry	= 46
FAT_nameConvertTo11	= 47
FAT_nameConvertFrom11	= 48
FAT_findFileInCluster	= 49
DOS_getFileSize	= 50
DOS_getDirEntry	= 51
KBDInit             	= 52
KBDGetKey           	= 53
KBDGetMods         	= 54
KBDCheckDirect      	= 55
SilverInit          	= 56
SilverSend           	= 57
SetCallBack         	= 58
PadInit             	= 59
PadSetup            	= 60
PadStart            	= 61
GetErrorCode        	= 62
PadVersion		= 63
InternalInfo		= 64
SendControlDataOut   	= 65

USBDriverEntryPoint = 404Ch




mouseRight    = 1
mouseLeft     = 2
mouseUp       = 4
mouseDown     = 8

mouseBitRight = 0
mouseBitLeft  = 1
mouseBitUp    = 2
mouseBitDown  = 3

MSDBOTState					= 0			;1 - bulk-only transport state (0 - nothing, 1 - getting data, 2 - CSW)
MSDLBA						= MSDBOTState+1	;4 - LBA address for read/write routines
MSDPartStart					= MSDLBA+4		;4 - start of FAT16 partition
MSDBPS						= MSDPartStart+4	;2 - bytes per sector (typically 512)
MSDDataInAddr					= MSDBPS+2		;2 - address of incoming/outgoing data
MSDDataInExpected				= MSDDataInAddr+2	;2 - data expected to be received/sent
MSDSPC						= MSDDataInExpected+2	;1 - sectors per cluster (typically 2, 4, and so on)
MSDFATSectors					= MSDSPC+1		;2 - number of sectors per FAT
MSDRootDirSectors				= MSDFATSectors+2	;2 - number of root directory sectors (something like 32)
MSDFirstDataSector				= MSDRootDirSectors+2	;4 - first data sector
MSDFirstRootDirSecNum				= MSDFirstDataSector+4;2 - first sector of root
MSDFATStart					= MSDFirstRootDirSecNum+2;2 - first sector of first FAT
MSDOutPipe					= MSDFATStart+2	;1 - bulk out pipe
MSDInPipe					= MSDOutPipe+1	;1 - bulk in pipe
MSDFAT_sector					= MSDInPipe+1		;2 - temporary storage for FAT_getNextCluster output
MSDDescBuf					= MSDFAT_sector+2	;128 - buffer for descriptors
MSDBulkBuf					= MSDDescBuf+128	;31 - incoming data buffer
MSDPacketData					= MSDBulkBuf+31	;64 - buffer to build data packets

errPort82			= 1
errPort91Bit2			= 2 ;Stall
errPort91Bit4			= 3
errPort4C			= 4
errPort4D	    	       = 5
errPort3A	           	= 6
errPort81                   = 7
errPort8C                   = 8
errPort8F                   = 9
errEPIndex                  = 10
errEPGetDes                 = 11
errNoDevice                 = 12
errFindDescriptor           = 13
errBadClass                 = 14
errTimeout                  = 15
errBadID                    = 16
errReadDescriptor           = 17
errInit                     = 18
errAutoSetup                = 19
errDemo                     = 20
errFindPipe                 = 21
errStub                     = 22
errGetMaxPacketSize		= 23
errSetAddress               = 24
errMSDSendCmd               = 25	;Problem sending bulk-only in/out commands
errMSDLargeSect             = 26	;Bytes-per-sector is larger than 512 (not supported)
errMSDNoParts               = 27	;No FAT16 primary partitions were found
errMSDNoMoreMemory          = 28	;Function failed because no more memory available (this is a bad thing, delete incomplete file/directories)
errMSDInvalidPartition      = 29	;Partition is invalid (does not contain 0AA55h)
errMSDNotFile               = 30	;Path is a directory or volume label, not file
errMSDNotValidPath          = 31	;Path is not in any way valid :)
errMSDCorruptFile           = 32	;File is corrupted or otherwise not as expected
errMSDNotFound              = 33	;File/directory was not found
errMSDDuplicateFound        = 34 ;Duplicate calculator variable was found and was not overwritten
errMSDNotEnoughMem          = 35 ;Not enough memory to create new calculator variable
errMSDAlreadyExists         = 36	;*.8x* of same name already exists
errMSDStrNotFound           = 37 ;String variable from BASIC OpenLib/ExecLib function not found
errMSDVarEmpty              = 38 ;Variable was empty and transfer was not completed (this is bad, delete incomplete file)
errMSDNoParts32		= 39 ;FAT32 partitions were found, but none were FAT16 (just a friendly message)
errNoData                   = 40
errVersion                  = 41


descDevice    = 1
descConfig    = 2
descString    = 3
descInterface = 4
descEndpoint  = 5
descDQ        = 6
descOSC       = 7
descIP        = 8
descOnTheGo   = 9
descHID       = 21h
descReport    = 22h
descHub       = 29h


kbdFlagNoLocks = 0
kbdFlagNumLock = 1
kbdFlagCapsLock = 2
kbdFlagScrollLock = 3
kbdFlagCallBack = 4

kbdLCtrlBit = 0
kbdLShiftBit = 1
kbdLAltBit = 2
kbdLGUIBit = 3
kbdRCtrlBit = 4
kbdRShiftBit = 5
kbdRAltBit = 6
kbdRGUIBit = 7

kbdLCtrl = 1
kbdLShift = 2
kbdLAlt = 4
kbdLGUI = 8
kbdRCtrl = 16
kbdRShift = 32
kbdRAlt = 64
kbdRGUI = 128

kbdK1         = 1Eh
kbdK2         = 1Fh
kbdK3         = 20h
kbdK4         = 21h
kbdK5         = 22h
kbdK6         = 23h
kbdK7         = 24h
kbdK8         = 25h
kbdK9         = 26h
kbdK0         = 27h

kbdKBackQuote = 35h       ;` ~
kbdKMinus     = 2Dh       ;- _
kbdKEqu       = 2Eh       ;= +
kbdKBackSpace = 2Ah
kbdKEsc       = 29h
kbdKTab       = 2Bh
kbdKCapsLock  = 39h
kbdKLBrack    = 2Fh
kbdKRBrack    = 30h
kbdKBackSlash = 31h
kbdKSemicolon = 33h
kbdKApostrophe = 34h
kbdKEnter     = 28h
kbdKComma     = 36h
kbdKPeriod    = 37h
kbdKSlash     = 38h
kbdKSpace     = 2Ch

kbdKA         = 4
kbdKB         = 5
kbdKC         = 6
kbdKD         = 7
kbdKE         = 8
kbdKF         = 9
kbdKG         = 0Ah
kbdKH         = 0Bh
kbdKI         = 0Ch
kbdKJ         = 0Dh
kbdKK         = 0Eh
kbdKL         = 0Fh
kbdKM         = 10h
kbdKN         = 11h
kbdKO         = 12h
kbdKP         = 13h
kbdKQ         = 14h
kbdKR         = 15h
kbdKS         = 16h
kbdKT         = 17h
kbdKU         = 18h
kbdKV         = 19h
kbdKW         = 1Ah
kbdKX         = 1Bh
kbdKY         = 1Ch
kbdKZ         = 1Dh

kbdKF1        = 3Ah
kbdKF2        = 3Bh
kbdKF3        = 3Ch
kbdKF4        = 3Dh
kbdKF5        = 3Eh
kbdKF6        = 3Fh
kbdKF7        = 40h
kbdKF8        = 41h
kbdKF9        = 42h
kbdKF10       = 43h
kbdKF11       = 44h
kbdKF12       = 45h

kbdKRight     = 4Fh
kbdKLeft      = 50h
kbdKDown      = 51h
kbdKUp        = 52h

kbdKSysRq     = 46h
kbdKScrollLock = 47h
kbdKBreak     = 48h
kbdKInsert    = 49h
kbdKHome      = 4Ah
kbdKPageUp    = 4Bh
kbdKDelete    = 4Ch
kbdKEnd       = 4Dh
kbdKPageDown  = 4Eh

kbdKNumLock   = 53h
kbdKNSlash    = 54h
kbdKNStar     = 55h
kbdKNMinus    = 56h
kbdKNPlus     = 57h
kbdKNEnter    = 58h
kbdKN1        = 59h
kbdKN2        = 5Ah
kbdKN3        = 5Bh
kbdKN4        = 5Ch
kbdKN5        = 5Dh
kbdKN6        = 5Eh
kbdKN7        = 5Fh
kbdKN8        = 60h
kbdKN9        = 61h
kbdKN0        = 62h
kbdKNPeriod   = 63h

#macro U_CALL(labelb)
  #if labelb = SilverInit
    ld ix,USBDriverCall+offCallBack
  #endif
  #if labelb = KBDInit
    ld ix,USBDriverCall+offCallBack
  #endif
  #if labelb = AutoSetup
    ld de,USBDriverCall+offCallBack
  #endif
  #if labelb = PadInit
    ld ix,USBDriverCall+offCallBack
  #endif
  #if labelb = MouseInit
    ld ix,USBDriverCall+offCallBack
  #endif
  ld a,labelb
  call USBDriverCall
#endmacro

#macro U_CALL_INIT(label)
 	ld	hl,uuAppName
	rst    20h
	rst    28h
	.dw    $4C4E
	jr     c,uuDone
	ld     de,USBDriverCall
	ld     hl,uuDriverCode
	ld     bc,uuDriverSize
	ldir
	ld     (USBDriverCall+uuMod1 + 1),a
	in     a,(6)
	ld     (USBDriverCall+uuMod3 +1),a
	or     a
	jr     uuDone
uuAppName:
	.db     $14,"USBDRV8X",0
uuDriverCode:
	push   af
	in     a,(6)
	ld     (USBDriverCall+uuMod2 +1),a
	call	USBDriverCall+uuCall1
	pop    af
	call   USBDriverEntryPoint
	push   af
uuMod2 = $ - uuDriverCode
	ld     a,0
	out    (6),a
	pop    af
	ret
uuCallBack:
uuMod3 = $-uuDriverCode
	ld     a,0
	out    (6),a
	call   label
uuCall1 EQU $-uuDriverCode
uuMod1 EQU $-uuDriverCode
	ld	a,0
	out    (6),a
	ret
uuDriverSize = $ - uuDriverCode
uuDone:
offCallBack          = uuCallBack-uuDriverCode
#endmacro