libnx
usb.h
Go to the documentation of this file.
1 /**
2  * @file usb.h
3  * @brief USB (usb:*) service IPC wrapper.
4  * @author yellows8
5  * @copyright libnx Authors
6  */
7 #pragma once
8 #include "../types.h"
9 #include "../services/sm.h"
10 
11 /// usb:ds Switch-as-device<>host USB comms, see also here: http://switchbrew.org/index.php?title=USB_services
12 
13 /// Names starting with "libusb" were changed to "usb" to avoid collision with actual libusb if it's ever used.
14 
15 #define USBDS_DEFAULT_InterfaceNumber 0x4 ///Value for usb_interface_descriptor bInterfaceNumber for automatically allocating the actual bInterfaceNumber.
16 
17 /// Imported from libusb with changed names.
18 /* Descriptor sizes per descriptor type */
19 #define USB_DT_INTERFACE_SIZE 9
20 #define USB_DT_ENDPOINT_SIZE 7
21 
22 /// Imported from libusb, with some adjustments.
24  uint8_t bLength;
25  uint8_t bDescriptorType; /// Must match USB_DT_ENDPOINT.
26  uint8_t bEndpointAddress; /// Should be one of the usb_endpoint_direction values, the endpoint-number is automatically allocated.
27  uint8_t bmAttributes;
28  uint16_t wMaxPacketSize;
29  uint8_t bInterval;
30 };
31 
32 /// Imported from libusb, with some adjustments.
34  uint8_t bLength;
35  uint8_t bDescriptorType; /// Must match USB_DT_INTERFACE.
36  uint8_t bInterfaceNumber; /// See also USBDS_DEFAULT_InterfaceNumber.
37  uint8_t bAlternateSetting; /// Must match 0.
38  uint8_t bNumEndpoints; /// Ignored.
39  uint8_t bInterfaceClass;
40  uint8_t bInterfaceSubClass;
41  uint8_t bInterfaceProtocol;
42  uint8_t iInterface; /// Ignored.
43 };
44 
45 typedef struct {
46  u16 idVendor; /// VID
47  u16 idProduct; /// PID
49  char Manufacturer[0x20];
50  char Product[0x20];
51  char SerialNumber[0x20];
53 
54 typedef struct {
55  u32 id; /// urbId from post-buffer cmds
57  u32 transferredSize;
58  u32 urb_status;
60 
61 typedef struct {
62  UsbDsReportEntry report[8];
63  u32 report_count;
65 
66 typedef struct {
67  bool initialized;
68  u32 interface_index;
69  Service h;
70 
71  Handle SetupEvent;
72  Handle CtrlInCompletionEvent;
73  Handle CtrlOutCompletionEvent;
75 
76 typedef struct {
77  bool initialized;
78  Service h;
79  Handle CompletionEvent;
81 
82 typedef enum {
83  UsbComplexId_Default = 0x2
84 } UsbComplexId;
85 
86 /// Imported from libusb, with changed names.
88  USB_CLASS_PER_INTERFACE = 0,
89  USB_CLASS_AUDIO = 1,
90  USB_CLASS_COMM = 2,
91  USB_CLASS_HID = 3,
92  USB_CLASS_PHYSICAL = 5,
93  USB_CLASS_PRINTER = 7,
94  USB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
95  USB_CLASS_IMAGE = 6,
96  USB_CLASS_MASS_STORAGE = 8,
97  USB_CLASS_HUB = 9,
98  USB_CLASS_DATA = 10,
99  USB_CLASS_SMART_CARD = 0x0b,
100  USB_CLASS_CONTENT_SECURITY = 0x0d,
101  USB_CLASS_VIDEO = 0x0e,
102  USB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
103  USB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
104  USB_CLASS_WIRELESS = 0xe0,
105  USB_CLASS_APPLICATION = 0xfe,
106  USB_CLASS_VENDOR_SPEC = 0xff
107 };
108 
109 /// Imported from libusb, with changed names.
111  USB_DT_DEVICE = 0x01,
112  USB_DT_CONFIG = 0x02,
113  USB_DT_STRING = 0x03,
114  USB_DT_INTERFACE = 0x04,
115  USB_DT_ENDPOINT = 0x05,
116  USB_DT_BOS = 0x0f,
117  USB_DT_DEVICE_CAPABILITY = 0x10,
118  USB_DT_HID = 0x21,
119  USB_DT_REPORT = 0x22,
120  USB_DT_PHYSICAL = 0x23,
121  USB_DT_HUB = 0x29,
122  USB_DT_SUPERSPEED_HUB = 0x2a,
123  USB_DT_SS_ENDPOINT_COMPANION = 0x30
124 };
125 
126 /// Imported from libusb, with changed names.
128  USB_ENDPOINT_IN = 0x80,
129  USB_ENDPOINT_OUT = 0x00
130 };
131 
132 /// Imported from libusb, with changed names.
134  USB_TRANSFER_TYPE_CONTROL = 0,
135  USB_TRANSFER_TYPE_ISOCHRONOUS = 1,
136  USB_TRANSFER_TYPE_BULK = 2,
137  USB_TRANSFER_TYPE_INTERRUPT = 3,
138  USB_TRANSFER_TYPE_BULK_STREAM = 4,
139 };
140 
141 /// Imported from libusb, with changed names.
143  USB_ISO_SYNC_TYPE_NONE = 0,
144  USB_ISO_SYNC_TYPE_ASYNC = 1,
145  USB_ISO_SYNC_TYPE_ADAPTIVE = 2,
146  USB_ISO_SYNC_TYPE_SYNC = 3
147 };
148 
149 /// Imported from libusb, with changed names.
151  USB_ISO_USAGE_TYPE_DATA = 0,
152  USB_ISO_USAGE_TYPE_FEEDBACK = 1,
153  USB_ISO_USAGE_TYPE_IMPLICIT = 2,
154 };
155 
156 Result usbDsInitialize(UsbComplexId complexId, const UsbDsDeviceInfo* deviceinfo);
157 
158 /// Exit usbDs. Any interfaces/endpoints which are left open are automatically closed, since otherwise usb-sysmodule won't fully reset usbds to defaults.
159 void usbDsExit(void);
160 
161 Service* usbDsGetServiceSession(void);
162 Handle usbDsGetStateChangeEvent(void);
163 
164 Result usbDsGetState(u32 *out);
165 Result usbDsGetDsInterface(UsbDsInterface** interface, struct usb_interface_descriptor* descriptor, const char *interface_name);
166 
167 /// Wait for initialization to finish where data-transfer is usable.
168 Result usbDsWaitReady(void);
169 
170 /// Parse usbDsReportData from the Get*ReportData commands, where urbId is from the post-buffer commands. Will return the converted urb_status result-value.
171 Result usbDsParseReportData(UsbDsReportData *reportdata, u32 urbId, u32 *requestedSize, u32 *transferredSize);
172 
173 /// IDsInterface
174 void usbDsInterface_Close(UsbDsInterface* interface);
175 Result usbDsInterface_GetDsEndpoint(UsbDsInterface* interface, UsbDsEndpoint** endpoint, struct usb_endpoint_descriptor* descriptor);
176 Result usbDsInterface_EnableInterface(UsbDsInterface* interface);
177 Result usbDsInterface_DisableInterface(UsbDsInterface* interface);
178 Result usbDsInterface_CtrlInPostBufferAsync(UsbDsInterface* interface, void* buffer, size_t size, u32 *urbId);
179 Result usbDsInterface_CtrlOutPostBufferAsync(UsbDsInterface* interface, void* buffer, size_t size, u32 *urbId);
180 Result usbDsInterface_GetCtrlInReportData(UsbDsInterface* interface, UsbDsReportData *out);
181 Result usbDsInterface_GetCtrlOutReportData(UsbDsInterface* interface, UsbDsReportData *out);
182 Result usbDsInterface_StallCtrl(UsbDsInterface* interface);
183 
184 /// IDsEndpoint
185 
186 void usbDsEndpoint_Close(UsbDsEndpoint* endpoint);
187 Result usbDsEndpoint_PostBufferAsync(UsbDsEndpoint* endpoint, void* buffer, size_t size, u32 *urbId);
188 Result usbDsEndpoint_GetReportData(UsbDsEndpoint* endpoint, UsbDsReportData *out);
189 Result usbDsEndpoint_StallCtrl(UsbDsEndpoint* endpoint);
190 
Definition: usb.h:54
Definition: usb.h:76
Result usbDsWaitReady(void)
Wait for initialization to finish where data-transfer is usable.
uint8_t bInterfaceClass
Ignored.
Definition: usb.h:39
usb_endpoint_direction
Imported from libusb, with changed names.
Definition: usb.h:127
usb_iso_usage_type
Imported from libusb, with changed names.
Definition: usb.h:150
uint16_t u16
16-bit unsigned integer.
Definition: types.h:22
void usbDsEndpoint_Close(UsbDsEndpoint *endpoint)
IDsEndpoint.
u16 bcdDevice
PID.
Definition: usb.h:48
void usbDsInterface_Close(UsbDsInterface *interface)
IDsInterface.
u32 Handle
Kernel object handle.
Definition: types.h:45
Service object structure.
Definition: sm.h:23
u32 Result
Function error code result type.
Definition: types.h:46
Definition: usb.h:66
Imported from libusb, with some adjustments.
Definition: usb.h:33
u32 requestedSize
urbId from post-buffer cmds
Definition: usb.h:56
uint32_t u32
32-bit unsigned integer.
Definition: types.h:23
usb_transfer_type
Imported from libusb, with changed names.
Definition: usb.h:133
Result usbDsParseReportData(UsbDsReportData *reportdata, u32 urbId, u32 *requestedSize, u32 *transferredSize)
Parse usbDsReportData from the Get*ReportData commands, where urbId is from the post-buffer commands...
uint8_t bEndpointAddress
Must match USB_DT_ENDPOINT.
Definition: usb.h:26
usb_iso_sync_type
Imported from libusb, with changed names.
Definition: usb.h:142
u16 idProduct
VID.
Definition: usb.h:47
Imported from libusb, with some adjustments.
Definition: usb.h:23
uint8_t bInterfaceNumber
Must match USB_DT_INTERFACE.
Definition: usb.h:36
void usbDsExit(void)
Exit usbDs. Any interfaces/endpoints which are left open are automatically closed, since otherwise usb-sysmodule won&#39;t fully reset usbds to defaults.
Definition: usb.h:61
uint8_t bmAttributes
Should be one of the usb_endpoint_direction values, the endpoint-number is automatically allocated...
Definition: usb.h:27
uint8_t bNumEndpoints
Must match 0.
Definition: usb.h:38
usb_descriptor_type
Imported from libusb, with changed names.
Definition: usb.h:110
Definition: usb.h:45
usb_class_code
Imported from libusb, with changed names.
Definition: usb.h:87
uint8_t bAlternateSetting
See also USBDS_DEFAULT_InterfaceNumber.
Definition: usb.h:37