LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
usb2.c
Go to the documentation of this file.
1/*
2 This file implements libogc usb API, but with ehci direct call
3
4 most of the file credit goes to libogc devs
5*/
6
7#define __usb_control_message(ehci, fd, b, c,d, e, f, g, h, i) ehci_control_message(ehci,fd,b,c,d,e,f,g)
8
9static s32 __usb_getdesc(struct ehci_hcd * ehci,struct ehci_device * fd, u8 *buffer, u8 type, u8 index, u8 size)
10{
11 printk("usb_get desc %X %X %p\n",type,index,buffer);
12 return __usb_control_message(ehci,fd, USB_ENDPOINT_IN ,USB_REQ_GETDESCRIPTOR, (type << 8) | index, 0, size, buffer, NULL, NULL);
13}
14
15static u32 __find_next_endpoint(u8 *buffer,u32 size)
16{
17 u8 *ptr = buffer;
18
19 while(size>0) {
20 if(buffer[1]==USB_DT_ENDPOINT || buffer[1]==USB_DT_INTERFACE) break;
21
22 size -= buffer[0];
23 buffer += buffer[0];
24 }
25
26 return (buffer - ptr);
27}
28
30{
31 u8 *buffer = NULL;
32 u8 *ptr = NULL;
36 s32 retval = 0;
37 u32 size,i;
38 u32 iConf, iInterface, iEndpoint;
39
40 buffer = USB_Alloc(sizeof(*udd));
41 if(buffer == NULL)
42 {
43 retval = -ENOMEM;
44 goto free_and_error;
45 }
46
47 retval = __usb_getdesc(ehci, fd, buffer, USB_DT_DEVICE, 0, USB_DT_DEVICE_SIZE);
48 if(retval < 0)
49 goto free_and_error;
50 memcpy(udd, buffer, USB_DT_DEVICE_SIZE);
51 USB_Free(buffer);
52
53 udd->bcdUSB = cpu_to_le16(udd->bcdUSB);
54 udd->idVendor = cpu_to_le16(udd->idVendor);
55 udd->idProduct = cpu_to_le16(udd->idProduct);
56 udd->bcdDevice = cpu_to_le16(udd->bcdDevice);
57
59 if(udd->configurations == NULL)
60 {
61 retval = -ENOMEM;
62 goto free_and_error;
63 }
64 memset(udd->configurations,0,udd->bNumConfigurations* sizeof(*udd->configurations));
65 for(iConf = 0; iConf < udd->bNumConfigurations; iConf++)
66 {
68 if(buffer == NULL)
69 {
70 retval = -ENOMEM;
71 goto free_and_error;
72 }
73
74 retval = __usb_getdesc(ehci, fd, buffer, USB_DT_CONFIG, iConf, USB_DT_CONFIG_SIZE);
75 ucd = &udd->configurations[iConf];
76 memcpy(ucd, buffer, USB_DT_CONFIG_SIZE);
77 USB_Free( buffer);
78
80 size = ucd->wTotalLength;
81 buffer = USB_Alloc( ucd->wTotalLength);
82 if(buffer == NULL)
83 {
84 retval = -ENOMEM;
85 goto free_and_error;
86 }
87
88 retval = __usb_getdesc(ehci, fd, buffer, USB_DT_CONFIG, iConf, ucd->wTotalLength);
89 if(retval < 0)
90 goto free_and_error;
91
92 ptr = buffer;
93 ptr += ucd->bLength;
94 size -= ucd->bLength;
95
96 retval = -ENOMEM;
97 ucd->interfaces = USB_Alloc(ucd->bNumInterfaces* sizeof(*ucd->interfaces));
98 if(ucd->interfaces == NULL)
99 goto free_and_error;
100 memset(ucd->interfaces,0,ucd->bNumInterfaces* sizeof(*ucd->interfaces));
101 for(iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++)
102 {
103 uid = &ucd->interfaces[iInterface];
104 memcpy(uid, ptr, USB_DT_INTERFACE_SIZE);
105 ptr += uid->bLength;
106 size -= uid->bLength;
107
108 uid->endpoints = USB_Alloc(uid->bNumEndpoints* sizeof(*uid->endpoints));
109 if(uid->endpoints == NULL)
110 goto free_and_error;
111 memset(uid->endpoints,0,uid->bNumEndpoints* sizeof(*uid->endpoints));
112
113 // This skips vendor and class specific descriptors
114 i = __find_next_endpoint(ptr, size);
115 uid->extra_size = i;
116 if(i>0)
117 {
118 uid->extra = USB_Alloc(i);
119 if(uid->extra == NULL)
120 goto free_and_error;
121 memcpy(uid->extra, ptr, i);
122 ptr += i;
123 size -= i;
124 }
125
126 for(iEndpoint = 0; iEndpoint < uid->bNumEndpoints; iEndpoint++)
127 {
128 ued = &uid->endpoints[iEndpoint];
129 memcpy(ued, ptr, USB_DT_ENDPOINT_SIZE);
130 ptr += ued->bLength;
132 }
133 }
134 USB_Free( buffer);
135 buffer = NULL;
136 }
137 retval = 0;
138
139free_and_error:
140 if(buffer != NULL)
141 USB_Free(buffer);
142 if(retval < 0)
144 return retval;
145}
146
148{
149 int iConf, iInterface;
152 if(udd->configurations != NULL)
153 {
154 for(iConf = 0; iConf < udd->bNumConfigurations; iConf++)
155 {
156 ucd = &udd->configurations[iConf];
157 if(ucd->interfaces != NULL)
158 {
159 for(iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++)
160 {
161 uid = &ucd->interfaces[iInterface];
162 if(uid->endpoints != NULL)
163 USB_Free(uid->endpoints);
164 if(uid->extra != NULL)
165 USB_Free(uid->extra);
166 }
167 USB_Free(ucd->interfaces);
168 }
169 }
171 }
172}
173
174
175s32 USB_WriteBlkMsg(struct ehci_hcd * ehci,struct ehci_device *fd,u8 bEndpoint,u16 wLength,void *rpData)
176{
177 return ehci_bulk_message(ehci,fd,bEndpoint,wLength,rpData);
178}
179
180s32 USB_WriteCtrlMsg(struct ehci_hcd * ehci,struct ehci_device *fd,u8 bmRequestType,u8 bmRequest,u16 wValue,u16 wIndex,u16 wLength,void *rpData)
181{
182 return __usb_control_message(ehci, fd,bmRequestType,bmRequest,wValue,wIndex,wLength,rpData,NULL,NULL);
183}
184
185s32 USB_GetConfiguration(struct ehci_hcd * ehci,struct ehci_device *fd, u8 *configuration)
186{
187 u8 *_configuration;
188 s32 retval;
189
190 _configuration = USB_Alloc( 1);
191 if(_configuration == NULL)
192 return -ENOMEM;
193
195 if(retval >= 0)
196 *configuration = *_configuration;
197 USB_Free( _configuration);
198
199 return retval;
200}
201s32 USB_SetConfiguration(struct ehci_hcd * ehci,struct ehci_device *fd, u8 configuration)
202{
204}
205s32 USB_SetAlternativeInterface(struct ehci_hcd * ehci,struct ehci_device *fd, u8 interface, u8 alternateSetting)
206{
207 if(alternateSetting == 0)
208 return -EINVAL;
210 USB_REQ_SETINTERFACE, alternateSetting, interface, 0, NULL, NULL, NULL);
211
212}
213s32 USB_ClearHalt(struct ehci_hcd * ehci,struct ehci_device *fd, u8 endpoint)
214{
217}
#define NULL
Definition: def.h:47
s32 ehci_bulk_message(struct ehci_hcd *ehci, struct ehci_device *dev, u8 bEndpoint, u16 wLength, void *rpData)
Definition: ehci.c:682
struct ehci_hcd * ehci
Definition: ehci.h:23
u32 ptr
Definition: iso9660.c:536
u32 size
Definition: iso9660.c:537
u8 bLength
Definition: usb.h:96
struct _usbinterfacedesc * interfaces
Definition: usb.h:104
u16 wTotalLength
Definition: usb.h:98
u8 bNumInterfaces
Definition: usb.h:99
u16 idProduct
Definition: usb.h:117
u8 bNumConfigurations
Definition: usb.h:122
struct _usbconfdesc * configurations
Definition: usb.h:123
u16 idVendor
Definition: usb.h:116
u16 bcdUSB
Definition: usb.h:111
u16 bcdDevice
Definition: usb.h:118
u8 bLength
Definition: usb.h:70
u16 wMaxPacketSize
Definition: usb.h:74
u8 extra_size
Definition: usb.h:90
u8 bNumEndpoints
Definition: usb.h:84
struct _usbendpointdesc * endpoints
Definition: usb.h:91
u8 * extra
Definition: usb.h:89
Definition: ehci.h:75
#define printk(a...)
Definition: tinyehci.c:50
#define cpu_to_le16(a)
Definition: tinyehci.c:55
void USB_FreeDescriptors(usb_devdesc *udd)
Definition: usb2.c:147
#define __usb_control_message(ehci, fd, b, c, d, e, f, g, h, i)
Definition: usb2.c:7
s32 USB_GetDescriptors(struct ehci_hcd *ehci, struct ehci_device *fd, usb_devdesc *udd)
Definition: usb2.c:29
s32 USB_SetAlternativeInterface(struct ehci_hcd *ehci, struct ehci_device *fd, u8 interface, u8 alternateSetting)
Definition: usb2.c:205
s32 USB_ClearHalt(struct ehci_hcd *ehci, struct ehci_device *fd, u8 endpoint)
Definition: usb2.c:213
s32 USB_SetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 configuration)
Definition: usb2.c:201
s32 USB_WriteBlkMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bEndpoint, u16 wLength, void *rpData)
Definition: usb2.c:175
s32 USB_GetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 *configuration)
Definition: usb2.c:185
s32 USB_WriteCtrlMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *rpData)
Definition: usb2.c:180
#define USB_DT_ENDPOINT_SIZE
Definition: usb.h:33
#define USB_FEATURE_ENDPOINT_HALT
Definition: usb.h:49
#define USB_CTRLTYPE_DIR_HOST2DEVICE
Definition: usb.h:38
#define USB_DT_ENDPOINT
Definition: usb.h:14
#define USB_REQ_SETINTERFACE
Definition: usb.h:26
#define USB_CTRLTYPE_DIR_DEVICE2HOST
Definition: usb.h:39
#define USB_REQ_GETCONFIG
Definition: usb.h:23
#define USB_DT_INTERFACE
Definition: usb.h:13
void USB_Free(void *ptr)
Definition: usb_os.c:121
#define USB_ENDPOINT_IN
Definition: usb.h:51
void * USB_Alloc(int size)
Definition: usb_os.c:116
#define USB_CTRLTYPE_REC_ENDPOINT
Definition: usb.h:46
#define USB_DT_DEVICE
Definition: usb.h:10
#define USB_CTRLTYPE_REC_DEVICE
Definition: usb.h:44
#define USB_REQ_SETCONFIG
Definition: usb.h:24
#define USB_CTRLTYPE_REC_INTERFACE
Definition: usb.h:45
#define USB_DT_CONFIG_SIZE
Definition: usb.h:31
#define USB_CTRLTYPE_TYPE_STANDARD
Definition: usb.h:40
#define USB_REQ_GETDESCRIPTOR
Definition: usb.h:21
#define USB_DT_CONFIG
Definition: usb.h:11
#define USB_REQ_CLEARFEATURE
Definition: usb.h:18
#define USB_DT_DEVICE_SIZE
Definition: usb.h:30
#define USB_DT_INTERFACE_SIZE
Definition: usb.h:32
u8 type
Definition: xenos_edid.h:1
uint8_t u8
8bit unsigned integer
Definition: xetypes.h:12
uint16_t u16
16bit unsigned integer
Definition: xetypes.h:13
int32_t s32
32bit signed integer
Definition: xetypes.h:19
uint32_t u32
32bit unsigned integer
Definition: xetypes.h:14