#pragma once

#include "Emu/Memory/vm_ptr.h"

#define MAX_SYS_USBD_TRANSFERS 0x44

// PS3 internal codes
enum PS3StandardUsbErrors : u32
{
	HC_CC_NOERR     = 0x00,
	EHCI_CC_MISSMF  = 0x10,
	EHCI_CC_XACT    = 0x20,
	EHCI_CC_BABBLE  = 0x30,
	EHCI_CC_DATABUF = 0x40,
	EHCI_CC_HALTED  = 0x50,
};

enum PS3IsochronousUsbErrors : u8
{
	USBD_HC_CC_NOERR   = 0x00,
	USBD_HC_CC_MISSMF  = 0x01,
	USBD_HC_CC_XACT    = 0x02,
	USBD_HC_CC_BABBLE  = 0x04,
	USBD_HC_CC_DATABUF = 0x08,
};

enum SysUsbdEvents : u32
{
	SYS_USBD_ATTACH            = 0x01,
	SYS_USBD_DETACH            = 0x02,
	SYS_USBD_TRANSFER_COMPLETE = 0x03,
	SYS_USBD_TERMINATE         = 0x04,
};

// PS3 internal structures
struct UsbInternalDevice
{
	u8 device_high; // System flag maybe (used in generating actual device number)
	u8 device_low;  // Just a number identifying the device (used in generating actual device number)
	u8 unk3;        // ? Seems to always be 2?
	u8 unk4;        // ?
};

struct UsbDeviceRequest
{
	u8 bmRequestType;
	u8 bRequest;
	be_t<u16> wValue;
	be_t<u16> wIndex;
	be_t<u16> wLength;
};

struct UsbDeviceIsoRequest
{
	vm::ptr<void> buf;
	be_t<u32> start_frame;
	be_t<u32> num_packets;
	be_t<u16> packets[8];
};

s32 sys_usbd_initialize(vm::ptr<u32> handle);
s32 sys_usbd_finalize(ppu_thread& ppu, u32 handle);
s32 sys_usbd_get_device_list(u32 handle, vm::ptr<UsbInternalDevice> device_list, u32 max_devices);
s32 sys_usbd_get_descriptor_size(u32 handle, u32 device_handle);
s32 sys_usbd_get_descriptor(u32 handle, u32 device_handle, vm::ptr<void> descriptor, u32 desc_size);
s32 sys_usbd_register_ldd(u32 handle, vm::ptr<char> s_product, u16 slen_product);
s32 sys_usbd_unregister_ldd();
s32 sys_usbd_open_pipe(u32 handle, u32 device_handle, u32 unk1, u64 unk2, u64 unk3, u32 endpoint, u64 unk4);
s32 sys_usbd_open_default_pipe(u32 handle, u32 device_handle);
s32 sys_usbd_close_pipe(u32 handle, u32 pipe_handle);
s32 sys_usbd_receive_event(ppu_thread& ppu, u32 handle, vm::ptr<u64> arg1, vm::ptr<u64> arg2, vm::ptr<u64> arg3);
s32 sys_usbd_detect_event();
s32 sys_usbd_attach(u32 handle);
s32 sys_usbd_transfer_data(u32 handle, u32 id_pipe, vm::ptr<u8> buf, u32 buf_size, vm::ptr<UsbDeviceRequest> request, u32 type_transfer);
s32 sys_usbd_isochronous_transfer_data(u32 handle, u32 id_pipe, vm::ptr<UsbDeviceIsoRequest> iso_request);
s32 sys_usbd_get_transfer_status(u32 handle, u32 id_transfer, u32 unk1, vm::ptr<u32> result, vm::ptr<u32> count);
s32 sys_usbd_get_isochronous_transfer_status(u32 handle, u32 id_transfer, u32 unk1, vm::ptr<UsbDeviceIsoRequest> request, vm::ptr<u32> result);
s32 sys_usbd_get_device_location();
s32 sys_usbd_send_event();
s32 sys_usbd_event_port_send(u32 handle, u64 arg1, u64 arg2, u64 arg3);
s32 sys_usbd_allocate_memory();
s32 sys_usbd_free_memory();
s32 sys_usbd_get_device_speed();
s32 sys_usbd_register_extra_ldd(u32 handle, vm::ptr<char> s_product, u16 slen_product, u16 id_vendor, u16 id_product_min, u16 id_product_max);
