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 <AUDDSpeakerPhoneDriver.h>
00040
00041 #include <AUDRequests.h>
00042
00043 #include <USBLib_Trace.h>
00044
00045
00046
00047
00048
00049
00050
00051
00052 typedef struct _AUDDStream {
00053
00054
00055
00056 uint8_t bAcInterface;
00057
00058 uint8_t bAsInterface;
00059
00060 uint8_t bEpNum;
00061
00062 uint8_t bUnitID;
00063
00064
00065
00066 uint16_t bNumChannels;
00067
00068 uint16_t bmMuteControls;
00069
00070 uint16_t *pVolumes;
00071 } AUDDStream;
00072
00073
00074
00075
00076 typedef struct _AUDDSpeakerPhoneDriver {
00077
00078
00079 USBDDriver * pUsbd;
00080
00081 uint8_t muted;
00082
00083 uint8_t interfaces[3];
00084
00085 AUDDStream speaker;
00086
00087 AUDDStream mic;
00088 } AUDDSpeakerPhoneDriver;
00089
00090
00091
00092
00093
00094
00095 static AUDDSpeakerPhoneDriver auddSpeakerPhoneDriver;
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 static uint32_t AUDDSpeakerPhone_Parse(USBGenericDescriptor* desc,
00107 AUDDSpeakerPhoneDriver* arg)
00108 {
00109
00110 if (desc->bLength == 0) {
00111 return USBD_STATUS_INVALID_PARAMETER;
00112 }
00113
00114 if (desc->bDescriptorType == USBGenericDescriptor_ENDPOINT) {
00115 USBEndpointDescriptor *pEP = (USBEndpointDescriptor*)desc;
00116 if (pEP->bmAttributes == USBEndpointDescriptor_ISOCHRONOUS) {
00117 if (pEP->bEndpointAddress & 0x80)
00118 arg->mic.bEpNum = pEP->bEndpointAddress & 0x7F;
00119 else
00120 arg->speaker.bEpNum = pEP->bEndpointAddress;
00121 }
00122 }
00123 return 0;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 static void AUDDSpeakerPhone_MuteReceived(uint32_t channel)
00133 {
00134 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00135 AUDDStream *pAuds;
00136
00137 if ((uint8_t)(channel >> 8) ==
00138 AUDDSpeakerPhoneDriverDescriptors_OUTPUTTERMINAL_REC) {
00139 pAuds = &pAudd->mic;
00140 }
00141 else {
00142 pAuds = &pAudd->speaker;
00143 }
00144
00145 if (pAudd->muted != pAuds->bmMuteControls) {
00146 pAuds->bmMuteControls = pAudd->muted;
00147 AUDDSpeakerPhoneDriver_MuteChanged(0, channel, pAudd->muted);
00148 }
00149 USBD_Write(0, 0, 0, 0, 0);
00150 }
00151
00152
00153
00154
00155
00156 static void AUDDSpeakerPhone_SetCUR(const USBGenericRequest* pReq)
00157 {
00158 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00159 uint8_t bIf = AUDGenericRequest_GetInterface(pReq);
00160 uint8_t bEntity = AUDGenericRequest_GetEntity(pReq);
00161 uint8_t bLength = USBGenericRequest_GetLength(pReq);
00162 uint8_t bCh = AUDFeatureUnitRequest_GetChannel(pReq);
00163 uint8_t bCtrl = AUDFeatureUnitRequest_GetControl(pReq);
00164 uint8_t bSet = 0;
00165 AUDDStream *pAuds = 0;
00166
00167 TRACE_INFO_WP("sCUR ");
00168 TRACE_DEBUG("\b(E%d, CtlS%d, Ch%d, L%d) ", bEntity, bCtrl, bCh, bLength);
00169
00170 if (bCtrl == AUDFeatureUnitRequest_MUTE
00171 && bLength == 1) {
00172
00173 if (bEntity == pAudd->speaker.bUnitID)
00174 pAuds = &pAudd->speaker;
00175 else if (bEntity == pAudd->mic.bUnitID)
00176 pAuds = &pAudd->mic;
00177
00178 if (pAuds != 0
00179 && bIf == pAuds->bAcInterface
00180 && bCh <= pAuds->bNumChannels) {
00181 bSet = 1;
00182 }
00183 }
00184
00185 if (bSet) {
00186
00187 uint32_t argument = bCh | (bEntity << 8);
00188 USBD_Read(0,
00189 &pAudd->muted,
00190 sizeof(uint8_t),
00191 (TransferCallback) AUDDSpeakerPhone_MuteReceived,
00192 (void *) argument);
00193 }
00194 else {
00195
00196 USBD_Stall(0);
00197 }
00198
00199 }
00200
00201
00202
00203
00204
00205 static void AUDDSpeakerPhone_GetCUR(const USBGenericRequest *pReq)
00206 {
00207 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00208 uint8_t bIf = AUDGenericRequest_GetInterface(pReq);
00209 uint8_t bEntity = AUDGenericRequest_GetEntity(pReq);
00210 uint8_t bLength = USBGenericRequest_GetLength(pReq);
00211 uint8_t bCh = AUDFeatureUnitRequest_GetChannel(pReq);
00212 uint8_t bCtrl = AUDFeatureUnitRequest_GetControl(pReq);
00213 uint8_t bGet = 0;
00214 AUDDStream *pAuds = 0;
00215
00216 TRACE_INFO_WP("gCUR ");
00217 TRACE_DEBUG("\b(E%d, CtlS%d, Ch%d, L%d) ", bEntity, bCtrl, bCh, bLength);
00218
00219 if (bCtrl == AUDFeatureUnitRequest_MUTE
00220 && bLength == 1) {
00221
00222 if (bEntity == pAudd->speaker.bUnitID)
00223 pAuds = &pAudd->speaker;
00224 else if (bEntity == pAudd->mic.bUnitID)
00225 pAuds = &pAudd->mic;
00226
00227 if (pAuds != 0
00228 && bIf == pAuds->bAcInterface
00229 && bCh <= pAuds->bNumChannels) {
00230 bGet = 1;
00231 }
00232 }
00233
00234 if (bGet) {
00235
00236 pAudd->muted = pAuds->bmMuteControls;
00237 USBD_Write(0, &pAudd->muted, sizeof(uint8_t), 0, 0);
00238 }
00239 else {
00240
00241 USBD_Stall(0);
00242 }
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 void AUDDSpeakerPhoneDriver_Initialize(const USBDDriverDescriptors *pDescriptors)
00254 {
00255 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00256 USBDDriver *pUsbd = USBD_GetDriver();
00257
00258 pAudd->pUsbd = pUsbd;
00259
00260
00261 pAudd->speaker.bNumChannels = 3;
00262 pAudd->speaker.bmMuteControls = 0;
00263 pAudd->speaker.pVolumes = 0;
00264
00265 pAudd->mic.bNumChannels = 1;
00266 pAudd->mic.bmMuteControls = 0;
00267 pAudd->mic.pVolumes = 0;
00268
00269 pAudd->mic.bAcInterface = AUDDSpeakerPhoneDriverDescriptors_CONTROL;
00270 pAudd->mic.bAsInterface = AUDDSpeakerPhoneDriverDescriptors_STREAMINGIN;
00271 pAudd->mic.bEpNum = 5;
00272 pAudd->mic.bUnitID = AUDDSpeakerPhoneDriverDescriptors_FEATUREUNIT_REC;
00273
00274 pAudd->speaker.bAcInterface = AUDDSpeakerPhoneDriverDescriptors_CONTROL;
00275 pAudd->speaker.bAsInterface = AUDDSpeakerPhoneDriverDescriptors_STREAMING;
00276 pAudd->speaker.bEpNum = 4;
00277 pAudd->speaker.bUnitID = AUDDSpeakerPhoneDriverDescriptors_FEATUREUNIT;
00278
00279
00280 USBDDriver_Initialize(pUsbd,
00281 pDescriptors,
00282 pAudd->interfaces);
00283 USBD_Init();
00284
00285 }
00286
00287
00288
00289
00290
00291
00292 void AUDDSpeakerPhoneDriver_ConfigurationChangeHandler(uint8_t cfgnum)
00293 {
00294 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00295 const USBDDriverDescriptors *pDescriptors = pAudd->pUsbd->pDescriptors;
00296 USBConfigurationDescriptor *pDesc;
00297
00298 if (cfgnum > 0) {
00299
00300
00301 if (USBD_HAL_IsHighSpeed() && pDescriptors->pHsConfiguration)
00302 pDesc = (USBConfigurationDescriptor*)pDescriptors->pHsConfiguration;
00303 else
00304 pDesc = (USBConfigurationDescriptor*)pDescriptors->pFsConfiguration;
00305
00306 USBGenericDescriptor_Parse((USBGenericDescriptor*)pDesc, pDesc->wTotalLength,
00307 (USBDescriptorParseFunction)AUDDSpeakerPhone_Parse, pAudd);
00308 }
00309 }
00310
00311
00312
00313
00314
00315
00316
00317 void AUDDSpeakerPhoneDriver_InterfaceSettingChangedHandler(uint8_t interface,
00318 uint8_t setting)
00319 {
00320 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00321
00322 if (interface == pAudd->speaker.bAsInterface) {
00323
00324 if (setting == 0 && pAudd->speaker.bEpNum) {
00325 USBD_HAL_ResetEPs(1 << pAudd->speaker.bEpNum,
00326 USBD_STATUS_CANCELED, 1);
00327 }
00328 AUDDSpeakerPhoneDriver_StreamSettingChanged(0, setting);
00329 }
00330 if (interface == pAudd->mic.bAsInterface) {
00331
00332 if (setting == 0 && pAudd->mic.bEpNum) {
00333 USBD_HAL_ResetEPs(1 << pAudd->mic.bEpNum,
00334 USBD_STATUS_CANCELED, 1);
00335 }
00336 AUDDSpeakerPhoneDriver_StreamSettingChanged(1, setting);
00337 }
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 void AUDDSpeakerPhoneDriver_RequestHandler(const USBGenericRequest *request)
00347 {
00348 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00349 USBDDriver *pUsbd = pAudd->pUsbd;
00350
00351 TRACE_INFO_WP("NewReq ");
00352
00353
00354 if (USBGenericRequest_GetType(request) == USBGenericRequest_CLASS) {
00355
00356
00357 switch (USBGenericRequest_GetRequest(request)) {
00358
00359 case AUDGenericRequest_SETCUR:
00360
00361 AUDDSpeakerPhone_SetCUR(request);
00362 break;
00363
00364 case AUDGenericRequest_GETCUR:
00365
00366 AUDDSpeakerPhone_GetCUR(request);
00367 break;
00368
00369 default:
00370
00371 TRACE_WARNING(
00372 "AUDDSpeakerPhoneDriver_RequestHandler: Unsupported request (%d)\n\r",
00373 USBGenericRequest_GetRequest(request));
00374 USBD_Stall(0);
00375 }
00376 }
00377
00378 else if (USBGenericRequest_GetType(request) == USBGenericRequest_STANDARD) {
00379
00380
00381 USBDDriver_RequestHandler(pUsbd, request);
00382 }
00383
00384 else {
00385
00386 TRACE_WARNING(
00387 "AUDDSpeakerPhoneDriver_RequestHandler: Unsupported request type (%d)\n\r",
00388 USBGenericRequest_GetType(request));
00389 USBD_Stall(0);
00390 }
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 uint8_t AUDDSpeakerPhoneDriver_Read(void *buffer,
00405 uint32_t length,
00406 TransferCallback callback,
00407 void *argument)
00408 {
00409 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00410 return USBD_Read(pAudd->speaker.bEpNum,
00411 buffer,
00412 length,
00413 callback,
00414 argument);
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 uint8_t AUDDSpeakerPhoneDriver_SetupWrite(void * pListInit,
00430 void * pDmaInit,
00431 uint16_t listSize,
00432 uint16_t delaySize,
00433 TransferCallback callback,
00434 void * argument)
00435 {
00436 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00437 uint8_t error;
00438 pDmaInit = pDmaInit;
00439
00440 if (pAudd->mic.bEpNum == 0)
00441 return USBRC_STATE_ERR;
00442
00443 error = USBD_HAL_SetupMblTransfer(pAudd->mic.bEpNum,
00444 pListInit,
00445 listSize,
00446 delaySize);
00447 if (error) return error;
00448 error = USBD_HAL_SetTransferCallback(
00449 pAudd->mic.bEpNum,
00450 callback, argument);
00451 return error;
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461 uint8_t AUDDSpeakerPhoneDriver_Write(void* buffer, uint16_t length)
00462 {
00463 AUDDSpeakerPhoneDriver *pAudd = &auddSpeakerPhoneDriver;
00464
00465 return USBD_HAL_Write(pAudd->mic.bEpNum,
00466 buffer, length);
00467 }
00468
00469