Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
usbd_core.c
00001 /** 00002 * @file usbd_core.c 00003 * @brief USB Device core 00004 * 00005 * DAPLink Interface Firmware 00006 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved 00007 * SPDX-License-Identifier: Apache-2.0 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00010 * not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00017 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 */ 00021 00022 #include <string.h> 00023 #include "rl_usb.h" 00024 #include "usb_for_lib.h" 00025 #include "info.h" 00026 00027 U16 USBD_DeviceStatus; 00028 U8 USBD_DeviceAddress; 00029 U8 USBD_Configuration; 00030 U32 USBD_EndPointMask; 00031 U32 USBD_EndPointHalt; 00032 U32 USBD_EndPointStall; /* EP must stay stalled */ 00033 U8 USBD_NumInterfaces; 00034 U8 USBD_HighSpeed; 00035 U8 USBD_ZLP; 00036 00037 USBD_EP_DATA USBD_EP0Data; 00038 USB_SETUP_PACKET USBD_SetupPacket; 00039 00040 #ifdef __RTX 00041 OS_TID USBD_RTX_DevTask; /* USB Device Task ID */ 00042 OS_TID USBD_RTX_EPTask[16]; /* USB Endpoint Task ID's */ 00043 OS_TID USBD_RTX_CoreTask; /* USB Core Task ID */ 00044 #endif 00045 00046 00047 __asm void $$USBD$$version(void) 00048 { 00049 /* Export a version number symbol for a version control. */ 00050 EXPORT __RL_USBD_VER 00051 __RL_USBD_VER EQU 0x470 00052 } 00053 00054 00055 /* 00056 * Init USB Device Core and Hardware 00057 * Parameters: None 00058 * Return Value: None 00059 */ 00060 00061 void usbd_init(void) 00062 { 00063 USBD_HighSpeed = __FALSE; 00064 usbd_class_init(); 00065 USBD_RTX_TaskInit(); 00066 USBD_Init(); 00067 } 00068 00069 00070 /* 00071 * USB Device Connect/Disconnect Function 00072 * Called by the User to Connect/Disconnect USB Device 00073 * Parameters: con: Connect/Disconnect 00074 * Return Value: None 00075 */ 00076 00077 void usbd_connect(BOOL con) 00078 { 00079 USBD_Connect(con); 00080 } 00081 00082 00083 /* 00084 * Reset USB Device Core 00085 * Parameters: None 00086 * Return Value: None 00087 */ 00088 00089 void usbd_reset_core(void) 00090 { 00091 USBD_DeviceStatus = usbd_power; 00092 USBD_DeviceAddress = 0; 00093 USBD_Configuration = 0; 00094 USBD_EndPointMask = 0x00010001; 00095 USBD_EndPointHalt = 0x00000000; 00096 USBD_EndPointStall = 0x00000000; 00097 } 00098 00099 00100 /* 00101 * USB Device Configured Function 00102 * Called by the User to check id USB Device is configured 00103 * Parameters: 00104 * Return Value: Configurated state (FALSE = unconfigured, TRUE = configured) 00105 */ 00106 00107 BOOL usbd_configured(void) 00108 { 00109 if (USBD_Configuration) { 00110 return (__TRUE); 00111 } 00112 00113 return (__FALSE); 00114 } 00115 00116 00117 /* 00118 * USB Device Request - Setup Stage 00119 * Parameters: None 00120 * Return Value: None 00121 */ 00122 00123 void USBD_SetupStage(void) 00124 { 00125 USBD_ReadEP(0x00, (U8 *)&USBD_SetupPacket, sizeof(USBD_SetupPacket)); 00126 } 00127 00128 00129 /* 00130 * USB Device Request - Data In Stage 00131 * Parameters: None 00132 * Return Value: None 00133 */ 00134 00135 void USBD_DataInStage(void) 00136 { 00137 U32 cnt; 00138 00139 if (USBD_EP0Data.Count > usbd_max_packet0) { 00140 cnt = usbd_max_packet0; 00141 } else { 00142 cnt = USBD_EP0Data.Count; 00143 } 00144 00145 if (!cnt) { 00146 USBD_ZLP = 0; 00147 } 00148 00149 cnt = USBD_WriteEP(0x80, USBD_EP0Data.pData, cnt); 00150 USBD_EP0Data.pData += cnt; 00151 USBD_EP0Data.Count -= cnt; 00152 } 00153 00154 00155 /* 00156 * USB Device Request - Data Out Stage 00157 * Parameters: None 00158 * Return Value: None 00159 */ 00160 00161 void USBD_DataOutStage(void) 00162 { 00163 U32 cnt; 00164 cnt = USBD_ReadEP(0x00, USBD_EP0Data.pData, USBD_EP0Data.Count); 00165 USBD_EP0Data.pData += cnt; 00166 USBD_EP0Data.Count -= cnt; 00167 } 00168 00169 00170 /* 00171 * USB Device Request - Status In Stage 00172 * Parameters: None 00173 * Return Value: None 00174 */ 00175 00176 void USBD_StatusInStage(void) 00177 { 00178 USBD_WriteEP(0x80, NULL, 0); 00179 } 00180 00181 00182 /* 00183 * USB Device Request - Status Out Stage 00184 * Parameters: None 00185 * Return Value: None 00186 */ 00187 00188 void USBD_StatusOutStage(void) 00189 { 00190 USBD_ReadEP(0x00, USBD_EP0Buf, usbd_max_packet0); 00191 } 00192 00193 00194 /* 00195 * Get Status USB Device Request 00196 * Parameters: None 00197 * Return Value: TRUE - Success, FALSE - Error 00198 */ 00199 00200 static inline BOOL USBD_ReqGetStatus(void) 00201 { 00202 U32 n, m; 00203 00204 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00205 case REQUEST_TO_DEVICE: 00206 USBD_EP0Data.pData = (U8 *)&USBD_DeviceStatus; 00207 break; 00208 00209 case REQUEST_TO_INTERFACE: 00210 if ((USBD_Configuration != 0) && (USBD_SetupPacket.wIndexL < USBD_NumInterfaces)) { 00211 *((__packed U16 *)USBD_EP0Buf) = 0; 00212 USBD_EP0Data.pData = USBD_EP0Buf; 00213 } else { 00214 return (__FALSE); 00215 } 00216 00217 break; 00218 00219 case REQUEST_TO_ENDPOINT: 00220 n = USBD_SetupPacket.wIndexL & 0x8F; 00221 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); 00222 00223 if (((USBD_Configuration != 0) || ((n & 0x0F) == 0)) && (USBD_EndPointMask & m)) { 00224 *((__packed U16 *)USBD_EP0Buf) = (USBD_EndPointHalt & m) ? 1 : 0; 00225 USBD_EP0Data.pData = USBD_EP0Buf; 00226 } else { 00227 return (__FALSE); 00228 } 00229 00230 break; 00231 00232 default: 00233 return (__FALSE); 00234 } 00235 00236 return (__TRUE); 00237 } 00238 00239 00240 /* 00241 * Set/Clear Feature USB Device Request 00242 * Parameters: sc: 0 - Clear, 1 - Set 00243 * Return Value: TRUE - Success, FALSE - Error 00244 */ 00245 00246 static inline BOOL USBD_ReqSetClrFeature(U32 sc) 00247 { 00248 U32 n, m; 00249 00250 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00251 case REQUEST_TO_DEVICE: 00252 if (USBD_SetupPacket.wValue == USB_FEATURE_REMOTE_WAKEUP) { 00253 if (sc) { 00254 USBD_WakeUpCfg(__TRUE); 00255 USBD_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP; 00256 } else { 00257 USBD_WakeUpCfg(__FALSE); 00258 USBD_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP; 00259 } 00260 } else { 00261 return (__FALSE); 00262 } 00263 00264 break; 00265 00266 case REQUEST_TO_INTERFACE: 00267 return (__FALSE); 00268 00269 case REQUEST_TO_ENDPOINT: 00270 n = USBD_SetupPacket.wIndexL & 0x8F; 00271 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); 00272 00273 if ((USBD_Configuration != 0) && ((n & 0x0F) != 0) && (USBD_EndPointMask & m)) { 00274 if (USBD_SetupPacket.wValue == USB_FEATURE_ENDPOINT_STALL) { 00275 if (sc) { 00276 USBD_SetStallEP(n); 00277 USBD_EndPointHalt |= m; 00278 } else { 00279 if ((USBD_EndPointStall & m) != 0) { 00280 return (__TRUE); 00281 } 00282 00283 USBD_ClrStallEP(n); 00284 USBD_ReqClrFeature_MSC(n); 00285 USBD_EndPointHalt &= ~m; 00286 } 00287 } else { 00288 return (__FALSE); 00289 } 00290 } else { 00291 return (__FALSE); 00292 } 00293 00294 break; 00295 00296 default: 00297 return (__FALSE); 00298 } 00299 00300 return (__TRUE); 00301 } 00302 00303 00304 /* 00305 * Set Address USB Device Request 00306 * Parameters: None 00307 * Return Value: TRUE - Success, FALSE - Error 00308 */ 00309 00310 static inline BOOL USBD_ReqSetAddress(void) 00311 { 00312 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00313 case REQUEST_TO_DEVICE: 00314 USBD_DeviceAddress = 0x80 | USBD_SetupPacket.wValueL; 00315 break; 00316 00317 default: 00318 return (__FALSE); 00319 } 00320 00321 return (__TRUE); 00322 } 00323 00324 00325 /* 00326 * Get Descriptor USB Device Request 00327 * Parameters: None 00328 * Return Value: TRUE - Success, FALSE - Error 00329 */ 00330 00331 static inline BOOL USBD_ReqGetDescriptor(void) 00332 { 00333 U8 *pD; 00334 U32 len, n; 00335 00336 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00337 case REQUEST_TO_DEVICE: 00338 switch (USBD_SetupPacket.wValueH) { 00339 case USB_DEVICE_DESCRIPTOR_TYPE: 00340 USBD_EP0Data.pData = (U8 *)USBD_DeviceDescriptor; 00341 len = USB_DEVICE_DESC_SIZE; 00342 break; 00343 00344 case USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE: 00345 if (!usbd_hs_enable) { 00346 return (__FALSE); /* High speed not enabled */ 00347 } 00348 00349 if (USBD_HighSpeed == __FALSE) { 00350 USBD_EP0Data.pData = (U8 *)USBD_DeviceQualifier; 00351 } else { 00352 USBD_EP0Data.pData = (U8 *)USBD_DeviceQualifier_HS; 00353 } 00354 00355 len = USB_DEVICE_QUALI_SIZE; 00356 break; 00357 00358 case USB_CONFIGURATION_DESCRIPTOR_TYPE: 00359 if ((!usbd_hs_enable) && (USBD_HighSpeed == __TRUE)) { 00360 return (__FALSE); /* High speed request but high-speed not enabled */ 00361 } 00362 00363 if (USBD_HighSpeed == __FALSE) { 00364 pD = (U8 *)USBD_ConfigDescriptor; 00365 ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bDescriptorType = USB_CONFIGURATION_DESCRIPTOR_TYPE; //same descriptor is used in other configuration 00366 } else { 00367 pD = (U8 *)USBD_ConfigDescriptor_HS; 00368 ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bDescriptorType = USB_CONFIGURATION_DESCRIPTOR_TYPE; //same descriptor is used in other configuration 00369 } 00370 00371 for (n = 0; n != USBD_SetupPacket.wValueL; n++) { 00372 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) { 00373 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00374 } 00375 } 00376 00377 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) { 00378 return (__FALSE); 00379 } 00380 00381 USBD_EP0Data.pData = pD; 00382 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00383 break; 00384 00385 case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE: 00386 if (!usbd_hs_enable) { 00387 return (__FALSE); /* High speed not enabled */ 00388 } 00389 00390 if (USBD_HighSpeed == __FALSE) { 00391 pD = (U8 *)USBD_ConfigDescriptor_HS; 00392 ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bDescriptorType = USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE; //same descriptor is used in main configuration 00393 } else { 00394 pD = (U8 *)USBD_ConfigDescriptor; 00395 ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bDescriptorType = USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE; //same descriptor is used in main configuration 00396 } 00397 00398 for (n = 0; n != USBD_SetupPacket.wValueL; n++) { 00399 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) { 00400 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00401 } 00402 } 00403 00404 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) { 00405 return (__FALSE); 00406 } 00407 00408 USBD_EP0Data.pData = pD; 00409 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; 00410 break; 00411 00412 case USB_STRING_DESCRIPTOR_TYPE: 00413 pD = (U8 *)USBD_StringDescriptor; 00414 00415 // added by sam to send unique id string descriptor 00416 if (USBD_SetupPacket.wValueL == 3) { 00417 USBD_EP0Data.pData = (uint8_t *)info_get_unique_id_string_descriptor(); 00418 len = ((USB_STRING_DESCRIPTOR *)USBD_EP0Data.pData)->bLength; 00419 break; 00420 } 00421 00422 for (n = 0; n != USBD_SetupPacket.wValueL; n++) { 00423 if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) { 00424 pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength; 00425 } 00426 } 00427 00428 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) { 00429 return (__FALSE); 00430 } 00431 00432 USBD_EP0Data.pData = pD; 00433 len = ((USB_STRING_DESCRIPTOR *)pD)->bLength; 00434 break; 00435 00436 case USB_BINARY_OBJECT_STORE_DESCRIPTOR_TYPE: 00437 if (!usbd_bos_enable) { 00438 return (__FALSE); /* High speed not enabled */ 00439 } 00440 00441 pD = (U8 *)USBD_BinaryObjectStoreDescriptor; 00442 USBD_EP0Data.pData = pD; 00443 00444 if (((USB_BINARY_OBJECT_STORE_DESCRIPTOR *)pD)->bLength == 0) { 00445 return (__FALSE); 00446 } 00447 00448 len = ((USB_BINARY_OBJECT_STORE_DESCRIPTOR *)pD)->wTotalLength; 00449 break; 00450 00451 default: 00452 return (__FALSE); 00453 } 00454 00455 break; 00456 00457 case REQUEST_TO_INTERFACE: 00458 if (!USBD_ReqGetDescriptor_HID(&pD, &len)) { 00459 return (__FALSE); 00460 } 00461 00462 break; 00463 00464 default: 00465 return (__FALSE); 00466 } 00467 00468 if (USBD_EP0Data.Count > len) { 00469 USBD_EP0Data.Count = len; 00470 00471 if (!(USBD_EP0Data.Count & (usbd_max_packet0 - 1))) { 00472 USBD_ZLP = 1; 00473 } 00474 } 00475 00476 return (__TRUE); 00477 } 00478 00479 00480 /* 00481 * Get Configuration USB Device Request 00482 * Parameters: None 00483 * Return Value: TRUE - Success, FALSE - Error 00484 */ 00485 00486 static inline BOOL USBD_ReqGetConfiguration(void) 00487 { 00488 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00489 case REQUEST_TO_DEVICE: 00490 USBD_EP0Data.pData = &USBD_Configuration; 00491 break; 00492 00493 default: 00494 return (__FALSE); 00495 } 00496 00497 return (__TRUE); 00498 } 00499 00500 00501 /* 00502 * Set Configuration USB Device Request 00503 * Parameters: None 00504 * Return Value: TRUE - Success, FALSE - Error 00505 */ 00506 00507 static inline BOOL USBD_ReqSetConfiguration(void) 00508 { 00509 USB_CONFIGURATION_DESCRIPTOR *pD; 00510 U32 alt = 0; 00511 U32 n, m; 00512 00513 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00514 case REQUEST_TO_DEVICE: 00515 if (USBD_SetupPacket.wValueL) { 00516 if ((!usbd_hs_enable) && (USBD_HighSpeed == __TRUE)) { 00517 return (__FALSE); /* High speed request but high-speed not enabled */ 00518 } 00519 00520 if (USBD_HighSpeed == __FALSE) { 00521 pD = (USB_CONFIGURATION_DESCRIPTOR *)USBD_ConfigDescriptor; 00522 } else { 00523 pD = (USB_CONFIGURATION_DESCRIPTOR *)USBD_ConfigDescriptor_HS; 00524 } 00525 00526 while (pD->bLength) { 00527 switch (pD->bDescriptorType) { 00528 case USB_CONFIGURATION_DESCRIPTOR_TYPE: 00529 case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE: 00530 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == USBD_SetupPacket.wValueL) { 00531 USBD_Configuration = USBD_SetupPacket.wValueL; 00532 USBD_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces; 00533 00534 for (n = 0; n < usbd_if_num; n++) { 00535 USBD_AltSetting[n] = 0; 00536 } 00537 00538 for (n = 1; n < 16; n++) { 00539 if (USBD_EndPointMask & (1 << n)) { 00540 USBD_DisableEP(n); 00541 } 00542 00543 if (USBD_EndPointMask & ((1 << 16) << n)) { 00544 USBD_DisableEP(n | 0x80); 00545 } 00546 } 00547 00548 USBD_EndPointMask = 0x00010001; 00549 USBD_EndPointHalt = 0x00000000; 00550 USBD_EndPointStall = 0x00000000; 00551 USBD_Configure(__TRUE); 00552 00553 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) { 00554 USBD_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; 00555 } else { 00556 USBD_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; 00557 } 00558 } else { 00559 pD = (USB_CONFIGURATION_DESCRIPTOR *)((U8 *)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength); 00560 continue; 00561 } 00562 00563 break; 00564 00565 case USB_INTERFACE_DESCRIPTOR_TYPE: 00566 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; 00567 break; 00568 00569 case USB_ENDPOINT_DESCRIPTOR_TYPE: 00570 if (alt == 0) { 00571 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; 00572 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); 00573 USBD_EndPointMask |= m; 00574 USBD_ConfigEP((void *)pD); 00575 USBD_EnableEP(n); 00576 USBD_ResetEP(n); 00577 } 00578 00579 break; 00580 } 00581 00582 pD = (USB_CONFIGURATION_DESCRIPTOR *)((U8 *)pD + pD->bLength); 00583 } 00584 } else { 00585 USBD_Configuration = 0; 00586 00587 for (n = 1; n < 16; n++) { 00588 if (USBD_EndPointMask & (1 << n)) { 00589 USBD_DisableEP(n); 00590 } 00591 00592 if (USBD_EndPointMask & ((1 << 16) << n)) { 00593 USBD_DisableEP(n | 0x80); 00594 } 00595 } 00596 00597 USBD_EndPointMask = 0x00010001; 00598 USBD_EndPointHalt = 0x00000000; 00599 USBD_EndPointStall = 0x00000000; 00600 USBD_Configure(__FALSE); 00601 } 00602 00603 if (USBD_Configuration != USBD_SetupPacket.wValueL) { 00604 return (__FALSE); 00605 } 00606 00607 break; 00608 00609 default: 00610 return (__FALSE); 00611 } 00612 00613 return (__TRUE); 00614 } 00615 00616 00617 /* 00618 * Get Interface USB Device Request 00619 * Parameters: None 00620 * Return Value: TRUE - Success, FALSE - Error 00621 */ 00622 00623 static inline BOOL USBD_ReqGetInterface(void) 00624 { 00625 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00626 case REQUEST_TO_INTERFACE: 00627 if ((USBD_Configuration != 0) && (USBD_SetupPacket.wIndexL < USBD_NumInterfaces)) { 00628 USBD_EP0Data.pData = USBD_AltSetting + USBD_SetupPacket.wIndexL; 00629 } else { 00630 return (__FALSE); 00631 } 00632 00633 break; 00634 00635 default: 00636 return (__FALSE); 00637 } 00638 00639 return (__TRUE); 00640 } 00641 00642 00643 /* 00644 * Set Interface USB Device Request 00645 * Parameters: None 00646 * Return Value: TRUE - Success, FALSE - Error 00647 */ 00648 00649 static inline BOOL USBD_ReqSetInterface(void) 00650 { 00651 USB_COMMON_DESCRIPTOR *pD; 00652 U32 ifn = 0, alt = 0, old = 0, msk = 0; 00653 U32 n, m; 00654 BOOL set; 00655 00656 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00657 case REQUEST_TO_INTERFACE: 00658 if (USBD_Configuration == 0) { 00659 return (__FALSE); 00660 } 00661 00662 set = __FALSE; 00663 00664 if ((!usbd_hs_enable) && (USBD_HighSpeed == __TRUE)) { 00665 return (__FALSE); /* High speed request but high-speed not enabled */ 00666 } 00667 00668 if (USBD_HighSpeed == __FALSE) { 00669 pD = (USB_COMMON_DESCRIPTOR *)USBD_ConfigDescriptor; 00670 } else { 00671 pD = (USB_COMMON_DESCRIPTOR *)USBD_ConfigDescriptor_HS; 00672 } 00673 00674 while (pD->bLength) { 00675 switch (pD->bDescriptorType) { 00676 case USB_CONFIGURATION_DESCRIPTOR_TYPE: 00677 case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE: 00678 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USBD_Configuration) { 00679 pD = (USB_COMMON_DESCRIPTOR *)((U8 *)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength); 00680 continue; 00681 } 00682 00683 break; 00684 00685 case USB_INTERFACE_DESCRIPTOR_TYPE: 00686 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber; 00687 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; 00688 msk = 0; 00689 00690 if ((ifn == USBD_SetupPacket.wIndexL) && (alt == USBD_SetupPacket.wValueL)) { 00691 set = __TRUE; 00692 old = USBD_AltSetting[ifn]; 00693 USBD_AltSetting[ifn] = (U8)alt; 00694 } 00695 00696 break; 00697 00698 case USB_ENDPOINT_DESCRIPTOR_TYPE: 00699 if (ifn == USBD_SetupPacket.wIndexL) { 00700 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; 00701 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); 00702 00703 if (alt == USBD_SetupPacket.wValueL) { 00704 USBD_EndPointMask |= m; 00705 USBD_EndPointHalt &= ~m; 00706 USBD_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); 00707 USBD_EnableEP(n); 00708 USBD_ResetEP(n); 00709 msk |= m; 00710 } else if ((alt == old) && ((msk & m) == 0)) { 00711 USBD_EndPointMask &= ~m; 00712 USBD_EndPointHalt &= ~m; 00713 USBD_DisableEP(n); 00714 } 00715 } 00716 00717 break; 00718 } 00719 00720 pD = (USB_COMMON_DESCRIPTOR *)((U8 *)pD + pD->bLength); 00721 } 00722 00723 break; 00724 00725 default: 00726 return (__FALSE); 00727 } 00728 00729 return (set); 00730 } 00731 00732 00733 /* 00734 * USB Device Endpoint 0 Event Callback 00735 * Parameters: event 00736 * Return Value: none 00737 */ 00738 00739 void USBD_EndPoint0(U32 event) 00740 { 00741 if (event & USBD_EVT_SETUP) { 00742 USBD_SetupStage(); 00743 USBD_DirCtrlEP(USBD_SetupPacket.bmRequestType.Dir); 00744 USBD_EP0Data.Count = USBD_SetupPacket.wLength; /* Number of bytes to transfer */ 00745 00746 switch (USBD_SetupPacket.bmRequestType.Type) { 00747 case REQUEST_STANDARD: 00748 switch (USBD_SetupPacket.bRequest) { 00749 case USB_REQUEST_GET_STATUS: 00750 if (!USBD_ReqGetStatus()) { 00751 goto stall; 00752 } 00753 00754 USBD_DataInStage(); 00755 break; 00756 00757 case USB_REQUEST_CLEAR_FEATURE: 00758 if (!USBD_ReqSetClrFeature(0)) { 00759 goto stall; 00760 } 00761 00762 USBD_StatusInStage(); 00763 #ifdef __RTX 00764 00765 if (__rtx) { 00766 if (USBD_RTX_CoreTask) { 00767 usbd_os_evt_set(USBD_EVT_CLR_FEATURE, USBD_RTX_CoreTask); 00768 } 00769 } else { 00770 #endif 00771 00772 if (USBD_P_Feature_Event) { 00773 USBD_P_Feature_Event(); 00774 } 00775 00776 #ifdef __RTX 00777 } 00778 00779 #endif 00780 break; 00781 00782 case USB_REQUEST_SET_FEATURE: 00783 if (!USBD_ReqSetClrFeature(1)) { 00784 goto stall; 00785 } 00786 00787 USBD_StatusInStage(); 00788 #ifdef __RTX 00789 00790 if (__rtx) { 00791 if (USBD_RTX_CoreTask) { 00792 usbd_os_evt_set(USBD_EVT_SET_FEATURE, USBD_RTX_CoreTask); 00793 } 00794 } else { 00795 #endif 00796 00797 if (USBD_P_Feature_Event) { 00798 USBD_P_Feature_Event(); 00799 } 00800 00801 #ifdef __RTX 00802 } 00803 00804 #endif 00805 break; 00806 00807 case USB_REQUEST_SET_ADDRESS: 00808 if (!USBD_ReqSetAddress()) { 00809 goto stall; 00810 } 00811 00812 USBD_SetAddress(USBD_DeviceAddress & 0x7F, 1); 00813 USBD_StatusInStage(); 00814 break; 00815 00816 case USB_REQUEST_GET_DESCRIPTOR: 00817 if (!USBD_ReqGetDescriptor()) { 00818 goto stall; 00819 } 00820 00821 USBD_DataInStage(); 00822 break; 00823 00824 case USB_REQUEST_SET_DESCRIPTOR: 00825 goto stall; 00826 00827 case USB_REQUEST_GET_CONFIGURATION: 00828 if (!USBD_ReqGetConfiguration()) { 00829 goto stall; 00830 } 00831 00832 USBD_DataInStage(); 00833 break; 00834 00835 case USB_REQUEST_SET_CONFIGURATION: 00836 if (!USBD_ReqSetConfiguration()) { 00837 goto stall; 00838 } 00839 00840 USBD_StatusInStage(); 00841 #ifdef __RTX 00842 00843 if (__rtx) { 00844 if (USBD_RTX_CoreTask) { 00845 usbd_os_evt_set(USBD_EVT_SET_CFG, USBD_RTX_CoreTask); 00846 } 00847 } else { 00848 #endif 00849 00850 if (USBD_P_Configure_Event) { 00851 USBD_P_Configure_Event(); 00852 } 00853 00854 #ifdef __RTX 00855 } 00856 00857 #endif 00858 break; 00859 00860 case USB_REQUEST_GET_INTERFACE: 00861 if (!USBD_ReqGetInterface()) { 00862 goto stall; 00863 } 00864 00865 USBD_DataInStage(); 00866 break; 00867 00868 case USB_REQUEST_SET_INTERFACE: 00869 if (!USBD_ReqSetInterface()) { 00870 goto stall; 00871 } 00872 00873 USBD_StatusInStage(); 00874 #ifdef __RTX 00875 00876 if (__rtx) { 00877 if (USBD_RTX_CoreTask) { 00878 usbd_os_evt_set(USBD_EVT_SET_IF, USBD_RTX_CoreTask); 00879 } 00880 } else { 00881 #endif 00882 00883 if (USBD_P_Interface_Event) { 00884 USBD_P_Interface_Event(); 00885 } 00886 00887 #ifdef __RTX 00888 } 00889 00890 #endif 00891 break; 00892 00893 default: 00894 goto stall; 00895 } 00896 00897 break; /* end case REQUEST_STANDARD */ 00898 00899 case REQUEST_CLASS: 00900 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00901 case REQUEST_TO_DEVICE: 00902 goto stall; /* not supported */ 00903 00904 case REQUEST_TO_INTERFACE: 00905 if (USBD_EndPoint0_Setup_HID_ReqToIF()) { 00906 goto setup_class_ok; 00907 } 00908 00909 if (USBD_EndPoint0_Setup_MSC_ReqToIF()) { 00910 goto setup_class_ok; 00911 } 00912 00913 if (USBD_EndPoint0_Setup_CDC_ReqToIF()) { 00914 goto setup_class_ok; 00915 } 00916 00917 goto stall; /* not supported */ 00918 00919 /* end case REQUEST_TO_INTERFACE */ 00920 00921 case REQUEST_TO_ENDPOINT: 00922 goto stall; 00923 00924 /* end case REQUEST_TO_ENDPOINT */ 00925 00926 default: 00927 goto stall; 00928 } 00929 00930 setup_class_ok: /* request finished successfully */ 00931 break; /* end case REQUEST_CLASS */ 00932 00933 case REQUEST_VENDOR: 00934 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00935 case REQUEST_TO_DEVICE: 00936 if (USBD_EndPoint0_Setup_WebUSB_ReqToDevice()) { 00937 goto setup_vendor_ok; 00938 } 00939 00940 if (USBD_EndPoint0_Setup_WinUSB_ReqToDevice()) { 00941 goto setup_vendor_ok; 00942 } 00943 00944 goto stall; 00945 00946 default: 00947 goto stall; 00948 } 00949 setup_vendor_ok: 00950 break; /* end case REQUEST_VENDOR */ 00951 00952 default: 00953 stall: 00954 if ((USBD_SetupPacket.bmRequestType.Dir == REQUEST_HOST_TO_DEVICE) && 00955 (USBD_SetupPacket.wLength != 0)) { 00956 USBD_SetStallEP(0x00); 00957 } else { 00958 USBD_SetStallEP(0x80); 00959 } 00960 00961 USBD_EP0Data.Count = 0; 00962 break; 00963 } 00964 } 00965 00966 if (event & USBD_EVT_OUT) { 00967 if (USBD_SetupPacket.bmRequestType.Dir == REQUEST_HOST_TO_DEVICE) { 00968 if (USBD_EP0Data.Count) { /* still data to receive ? */ 00969 USBD_DataOutStage(); /* receive data */ 00970 00971 if (USBD_EP0Data.Count == 0) { /* data complete ? */ 00972 switch (USBD_SetupPacket.bmRequestType.Type) { 00973 case REQUEST_STANDARD: 00974 goto stall_i; /* not supported */ 00975 00976 case REQUEST_CLASS: 00977 switch (USBD_SetupPacket.bmRequestType.Recipient) { 00978 case REQUEST_TO_DEVICE: 00979 goto stall_i; /* not supported */ 00980 00981 case REQUEST_TO_INTERFACE: 00982 if (USBD_EndPoint0_Out_HID_ReqToIF()) { 00983 goto out_class_ok; 00984 } 00985 00986 if (USBD_EndPoint0_Out_CDC_ReqToIF()) { 00987 goto out_class_ok; 00988 } 00989 00990 goto stall_i; 00991 00992 /* end case REQUEST_TO_INTERFACE */ 00993 00994 case REQUEST_TO_ENDPOINT: 00995 goto stall_i; 00996 00997 /* end case REQUEST_TO_ENDPOINT */ 00998 00999 default: 01000 goto stall_i; 01001 } 01002 01003 out_class_ok: /* request finished successfully */ 01004 break; /* end case REQUEST_CLASS */ 01005 01006 default: 01007 stall_i: 01008 USBD_SetStallEP(0x80); 01009 USBD_EP0Data.Count = 0; 01010 break; 01011 } 01012 } 01013 } 01014 } else { 01015 USBD_StatusOutStage(); /* receive Acknowledge */ 01016 } 01017 } /* end USBD_EVT_OUT */ 01018 01019 if (event & USBD_EVT_IN) { 01020 if (USBD_SetupPacket.bmRequestType.Dir == REQUEST_DEVICE_TO_HOST) { 01021 if (USBD_EP0Data.Count || USBD_ZLP) { 01022 USBD_DataInStage(); /* send data */ 01023 } 01024 } else { 01025 if (USBD_DeviceAddress & 0x80) { 01026 USBD_DeviceAddress &= 0x7F; 01027 USBD_SetAddress(USBD_DeviceAddress, 0); 01028 } 01029 } 01030 } /* end USBD_EVT_IN */ 01031 01032 if (event & USBD_EVT_OUT_STALL) { 01033 USBD_ClrStallEP(0x00); 01034 } 01035 01036 if (event & USBD_EVT_IN_STALL) { 01037 USBD_ClrStallEP(0x80); 01038 } 01039 } 01040 01041 01042 /* 01043 * USB Device Endpoint 0 RTX Task 01044 * Parameters: none 01045 * Return Value: none 01046 */ 01047 01048 #ifdef __RTX 01049 void USBD_RTX_EndPoint0(void) 01050 { 01051 for (;;) { 01052 usbd_os_evt_wait_or(0xFFFF, 0xFFFF); 01053 USBD_EndPoint0(usbd_os_evt_get()); 01054 } 01055 } 01056 #endif
Generated on Tue Jul 12 2022 15:37:26 by
1.7.2