RetroArch
usb.h
Go to the documentation of this file.
1 #ifndef __USB_H__
2 #define __USB_H__
3 
4 #if defined(HW_RVL)
5 
6 #include <gcutil.h>
7 #include <gctypes.h>
8 
9 #define USB_MAXPATH IPC_MAXPATH_LEN
10 
11 #define USB_OK 0
12 #define USB_FAILED 1
13 
14 #define USB_CLASS_HID 0x03
15 #define USB_SUBCLASS_BOOT 0x01
16 #define USB_PROTOCOL_KEYBOARD 0x01
17 #define USB_PROTOCOL_MOUSE 0x02
18 
19 #define USB_REPTYPE_INPUT 0x01
20 #define USB_REPTYPE_OUTPUT 0x02
21 #define USB_REPTYPE_FEATURE 0x03
22 
23 /* Descriptor types */
24 #define USB_DT_DEVICE 0x01
25 #define USB_DT_CONFIG 0x02
26 #define USB_DT_STRING 0x03
27 #define USB_DT_INTERFACE 0x04
28 #define USB_DT_ENDPOINT 0x05
29 #define USB_DT_DEVICE_QUALIFIER 0x06
30 #define USB_DT_OTHER_SPEED_CONFIG 0x07
31 #define USB_DT_INTERFACE_POWER 0x08
32 #define USB_DT_OTG 0x09
33 #define USB_DT_DEBUG 0x10
34 #define USB_DT_INTERFACE_ASSOCIATION 0x11
35 #define USB_DT_HID 0x21
36 #define USB_DT_REPORT 0x22
37 #define USB_DT_PHYSICAL 0x23
38 #define USB_DT_CLASS_SPECIFIC_INTERFACE 0x24
39 #define USB_DT_CLASS_SPECIFIC_ENDPOINT 0x25
40 #define USB_DT_HUB 0x29
41 
42 /* Standard requests */
43 #define USB_REQ_GETSTATUS 0x00
44 #define USB_REQ_CLEARFEATURE 0x01
45 #define USB_REQ_SETFEATURE 0x03
46 #define USB_REQ_SETADDRESS 0x05
47 #define USB_REQ_GETDESCRIPTOR 0x06
48 #define USB_REQ_SETDESCRIPTOR 0x07
49 #define USB_REQ_GETCONFIG 0x08
50 #define USB_REQ_SETCONFIG 0x09
51 #define USB_REQ_GETINTERFACE 0x0A
52 #define USB_REQ_SETINTERFACE 0x0B
53 #define USB_REQ_SYNCFRAME 0x0C
54 
55 #define USB_REQ_GETREPORT 0x01
56 #define USB_REQ_GETIDLE 0x02
57 #define USB_REQ_GETPROTOCOL 0x03
58 #define USB_REQ_SETREPORT 0x09
59 #define USB_REQ_SETIDLE 0x0A
60 #define USB_REQ_SETPROTOCOL 0x0B
61 
62 /* Descriptor sizes per descriptor type */
63 #define USB_DT_DEVICE_SIZE 18
64 #define USB_DT_CONFIG_SIZE 9
65 #define USB_DT_INTERFACE_SIZE 9
66 #define USB_DT_ENDPOINT_SIZE 7
67 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
68 #define USB_DT_HID_SIZE 9
69 #define USB_DT_HUB_NONVAR_SIZE 7
70 
71 /* control message request type bitmask */
72 #define USB_CTRLTYPE_DIR_HOST2DEVICE (0<<7)
73 #define USB_CTRLTYPE_DIR_DEVICE2HOST (1<<7)
74 #define USB_CTRLTYPE_TYPE_STANDARD (0<<5)
75 #define USB_CTRLTYPE_TYPE_CLASS (1<<5)
76 #define USB_CTRLTYPE_TYPE_VENDOR (2<<5)
77 #define USB_CTRLTYPE_TYPE_RESERVED (3<<5)
78 #define USB_CTRLTYPE_REC_DEVICE 0
79 #define USB_CTRLTYPE_REC_INTERFACE 1
80 #define USB_CTRLTYPE_REC_ENDPOINT 2
81 #define USB_CTRLTYPE_REC_OTHER 3
82 
83 #define USB_REQTYPE_INTERFACE_GET (USB_CTRLTYPE_DIR_DEVICE2HOST|USB_CTRLTYPE_TYPE_CLASS|USB_CTRLTYPE_REC_INTERFACE)
84 #define USB_REQTYPE_INTERFACE_SET (USB_CTRLTYPE_DIR_HOST2DEVICE|USB_CTRLTYPE_TYPE_CLASS|USB_CTRLTYPE_REC_INTERFACE)
85 #define USB_REQTYPE_ENDPOINT_GET (USB_CTRLTYPE_DIR_DEVICE2HOST|USB_CTRLTYPE_TYPE_CLASS|USB_CTRLTYPE_REC_ENDPOINT)
86 #define USB_REQTYPE_ENDPOINT_SET (USB_CTRLTYPE_DIR_HOST2DEVICE|USB_CTRLTYPE_TYPE_CLASS|USB_CTRLTYPE_REC_ENDPOINT)
87 
88 #define USB_FEATURE_ENDPOINT_HALT 0
89 
90 #define USB_ENDPOINT_INTERRUPT 0x03
91 #define USB_ENDPOINT_IN 0x80
92 #define USB_ENDPOINT_OUT 0x00
93 
94 #define USB_OH0_DEVICE_ID 0x00000000 // for completion
95 #define USB_OH1_DEVICE_ID 0x00200000
96 
97 #ifdef __cplusplus
98  extern "C" {
99 #endif /* __cplusplus */
100 
101 typedef struct _usbendpointdesc
102 {
103  u8 bLength;
104  u8 bDescriptorType;
105  u8 bEndpointAddress;
106  u8 bmAttributes;
107  u16 wMaxPacketSize;
108  u8 bInterval;
109 } ATTRIBUTE_PACKED usb_endpointdesc;
110 
111 typedef struct _usbinterfacedesc
112 {
113  u8 bLength;
114  u8 bDescriptorType;
115  u8 bInterfaceNumber;
116  u8 bAlternateSetting;
117  u8 bNumEndpoints;
118  u8 bInterfaceClass;
119  u8 bInterfaceSubClass;
120  u8 bInterfaceProtocol;
121  u8 iInterface;
122  u8 *extra;
123  u16 extra_size;
124  struct _usbendpointdesc *endpoints;
125 } ATTRIBUTE_PACKED usb_interfacedesc;
126 
127 typedef struct _usbconfdesc
128 {
129  u8 bLength;
130  u8 bDescriptorType;
131  u16 wTotalLength;
132  u8 bNumInterfaces;
133  u8 bConfigurationValue;
134  u8 iConfiguration;
135  u8 bmAttributes;
136  u8 bMaxPower;
137  struct _usbinterfacedesc *interfaces;
138 } ATTRIBUTE_PACKED usb_configurationdesc;
139 
140 typedef struct _usbdevdesc
141 {
142  u8 bLength;
143  u8 bDescriptorType;
144  u16 bcdUSB;
145  u8 bDeviceClass;
146  u8 bDeviceSubClass;
147  u8 bDeviceProtocol;
148  u8 bMaxPacketSize0;
149  u16 idVendor;
150  u16 idProduct;
151  u16 bcdDevice;
152  u8 iManufacturer;
153  u8 iProduct;
154  u8 iSerialNumber;
155  u8 bNumConfigurations;
156  struct _usbconfdesc *configurations;
157 } ATTRIBUTE_PACKED usb_devdesc;
158 
159 typedef struct _usbhiddesc
160 {
161  u8 bLength;
162  u8 bDescriptorType;
163  u16 bcdHID;
164  u8 bCountryCode;
165  u8 bNumDescriptors;
166  struct {
167  u8 bDescriptorType;
168  u16 wDescriptorLength;
169  } ATTRIBUTE_PACKED descr[1];
170 } ATTRIBUTE_PACKED usb_hiddesc;
171 
172 typedef struct _usb_device_entry {
173  s32 device_id;
174  u16 vid;
175  u16 pid;
176  u32 token;
177 } usb_device_entry;
178 
179 typedef s32 (*usbcallback)(s32 result,void *usrdata);
180 
181 s32 USB_Initialize();
182 s32 USB_Deinitialize();
183 
184 s32 USB_OpenDevice(s32 device_id,u16 vid,u16 pid,s32 *fd);
185 s32 USB_CloseDevice(s32 *fd);
186 s32 USB_CloseDeviceAsync(s32 *fd,usbcallback cb,void *usrdata);
187 
188 s32 USB_GetDescriptors(s32 fd, usb_devdesc *udd);
189 void USB_FreeDescriptors(usb_devdesc *udd);
190 
191 s32 USB_GetGenericDescriptor(s32 fd,u8 type,u8 index,u8 interface,void *data,u32 size);
192 s32 USB_GetHIDDescriptor(s32 fd,u8 interface,usb_hiddesc *uhd,u32 size);
193 
194 s32 USB_GetDeviceDescription(s32 fd,usb_devdesc *devdesc);
195 s32 USB_DeviceRemovalNotifyAsync(s32 fd,usbcallback cb,void *userdata);
196 s32 USB_DeviceChangeNotifyAsync(u8 interface_class,usbcallback cb,void *userdata);
197 
198 s32 USB_SuspendDevice(s32 fd);
199 s32 USB_ResumeDevice(s32 fd);
200 
201 s32 USB_ReadIsoMsg(s32 fd,u8 bEndpoint,u8 bPackets,u16 *rpPacketSizes,void *rpData);
202 s32 USB_ReadIsoMsgAsync(s32 fd,u8 bEndpoint,u8 bPackets,u16 *rpPacketSizes,void *rpData,usbcallback cb,void *userdata);
203 
204 s32 USB_ReadIntrMsg(s32 fd,u8 bEndpoint,u16 wLength,void *rpData);
205 s32 USB_ReadIntrMsgAsync(s32 fd,u8 bEndpoint,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
206 
207 s32 USB_ReadBlkMsg(s32 fd,u8 bEndpoint,u16 wLength,void *rpData);
208 s32 USB_ReadBlkMsgAsync(s32 fd,u8 bEndpoint,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
209 
210 s32 USB_ReadCtrlMsg(s32 fd,u8 bmRequestType,u8 bmRequest,u16 wValue,u16 wIndex,u16 wLength,void *rpData);
211 s32 USB_ReadCtrlMsgAsync(s32 fd,u8 bmRequestType,u8 bmRequest,u16 wValue,u16 wIndex,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
212 
213 s32 USB_WriteIsoMsg(s32 fd,u8 bEndpoint,u8 bPackets,u16 *rpPacketSizes,void *rpData);
214 s32 USB_WriteIsoMsgAsync(s32 fd,u8 bEndpoint,u8 bPackets,u16 *rpPacketSizes,void *rpData,usbcallback cb,void *userdata);
215 
216 s32 USB_WriteIntrMsg(s32 fd,u8 bEndpoint,u16 wLength,void *rpData);
217 s32 USB_WriteIntrMsgAsync(s32 fd,u8 bEndpoint,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
218 
219 s32 USB_WriteBlkMsg(s32 fd,u8 bEndpoint,u16 wLength,void *rpData);
220 s32 USB_WriteBlkMsgAsync(s32 fd,u8 bEndpoint,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
221 
222 s32 USB_WriteCtrlMsg(s32 fd,u8 bmRequestType,u8 bmRequest,u16 wValue,u16 wIndex,u16 wLength,void *rpData);
223 s32 USB_WriteCtrlMsgAsync(s32 fd,u8 bmRequestType,u8 bmRequest,u16 wValue,u16 wIndex,u16 wLength,void *rpData,usbcallback cb,void *usrdata);
224 
225 s32 USB_GetConfiguration(s32 fd, u8 *configuration);
226 s32 USB_SetConfiguration(s32 fd, u8 configuration);
227 s32 USB_SetAlternativeInterface(s32 fd, u8 interface, u8 alternateSetting);
228 s32 USB_ClearHalt(s32 fd, u8 endpointAddress);
229 s32 USB_GetDeviceList(usb_device_entry *descr_buffer,u8 num_descr,u8 interface_class,u8 *cnt_descr);
230 
231 s32 USB_GetAsciiString(s32 fd,u8 bIndex,u16 wLangID,u16 wLength,void *rpData);
232 
233 #ifdef __cplusplus
234  }
235 #endif /* __cplusplus */
236 
237 #endif /* defined(HW_RVL) */
238 
239 #endif
int32_t s32
32bit signed integer
Definition: gctypes.h:24
Data type definitions.
#define ATTRIBUTE_PACKED
Definition: gccore.h:95
GLsizeiptr size
Definition: glext.h:6559
Definition: ibxm.h:9
GLenum type
Definition: glext.h:6233
uint16_t u16
16bit unsigned integer
Definition: gctypes.h:18
GLuint64EXT * result
Definition: glext.h:12211
GLuint index
Definition: glext.h:6671
uint8_t u8
8bit unsigned integer
Definition: gctypes.h:17
uint32_t u32
32bit unsigned integer
Definition: gctypes.h:19