otgapi.c 44.8 KB
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
/*HEADER******************************************************************
**************************************************************************
*** 
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved                                          
***                                                              
*** This software embodies materials and concepts which are      
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license   
*** agreement with ARC International             
***
*** $Workfile:otgapi.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:01 $
***
*** Description:      
***  This file contains the USB On-The-Go generic functions.
***                                                               
**************************************************************************
*END*********************************************************************/
#ifndef __USB_OS_MQX__
   #include "types.h"
   #include "hostapi.h"
   #include "devapi.h"
   #include "otgapi.h"
   #include "usb.h"
   #include "vusb11.h"
   #include "usbprv.h"
   #include "usbdprv.h"
   #include "usbhprv.h"
   #include "usboprv.h"
#else
   #include "mqx.h"
   #include "bsp.h"
   #include "usbprv.h"
   #include "usboprv.h"
#endif

#ifndef __USB_OS_MQX__
extern USB_DEV_STATE_STRUCT_PTR global_usb_device_state_struct_ptr;
#ifndef PERIPHERAL_ONLY
extern USB_HOST_STATE_STRUCT_PTR usb_host_state_struct_ptr;
#endif
#endif

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_init
* Returned Value : USB_OK or error code
* Comments       :
*     The _usb_otg_init routine initializes the USB OTG API data structures
*     and the OTG hardware.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_otg_init
   (
      /* [IN] Device number to initialize */
      uint_8                     device_number,

      /* [IN] the usb otg initialize data structure */
      USB_OTG_INIT_STRUCT_PTR    init_ptr,

      /* [OUT] the usb device handle */
      _usb_otg_handle _PTR_      handle
   )
{ /* Body */
   uint_8                        returncode;
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   
#ifdef __USB_OS_MQX__   
   USB_CALLBACK_FUNCTIONS_STRUCT_PTR call_back_table_ptr;
#endif
   
   /* 
   ** Check the input data: When a_bus_drop is TRUE, then a_bus_req must be 
   ** FALSE. Section 6.6.1.1.
   */
   if (init_ptr->A_BUS_DROP && init_ptr->A_BUS_REQ) {
      return USBERR_INIT_DATA; 
   } /* Endif */ 
   
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)
      USB_memalloc(sizeof(USB_OTG_STATE_STRUCT));
      
   if (usb_otg_ptr == NULL) {
      return USBERR_ALLOC;
   } /* Endif */
   
   USB_memzero((uchar_ptr)usb_otg_ptr, sizeof(USB_OTG_STATE_STRUCT));
   
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   
   usb_otg_ptr->DEV_NUM = device_number;
   
#ifdef __USB_OS_MQX__   
   call_back_table_ptr = (USB_CALLBACK_FUNCTIONS_STRUCT_PTR)
      _mqx_get_io_component_handle(IO_USB_COMPONENT);
   
   usb_otg_ptr->CALLBACK_STRUCT_PTR = (call_back_table_ptr + device_number);
      
   if (!usb_otg_ptr->CALLBACK_STRUCT_PTR) {
      USB_memfree((uchar_ptr)usb_otg_ptr);
      return USBERR_DRIVER_NOT_INSTALLED;
   } /* Endif */
#endif

   /* Initialize the state structure with default values.*/
   s_ptr->A_BUS_DROP = init_ptr->A_BUS_DROP;
   s_ptr->A_BUS_REQ  = init_ptr->A_BUS_REQ;
   s_ptr->B_BUS_REQ  = init_ptr->B_BUS_REQ;
   s_ptr->STATE = OTG_START;
   usb_otg_ptr->SERVICE = init_ptr->SERVICE;
      
   /* Initialize the state structure */
   _usb_otg_reset_state_machine(usb_otg_ptr);
   
   *handle = (_usb_otg_handle)usb_otg_ptr;
   
#ifndef __USB_OS_MQX__
#ifdef __VUSB32__
   returncode = _usb_otg_vusb11_init(*handle);
#endif   
#else
   returncode = usb_otg_ptr->CALLBACK_STRUCT_PTR->OTG_INIT(usb_otg_ptr);
#endif
   
   if (returncode != USB_OK) {
      /* There was an error during initialization. Free all the 
      ** allocated memory 
      */
      USB_memfree((uchar_ptr)usb_otg_ptr);
      return returncode;
   } /* Endif */
   return USB_OK;
} /* Endbody */

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_otg_shutdown
*  Returned Value : 
*  Comments       :
*        Shutdown an initialized USB OTG device.
*
*END*-----------------------------------------------------------------*/
void _usb_otg_shutdown
   (
      /* [IN] the USB_otg_initialize state structure */
      _usb_otg_handle         handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR   usb_otg_ptr;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;

   if (usb_otg_ptr->STATE_STRUCT.DEVICE_UP) {
       _usb_device_shutdown(global_usb_device_state_struct_ptr);
   } /* Endif */

#ifndef PERIPHERAL_ONLY   
   if(usb_otg_ptr->STATE_STRUCT.HOST_UP){
       _usb_host_shutdown(usb_host_state_struct_ptr);
   } /* Endif */
#endif
   
#ifndef __USB_OS_MQX__   
#ifdef __VUSB32__
   _usb_otg_vusb11_shutdown((_usb_otg_handle)usb_otg_ptr);
#endif   
#else
    usb_otg_ptr->CALLBACK_STRUCT_PTR->OTG_SHUTDOWN(usb_otg_ptr);
#endif
   /* Free all internal resources allocated */
   USB_memfree((uchar_ptr)usb_otg_ptr);
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_set_status
* Returned Value : USB_OK or error code
* Comments       :
*     Set the value of a specified OTG parameter.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_otg_set_status
   (
      /* [IN] Handle to the usb device */
      _usb_otg_handle   handle,
      
      /* [IN] What to set the status of */
      uint_8            component,
      
      /* [IN] What to set the status to */
      boolean           status
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR   usb_otg_ptr;
   boolean                    state_change;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   
   if (component < USB_OTG_A_BUS_DROP) {
      state_change = TRUE;
   } else {
      state_change = FALSE;
   } /* Endif */
   USB_lock();
   switch (component) {
      
#ifndef PERIPHERAL_ONLY
      case USB_OTG_A_SET_B_HNP_ENABLE:
         usb_otg_ptr->STATE_STRUCT.A_SET_B_HNP_EN = status;
         break;

      case USB_OTG_A_BUS_DROP:
         usb_otg_ptr->STATE_STRUCT.A_BUS_DROP = status;
         break;

      case USB_OTG_A_BUS_REQ:
         usb_otg_ptr->STATE_STRUCT.A_BUS_REQ = status;
         break;
      
      case USB_OTG_A_BUS_SUSPEND_REQ:
         usb_otg_ptr->STATE_STRUCT.A_BUS_SUSPEND_REQ = status;
         break;
      
      case USB_OTG_A_BUS_SUSPEND:
         usb_otg_ptr->STATE_STRUCT.A_BUS_SUSPEND = status;
         break;
      
      case USB_OTG_A_CONN:
         usb_otg_ptr->STATE_STRUCT.A_CONN = status;
         break;
      
      case USB_OTG_HOST_ACTIVE:
         usb_otg_ptr->STATE_STRUCT.HOST_UP = status;
         break;
      
      case USB_OTG_A_VBUS_ON:
        /*
        ** The A-device turn ON V_BUS:
        ** if the A-device application is not wanting to drop 
        ** the bus (a_bus_drop = FALSE), and if the A-device Application 
        ** is requesting the bus (a_bus_req = TRUE), or SRP is detected 
        ** on the bus (a_srp_det = TRUE).
        */
        usb_otg_ptr->STATE_STRUCT.A_BUS_DROP = FALSE;
        usb_otg_ptr->STATE_STRUCT.A_BUS_REQ = TRUE;
        break;
      
      case USB_OTG_A_BUS_RESUME:
         usb_otg_ptr->STATE_STRUCT.A_BUS_RESUME = status;
         break;
#endif         
     
      case USB_OTG_B_HNP_ENABLE:
         usb_otg_ptr->STATE_STRUCT.B_HNP_ENABLE = status;
         break;
      
      case USB_OTG_B_BUS_REQ:
         usb_otg_ptr->STATE_STRUCT.B_BUS_REQ = status;
         break;
     
      case USB_OTG_B_CONN:
         usb_otg_ptr->STATE_STRUCT.B_CONN = status;
         break;
     
      case USB_OTG_B_BUS_SUSPEND:
         usb_otg_ptr->STATE_STRUCT.B_BUS_SUSPEND = status;
         break;

      case USB_OTG_B_BUS_RESUME:
         usb_otg_ptr->STATE_STRUCT.B_BUS_RESUME = status;
         break;
     
      case USB_OTG_DEVICE_ACTIVE:
         usb_otg_ptr->STATE_STRUCT.DEVICE_UP = status;
         break;
    
      case USB_OTG_B_SRP_REQ:
        if (usb_otg_ptr->STATE_STRUCT.STATE != B_IDLE) {
           USB_unlock();
           return  USBERR_SRP_REQ_INVALID_STATE;
        } /* Endif */
        /* 
        ** set the user condition to start SRP. When we call the state 
        ** machine  SRP req will be activated if preconditions are met
        */
#ifdef PERIPHERAL_ONLY
        usb_otg_ptr->STATE_STRUCT.B_SESS_REQ = TRUE;
#endif        
        usb_otg_ptr->STATE_STRUCT.B_BUS_REQ = TRUE;
        break;
      
      default:
        USB_unlock();
        return USBERR_BAD_STATUS;
   } /* Endswitch */
   
   if (state_change) {
      _usb_otg_state_machine((_usb_otg_handle)usb_otg_ptr);
   } /* Endif */
   USB_unlock();
   return USB_OK;   
} /* EndBody */   


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_get_status
* Returned Value : USB_OK or error code
* Comments       :
*     Get the value of a specified OTG parameter.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_otg_get_status
   (
      /* [IN] Handle to the USB otg */
      _usb_otg_handle      handle,
      
      /* [IN] What to get the status of */
      uint_8               component,
      
      /* [OUT] The requested status */
      uint_32_ptr          status
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR usb_otg_ptr;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   
   USB_lock();
   switch (component) {
#ifndef PERIPHERAL_ONLY
      case USB_OTG_A_SET_B_HNP_ENABLE:
         *status = usb_otg_ptr->STATE_STRUCT.A_SET_B_HNP_EN;
         break;
      
      case USB_OTG_A_BUS_DROP:
         *status = usb_otg_ptr->STATE_STRUCT.A_BUS_DROP;
         break;
      
      case USB_OTG_A_BUS_REQ:
         *status = usb_otg_ptr->STATE_STRUCT.A_BUS_REQ;
         break;
    
      case USB_OTG_A_BUS_SUSPEND:
         *status = usb_otg_ptr->STATE_STRUCT.A_BUS_SUSPEND;
         break;
    
      case USB_OTG_A_CONN:
         *status = usb_otg_ptr->STATE_STRUCT.A_CONN;
         break;
       
      case USB_OTG_A_BUS_RESUME:
         *status = usb_otg_ptr->STATE_STRUCT.A_BUS_RESUME;
         break;
    
      case USB_OTG_HOST_ACTIVE:
         *status = usb_otg_ptr->STATE_STRUCT.HOST_UP;
         break;
#endif     
      case USB_OTG_STATE:
         *status = usb_otg_ptr->STATE_STRUCT.STATE;
         break;

      case USB_OTG_B_HNP_ENABLE:
         *status = usb_otg_ptr->STATE_STRUCT.B_HNP_ENABLE;
         break;

      case USB_OTG_B_BUS_REQ:
         *status = usb_otg_ptr->STATE_STRUCT.B_BUS_REQ;
         break;
     
      case USB_OTG_B_CONN:
         *status = usb_otg_ptr->STATE_STRUCT.B_CONN;
         break;
     
      case USB_OTG_B_BUS_SUSPEND:
         *status = usb_otg_ptr->STATE_STRUCT.B_BUS_SUSPEND;
         break;
     
      case USB_OTG_B_BUS_RESUME:
         *status = usb_otg_ptr->STATE_STRUCT.B_BUS_RESUME;
         break;
     
      case USB_OTG_DEVICE_ACTIVE:
         *status = usb_otg_ptr->STATE_STRUCT.DEVICE_UP;
         break;
     
      default:
         USB_unlock();
         return USBERR_BAD_STATUS;
   } /* Endswitch */
   USB_unlock();
   
   return USB_OK;   
} /* EndBody */  

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_state_machine
* Returned Value : None
* Comments       :
*                The OTG state machine.
* 
*END*--------------------------------------------------------------------*/
void  _usb_otg_state_machine
   (
      /* [IN] Handle to the USB otg */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   USB_STRUCT_PTR                usb_reg;
   uint_8                        state;
   uint_32                       hw_signal;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR; 
   usb_reg = usb_otg_ptr->USB_REG_PTR;
   state = s_ptr->STATE;
   hw_signal = s_ptr->OTG_INT_STATUS;
 
#ifdef OTG_LOG_STATE
   usb_otg_ptr->LOG_STATE[usb_otg_ptr->LOG_STATE_COUNT++] = 
      usb_otg_ptr->STATE_STRUCT.STATE;
   if (usb_otg_ptr->LOG_STATE_COUNT >= 99) {
      usb_otg_ptr->LOG_STATE_COUNT = 0;
  } /* Endif */
#endif 
   
#ifdef PERIPHERAL_ONLY 
   switch (state) {
      case B_IDLE:
        _usb_otg_process_b_idle((pointer)usb_otg_ptr);
         break;
      case B_SRP_INIT:
         _usb_otg_process_b_srp_init((pointer)usb_otg_ptr);
         break;
      case B_PERIPHERAL:
         if (SESS_VLD_FALSE(otg_reg)) {
            s_ptr->STATE = B_IDLE;
            VBUS_CHG_OFF(otg_reg);
            VBUS_DSCHG_ON(otg_reg);
         } /* Endif */
         break;
      default:
         break;
   } /* Endswitch */
#else 
   
   /*
   ** process exception if  not start state
   */
   if (state){
 	   if(_usb_otg_process_exceptions((pointer)usb_otg_ptr) ){
 	      if (state != s_ptr->STATE) {
#ifdef OTG_LOG_STATE
            usb_otg_ptr->LOG_STATE[usb_otg_ptr->LOG_STATE_COUNT++] = 
               usb_otg_ptr->STATE_STRUCT.STATE;
            if (usb_otg_ptr->LOG_STATE_COUNT>=99) {
               usb_otg_ptr->LOG_STATE_COUNT = 0;
            } /* Endif */
#endif 
         return;  /* state is changed so return */
         }
	   } /* Endif */
   } /* Endif */
   
   switch (state) {
      case OTG_START:
         if(ID(otg_reg)) {
            s_ptr->STATE = B_IDLE;
            _usb_otg_process_b_idle((pointer)usb_otg_ptr);
            break;
         } else {
            /* ID is false when A device is identified */
            s_ptr->STATE = A_IDLE;
         } /* Endif */
         /* Fall through the A_IDLE state */
      case A_IDLE:
         _usb_otg_process_a_idle((pointer)usb_otg_ptr);
         break;
      
      case A_WAIT_VRISE:
         _usb_otg_process_a_wait_vrise((pointer)usb_otg_ptr);
         break;
      
      case A_WAIT_BCON:
         /*
         ** After waiting long enough to insure that the D+ line cannot 
         ** be high due to the residual effect of the A-device pull-up, 
         ** (see Section 5.1.9), the A-device sees that the D+ line 
         ** is high (and D- low) indicating that the B-device is 
         ** signaling a connect and is ready to respond as a Peripheral. 
         ** At this point, the A-device becomes Host and asserts bus 
         ** reset to start using the bus.
         */
         if (s_ptr->B_CONN) {
            s_ptr->B_BUS_SUSPEND = FALSE;
            s_ptr->STATE = A_HOST;
            TA_WAIT_BCON_TMR_OFF(s_ptr);
            /* 
            ** If the B device is not supported by A device 
            ** a _bus_req = FALSE will set by the application  
            ** during initial communication with the device
            */
         } /* Endif */
         break;
      
      case A_HOST:
         /*
         ** If mini B plug is removed RESET ( DETACH) interrupt will 
         ** happen on A-Device.The DETACH will processed by the host 
         ** ISR  and set b_conn to  FALSE 
         */
         if (!s_ptr->B_CONN) {
            /* switch to  a_wait_bcon */
            s_ptr->STATE = A_WAIT_BCON;
            TA_WAIT_BCON_TMR_OFF(s_ptr);
            break;
         } /* Endif */
      
         /************   Starting HNP **************************************
         ** When the A-device is in the a_host state and has set the dual-role 
         ** B-device's HNP enable bit (b_hnp_enable = TRUE ie, a_suspend_req
         ** = TRUE) the A-device shall place the connection to the B-device into 
         ** Suspend when it is finished using the bus.  We can go to the 
         ** suspend state also if user want to power down.
         */
         if (!s_ptr->A_BUS_REQ || s_ptr->A_BUS_SUSPEND_REQ) {
            s_ptr->STATE = A_SUSPEND;
            TA_AIDL_BDIS_TMR_ON(s_ptr,TA_AIDL_BDIS); 
            /* CALL the Host API function to suspend the bus */
            _usb_host_bus_control(usb_host_state_struct_ptr, USB_SUSPEND_SOF);
         } /* Endif */   
         break;
      
      case A_SUSPEND:
         /* b_conn = FALSE if a RESET interrupt happens and it will set by
         ** the application; a_set_b_hnp_en is false as default and set to
         ** if the request is not stalled
         */
         if (!s_ptr->B_CONN && !s_ptr->A_SET_B_HNP_EN) {
            TA_AIDL_BDIS_TMR_OFF(s_ptr);
            s_ptr->B_BUS_RESUME = FALSE;
            /* switch to  a_wait_bcon */
            s_ptr->STATE = A_WAIT_BCON;
            TA_WAIT_BCON_TMR_ON(s_ptr, TA_WAIT_BCON);
            break;
         } /* Endif */
         
         /*
         ** If the B-device disconnects after the bus has been suspended, 
         ** then this is an indication that the B-device is attempting 
         ** to become Host. When the A-device detects the disconnect 
         ** from the B-device, it shall turn on its D+ pull-up resistor 
         ** within 3 ms (TA_BDIS_ACON max) to acknowledge
         ** the request from the B-device.
         */
         if (!s_ptr->B_CONN && s_ptr->A_SET_B_HNP_EN) {
            PULL_UP_PULL_DOWN_IDLE(otg_reg);
            if (s_ptr->HOST_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_DOWN); 
            } /* Endif */
            if (!s_ptr->DEVICE_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_UP); 
            } /* Endif */
            PULL_UP_PULL_DOWN_DEVICE_CTRL(otg_reg);
            s_ptr->STATE = A_PERIPHERAL;
            TA_AIDL_BDIS_TMR_OFF(s_ptr);
            s_ptr->A_SET_B_HNP_EN = FALSE ;
            s_ptr->A_BUS_SUSPEND_REQ = FALSE;
            break;
         } /* Endif */
         
         if (s_ptr->A_BUS_REQ || s_ptr->B_BUS_RESUME) {
            s_ptr->STATE = A_HOST;
            s_ptr->B_BUS_RESUME = FALSE;
            TA_AIDL_BDIS_TMR_OFF(s_ptr);
         } /* Endif */
         break;
      
      case A_PERIPHERAL:
         /*
         ** A-device detects lack of bus activity for more than 3 ms 
         ** (TA_BIDL_ADIS min) and turns off its D+ pull-up.Alternatively, 
         ** if the A-device has no further need to communicate with the 
         ** B-device, the A-device may turn off VBUS and end the session.
         ** Sleep INT will be processed by the  device ISR b_bus_suspend
         ** variable will be set by the application
         ** The following will be done when this happens:
         **   
         **   - disconnect its pull up
         **   - allow time for the data line to discharge
         **   - check if the B-device has connected its pull up
         */
      
         if (s_ptr->B_BUS_SUSPEND) {
            if (s_ptr->DEVICE_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_DOWN);
               PULL_UP_PULL_DOWN_IDLE(otg_reg);
            } /* Endif */
            if (!s_ptr->HOST_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_UP); 
               PULL_UP_PULL_DOWN_HOST_CTRL(otg_reg);
            } /* Endif */
            /* switch to  a_wait_bcon */
            s_ptr->STATE = A_WAIT_BCON;
#ifndef  TA_WAIT_BCON_TMR_FOR_EVER
            TA_WAIT_BCON_TMR_ON(s_ptr, TA_WAIT_BCON);
#endif         
         } /* Endif */  
         break;
      
      case A_WAIT_VFALL:
         _usb_otg_process_a_wait_vfall(usb_otg_ptr);
         break;
      
      case B_IDLE:
        _usb_otg_process_b_idle((pointer)usb_otg_ptr);
         break;
      
      case B_SRP_INIT:
         _usb_otg_process_b_srp_init((pointer)usb_otg_ptr);
         break;
      
      case B_PERIPHERAL:
            
         /* a_bus_suspend ( SLEEP_INT) will be processed by the DEVICE ISR */
         /*
         ** B-device detects that bus is idle for more than 3 ms 
         ** (TB_AIDL_BDIS min) and begins HNP by turning off pull-up on D+. 
         ** This allows the bus to discharge to the SE0 state. 
         ** If the bus was operating in HS mode, the B-device will first 
         ** enter the full-speed mode and turn on its D+ pull-up resistor 
         ** for at least TB_FS_BDIS min before turning off its pull up 
         ** to start the HNP sequence.
         ** The following will be done when this happens:
         **   
         **   disconnect its pull up
         **   allow time for the data line to discharge
         **   check if the A-device has connected its pull up
         */
         if (s_ptr->B_HNP_ENABLE && s_ptr->B_BUS_REQ && 
            s_ptr->A_BUS_SUSPEND) 
         {
            if (s_ptr->DEVICE_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_DOWN);
               PULL_UP_PULL_DOWN_IDLE(otg_reg);
            } /* Endif */
            
            if (!s_ptr->HOST_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_UP); 
               /* No VBUS_ON */
               PULL_UP_PULL_DOWN_HOST_CTRL(otg_reg);
            } /* Endif */
            s_ptr->STATE = B_WAIT_ACON;
            TB_ASE0_BRST_TMR_ON(s_ptr, TB_ASE0_BRST);
         } /* Endif */
         break;
      
      case B_WAIT_ACON:
         /*
         ** After waiting long enough to insure that the D+ line 
         ** cannot be high due to the residual effect of the B-device 
         ** pull-up,(see Section 5.1.9), the B-device sees that the 
         ** D+ line is high and D- low, (i.e. J state). This indicates 
         ** that the A-device has recognized the HNP request from the 
         ** B-device. At this point, the B-device becomes Host and 
         ** asserts bus reset to start using the bus. The B-device 
         ** must assert the bus reset (SE0) within 1 ms (TB_ACON_BSE0 max) 
         ** of the time that the A-device turns on its
         ** pull-up.
         */
         /* ATTACH_INT will be processed by the host ISR */
         if (s_ptr->A_CONN) {
            s_ptr->A_BUS_SUSPEND = FALSE;
            s_ptr->B_HNP_ENABLE = FALSE;
            TB_ASE0_BRST_TMR_OFF(s_ptr);
            s_ptr->JSTATE = FALSE;
            s_ptr->STATE = B_HOST;
            
      	   break;
         } /* Endif */
         
         /*
         ** While waiting in the b_wait_acon state, the B-device 
         ** may detect a K state on the bus. This indicates that the 
         ** A-device is signaling a resume condition and is retaining 
         ** control of the bus. In this case, the B-device will return 
         ** to the b_peripheral state.
         */
         /* 
         ** a_bus_resume will be set by the host ISR when K state is 
         ** detected  if (a_bus_resume || b_ase0_brst_tmr) 
         */
         if (s_ptr->A_BUS_RESUME || (TB_ASE0_BRST_TMR_EXPIRED(s_ptr) && 
            s_ptr->JSTATE)) 
         {
            s_ptr->JSTATE = FALSE;
            TB_ASE0_BRST_TMR_OFF(s_ptr);
            s_ptr->A_BUS_RESUME = FALSE;
            if (s_ptr->HOST_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_DOWN); 
               PULL_UP_PULL_DOWN_IDLE(otg_reg);
            } /* Endif */
            if (!s_ptr->DEVICE_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_UP); 
               PULL_UP_PULL_DOWN_DEVICE_CTRL(otg_reg);
            } /* Endif */
            s_ptr->STATE = B_PERIPHERAL;
            s_ptr->A_BUS_SUSPEND = FALSE;
            s_ptr->B_HNP_ENABLE = FALSE;
            break;
         } /* Endif */
         
         if (TB_ASE0_BRST_TMR_EXPIRED(s_ptr)) {
            TB_ASE0_BRST_TMR_OFF(s_ptr);
         } /* Endif */
         break;
      
      case B_HOST:
         /* 
         ** If the B-device at any time detects more than 3.125 ms of SE0 
         ** (TB_ASE0_BRST min), then this is an indication that the 
         ** A-device is remaining Host and is resetting the bus. In this 
         ** case the B-device shall return to the b_peripheral state 
         ** and start to process the bus reset before TB_ASE0_BRST max.
         */
         if (!s_ptr->B_BUS_REQ || !s_ptr->A_CONN) {
            if (s_ptr->HOST_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_DOWN); 
            } /* Endif */
            PULL_UP_PULL_DOWN_IDLE(otg_reg);
            if (!s_ptr->DEVICE_UP) {
               usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_UP); 
            } /* Endif */
            PULL_UP_PULL_DOWN_DEVICE_CTRL(otg_reg);  
            s_ptr->STATE = B_PERIPHERAL;
         } /* Endif */
         break;
      default:
         break;
   } /* Endswitch */   
#endif  /* PERIPHERAL-ONLY */  
   
   if ((s_ptr->TA_WAIT_VRISE_TMR > 0) ||
       (s_ptr->TA_WAIT_BCON_TMR > 0)  ||
       (s_ptr->TA_AIDL_BDIS_TMR > 0)  ||
       (s_ptr->TB_DATA_PLS_TMR > 0)   ||
       (s_ptr->TB_SRP_INIT_TMR > 0)   ||
       (s_ptr->TB_SRP_FAIL_TMR > 0)   ||
       (s_ptr->TB_ASE0_BRST_TMR > 0)  ||
       (s_ptr->TB_SE0_SRP_TMR > 0)    ||
       (s_ptr->A_SRP_TMR > 0)) 
   {
      START_TIMER(otg_reg); 
   } else {
      STOP_TIMER(otg_reg);
   } /* Endif */
   
#ifdef OTG_LOG_STATE
   if (state != s_ptr->STATE) {
      usb_otg_ptr->LOG_STATE[usb_otg_ptr->LOG_STATE_COUNT++] = 
            usb_otg_ptr->STATE_STRUCT.STATE;
      if (usb_otg_ptr->LOG_STATE_COUNT>=99) {
         usb_otg_ptr->LOG_STATE_COUNT = 0;
      } /* Endif */
   }
#endif 

} /* EndBody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_reset_state_machine
* Returned Value : TRUE or FALSE
* Comments       :
*     Reset the state machine to its orginal state.
*END*--------------------------------------------------------------------*/
void  _usb_otg_reset_state_machine
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR   usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;

   USB_memzero((uchar_ptr)(&s_ptr->OTG_INT_STATUS), 
      sizeof(OTG_STATE_MACHINE_STRUCT) - 
      (11*sizeof(uint_32) + sizeof(uchar)));

   s_ptr->TA_WAIT_VRISE_TMR = -1;
   s_ptr->TA_WAIT_BCON_TMR = -1;
   s_ptr->TA_AIDL_BDIS_TMR = -1;
   s_ptr->TB_DATA_PLS_TMR = -1;
   s_ptr->TB_SRP_INIT_TMR = -1;
   s_ptr->TB_SRP_FAIL_TMR = -1;
   s_ptr->TB_ASE0_BRST_TMR = -1;
   s_ptr->TB_SE0_SRP_TMR = -1;
   s_ptr->A_SRP_TMR = -1;
   
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_exceptions
* Returned Value : TRUE or FALSE
* Comments       :
*     process the  exception signals and switch  state and return TRUE.
*END*--------------------------------------------------------------------*/
boolean  _usb_otg_process_exceptions
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   uint_8                        previous_state;
   uint_8                        state;
   uint_32                       otg_int_status;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR;
   state = s_ptr->STATE;
   otg_int_status = s_ptr->OTG_INT_STATUS;

   /* Check exception for A device */
   previous_state = state;
   
   if ((state < B_IDLE)) {
      /* 
      ** Exceptions are as follows.
      ** 1. Transition to B_IDLE when id = TRUE.
      ** 2. Transition to A_WAIT_VFALL when V_BUS is voltage is fallen below 
      ** 3. 4 VDC or not able to set the V_BUS voltage.
      ** 4. If a_wait_b_con_timout  is set.
      ** 5. If  a_aidl_bdis_tmout  is set.
      */ 
      
      if ( (ID_CHG(otg_int_status) && ID(otg_reg))|| 
           ( (A_VBUS_CHG(otg_int_status) && A_VBUS_VLD_FALSE(otg_reg)) ) ||
           ( TA_WAIT_VRISE_TMR_EXPIRED(s_ptr)) || 
           ( TA_WAIT_BCON_TMR_EXPIRED(s_ptr)) ||  
           ( TA_AIDL_BDIS_TMR_EXPIRED(s_ptr)))
      {
#ifndef PERIPHERAL_ONLY      
            if ((state == A_IDLE) && (ID_CHG(otg_int_status) && ID(otg_reg))) {
               s_ptr->STATE = B_IDLE;
               _usb_otg_process_b_idle((pointer)usb_otg_ptr);
            } else if ((state != A_IDLE) && (state != A_WAIT_VFALL)) {
               if ((TA_WAIT_VRISE_TMR_EXPIRED(s_ptr)) || 
                  (A_VBUS_CHG(otg_int_status) && A_VBUS_VLD_FALSE(otg_reg)))
               {
                  s_ptr->A_BUS_DROP = TRUE;
                  usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, 
                     USB_OTG_OVER_CURRENT); 
               } /* Endif */
               s_ptr->STATE = A_WAIT_VFALL;
               VBUS_CHG_OFF(otg_reg);
               VBUS_OFF(otg_reg);
               _usb_otg_process_a_wait_vfall(usb_otg_ptr);
            } /* Endif */
#endif            
      } /* Endif */      	
   } else {
      /*
      ** Process B- device exceptions
      */
      if ((ID_CHG(otg_int_status) && ID_FALSE(otg_reg)) ||
         (SESS_VLD_CHG(otg_int_status) && SESS_VLD_FALSE(otg_reg))) 
      {
         VBUS_CHG_OFF(otg_reg);
         VBUS_OFF(otg_reg);
         if (ID(otg_reg) ) {
            s_ptr->STATE = B_IDLE;
            _usb_otg_process_b_idle((pointer)usb_otg_ptr);
         } else {
#ifndef PERIPHERAL_ONLY
            s_ptr->STATE = A_IDLE;
            /* A unique case where we need to set this to TRUE */
            s_ptr->A_BUS_REQ = TRUE;
            _usb_otg_process_a_idle((pointer)usb_otg_ptr);
#endif            
         } /* Endif */
      } /* endif */
   } /* Endif */
   if (s_ptr->STATE != previous_state){
     return (TRUE);
   } else {
     return (FALSE);
   }
} /* EndBody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_b_idle
* Returned Value : None
* Comments       :
*     Process B_IDLE state.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_b_idle
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   int_32				            srp_tmr;
   boolean                       se0_srp;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR; 
   
   srp_tmr = s_ptr->TB_SE0_SRP_TMR;
   se0_srp = s_ptr->B_SE0_SRP;
   _usb_otg_reset_state_machine(usb_otg_ptr);
   s_ptr->TB_SE0_SRP_TMR = srp_tmr;
   s_ptr->B_SE0_SRP = se0_srp;
   
   if (s_ptr->HOST_UP) {
      usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_DOWN);
      PULL_UP_PULL_DOWN_IDLE(otg_reg);
   } /* Endif */
    
   /*
   ** When a B-device detects that the voltage on VBUS is greater 
   ** than the B-Device Session Valid threshold (VSESS_VLD), 
   ** then the B-device shall consider a session to be in progress. 
   ** After the VBUS voltage crosses this threshold, the B-device 
   ** shall assert either the D+ or D- data-line within 100 ms.
   */
   if (SESS_VLD(otg_reg)) {
      _usb_otg_process_b_device_session_valid(handle);
   } else {
      /*  ********* Starting  SRP *****************************************
      ** When the B-device detects that VBUS has gone below its Session End 
      ** threshold and detects that both D+ and D- have been low (SE0) for 
      ** at least 2 ms (TB_SE0_SRP min), then any previous session on the 
      ** A-device is over and a new session may start. The B-device may 
      ** initiate the SRP any time the initial conditions of Section 5.3.2 
      ** are met.
      */
      /* activate the timer only if we want to do SRP */
      if ((s_ptr->B_BUS_REQ || s_ptr->B_SESS_REQ) && 
         (CHECK_TB_SE0_SRP_TMR_OFF(s_ptr))) 
      {
         TB_SE0_SRP_TMR_ON(s_ptr, TB_SE0_SRP);
      } /* Endif */
     
      if ((s_ptr->B_BUS_REQ || s_ptr->B_SESS_REQ) && (B_SESS_END(otg_reg)) && 
         (s_ptr->B_SE0_SRP)) 
      {
         TB_SE0_SRP_TMR_OFF(s_ptr);
         s_ptr->B_SE0_SRP = FALSE;
         /*
         ** initial conditions are met as described above and then turns 
         ** on its data line pull-up resistor (either D+ or D-) for a 
         ** period of 5 ms to 10 ms (TB_DATA_PLS). A dual-role B-device is 
         ** only allowed to initiate SRP at full-speed, and thus shall 
         ** only pull up D+. The duration of such a data line pulse is 
         ** sufficient to allow the A-device to reject spurious voltage 
         ** transients on the data lines.
         */
         DP_HIGH_ON(otg_reg);  
         TB_SRP_FAIL_TMR_ON(s_ptr, TB_SRP_FAIL);
         TB_SRP_INIT_TMR_ON(s_ptr, TB_SRP_INIT);
         TB_DATA_PLS_TMR_ON(s_ptr, TB_DATA_PLS);
         s_ptr->STATE = B_SRP_INIT;
         s_ptr->B_SRP_DONE = FALSE;
         usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_SRP_ACTIVE); 
      } /* Endf */
   } /* Endif */	
} /* EndBody */

#ifndef PERIPHERAL_ONLY
/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_a_idle
* Returned Value : None
* Comments       :
*     Process B_IDLE state.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_a_idle
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   boolean                       a_srp_det;
   int_32                        a_srp_tmr;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR; 
   
   a_srp_det = s_ptr->A_SRP_DET;
   a_srp_tmr = s_ptr->A_SRP_TMR;
   _usb_otg_reset_state_machine(usb_otg_ptr);
   s_ptr->A_SRP_DET = a_srp_det;
   s_ptr->A_SRP_TMR = a_srp_tmr;
   
   if (ID(otg_reg)) {
      s_ptr->STATE = B_IDLE;
      _usb_otg_process_b_idle((pointer)usb_otg_ptr);
      return ;
   } /* Endif */

   if (s_ptr->DEVICE_UP) {
      usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_DOWN);
      PULL_UP_PULL_DOWN_IDLE(otg_reg);
   } /* Endif */
   
   if (!s_ptr->HOST_UP) {
      usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_HOST_UP);
      PULL_UP_PULL_DOWN_HOST_CTRL(otg_reg);
   } /* Endif */
   
   if ((!s_ptr->A_BUS_REQ) && (s_ptr->A_SRP_TMR == -1) && 
      (s_ptr->A_DATA_PULSE_DET)) 
   {
      A_SRP_TMR_ON(s_ptr, TB_DATA_PLS_MIN);
   } /* Endif */
   
   /* ************  SRP detection ****************************
   ** The A-device continuously monitors VBUS as long as power is 
   ** available on the A-device. An A-device that is designed to 
   ** detect the VBUS pulsing method will detect that VBUS has 
   ** gone above the A-device Session Valid threshold (VA_SESS_VLD) 
   ** and generate an indication that SRP has been detected.
   */
   if ((!s_ptr->A_BUS_DROP) && (SESS_VLD(otg_reg) || s_ptr->A_SRP_DET || 
      s_ptr->A_BUS_REQ))
   {
      s_ptr->STATE = A_WAIT_VRISE;
      VBUS_ON(otg_reg);
      TA_WAIT_VRISE_TMR_ON(s_ptr, TA_WAIT_VRISE);
      _usb_otg_process_a_wait_vrise((pointer)usb_otg_ptr);
   } /* Endif */
} /* EndBody */


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_a_wait_vrise
* Returned Value : None
* Comments       :
*     Process A_WAIT_VRISE state.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_a_wait_vrise
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR;
   
   if (A_VBUS_VLD(otg_reg)) {
      TA_WAIT_VRISE_TMR_OFF(s_ptr);
      /* switch to  a_wait_bcon */
      s_ptr->STATE = A_WAIT_BCON;
#ifndef TA_WAIT_BCON_TMR_FOR_EVER
      TA_WAIT_BCON_TMR_ON(s_ptr, TA_WAIT_BCON);
#endif
   } /* Endif */
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_a_wait_vfall
* Returned Value : None
* Comments       :
*     Process A_WAIT_VFALL state.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_a_wait_vfall
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   uint_32                       hw_signal;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR;
   hw_signal = s_ptr->OTG_INT_STATUS;
   
   if ((SESS_VLD_CHG(hw_signal)) && (SESS_VLD_FALSE(otg_reg))) {
      if ( (ID(otg_reg)) ||
         (s_ptr->A_BUS_REQ) || 
         (!s_ptr->B_CONN && SESS_VLD_FALSE(otg_reg)))
      {
         s_ptr->STATE = A_IDLE;
         VBUS_CHG_OFF(otg_reg);
         VBUS_OFF(otg_reg);
         _usb_otg_process_a_idle((pointer)usb_otg_ptr);
      } /* Endif */
   } /* Endif */
} /* EndBody */

#endif

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_b_device_session_valid
* Returned Value : None
* Comments       :
*     Process B device session valid.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_b_device_session_valid
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR;
   
   if (s_ptr->STATE == B_SRP_INIT) {
      TB_SRP_FAIL_TMR_OFF(s_ptr);
      TB_SRP_INIT_TMR_OFF(s_ptr);
      TB_DATA_PLS_TMR_OFF(s_ptr);
      TB_SE0_SRP_TMR_OFF(s_ptr);
   } /* Endif */
   
   s_ptr->STATE = B_PERIPHERAL;
   
   VBUS_CHG_OFF(otg_reg);
   
   if (!s_ptr->DEVICE_UP) {
      usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_DEVICE_UP); 
      PULL_UP_PULL_DOWN_DEVICE_CTRL(otg_reg);
   } /* Endif */

} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_b_srp_init
* Returned Value : None
* Comments       :
*     Process B_SRP_INIT state.
* 
*END*--------------------------------------------------------------------*/
void _usb_otg_process_b_srp_init
   (
      /* [IN] The OTG handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   OTG_STRUCT_PTR                otg_reg;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR; 
   
   /*
   ** When a B-device detects that the voltage on VBUS is greater than the 
   ** B-Device Session Valid threshold (VSESS_VLD), then the B-device 
   ** shall consider a session to be in progress. After the VBUS voltage
   ** crosses this threshold, the B-device shall assert either the D+ or 
   ** D- data-line within 100 ms(TB_SVLD_BCON max).
   */
   if (SESS_VLD(otg_reg)) {
      _usb_otg_process_b_device_session_valid(handle);
   } else {
      /* check_srp_activity */
      if (TB_DATA_PLS_TMR_EXPIRED(s_ptr))
      {
         TB_DATA_PLS_TMR_OFF(s_ptr);
         DP_HIGH_OFF(otg_reg);
         /* An A-device is only required to respond to one of the two 
         ** SRP signaling methods. A B-device shall use both methods 
         ** when initiating SRP to insure that the A-device responds.
         */
         VBUS_CHG_ON(otg_reg);
      } /*Endif */
   
      if (TB_SRP_INIT_TMR_EXPIRED(s_ptr))
      {
         TB_SRP_INIT_TMR_OFF(s_ptr);
         VBUS_CHG_OFF(otg_reg);
      } /* Endif */
      
      /*
      ** The error call back have the following requirement:
      ** After initiating SRP, the B-device is required to wait at least 
      ** 5 seconds (TB_TB_SRP_FAIL min) for the A-device to respond, 
      ** before informing the user that the communication attempt 
      ** has failed.
      **
        */
      if (TB_SRP_FAIL_TMR_EXPIRED(s_ptr)){
         TB_SRP_FAIL_TMR_OFF(s_ptr);
         s_ptr->B_SRP_DONE = FALSE;
         s_ptr->STATE = B_IDLE;
         VBUS_CHG_OFF(otg_reg);
         DP_HIGH_OFF(otg_reg);
         usb_otg_ptr->SERVICE((pointer)usb_otg_ptr, USB_OTG_SRP_FAIL); 
         _usb_otg_process_b_idle((pointer)usb_otg_ptr);
      } /* Endif */
   } /* Endif */
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_otg_process_timers
* Returned Value : TRUE/ FALSE
* Comments       :
*     If any timers expired, the state machine will be executed and returns
*     TRUE, otherwise it will return FALSE.
* 
*END*--------------------------------------------------------------------*/
boolean _usb_otg_process_timers
   (
      /* [IN]  OTG device handle */
      _usb_otg_handle   handle
   )
{ /* Body */
   USB_OTG_STATE_STRUCT_PTR      usb_otg_ptr;
   OTG_STATE_MACHINE_STRUCT_PTR  s_ptr;
   USB_STRUCT_PTR                usb_reg;
   OTG_STRUCT_PTR                otg_reg;
   boolean                       timer_expired = FALSE;
   
   usb_otg_ptr = (USB_OTG_STATE_STRUCT_PTR)handle;
   s_ptr = &usb_otg_ptr->STATE_STRUCT;
   otg_reg = usb_otg_ptr->OTG_REG_PTR; 
   usb_reg = usb_otg_ptr->USB_REG_PTR; 

   /* 
   ** Process all the debouncing timers first
   */
   if (s_ptr->TB_SE0_SRP_TMR > 0) {
      
      if (SE0(usb_reg) && 
         (STABLE_J_SE0_LINE_STATE(otg_reg))) 
      {
         /* signal is constant for 1 ms, so decrement counter */
         s_ptr->TB_SE0_SRP_TMR--;
      } else {
        /* random behaviour; Reset the counter */
        TB_SE0_SRP_TMR_ON(s_ptr, TB_SE0_SRP);
      } /* Endif */
      
      if (TB_SE0_SRP_TMR_EXPIRED(s_ptr)) {
         s_ptr->B_SE0_SRP = TRUE;
         timer_expired = TRUE;
      } /* Endif */
   } /* Endif */          

   /* Process all the A timers */
	if (s_ptr->TA_WAIT_VRISE_TMR > 0) {
	   s_ptr->TA_WAIT_VRISE_TMR--;
	   if (TA_WAIT_VRISE_TMR_EXPIRED(s_ptr)) {
	      timer_expired = TRUE;
	   } /* Endif */
	} /* Endif */

	if (s_ptr->TA_WAIT_BCON_TMR > 0) {
	   s_ptr->TA_WAIT_BCON_TMR--;
	   if (TA_WAIT_BCON_TMR_EXPIRED(s_ptr)) {
	      timer_expired = TRUE;
	   } /* Endif */
	} /* Endif */

	if (s_ptr->TA_AIDL_BDIS_TMR > 0) {
	   s_ptr->TA_AIDL_BDIS_TMR--;
	   if (TA_AIDL_BDIS_TMR_EXPIRED(s_ptr)) {
	      timer_expired = TRUE;
	   } /* Endif */
	} /* Endif */

   /* process all the B timers */
	if (s_ptr->TB_DATA_PLS_TMR > 0) {
	   s_ptr->TB_DATA_PLS_TMR--;
	   if (TB_DATA_PLS_TMR_EXPIRED(s_ptr)) {
	      timer_expired = TRUE;
	   } /* Endif */
	} /* Endif */
	
   if (s_ptr->TB_SRP_INIT_TMR > 0) {
	   s_ptr->TB_SRP_INIT_TMR--;
	   if (TB_SRP_INIT_TMR_EXPIRED(s_ptr)) {
	      timer_expired = TRUE;
	   } /* Endif */
	} /* Endif */
   
   if (s_ptr->TB_SRP_FAIL_TMR > 0) {
      s_ptr->TB_SRP_FAIL_TMR--;
      if (TB_SRP_FAIL_TMR_EXPIRED(s_ptr)) {
         timer_expired = TRUE;
      } /* Endif */
   } /* Endif */
   
   if (s_ptr->TB_ASE0_BRST_TMR > 0) {
      s_ptr->TB_ASE0_BRST_TMR--;
      if (TB_ASE0_BRST_TMR_EXPIRED(s_ptr)) {
         timer_expired = TRUE;
      } /* Endif */
   } /* Endif */
   
   if (s_ptr->A_SRP_TMR > 0) {
      s_ptr->A_SRP_TMR--;
      if (A_SRP_TMR_EXPIRED(s_ptr)) {
         s_ptr->A_SRP_TMR = -1;
         s_ptr->A_DATA_PULSE_DET = FALSE;
         timer_expired = TRUE;
         if (JSTATE(usb_reg) && 
            (STABLE_J_SE0_LINE_STATE(otg_reg))) 
         {
            s_ptr->A_SRP_DET = TRUE;
         } /* Endif */
      } /* Endif */
   } /* Endif */
   
   if (timer_expired) {
      _usb_otg_state_machine((pointer)usb_otg_ptr);
   } /* Endif */
   return timer_expired;
} /* EndBody */

/* EOF */