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
00040
00041
00042
00043 #include <AUDDSpeakerPhone.h>
00044
00045 #include <USBDescriptors.h>
00046 #include <USBRequests.h>
00047 #include <AUDDescriptors.h>
00048 #include <AUDRequests.h>
00049
00050 #include <USBD_HAL.h>
00051
00052 #include <USBLib_Trace.h>
00053
00054
00055
00056
00057
00058
00059 typedef struct _AUDDParseData {
00060
00061 AUDDSpeakerPhone *pAudf;
00062
00063 USBInterfaceDescriptor *pIfDesc;
00064
00065 } AUDDParseData;
00066
00067
00068 typedef struct _AUDDXfrExt {
00069
00070 AUDDStream *pStream;
00071
00072 uint16_t usbBuffer;
00073
00074 uint8_t bEntity;
00075
00076 uint8_t bCh;
00077 } AUDDXfrExt;
00078
00079
00080
00081
00082
00083
00084 static AUDDXfrExt auddXfrData;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 static uint32_t AUDDSpeakerPhone_Parse(USBGenericDescriptor *pDesc,
00096 AUDDParseData *pArg)
00097 {
00098 AUDDStream *pSpeaker = pArg->pAudf->pSpeaker;
00099 AUDDStream *pMic = pArg->pAudf->pMicrophone;
00100 USBEndpointDescriptor *pEp = (USBEndpointDescriptor *)pDesc;
00101 uint8_t bSpeakerDone = 0, bMicDone = 0;
00102
00103
00104 if (pDesc->bLength == 0)
00105 return USBRC_PARAM_ERR;
00106
00107
00108 if (pDesc->bDescriptorType == USBGenericDescriptor_INTERFACE) {
00109 USBInterfaceDescriptor *pIf = (USBInterfaceDescriptor *)pDesc;
00110
00111
00112 if (pIf->bInterfaceClass ==
00113 AUDControlInterfaceDescriptor_CLASS
00114 && pIf->bInterfaceSubClass ==
00115 AUDControlInterfaceDescriptor_SUBCLASS) {
00116 pArg->pIfDesc = pIf;
00117
00118 if (pSpeaker) pSpeaker->bAcInterface = pIf->bInterfaceNumber;
00119
00120 if (pMic) pMic->bAcInterface = pIf->bInterfaceNumber;
00121 }
00122
00123 else if (pIf->bInterfaceClass ==
00124 AUDStreamingInterfaceDescriptor_CLASS
00125 && pIf->bInterfaceSubClass ==
00126 AUDStreamingInterfaceDescriptor_SUBCLASS)
00127 pArg->pIfDesc = pIf;
00128
00129 else if (pArg->pIfDesc)
00130 return USBRC_PARTIAL_DONE;
00131 }
00132
00133 if (pArg->pIfDesc) {
00134
00135
00136
00137 if (pDesc->bDescriptorType == USBGenericDescriptor_ENDPOINT
00138 && (pEp->bmAttributes & 0x3) == USBEndpointDescriptor_ISOCHRONOUS) {
00139 if (pEp->bEndpointAddress & 0x80
00140 && pMic) {
00141 pMic->bEndpointIn = pEp->bEndpointAddress & 0x7F;
00142 pMic->bAsInterface = pArg->pIfDesc->bInterfaceNumber;
00143
00144 pMic->bFeatureUnitIn = AUDD_ID_MicrophoneFU;
00145 } else if (pSpeaker) {
00146 pSpeaker->bEndpointOut = pEp->bEndpointAddress;
00147 pSpeaker->bAsInterface = pArg->pIfDesc->bInterfaceNumber;
00148
00149 pSpeaker->bFeatureUnitOut = AUDD_ID_SpeakerFU;
00150 }
00151 }
00152 }
00153
00154 if (pSpeaker) {
00155 if (pSpeaker->bAcInterface != 0xFF
00156 && pSpeaker->bAsInterface != 0xFF
00157 && pSpeaker->bFeatureUnitOut != 0xFF
00158 && pSpeaker->bEndpointOut != 0)
00159 bSpeakerDone = 1;
00160 } else bSpeakerDone = 1;
00161
00162 if (pMic) {
00163 if (pMic->bAcInterface != 0xFF
00164 && pMic->bAsInterface != 0xFF
00165 && pMic->bFeatureUnitIn != 0xFF
00166 && pMic->bEndpointIn != 0)
00167 bMicDone = 1;
00168 } else bMicDone = 1;
00169
00170 if (bSpeakerDone && bMicDone)
00171 return USBRC_FINISHED;
00172
00173 return USBRC_SUCCESS;
00174 }
00175
00176
00177
00178
00179
00180
00181 static void AUDD_MuteReceived(AUDDXfrExt *pData)
00182 {
00183 AUDDStream_ChangeMute(pData->pStream,
00184 pData->bCh,
00185 (uint8_t)pData->usbBuffer);
00186 USBD_Write(0, 0, 0, 0, 0);
00187 }
00188
00189
00190
00191
00192
00193
00194 static void AUDD_VolumeReceived(AUDDXfrExt *pData)
00195 {
00196 AUDDStream_SetVolume(pData->pStream,
00197 pData->bCh,
00198 pData->usbBuffer);
00199 USBD_Write(0, 0, 0, 0, 0);
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 static AUDDStream *AUDD_GetCtlStream(
00211 AUDDSpeakerPhone *pAudf,
00212 uint8_t bAcInterface,
00213 uint8_t bEntity,
00214 uint8_t bChannel)
00215 {
00216 AUDDStream *pAuds = 0;
00217
00218 if (bEntity == pAudf->pSpeaker->bFeatureUnitOut
00219 || bEntity == pAudf->pSpeaker->bFeatureUnitIn)
00220 pAuds = pAudf->pSpeaker;
00221 else if (bEntity == pAudf->pMicrophone->bFeatureUnitIn
00222 || bEntity == pAudf->pMicrophone->bFeatureUnitOut)
00223 pAuds = pAudf->pMicrophone;
00224
00225 if (pAuds != 0
00226 && bAcInterface == pAuds->bAcInterface
00227 && bChannel <= pAuds->bNumChannels)
00228 return pAuds;
00229
00230 return 0;
00231 }
00232
00233
00234
00235
00236
00237
00238 static void AUDD_SetCUR(
00239 AUDDSpeakerPhone *pAudf,
00240 const USBGenericRequest *pReq)
00241 {
00242 uint8_t bIf = AUDGenericRequest_GetInterface(pReq);
00243 uint8_t bEntity = AUDGenericRequest_GetEntity(pReq);
00244 uint8_t bLength = USBGenericRequest_GetLength(pReq);
00245 uint8_t bCh = AUDFeatureUnitRequest_GetChannel(pReq);
00246 uint8_t bCtrl = AUDFeatureUnitRequest_GetControl(pReq);
00247 uint8_t bSet = 1;
00248 AUDDStream *pAuds = AUDD_GetCtlStream(pAudf, bIf, bEntity, bCh);
00249 TransferCallback fCallback;
00250
00251 TRACE_INFO_WP("sCUR ");
00252 TRACE_DEBUG("\b(E%d, CtlS%d, Ch%d, L%d) ", bEntity, bCtrl, bCh, bLength);
00253
00254
00255 if (bCtrl == AUDFeatureUnitRequest_MUTE
00256 && bLength == 1
00257 && pAuds)
00258 fCallback = (TransferCallback) AUDD_MuteReceived;
00259 else if (bCtrl == AUDFeatureUnitRequest_VOLUME
00260 && bLength == 2
00261 && pAuds && pAuds->pwVolumes)
00262 fCallback = (TransferCallback) AUDD_VolumeReceived;
00263 else
00264 bSet = 0;
00265
00266 if (bSet) {
00267
00268 auddXfrData.pStream = pAuds;
00269 auddXfrData.bEntity = bEntity;
00270 auddXfrData.bCh = bCh;
00271 USBD_Read(0,
00272 &auddXfrData.usbBuffer,
00273 bLength,
00274 fCallback,
00275 (void *) &auddXfrData);
00276 } else
00277
00278 USBD_Stall(0);
00279
00280 }
00281
00282
00283
00284
00285
00286
00287 static void AUDD_GetCUR(
00288 AUDDSpeakerPhone *pAudf,
00289 const USBGenericRequest *pReq)
00290 {
00291 uint8_t bIf = AUDGenericRequest_GetInterface(pReq);
00292 uint8_t bEntity = AUDGenericRequest_GetEntity(pReq);
00293 uint8_t bLength = USBGenericRequest_GetLength(pReq);
00294 uint8_t bCh = AUDFeatureUnitRequest_GetChannel(pReq);
00295 uint8_t bCtrl = AUDFeatureUnitRequest_GetControl(pReq);
00296 uint8_t bGet = 1;
00297 AUDDStream *pAuds = AUDD_GetCtlStream(pAudf, bIf, bEntity, bCh);
00298
00299 TRACE_INFO_WP("gCUR ");
00300 TRACE_INFO_WP("\b(E%d, CtlS%d, Ch%d, L%d) ", bEntity, bCtrl, bCh, bLength);
00301
00302
00303 if (bCtrl == AUDFeatureUnitRequest_MUTE
00304 && bLength == 1
00305 && pAuds)
00306 auddXfrData.usbBuffer = ((pAuds->bmMute & (1 << bCh)) > 0);
00307 else if (bCtrl == AUDFeatureUnitRequest_VOLUME
00308 && bLength == 2
00309 && pAuds && pAuds->pwVolumes)
00310 auddXfrData.usbBuffer = pAuds->pwVolumes[bCh];
00311 else
00312 bGet = 0;
00313
00314 if (bGet)
00315
00316 USBD_Write(0, &auddXfrData.usbBuffer, bLength, 0, 0);
00317 else
00318
00319 USBD_Stall(0);
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 void AUDDStream_Initialize(AUDDStream *pAuds,
00337 uint8_t numChannels,
00338 uint16_t wChannelVolumes[],
00339 AUDDStreamEventCallback fCallback,
00340 void *pArg)
00341 {
00342 pAuds->bAcInterface = 0xFF;
00343 pAuds->bFeatureUnitOut = 0xFF;
00344 pAuds->bFeatureUnitIn = 0xFF;
00345 pAuds->bAsInterface = 0xFF;
00346 pAuds->bEndpointOut = 0;
00347 pAuds->bEndpointIn = 0;
00348
00349 pAuds->bNumChannels = numChannels;
00350 pAuds->bmMute = 0;
00351 pAuds->pwVolumes = wChannelVolumes;
00352
00353 pAuds->fCallback = fCallback;
00354 pAuds->pArg = pArg;
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 uint32_t AUDDStream_IsRequestAccepted(
00364 AUDDStream *pAuds,
00365 const USBGenericRequest *pReq)
00366 {
00367 uint8_t bIf = AUDGenericRequest_GetInterface(pReq);
00368 uint8_t bEntity = AUDGenericRequest_GetEntity(pReq);
00369 uint8_t bCh = AUDFeatureUnitRequest_GetChannel(pReq);
00370
00371
00372 if (bIf == pAuds->bAcInterface) {
00373 if (bCh > pAuds->bNumChannels)
00374 return 0;
00375
00376 if (bEntity != pAuds->bFeatureUnitIn
00377 && bEntity != pAuds->bFeatureUnitOut)
00378 return 0;
00379 }
00380
00381 else
00382 return 0;
00383
00384 return 1;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393 uint32_t AUDDStream_ChangeMute(AUDDStream *pAuds,
00394 uint8_t bChannel,
00395 uint8_t bMute)
00396 {
00397 if (pAuds->bNumChannels < bChannel)
00398 return USBRC_PARAM_ERR;
00399
00400 pAuds->bmMute = bMute;
00401
00402 if (pAuds->fCallback)
00403 pAuds->fCallback(AUDD_EC_MuteChanged,
00404 bChannel,
00405 pAuds->pArg);
00406
00407 return USBRC_SUCCESS;
00408 }
00409
00410
00411
00412
00413
00414
00415
00416 uint32_t AUDDStream_SetVolume(AUDDStream *pAuds,
00417 uint8_t bChannel,
00418 uint16_t wVolume)
00419 {
00420 if (pAuds->pwVolumes == 0)
00421 return USBRC_PARAM_ERR;
00422
00423 if (bChannel > pAuds->bNumChannels)
00424 return USBRC_PARAM_ERR;
00425
00426 pAuds->pwVolumes[bChannel] = wVolume;
00427
00428 if (pAuds->fCallback) {
00429 pAuds->fCallback(AUDD_EC_VolumeChanged,
00430 bChannel,
00431 pAuds->pArg);
00432 }
00433
00434 return USBRC_SUCCESS;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 uint32_t AUDDStream_Read(
00450 AUDDStream *pAuds,
00451 void *pData, uint32_t dwSize,
00452 TransferCallback fCallback, void *pArg)
00453 {
00454 if (pAuds->bEndpointOut == 0)
00455 return USBRC_PARAM_ERR;
00456
00457 return USBD_Read(pAuds->bEndpointOut,
00458 pData, dwSize,
00459 fCallback, pArg);
00460 }
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 uint32_t AUDDStream_SetupWrite(
00475 AUDDStream *pAuds,
00476 void *pListInit,
00477 void *pDmaInit,
00478 uint16_t listSize,
00479 uint16_t delaySize,
00480 TransferCallback callback,
00481 void *argument)
00482 {
00483 uint32_t error;
00484
00485 pDmaInit = pDmaInit;
00486
00487 if (pAuds->bEndpointIn == 0)
00488 return USBRC_STATE_ERR;
00489
00490 error = USBD_HAL_SetupMblTransfer(pAuds->bEndpointIn,
00491 pListInit,
00492 listSize,
00493 delaySize);
00494
00495 if (error) return error;
00496
00497 error = USBD_HAL_SetTransferCallback(pAuds->bEndpointIn,
00498 callback, argument);
00499 return error;
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 uint32_t AUDDStream_Write(AUDDStream *pAuds, void *pBuffer, uint16_t wLength)
00512 {
00513 if (pAuds->bEndpointIn == 0)
00514 return USBRC_STATE_ERR;
00515
00516 return USBD_HAL_Write(pAuds->bEndpointIn,
00517 pBuffer, wLength);
00518 }
00519
00520
00521
00522
00523
00524 uint32_t AUDDStream_Close(AUDDStream *pStream)
00525 {
00526 uint32_t bmEPs = 0;
00527
00528
00529 if (pStream->bEndpointIn)
00530 bmEPs |= 1 << pStream->bEndpointIn;
00531
00532
00533 if (pStream->bEndpointOut)
00534 bmEPs |= 1 << pStream->bEndpointOut;
00535
00536 USBD_HAL_ResetEPs(bmEPs, USBRC_CANCELED, 1);
00537
00538 return USBRC_SUCCESS;
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 void AUDDSpeakerPhone_InitializeStream(
00557 AUDDStream *pAuds,
00558 uint8_t numChannels,
00559 uint16_t wChannelVolumes[],
00560 AUDDStreamEventCallback fCallback,
00561 void *pArg)
00562 {
00563 pAuds->bAcInterface = 0xFF;
00564 pAuds->bFeatureUnitOut = 0xFF;
00565 pAuds->bFeatureUnitIn = 0xFF;
00566 pAuds->bAsInterface = 0xFF;
00567 pAuds->bEndpointOut = 0;
00568 pAuds->bEndpointIn = 0;
00569
00570 pAuds->bNumChannels = numChannels;
00571 pAuds->bmMute = 0;
00572 pAuds->pwVolumes = wChannelVolumes;
00573
00574 pAuds->fCallback = fCallback;
00575 pAuds->pArg = pArg;
00576 }
00577
00578
00579
00580
00581
00582
00583
00584
00585 void AUDDSpeakerPhone_Initialize(
00586 AUDDSpeakerPhone *pAudf,
00587 USBDDriver *pUsbd,
00588 AUDDStream *pSpeaker,
00589 AUDDStream *pMicrophone)
00590 {
00591 pAudf->pUsbd = pUsbd;
00592 pAudf->pSpeaker = pSpeaker;
00593 pAudf->pMicrophone = pMicrophone;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602 USBGenericDescriptor *AUDDSpeakerPhone_ParseInterfaces(
00603 AUDDSpeakerPhone *pAudf,
00604 USBGenericDescriptor *pDescriptors,
00605 uint32_t dwLength)
00606 {
00607 AUDDParseData data;
00608
00609 data.pAudf = pAudf;
00610 data.pIfDesc = 0;
00611
00612 return USBGenericDescriptor_Parse(pDescriptors,
00613 dwLength,
00614 (USBDescriptorParseFunction)AUDDSpeakerPhone_Parse,
00615 (void *)&data);
00616 }
00617
00618
00619
00620
00621
00622
00623 uint32_t AUDDSpeakerPhone_CloseStream(
00624 AUDDSpeakerPhone *pAudf,
00625 uint32_t bInterface)
00626 {
00627 if (pAudf->pSpeaker->bAsInterface == bInterface) {
00628
00629
00630
00631 } else if (pAudf->pMicrophone->bAsInterface == bInterface) {
00632
00633
00634
00635 }
00636
00637 return USBRC_SUCCESS;
00638 }
00639
00640
00641
00642
00643
00644
00645
00646 uint32_t AUDDSpeakerPhone_RequestHandler(
00647 AUDDSpeakerPhone *pAudf,
00648 const USBGenericRequest *pRequest)
00649 {
00650
00651
00652 if (USBGenericRequest_GetType(pRequest) != USBGenericRequest_CLASS)
00653 return USBRC_PARAM_ERR;
00654
00655 TRACE_INFO_WP("Aud ");
00656
00657 switch (USBGenericRequest_GetRequest(pRequest)) {
00658 case AUDGenericRequest_SETCUR:
00659 AUDD_SetCUR(pAudf, pRequest);
00660 break;
00661
00662 case AUDGenericRequest_GETCUR:
00663 AUDD_GetCUR(pAudf, pRequest);
00664 break;
00665
00666 default:
00667 return USBRC_PARAM_ERR;
00668 }
00669
00670 return USBRC_SUCCESS;
00671 }
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685 uint32_t AUDDSpeakerPhone_Read(
00686 AUDDSpeakerPhone *pAudf,
00687 void *pData, uint32_t dwSize,
00688 TransferCallback fCallback, void *pArg)
00689 {
00690 if (pAudf->pSpeaker == 0)
00691 return USBRC_PARAM_ERR;
00692
00693 if (pAudf->pSpeaker->bEndpointOut == 0)
00694 return USBRC_PARAM_ERR;
00695
00696 return USBD_Read(pAudf->pSpeaker->bEndpointOut,
00697 pData, dwSize,
00698 fCallback, pArg);
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 uint32_t AUDDSpeakerPhone_SetupWrite(
00714 AUDDSpeakerPhone *pAudf,
00715 void *pListInit,
00716 void *pDmaInit,
00717 uint16_t listSize,
00718 uint16_t delaySize,
00719 TransferCallback callback,
00720 void *argument)
00721 {
00722 uint32_t error;
00723
00724 pDmaInit = pDmaInit;
00725
00726 if (pAudf->pMicrophone == 0)
00727 return USBRC_PARAM_ERR;
00728
00729 if (pAudf->pMicrophone->bEndpointIn == 0)
00730 return USBRC_STATE_ERR;
00731
00732 error = USBD_HAL_SetupMblTransfer(pAudf->pMicrophone->bEndpointIn,
00733 pListInit,
00734 listSize,
00735 delaySize);
00736
00737 if (error) return error;
00738
00739 error = USBD_HAL_SetTransferCallback(
00740 pAudf->pMicrophone->bEndpointIn,
00741 callback, argument);
00742 return error;
00743 }
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 uint32_t AUDDSpeakerPhone_Write(AUDDSpeakerPhone *pAudf, void *pBuffer,
00755 uint16_t wLength)
00756 {
00757 if (pAudf->pSpeaker == 0)
00758 return USBRC_PARAM_ERR;
00759
00760 if (pAudf->pSpeaker->bEndpointIn == 0)
00761 return USBRC_STATE_ERR;
00762
00763 return USBD_HAL_Write(pAudf->pSpeaker->bEndpointIn,
00764 pBuffer, wLength);
00765 }
00766
00767
00768