1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/**
******************************************************************************
* @file USB_Example/usb_prop.c
* @author MCD Application Team
* @version V1.1.0
* @date 20-September-2012
* @brief All processing related to Demo
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "hw_config.h"
#include "globalVariables.h"
#include "foculusTypes.h"
#include "string.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint8_t featureReportData[128]; //This array is used to exchange received Feature Report data with the main loop (and sometimes it gets corrupted :( )
uint32_t ProtocolValue; //Something from the ST monkeys
__IO int8_t newConfigFrameReceived = -1;//This value is polled by the main loop, waiting for new Feature Report data
uint8_t EP0debugBuffer[128]; //The data of any unknown USB requests will be written to (and read from) here
/* -------------------------------------------------------------------------- */
/* Structures initializations */
/* -------------------------------------------------------------------------- */
DEVICE Device_Table = { EP_NUM, 1 };
DEVICE_PROP Device_Property = { Hid_init, Hid_Reset, Hid_Status_In,
Hid_Status_Out, Hid_Data_Setup, Hid_NoData_Setup,
Hid_Get_Interface_Setting, Hid_GetDeviceDescriptor,
Hid_GetConfigDescriptor, Hid_GetStringDescriptor, 0,
USB_PACKET_SIZE /*MAX PACKET SIZE*/
};
USER_STANDARD_REQUESTS User_Standard_Requests = {
Hid_GetConfiguration, Hid_SetConfiguration,
Hid_GetInterface,
Hid_SetInterface,
Hid_GetStatus,
Hid_ClearFeature,
Hid_SetEndPointFeature,
Hid_SetDeviceFeature, Hid_SetDeviceAddress };
ONE_DESCRIPTOR Device_Descriptor = { (uint8_t*) Hid_DeviceDescriptor,
HID_SIZ_DEVICE_DESC };
ONE_DESCRIPTOR Config_Descriptor = { (uint8_t*) Hid_ConfigDescriptor,
HID_SIZ_CONFIG_DESC };
ONE_DESCRIPTOR Hid_Report_Descriptor = { (uint8_t *) Hid_ReportDescriptor,
HID_SIZ_REPORT_DESC };
ONE_DESCRIPTOR Mouse_Hid_Descriptor = { (uint8_t*) Hid_ConfigDescriptor
+ HID_OFF_HID_DESC,
HID_SIZ_HID_DESC };
ONE_DESCRIPTOR String_Descriptor[4] = { { (uint8_t*) Hid_StringLangID,
HID_SIZ_STRING_LANGID }, { (uint8_t*) Hid_StringVendor,
HID_SIZ_STRING_VENDOR }, { (uint8_t*) Hid_StringProduct,
HID_SIZ_STRING_PRODUCT }, { (uint8_t*) Hid_StringSerial,
HID_SIZ_STRING_SERIAL } };
/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Extern function prototypes ------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Hid init routine.
* @param None
* @retval None
*/
void Hid_init(void) {
/* Update the serial number string descriptor with the data from the unique
ID*/
Get_SerialNum();
pInformation->Current_Configuration = 0;
/* Connect the device */
PowerOn();
/* Perform basic device initialization operations */
USB_SIL_Init();
bDeviceState = UNCONNECTED;
}
/**
* @brief Hid reset routine.
* @param None
* @retval None
*/
void Hid_Reset(void) {
/* Set Hid_DEVICE as not configured */
pInformation->Current_Configuration = 0;
pInformation->Current_Interface = 0;/*the default Interface*/
/* Current Feature initialization */
//pInformation->Current_FeatuHidstick_ConfigDescriptor[7];
pInformation->Current_Feature = Hid_ConfigDescriptor[7];
SetBTABLE(BTABLE_ADDRESS);
/* Initialize Endpoint 0 */
SetEPType(ENDP0, EP_CONTROL);
SetEPTxStatus(ENDP0, EP_TX_STALL);
SetEPRxAddr(ENDP0, ENDP0_RXADDR);
SetEPTxAddr(ENDP0, ENDP0_TXADDR);
Clear_Status_Out(ENDP0);
SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
SetEPRxValid(ENDP0);
/* Initialize Endpoint 1 */
SetEPType(ENDP1, EP_INTERRUPT);
SetEPTxAddr(ENDP1, ENDP1_TXADDR);
SetEPRxAddr(ENDP1, ENDP1_RXADDR);
SetEPTxCount(ENDP1, USB_PACKET_SIZE);
SetEPRxCount(ENDP1, USB_PACKET_SIZE);
SetEPRxStatus(ENDP1, EP_RX_VALID);
SetEPTxStatus(ENDP1, EP_TX_NAK);
/* Set this device to response on default address */
SetDeviceAddress(0);
bDeviceState = ATTACHED;
}
/**
* @brief Update the device state to configured.
* @param None
* @retval None
*/
void Hid_SetConfiguration(void) {
DEVICE_INFO *pInfo = &Device_Info;
if (pInfo->Current_Configuration != 0) {
/* Device configured */
bDeviceState = CONFIGURED;
}
}
/**
* @brief Update the device state to addressed.
* @param None
* @retval None
*/
void Hid_SetDeviceAddress(void) {
bDeviceState = ADDRESSED;
}
//This seems to be called during CONFIGURATION REQUESTS only (python device open for example)
void Hid_Status_In(void) {
}
//Called on every Control_IN transfer (when data goes to the HOST)
void Hid_Status_Out(void) {
}
/**
* @brief Handle the data class specific requests.
* @param RequestNo: Request Nb.
* @retval USB_UNSUPPORT or USB_SUCCESS.
*/
RESULT Hid_Data_Setup(uint8_t RequestNo) {
uint8_t *(*CopyRoutine)( uint16_t);
CopyRoutine = NULL;
// *********************************************************************
// * Handle GET_DESCRIPTOR request on control endpoint 0
// *********************************************************************
if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
&& (RequestNo == GET_DESCRIPTOR)
&& (pInformation->USBwIndex0 == 0)) {
if (pInformation->USBwValue1 == REPORT_DESCRIPTOR) {
CopyRoutine = Hid_GetReportDescriptor;
} else if (pInformation->USBwValue1 == HID_DESCRIPTOR_TYPE) {
CopyRoutine = Hid_GetHIDDescriptor;
}
} /* End of GET_DESCRIPTOR */
// *********************************************************************
// * Handle all HID class request on endpoint 0
// *********************************************************************
else if ( (pInformation->USBbmRequestType&0x60) == CLASS_REQUEST) { //| INTERFACE_RECIPIENT
switch (RequestNo) {
case GET_PROTOCOL:
CopyRoutine = Hid_GetProtocolValue;
break;
case GET_REPORT:
// --- GET_REPORT --- send OCULUS config data to PC
CopyRoutine = HID_GetReport_Feature;
break;
case SET_REPORT:
// --- SET_REPORT --- receive OCULUS config data from PC
CopyRoutine = HID_SetReport_Feature;
break;
}
}
if (CopyRoutine == NULL) {
return USB_UNSUPPORT;
}
pInformation->Ctrl_Info.CopyData = CopyRoutine;
pInformation->Ctrl_Info.Usb_wOffset = 0;
(*CopyRoutine)(0);
return USB_SUCCESS;
}
/*******************************************************************************
* Function Name : HID_SetReport_Feature
* Description : Set Feature request handling
* Input : Length.
* Output : None.
* Return : Buffer
*******************************************************************************/
uint8_t *HID_SetReport_Feature(uint16_t Length) {
// Okay, this function gets called when a USB packet has been received completely and needs to be copied to featureReportData[]
// Then the data gets evaluated later by the main loop.
// I assume we are now in some interrupt routine. The usbdriver calls this function, gets the address where to copy
// the data and copies it immediately. Then it returns from the interrupt back to main. Thats why we can set the new
// data received flag, before the data actually is available. For the main routine, the timing will be fine.
uint8_t nBytesInThisRequest=0, requestwValueL;
__IO uint8_t *pointerToBuffer;
//Get the lower byte of the wValue, which should contain the commandID as sent by the Oculus SDK
requestwValueL = pInformation->USBwValues.bw.bb0; //Should have been set by the SETUP transaction stage
switch ( requestwValueL ) { //Copy data in temporary array, so that the main loop can take care of it
case Packet_Config:
case Packet_Scale:
case Packet_KeepAlive:
case Packet_DisplayInfo:
pointerToBuffer = featureReportData;
nBytesInThisRequest = 128;
newConfigFrameReceived = requestwValueL; //Set the new ConfigFrame received flag, which is polled by main routine
break;
default: //Unknown requests go to the debugBuffer
pointerToBuffer = EP0debugBuffer;
nBytesInThisRequest = 128;
}
if (Length == 0) {
pInformation->Ctrl_Info.Usb_wLength = nBytesInThisRequest;
return NULL;
} else {
return (uint8_t*)pointerToBuffer;
}
}
/*******************************************************************************
* Function Name : HID_SetReport_Feature
* Description : Set Feature request handling
* Input : Length.
* Output : None.
* Return : Buffer
* This will simply send a number of _bytesToReturn_ bytes over USB (ugly ST style callback)
*******************************************************************************/
uint8_t *HID_GetReport_Feature(uint16_t Length) {
uint8_t nBytesInThisRequest=0, requestwValueL;
volatile uint8_t *pointerToBuffer;
//Get the lower byte of the wValue, which should contain the commandID as sent by the Oculus SDK
requestwValueL = pInformation->USBwValues.bw.bb0; //Should have been set by the SETUP transaction stage
switch (requestwValueL) {
case Packet_Config:
pointerToBuffer = dataConfig.rawUSBdata;
nBytesInThisRequest = 7;//sizeof(dataConfig)-1; //No idea why -1, but seems to fit in windows
break;
case Packet_Scale:
pointerToBuffer = dataScale.rawUSBdata;
nBytesInThisRequest = 8;//sizeof(dataScale)-1;
break;
case Packet_KeepAlive:
pointerToBuffer = dataKeepAlive.rawUSBdata;
nBytesInThisRequest = 5;//sizeof(dataKeepAlive)-1;
break;
case Packet_DisplayInfo:
pointerToBuffer = dataDisplayInfo.rawUSBdata;
nBytesInThisRequest = 56;//sizeof(dataDisplayInfo)-1;
break;
default:
pointerToBuffer = EP0debugBuffer;
nBytesInThisRequest = 127;
}
if (Length == 0) {
pInformation->Ctrl_Info.Usb_wLength = nBytesInThisRequest;
return NULL;
} else {
return (uint8_t*)pointerToBuffer;
}
}
/**
* @brief Handle the no data class specific requests
* @param RequestNo: Request Nb.
* @retval USB_UNSUPPORT or USB_SUCCESS.
*/
RESULT Hid_NoData_Setup(uint8_t RequestNo) {
if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
&& (RequestNo == SET_PROTOCOL)) {
return Hid_SetProtocol();
}
else {
return USB_UNSUPPORT;
}
}
/**
* @brief Gets the device descriptor.
* @param Length: Length.
* @retval The address of the device descriptor.
*/
uint8_t *Hid_GetDeviceDescriptor(uint16_t Length) {
return Standard_GetDescriptorData(Length, &Device_Descriptor);
}
/**
* @brief Gets the configuration descriptor.
* @param Length: Length.
* @retval The address of the configuration descriptor.
*/
uint8_t *Hid_GetConfigDescriptor(uint16_t Length) {
return Standard_GetDescriptorData(Length, &Config_Descriptor);
}
/**
* @brief Gets the string descriptors according to the needed index
* @param Length: Length.
* @retval The address of the string descriptors.
*/
uint8_t *Hid_GetStringDescriptor(uint16_t Length) {
uint8_t wValue0 = pInformation->USBwValue0;
if (wValue0 > 4) {
return NULL;
} else {
return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
}
}
/**
* @brief Gets the HID report descriptor.
* @param Length: Length.
* @retval The address of the configuration descriptor.
*/
uint8_t *Hid_GetReportDescriptor(uint16_t Length) {
return Standard_GetDescriptorData(Length, &Hid_Report_Descriptor);
}
/**
* @brief Gets the HID descriptor.
* @param Length: Length.
* @retval The address of the configuration descriptor.
*/
uint8_t *Hid_GetHIDDescriptor(uint16_t Length) {
return Standard_GetDescriptorData(Length, &Mouse_Hid_Descriptor);
}
/**
* @brief tests the interface and the alternate setting according to the supported one.
* @param Interface: interface number.
* @param AlternateSetting : Alternate Setting number.
* @retval USB_SUCCESS or USB_UNSUPPORT.
*/
RESULT Hid_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting) {
if (AlternateSetting > 0) {
return USB_UNSUPPORT;
} else if (Interface > 0) {
return USB_UNSUPPORT;
}
return USB_SUCCESS;
}
/**
* @brief Hid Set Protocol request routine.
* @param None.
* @retval USB_SUCCESS
*/
RESULT Hid_SetProtocol(void) {
uint8_t wValue0 = pInformation->USBwValue0;
ProtocolValue = wValue0;
return USB_SUCCESS;
}
/**
* @brief Get the protocol value
* @param Length: length
* @retval address of the protocol value.
*/
uint8_t *Hid_GetProtocolValue(uint16_t Length) {
if (Length == 0) {
pInformation->Ctrl_Info.Usb_wLength = 1;
return NULL;
} else {
return (uint8_t *) (&ProtocolValue);
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|