haze: usb_session: style

This commit is contained in:
Liam 2023-04-15 19:46:22 -04:00
commit dce6173137
2 changed files with 66 additions and 65 deletions

View file

@ -59,10 +59,10 @@ namespace haze {
PtpOperationCode_DeleteObject, PtpOperationCode_DeleteObject,
}; };
constexpr PtpEventCode SupportedEventCodes[] = {}; constexpr PtpEventCode SupportedEventCodes[] = { /* ... */};
constexpr PtpDevicePropertyCode SupportedPropertyCodes[] = {}; constexpr PtpDevicePropertyCode SupportedPropertyCodes[] = { /* ...*/ };
constexpr PtpObjectFormatCode SupportedCaptureFormats[] = {}; constexpr PtpObjectFormatCode SupportedCaptureFormats[] = { /* ...*/ };
constexpr PtpObjectFormatCode SupportedPlaybackFormats[] = {}; constexpr PtpObjectFormatCode SupportedPlaybackFormats[] = { /* ...*/ };
constexpr StorageId SupportedStorageIds[] = { constexpr StorageId SupportedStorageIds[] = {
StorageId_SdmcFs, StorageId_SdmcFs,

View file

@ -19,43 +19,43 @@ namespace haze {
namespace { namespace {
static constexpr u32 DefaultInterfaceNumber = 0; constexpr const u32 DefaultInterfaceNumber = 0;
} }
Result UsbSession::Initialize1x(const UsbCommsInterfaceInfo *info) { Result UsbSession::Initialize1x(const UsbCommsInterfaceInfo *info) {
struct usb_interface_descriptor interface_descriptor = { struct usb_interface_descriptor interface_descriptor = {
.bLength = USB_DT_INTERFACE_SIZE, .bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE, .bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = DefaultInterfaceNumber, .bInterfaceNumber = DefaultInterfaceNumber,
.bInterfaceClass = info->bInterfaceClass, .bInterfaceClass = info->bInterfaceClass,
.bInterfaceSubClass = info->bInterfaceSubClass, .bInterfaceSubClass = info->bInterfaceSubClass,
.bInterfaceProtocol = info->bInterfaceProtocol, .bInterfaceProtocol = info->bInterfaceProtocol,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_in = { struct usb_endpoint_descriptor endpoint_descriptor_in = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_IN, .bEndpointAddress = USB_ENDPOINT_IN,
.bmAttributes = USB_TRANSFER_TYPE_BULK, .bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength, .wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_out = { struct usb_endpoint_descriptor endpoint_descriptor_out = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_OUT, .bEndpointAddress = USB_ENDPOINT_OUT,
.bmAttributes = USB_TRANSFER_TYPE_BULK, .bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength, .wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_interrupt = { struct usb_endpoint_descriptor endpoint_descriptor_interrupt = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_IN, .bEndpointAddress = USB_ENDPOINT_IN,
.bmAttributes = USB_TRANSFER_TYPE_INTERRUPT, .bmAttributes = USB_TRANSFER_TYPE_INTERRUPT,
.wMaxPacketSize = 0x18, .wMaxPacketSize = 0x18,
.bInterval = 0x4, .bInterval = 0x4,
}; };
/* Set up interface. */ /* Set up interface. */
@ -71,45 +71,45 @@ namespace haze {
Result UsbSession::Initialize5x(const UsbCommsInterfaceInfo *info) { Result UsbSession::Initialize5x(const UsbCommsInterfaceInfo *info) {
struct usb_interface_descriptor interface_descriptor = { struct usb_interface_descriptor interface_descriptor = {
.bLength = USB_DT_INTERFACE_SIZE, .bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE, .bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = DefaultInterfaceNumber, .bInterfaceNumber = DefaultInterfaceNumber,
.bNumEndpoints = 3, .bNumEndpoints = 3,
.bInterfaceClass = info->bInterfaceClass, .bInterfaceClass = info->bInterfaceClass,
.bInterfaceSubClass = info->bInterfaceSubClass, .bInterfaceSubClass = info->bInterfaceSubClass,
.bInterfaceProtocol = info->bInterfaceProtocol, .bInterfaceProtocol = info->bInterfaceProtocol,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_in = { struct usb_endpoint_descriptor endpoint_descriptor_in = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_IN, .bEndpointAddress = USB_ENDPOINT_IN,
.bmAttributes = USB_TRANSFER_TYPE_BULK, .bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength, .wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_out = { struct usb_endpoint_descriptor endpoint_descriptor_out = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_OUT, .bEndpointAddress = USB_ENDPOINT_OUT,
.bmAttributes = USB_TRANSFER_TYPE_BULK, .bmAttributes = USB_TRANSFER_TYPE_BULK,
.wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength, .wMaxPacketSize = PtpUsbBulkHighSpeedMaxPacketLength,
}; };
struct usb_endpoint_descriptor endpoint_descriptor_interrupt = { struct usb_endpoint_descriptor endpoint_descriptor_interrupt = {
.bLength = USB_DT_ENDPOINT_SIZE, .bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT, .bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_ENDPOINT_IN, .bEndpointAddress = USB_ENDPOINT_IN,
.bmAttributes = USB_TRANSFER_TYPE_INTERRUPT, .bmAttributes = USB_TRANSFER_TYPE_INTERRUPT,
.wMaxPacketSize = 0x18, .wMaxPacketSize = 0x18,
.bInterval = 0x4, .bInterval = 0x4,
}; };
struct usb_ss_endpoint_companion_descriptor endpoint_companion = { struct usb_ss_endpoint_companion_descriptor endpoint_companion = {
.bLength = sizeof(struct usb_ss_endpoint_companion_descriptor), .bLength = sizeof(struct usb_ss_endpoint_companion_descriptor),
.bDescriptorType = USB_DT_SS_ENDPOINT_COMPANION, .bDescriptorType = USB_DT_SS_ENDPOINT_COMPANION,
.bMaxBurst = 0x0F, .bMaxBurst = 0x0F,
.bmAttributes = 0x00, .bmAttributes = 0x00,
.wBytesPerInterval = 0x00, .wBytesPerInterval = 0x00,
}; };
@ -150,30 +150,31 @@ namespace haze {
Result UsbSession::Initialize(const UsbCommsInterfaceInfo *info, u16 id_vendor, u16 id_product) { Result UsbSession::Initialize(const UsbCommsInterfaceInfo *info, u16 id_vendor, u16 id_product) {
R_TRY(usbDsInitialize()); R_TRY(usbDsInitialize());
if (hosversionAtLeast(5,0,0)) { if (hosversionAtLeast(5, 0, 0)) {
u8 iManufacturer, iProduct, iSerialNumber; static const u16 supported_langs[1] = { 0x0409 };
static const u16 supported_langs[1] = {0x0409};
R_TRY(usbDsAddUsbLanguageStringDescriptor(NULL, supported_langs, sizeof(supported_langs)/sizeof(u16))); R_TRY(usbDsAddUsbLanguageStringDescriptor(NULL, supported_langs, util::size(supported_langs)));
u8 iManufacturer, iProduct, iSerialNumber;
R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iManufacturer), "Nintendo")); R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iManufacturer), "Nintendo"));
R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iProduct), "Nintendo Switch")); R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iProduct), "Nintendo Switch"));
R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iSerialNumber), "SerialNumber")); R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iSerialNumber), "SerialNumber"));
// Send device descriptors /* Send device descriptors */
struct usb_device_descriptor device_descriptor = { struct usb_device_descriptor device_descriptor = {
.bLength = USB_DT_DEVICE_SIZE, .bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE, .bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200, .bcdUSB = 0x0200,
.bDeviceClass = 0x00, .bDeviceClass = 0x00,
.bDeviceSubClass = 0x00, .bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00, .bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 0x40, .bMaxPacketSize0 = 0x40,
.idVendor = id_vendor, .idVendor = id_vendor,
.idProduct = id_product, .idProduct = id_product,
.bcdDevice = 0x0100, .bcdDevice = 0x0100,
.iManufacturer = iManufacturer, .iManufacturer = iManufacturer,
.iProduct = iProduct, .iProduct = iProduct,
.iSerialNumber = iSerialNumber, .iSerialNumber = iSerialNumber,
.bNumConfigurations = 0x01 .bNumConfigurations = 0x01
}; };
R_TRY(usbDsSetUsbDeviceDescriptor(UsbDeviceSpeed_High, std::addressof(device_descriptor))); R_TRY(usbDsSetUsbDeviceDescriptor(UsbDeviceSpeed_High, std::addressof(device_descriptor)));
@ -207,9 +208,9 @@ namespace haze {
R_TRY(usbDsSetBinaryObjectStore(bos, sizeof(bos))); R_TRY(usbDsSetBinaryObjectStore(bos, sizeof(bos)));
} }
R_TRY(hosversionAtLeast(5,0,0) ? this->Initialize5x(info) : this->Initialize1x(info)); R_TRY(hosversionAtLeast(5, 0, 0) ? this->Initialize5x(info) : this->Initialize1x(info));
if (hosversionAtLeast(5,0,0)) { if (hosversionAtLeast(5, 0, 0)) {
R_TRY(usbDsEnable()); R_TRY(usbDsEnable());
} }
@ -233,7 +234,7 @@ namespace haze {
} }
Result UsbSession::TransferAsync(UsbSessionEndpoint ep, void *buffer, size_t size, u32 *out_urb_id) { Result UsbSession::TransferAsync(UsbSessionEndpoint ep, void *buffer, size_t size, u32 *out_urb_id) {
return usbDsEndpoint_PostBufferAsync(m_endpoints[ep], buffer, size, out_urb_id); R_RETURN(usbDsEndpoint_PostBufferAsync(m_endpoints[ep], buffer, size, out_urb_id));
} }
Result UsbSession::GetTransferResult(UsbSessionEndpoint ep, u32 urb_id, u32 *out_transferred_size) { Result UsbSession::GetTransferResult(UsbSessionEndpoint ep, u32 urb_id, u32 *out_transferred_size) {