libtransistor
A userland library for the Nintendo Switch
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
usb.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include<libtransistor/types.h>
14 
15 typedef enum {
16  TRN_USB_DT_DEVICE = 0x1,
17  TRN_USB_DT_CONFIGURATION = 0x2,
18  TRN_USB_DT_STRING = 0x3,
19  TRN_USB_DT_INTERFACE = 0x4,
20  TRN_USB_DT_ENDPOINT = 0x5,
21  TRN_USB_DT_BOS = 0xf,
22  TRN_USB_DT_DEVICE_CAPABILITY = 0x10
23 } trn_usb_descriptor_type_t;
24 
25 typedef enum {
26  TRN_USB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1,
27  TRN_USB_BT_USB_2_0_EXTENSION = 2,
28  TRN_USB_BT_SS_USB_DEVICE_CAPABILITY = 3,
29  TRN_USB_BT_CONTAINER_ID = 4,
30 } trn_usb_bos_type_t;
31 
32 typedef enum {
33  TRN_USB_ENDPOINT_OUT = 0x00,
34  TRN_USB_ENDPOINT_IN = 0x80,
35 } trn_usb_endpoint_direction_t;
36 
37 typedef enum {
38  TRN_USB_TRANSFER_TYPE_CONTROL = 0x0,
39  TRN_USB_TRANSFER_TYPE_ISOCHRONOUS = 0x1,
40  TRN_USB_TRANSFER_TYPE_BULK = 0x2,
41  TRN_USB_TRANSFER_TYPE_INTERRUPT = 0x3,
42 } trn_usb_transfer_type_t;
43 
44 
45 typedef struct __attribute__((packed, aligned(1))) {
46  uint8_t bLength;
47  uint8_t bDescriptorType;
48 } usb_descriptor_t;
49 
50 typedef struct __attribute__((packed, aligned(1))) {
51  uint8_t bLength;
52  uint8_t bDescriptorType; //< must be TRN_USB_DT_DEVICE
53  uint16_t bcdUSB;
54  uint8_t bDeviceClass;
55  uint8_t bDeviceSubClass;
56  uint8_t bDeviceProtocol;
57  uint8_t bMaxPacketSize;
58  uint16_t idVendor;
59  uint16_t idProduct;
60  uint16_t bcdDevice;
61  uint8_t iManufacturer;
62  uint8_t iProduct;
63  uint8_t iSerialNumber;
64  uint8_t bNumConfigurations;
65 } usb_device_descriptor_t;
66 
67 typedef struct __attribute__((packed, aligned(1))) {
68  uint8_t bLength;
69  uint8_t bDescriptorType; //< must be TRN_USB_DT_CONFIGURATION
70  uint16_t wTotalLength;
71  uint8_t bNumInterfaces;
72  uint8_t bConfigurationValue;
73  uint8_t iConfiguration;
74  uint8_t bmAttributes;
75  uint8_t bMaxPower;
76 } usb_configuration_descriptor_t;
77 
78 typedef struct __attribute__((packed, aligned(1))) {
79  uint8_t bLength; //< must be 0x9
80  uint8_t bDescriptorType; //< must be TRN_USB_DT_INTERFACE
81  uint8_t bInterfaceNumber; //< 0x4 means allocate automatically
82  uint8_t bAlternateSetting; //< must be 0x00
83  uint8_t bNumEndpoints; //< ignored
84  uint8_t bInterfaceClass; // 0xff
85  uint8_t bInterfaceSubClass; // 0xff
86  uint8_t bInterfaceProtocol; // 0xff
87  uint8_t iInterface; //< ignored
88 } usb_interface_descriptor_t;
89 
90 typedef struct __attribute__((packed, aligned(1))) {
91  uint8_t bLength; //< must be 0x7
92  uint8_t bDescriptorType; //< must be TRN_USB_DT_ENDPOINT
93  uint8_t bEndpointAddress; //< actual address is automatically allocated, but direction is indicated here by \ref trn_usb_endpoint_direction_t.
94  uint8_t bmAttributes;
95  uint16_t wMaxPacketSize;
96  uint8_t bInterval;
97 } usb_endpoint_descriptor_t;
98 
99 typedef struct __attribute__((packed, aligned(1))) {
100  uint8_t bLength;
101  uint8_t bDescriptorType;
102  union {
103  trn_char16_t bString[0x40];
104  uint16_t wLANGID[0x40];
105  };
106 } usb_string_descriptor_t;
107 
108 typedef struct __attribute__((packed, aligned(1))) {
109  uint8_t bLength;
110  uint8_t bDescriptorType;
111  uint8_t bDevCapabilityType;
112  uint8_t dev_capability_data[0];
113 } usb_bos_dev_capability_descriptor_t;
114 
115 typedef struct __attribute__((packed, aligned(1))) {
116  uint8_t bLength;
117  uint8_t bDescriptorType;
118  uint16_t wTotalLength;
119  usb_bos_dev_capability_descriptor_t capabilities[0];
120 } usb_bos_descriptor_t;
121 
122 typedef struct {
123  uint16_t id_vendor;
124  uint16_t id_product;
125  uint16_t bcd_device;
126  char manufacturer[0x20];
127  char product[0x20];
128  char serial_number[0x20];
130 
131 typedef enum {
132  USB_DS_STATE_INITIAL = 0,
133  USB_DS_STATE_INIT_STARTING = 6,
134  USB_DS_STATE_INIT2 = 3,
135  USB_DS_STATE_INIT3 = 4,
136  USB_DS_STATE_INITIALIZED = 5,
137 
138  USB_DS_STATE_MAX = 0xFFFFFFFF
139 } usb_ds_state_t;
140 
143 
148 result_t usb_ds_init(uint32_t complex_id, usb_device_data_t *data);
149 
156 result_t usb_ds_get_interface(usb_interface_descriptor_t *descriptor, const char *name, usb_ds_interface_t *out);
157 
162 
166 result_t usb_ds_get_state(usb_ds_state_t *state);
167 
171 void usb_ds_finalize();
172 
173 // private
174 typedef struct {
175  bool valid;
176  uint16_t bcdUSB;
177  uint32_t speed_mode;
179 extern _usb_speed_info_t _usb_speed_info[];
180 extern uint8_t _usb_next_in_ep_number;
181 extern uint8_t _usb_next_out_ep_number;
182 
183 result_t _usb_ds_enable();
184 result_t _usb_ds_disable();
185 result_t _usb_ds_500_append_configuration_data(usb_ds_interface_t *interface, uint32_t speed_mode, usb_descriptor_t *descriptor);
186 
187 #ifdef __cplusplus
188 }
189 #endif
Various system types.
void usb_ds_finalize()
Finalize USB as device services.
Definition: graphic_buffer_queue.h:83
uint32_t result_t
Function result.
Definition: types.h:51
handle_t revent_h
revent handle
Definition: types.h:47
USB-as-device interface.
Definition: usb.h:122
String encoding functions Based on https://github.com/nothings/stb.
Definition: interface.h:20
result_t usb_ds_get_state_change_event(revent_h *evt)
Get state change event.
result_t usb_ds_get_state(usb_ds_state_t *state)
Get state.
result_t usb_ds_init(uint32_t complex_id, usb_device_data_t *data)
Initialize USB-as-device services.
Definition: usb.h:174
USB-as-device endpoint.
result_t usb_ds_get_interface(usb_interface_descriptor_t *descriptor, const char *name, usb_ds_interface_t *out)
Open a new USB interface.