00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <stdint.h>
00040
00041 #include "HIDDTransferDriver.h"
00042 #include <USBLib_Trace.h>
00043
00044 #include <USBRequests.h>
00045 #include <HIDDescriptors.h>
00046 #include <HIDDFunction.h>
00047
00048 #include <USBD_HAL.h>
00049
00050 #include <string.h>
00051
00052
00053
00054
00055
00056
00057
00058
00059 typedef struct _HIDDTransferReport {
00060
00061 HIDDReportEventCallback fCallback;
00062
00063 void* pArg;
00064
00065
00066 uint16_t wMaxSize;
00067
00068 uint16_t wTransferred;
00069
00070 uint8_t bIdleRate;
00071
00072 uint8_t bDelay;
00073
00074 uint8_t bID;
00075
00076 uint8_t bData[HIDDTransferDriver_REPORTSIZE];
00077 } HIDDTransferReport;
00078
00079
00080
00081
00082
00083 typedef struct _HIDDTransferDriver {
00084
00085
00086 HIDDFunction hidFunction;
00087
00088
00089 HIDDReport *inputReports[1];
00090
00091 HIDDReport *outputReports[1];
00092
00093
00094
00095 uint16_t iReportLen;
00096
00097 uint8_t iReportBuf[HIDDTransferDriver_REPORTSIZE];
00098
00099 } HIDDTransferDriver;
00100
00101
00102
00103
00104
00105
00106 static HIDDTransferReport inputReport;
00107
00108
00109 static HIDDTransferReport outputReport;
00110
00111
00112 static HIDDTransferDriver hiddTransferDriver;
00113
00114
00115 static const uint8_t hiddTransferReportDescriptor[] = {
00116
00117
00118 HIDReport_GLOBAL_USAGEPAGE + 2, 0xFF, 0xFF,
00119
00120
00121 HIDReport_LOCAL_USAGE + 1, 0xFF,
00122 HIDReport_COLLECTION + 1, HIDReport_COLLECTION_APPLICATION,
00123
00124
00125 HIDReport_LOCAL_USAGE + 1, 0xFF,
00126 HIDReport_GLOBAL_REPORTCOUNT + 1, HIDDTransferDriver_REPORTSIZE,
00127 HIDReport_GLOBAL_REPORTSIZE + 1, 8,
00128 HIDReport_GLOBAL_LOGICALMINIMUM + 1, (uint8_t) -128,
00129 HIDReport_GLOBAL_LOGICALMAXIMUM + 1, (uint8_t) 127,
00130 HIDReport_INPUT + 1, 0,
00131
00132
00133 HIDReport_LOCAL_USAGE + 1, 0xFF,
00134 HIDReport_GLOBAL_REPORTCOUNT + 1, HIDDTransferDriver_REPORTSIZE,
00135 HIDReport_GLOBAL_REPORTSIZE + 1, 8,
00136 HIDReport_GLOBAL_LOGICALMINIMUM + 1, (uint8_t) -128,
00137 HIDReport_GLOBAL_LOGICALMAXIMUM + 1, (uint8_t) 127,
00138 HIDReport_OUTPUT + 1, 0,
00139 HIDReport_ENDCOLLECTION
00140 };
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 static uint8_t HIDDTransferDriver_GetDescriptor(uint8_t type,
00153 uint8_t length)
00154 {
00155 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00156 HIDDFunction *pHidd = &pDrv->hidFunction;
00157
00158 const USBConfigurationDescriptor *pConfiguration;
00159 HIDDescriptor *hidDescriptors[2];
00160
00161 switch (type) {
00162
00163 case HIDGenericDescriptor_REPORT:
00164 TRACE_INFO("Report ");
00165
00166
00167 if (length > HIDDTransferDriver_REPORTDESCRIPTORSIZE) {
00168
00169 length = HIDDTransferDriver_REPORTDESCRIPTORSIZE;
00170 }
00171 USBD_Write(0, &hiddTransferReportDescriptor, length, 0, 0);
00172 break;
00173
00174 case HIDGenericDescriptor_HID:
00175 TRACE_INFO("HID ");
00176
00177
00178 if (USBD_IsHighSpeed()) {
00179
00180 pConfiguration =
00181 pHidd->pUsbd->pDescriptors->pHsConfiguration;
00182 }
00183 else {
00184
00185 pConfiguration =
00186 pHidd->pUsbd->pDescriptors->pFsConfiguration;
00187 }
00188
00189
00190 USBConfigurationDescriptor_Parse(pConfiguration, 0, 0,
00191 (USBGenericDescriptor **) hidDescriptors);
00192
00193
00194 if (length > sizeof(HIDDescriptor)) {
00195
00196 length = sizeof(HIDDescriptor);
00197 }
00198 USBD_Write(0, hidDescriptors[0], length, 0, 0);
00199 break;
00200
00201 default:
00202 return 0;
00203 }
00204
00205 return 1;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 static void HIDDTransferDriver_ReportReceived(void *pArg,
00216 uint8_t status,
00217 uint32_t transferred,
00218 uint32_t remaining)
00219 {
00220 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00221 remaining = remaining; status = status; pArg = pArg;
00222 pDrv->iReportLen = transferred;
00223 USBD_Write(0, 0, 0, 0, 0);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 void HIDDTransferDriver_Initialize(const USBDDriverDescriptors * pDescriptors)
00235 {
00236 HIDDTransferDriver * pDrv = &hiddTransferDriver;
00237 USBDDriver *pUsbd = USBD_GetDriver();
00238
00239
00240 pDrv->inputReports[0] = (HIDDReport*)&inputReport;
00241 HIDDFunction_InitializeReport((HIDDReport *)pDrv->inputReports[0],
00242 HIDDTransferDriver_REPORTSIZE,
00243 0,
00244 0, 0);
00245
00246 pDrv->outputReports[0] = (HIDDReport*)&outputReport;
00247 HIDDFunction_InitializeReport((HIDDReport *)pDrv->outputReports[0],
00248 HIDDTransferDriver_REPORTSIZE,
00249 0,
00250 0, 0);
00251
00252
00253 USBDDriver_Initialize(pUsbd,
00254 pDescriptors,
00255 0);
00256
00257 HIDDFunction_Initialize(&pDrv->hidFunction,
00258 pUsbd, 0,
00259 hiddTransferReportDescriptor,
00260 (HIDDReport **)(&pDrv->inputReports), 1,
00261 (HIDDReport **)(&pDrv->outputReports), 1);
00262
00263 USBD_Init();
00264 }
00265
00266
00267
00268
00269
00270 void HIDDTransferDriver_ConfigurationChangedHandler(uint8_t cfgnum)
00271 {
00272 const USBDDriverDescriptors * pDescriptors = USBD_GetDriver()->pDescriptors;
00273 HIDDTransferDriver * pDrv = &hiddTransferDriver;
00274 HIDDFunction * pHidd = &pDrv->hidFunction;
00275
00276 USBConfigurationDescriptor *pDesc;
00277
00278 if (cfgnum > 0) {
00279
00280
00281 if (USBD_HAL_IsHighSpeed() && pDescriptors->pHsConfiguration)
00282 pDesc = (USBConfigurationDescriptor*)pDescriptors->pHsConfiguration;
00283 else
00284 pDesc = (USBConfigurationDescriptor*)pDescriptors->pFsConfiguration;
00285 HIDDFunction_ParseInterface(pHidd,
00286 (USBGenericDescriptor*)pDesc,
00287 pDesc->wTotalLength);
00288
00289
00290 HIDDFunction_StartPollingOutputs(pHidd);
00291 }
00292 }
00293
00294
00295
00296
00297
00298 void HIDDTransferDriver_RequestHandler(const USBGenericRequest *request)
00299 {
00300 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00301 HIDDFunction *pHidd = &pDrv->hidFunction;
00302
00303 TRACE_INFO("NewReq ");
00304
00305
00306 if (USBGenericRequest_GetType(request) == USBGenericRequest_STANDARD) {
00307
00308
00309 switch (USBGenericRequest_GetRequest(request)) {
00310
00311 case USBGenericRequest_GETDESCRIPTOR:
00312
00313
00314 if (!HIDDTransferDriver_GetDescriptor(
00315 USBGetDescriptorRequest_GetDescriptorType(request),
00316 USBGenericRequest_GetLength(request))) {
00317
00318 USBDDriver_RequestHandler(pHidd->pUsbd,
00319 request);
00320 }
00321 return;
00322
00323 case USBGenericRequest_CLEARFEATURE:
00324
00325
00326 switch (USBFeatureRequest_GetFeatureSelector(request)) {
00327 case USBFeatureRequest_ENDPOINTHALT:
00328 { uint8_t ep =
00329 USBGenericRequest_GetEndpointNumber(request);
00330 if (USBD_IsHalted(ep)) {
00331
00332
00333 USBD_Unhalt(ep);
00334 if (ep == pHidd->bPipeOUT) {
00335 HIDDFunction_StartPollingOutputs(pHidd);
00336 }
00337 }
00338
00339 USBD_Write(0, 0, 0, 0, 0);
00340 return;
00341 }
00342 }
00343 break;
00344
00345 }
00346 }
00347
00348 else if (USBGenericRequest_GetType(request) == USBGenericRequest_CLASS) {
00349
00350 switch (USBGenericRequest_GetRequest(request)) {
00351
00352 case HIDGenericRequest_SETREPORT:
00353 {
00354 uint16_t length = USBGenericRequest_GetLength(request);
00355 uint8_t type = HIDReportRequest_GetReportType(request);
00356 if (type == HIDReportRequest_OUTPUT) {
00357 if (length > HIDDTransferDriver_REPORTSIZE)
00358 length = HIDDTransferDriver_REPORTSIZE;
00359 USBD_Read(0,
00360 pDrv->iReportBuf,
00361 length,
00362 HIDDTransferDriver_ReportReceived,
00363 0);
00364 }
00365 else {
00366
00367 USBD_Stall(0);
00368 }
00369 }
00370 return;
00371 }
00372 }
00373
00374
00375
00376 if (USBRC_SUCCESS == HIDDFunction_RequestHandler(pHidd,
00377 request)) {
00378 return;
00379 }
00380 else
00381 USBDDriver_RequestHandler(pHidd->pUsbd, request);
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391 uint16_t HIDDTransferDriver_ReadReport(void *pData,
00392 uint32_t dwLength)
00393 {
00394 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00395
00396 if (pData == 0) {
00397
00398 return pDrv->iReportLen;
00399 }
00400
00401 if (dwLength > HIDDTransferDriver_REPORTSIZE) {
00402
00403 dwLength = HIDDTransferDriver_REPORTSIZE;
00404 }
00405 if (dwLength > pDrv->iReportLen) {
00406
00407 dwLength = pDrv->iReportLen;
00408 }
00409 pDrv->iReportLen = 0;
00410 memcpy(pData, pDrv->iReportBuf, dwLength);
00411
00412 return dwLength;
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422 uint16_t HIDDTransferDriver_Read(void *pData,
00423 uint32_t dLength)
00424 {
00425 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00426 if (pData == 0) {
00427
00428 return pDrv->outputReports[0]->wTransferred;
00429 }
00430
00431 if (dLength > HIDDTransferDriver_REPORTSIZE) {
00432
00433 dLength = HIDDTransferDriver_REPORTSIZE;
00434 }
00435 if (dLength > pDrv->outputReports[0]->wTransferred) {
00436
00437 dLength = pDrv->outputReports[0]->wTransferred;
00438 }
00439 pDrv->outputReports[0]->wTransferred = 0;
00440 memcpy(pData, pDrv->outputReports[0]->bData, dLength);
00441
00442 return dLength;
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452 uint8_t HIDDTransferDriver_Write(const void *pData,
00453 uint32_t dLength,
00454 TransferCallback fCallback,
00455 void *pArg)
00456 {
00457 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00458 if (dLength != HIDDTransferDriver_REPORTSIZE) {
00459
00460 dLength = HIDDTransferDriver_REPORTSIZE;
00461 }
00462 return USBD_Write(pDrv->hidFunction.bPipeIN,
00463 pData, dLength,
00464 fCallback, pArg);
00465 }
00466
00467
00468
00469
00470
00471 void HIDDTransferDriver_RemoteWakeUp(void)
00472 {
00473 HIDDTransferDriver *pDrv = &hiddTransferDriver;
00474
00475
00476 if (USBDDriver_IsRemoteWakeUpEnabled(pDrv->hidFunction.pUsbd)) {
00477
00478 USBD_RemoteWakeUp();
00479 }
00480 }
00481
00482