demo_h.c 33.3 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
/*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:demo_h.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:00 $
***
*** Description:      
***   This file contains the host specific code for the demo
***                                                               
**************************************************************************
*END*********************************************************************/
#ifndef __USB_OS_MQX__
   #include "types.h"
   #include "otgapi.h"
   #include "usb.h"
   #include "devapi.h"
   #include "hostapi.h"
   #include "demo.h"
   #include "demo_d.h"
   #include "test_h.h"
   
   #include "stdio.h"
   #include "stdlib.h"
   #include "string.h"
#else
   #include "mqx.h"
   #include "bsp.h"
   #include "devapi.h"
   #include "hostapi.h"
   #include "demo.h"
#endif

volatile APP_GLOBAL_STRUCT app_test_struct;
extern _usb_otg_handle     app_otg_handle;
extern volatile boolean    HOST_REGISTER_SERVICES;
extern volatile boolean TEST_MASS_STORAGE_ENABLED, DEVICE_RETURNED_STALL = FALSE;
extern _usb_device_handle  dev_handle;

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetAddress
* Returned Value : None
* Comments       :
*     Chapter 9 SetAddress command
*     See section 9.4.6 (page 192) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetAddress_host
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{ /* Body */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      setup_ptr->WLENGTH, 0);
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetDescription
* Returned Value : None
* Comments       :
*     Chapter 9 GetDescription command
*     See section 9.4.3 (page 189) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9GetDescription_host
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{ /* Body */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      setup_ptr->WLENGTH, 0);
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetConfig
* Returned Value : None
* Comments       :
*     Chapter 9 SetConfig command
*     See section 9.4.7 (page 193) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetConfig_host
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{ /* Body */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      setup_ptr->WLENGTH, 0);
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetFeature_host
* Returned Value : None
* Comments       :
*     Chapter 9 SetFeature command
* 
*END*--------------------------------------------------------------------*/
void ch9SetFeature_host
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{ /* Body */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      setup_ptr->WLENGTH, 0);
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9Vendor
* Returned Value : 
* Comments       :
*     Chapter 9 Vendor specific command
* 
*END*--------------------------------------------------------------------*/
void ch9Vendor_host
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{ /* Body */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      setup_ptr->WLENGTH, 0);
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_default_pipe
* Returned Value : 
* Comments       :
*     Callback function to process completed transactions on the default pipe
* 
*END*--------------------------------------------------------------------*/
void app_process_default_pipe
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */

   if (!tx_set_address.WVALUE) {
      /* Enumerate */
      tx_set_address.WVALUE++;
      ch9SetAddress_host(handle, app_test_struct.DEFAULT_PIPE, &tx_set_address, 
         (uchar_ptr)&app_test_struct.RX_TEST_BUFFER);
   } else {
      app_test_struct.USB_CURRENT_ADDRESS = tx_set_address.WVALUE;
      app_test_struct.ENUMERATION_DONE = TRUE;
   } /* Endif */   
   
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_attach
* Returned Value : 
* Comments       :
*     Callback function to process the device attach event
* 
*END*--------------------------------------------------------------------*/
void app_process_attach
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */
   uint_32 state;
   
   app_test_struct.DEVICE_DETACHED = FALSE;
   
   if (TEST_MASS_STORAGE_ENABLED) {
      _usb_device_cancel_transfer(dev_handle, 1, USB_RECV);
      _usb_device_cancel_transfer(dev_handle, 1, USB_SEND);
      TEST_MASS_STORAGE_ENABLED = FALSE;
   } /* Endif */
   
   _usb_otg_get_status(app_otg_handle,USB_OTG_STATE,&state);
   
   if (state < B_IDLE) {
      if (app_test_struct.DO_SRP) {
         _usb_otg_get_status(app_otg_handle, USB_OTG_B_BUS_REQ, &state);
         if (state) {
            _usb_otg_set_status(app_otg_handle, USB_OTG_B_BUS_REQ, FALSE);
         } /* Endif */
      } /* Endif */
      _usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_REQ, TRUE);
      _usb_otg_set_status(app_otg_handle, USB_OTG_B_CONN, TRUE);
   } else {
      _usb_otg_set_status(app_otg_handle, USB_OTG_A_CONN, TRUE);
   } /* Endif */
   
   app_test_struct.DEFAULT_PIPE = _usb_host_open_pipe(handle, 0, 0, USB_RECV, 0, 
      USB_CONTROL_PIPE, 64, 0, 0);

   _usb_host_register_service(handle, app_test_struct.DEFAULT_PIPE, 
      app_process_default_pipe);
   
   /* Get first 8 bytes of the device descriptor */
   ch9GetDescription_host(handle, app_test_struct.DEFAULT_PIPE, &tx_get_desc, 
      (uchar_ptr)&app_test_struct.RX_DESC_DEVICE);
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_detach
* Returned Value : 
* Comments       :
*     Callback function to process the device detach event
* 
*END*--------------------------------------------------------------------*/
void app_process_detach
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */
   uint_32  state;

   _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
   
   if (state < B_IDLE) {
      _usb_otg_get_status(app_otg_handle, USB_OTG_B_CONN, &state);
      if (state) {
         _usb_otg_set_status(app_otg_handle, USB_OTG_B_CONN, FALSE);
      } /* Endif */
   } else {
      _usb_otg_get_status(app_otg_handle, USB_OTG_A_CONN, &state);
      if (state) {
         _usb_otg_set_status(app_otg_handle, USB_OTG_A_CONN, FALSE);
      } /* Endif */
   } /* Endif */
   
   /* Cancel all transfers */
   
   if (state) {
      _usb_host_cancel_transfer(handle, app_test_struct.DEFAULT_PIPE);
      _usb_host_cancel_transfer(handle, app_test_struct.APP_PIPE1);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_INT_IN);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_INT_OUT);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_ISO_IN);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_ISO_OUT);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_BULK_IN);
      _usb_host_cancel_transfer(handle, app_test_struct.PIPE_BULK_OUT);
   
      _usb_host_close_all_pipes(handle);
      app_test_struct.ENUMERATION_DONE = FALSE;
      app_test_struct.DEVICE_DETACHED = TRUE;
      tx_set_address.WVALUE = 0;
      app_test_struct.USB_CURRENT_ADDRESS = 0;
   } /* Endif */   
   
   if (app_test_struct.DO_SRP) {
      _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
      if (state >= B_IDLE) {
         _usb_otg_get_status(app_otg_handle, USB_OTG_B_BUS_REQ, &state);
         if (state) {
            _usb_otg_set_status(app_otg_handle, USB_OTG_B_BUS_REQ, FALSE);
         } /* Endif */
      } /* Endif */
   } /* Endif */
   
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_host_resume
* Returned Value : 
* Comments       :
*     Callback function to process the remote-wakeup/K-Attach event
* 
*END*--------------------------------------------------------------------*/
void app_process_host_resume
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */
   uint_32  state;

   _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
   
   if (state < B_IDLE) {
      _usb_otg_set_status(app_otg_handle, USB_OTG_B_BUS_RESUME, TRUE);
   } else {
      _usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_RESUME, TRUE);
   } /* Endif */
   
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_packet_stall
* Returned Value : 
* Comments       :
*     Callback function to take action upon STALL from the device
* 
*END*--------------------------------------------------------------------*/
void app_process_packet_stall
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */
   DEVICE_RETURNED_STALL = TRUE;
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_HNP
* Returned Value : 
* Comments       :
*     function to process the HNP request
* 
*END*--------------------------------------------------------------------*/
void app_process_HNP
   (
      _usb_host_handle  handle
   )
{ /* Body */
   uint_32 state, i, total_len;
   boolean  otg_des_found = FALSE;
   
   _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
   
   if (state < B_IDLE) {
   
      app_test_struct.TRANSACTION_COMPLETE = FALSE;
   
      /* Get the configuration descriptor */   
      ch9GetDescription_host(handle, app_test_struct.APP_PIPE1, 
         &tx_get_config_desc, (uchar_ptr)app_test_struct.RX_CONFIG_DESC);
   
      while (!app_test_struct.TRANSACTION_COMPLETE) {
         /* Wait until transaction is complete */
         if (app_test_struct.DEVICE_DETACHED) {
            return;
         } /* Endif */
      } /* EndWhile */
      
      /* Find out the length of the transfer */
      
      app_test_struct.TRANSACTION_COMPLETE = FALSE;
      
      /* Find out the length of the transfer */
      total_len = app_test_struct.RX_CONFIG_DESC[2];
      total_len |= app_test_struct.RX_CONFIG_DESC[3] << 8;
      for (i=0;i < total_len;) {
         /* checking for OTG descriptor type */
         if (app_test_struct.RX_CONFIG_DESC[i+1] == 
            USB_OTG_DESCRIPTOR_TYPE) 
         {
            otg_des_found = TRUE;
            break;
         } else {
            i = i + app_test_struct.RX_CONFIG_DESC[i];
         } /* Endif */
      } /* Endfor */
      
      /* Check if HNP enabled for device */
      if ((otg_des_found) && ((app_test_struct.RX_CONFIG_DESC[i+2]) & 0x02)) {
         ch9SetFeature_host(handle, app_test_struct.APP_PIPE1, 
            &tx_set_feature, NULL);
         
         while (!app_test_struct.TRANSACTION_COMPLETE) {
            /* Wait until transaction is complete */
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */
         _usb_otg_set_status(app_otg_handle, USB_OTG_A_SET_B_HNP_ENABLE, TRUE);
         _usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_SUSPEND_REQ, TRUE);
      } /* Endif */
   } /* Endif */ 
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : app_process_pipe_transaction_done
* Returned Value : 
* Comments       :
*     Callback function to process the transaction done event
* 
*END*--------------------------------------------------------------------*/
void app_process_pipe_transaction_done
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{ /* Body */
   app_test_struct.TRANSACTION_COMPLETE = TRUE;
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : main
* Returned Value : 
* Comments       :
*     The echo test function
* 
*END*--------------------------------------------------------------------*/
void host_main
   (
      _usb_host_handle  handle
   ) 
{
   int_16               i;
   uint_8               error;
   uint_32              state = 0, loop_count = 0, vendor_id, total_len;
   char                 c;
   
   while(!app_test_struct.DEVICE_DETACHED) {
      if (!app_test_struct.ENUMERATION_DONE) {

         while (!app_test_struct.ENUMERATION_DONE) {
            /* Wait until enumeration is done */
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */

         if (app_test_struct.ENUMERATION_DONE) {
         
            app_test_struct.APP_PIPE1 = _usb_host_open_pipe(handle, 
               app_test_struct.USB_CURRENT_ADDRESS, 0, USB_RECV, 0, 
               USB_CONTROL_PIPE, app_test_struct.RX_DESC_DEVICE.BMAXPACKETSIZE, 
               0, 0);

            if (HOST_REGISTER_SERVICES) {
               error = _usb_host_register_service(handle, app_test_struct.APP_PIPE1, 
                  app_process_pipe_transaction_done);
         
               if (error != USB_OK) {
                  printf("\nUSB Service Registration failed. Error: %x", error);
                  fflush(stdout);
               } /* Endif */
            } /* Endif */
            
            app_test_struct.TRANSACTION_COMPLETE = FALSE;
            
            /* Insert Start HNP code */
         
            _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
            
            if (state == A_HOST) {
               printf("\nWould you like to start HNP (y/n)?:");
               fflush(stdout);
               c = getchar();
               fflush(stdin);
               if ((c == 'y') || (c == 'Y')) {
                  app_test_struct.DO_HNP = TRUE;
                  app_test_struct.DO_SRP = FALSE;
               } else {
                  app_test_struct.DO_HNP = FALSE;
               } /* Endif */
#ifdef   HOSTLINK_IO
               c = getchar();
               fflush(stdin);
#endif               
            } /* Endif */
            
            /* HNP activation code  starts here */         
            if (app_test_struct.DO_HNP) {
               app_process_HNP(handle);
            } /* Endif */
            /* HNP activation code  ends here */         

            /* Get the full device descriptor */   
            ch9GetDescription_host(handle, app_test_struct.APP_PIPE1, &tx_get_desc, 
               (uchar_ptr)&app_test_struct.RX_DESC_DEVICE);

            while (!app_test_struct.TRANSACTION_COMPLETE) {
               /* Wait until transaction is complete */
               if (app_test_struct.DEVICE_DETACHED) {
                  return;
               } /* Endif */
            } /* EndWhile */
            
            
            vendor_id = app_test_struct.RX_DESC_DEVICE.IDVENDOR;
            
            if (vendor_id != ARC_VENDOR_ID) {
            
                  printf("\nDevice enumeration complete"); 
                  printf("\nVendor ID: %x", vendor_id);
                  fflush(stdout);
               
                  app_test_struct.TRANSACTION_COMPLETE = FALSE;
                  
                  if (app_test_struct.RX_DESC_DEVICE.BMANUFACTURER) {
                     /* Get the Manufacturer's string */
                     tx_get_string_desc.WVALUE |= 
                        app_test_struct.RX_DESC_DEVICE.BMANUFACTURER;
                
                     tx_get_string_desc.WLENGTH = 0xFF;

                     ch9GetDescription_host(handle, app_test_struct.APP_PIPE1, 
                        &tx_get_string_desc, 
                        (uchar_ptr)app_test_struct.RX_TEST_BUFFER);

                     while (!app_test_struct.TRANSACTION_COMPLETE) {
                        /* Wait until transaction is complete */
                        if (app_test_struct.DEVICE_DETACHED) {
                           return;
                        } /* Endif */
                     
                        if (DEVICE_RETURNED_STALL) {
                           _usb_host_cancel_transfer(handle, app_test_struct.APP_PIPE1);
                           DEVICE_RETURNED_STALL = FALSE;
                           printf("\nDevice does not support Get Manufacturer String command");
                           fflush(stdout);
                           break;
                        } /* Endif */
                     } /* EndWhile */
                

                     total_len = (app_test_struct.RX_TEST_BUFFER[0] & 0x00FF);
                
                     if (total_len) {
                        printf("\nThe Device's Manufacturer string:");
                
                        for (i=2;i<total_len;i++) {
                           printf("%c", app_test_struct.RX_TEST_BUFFER[i]);
                           /* Unicode is 2 bytes */
                           i++;
                        } /* Endfor */
                     } /* Endif */
                  } else {
                     printf("\nManufacturer string index is zero");
                  } /* Endif */
                  
                  if (app_test_struct.RX_DESC_DEVICE.BPRODUCT) {
                     /* Get the Product string */
                     app_test_struct.TRANSACTION_COMPLETE = FALSE;
                
                     tx_get_string_desc.WVALUE |= 
                        app_test_struct.RX_DESC_DEVICE.BPRODUCT;
                
                     tx_get_string_desc.WLENGTH = 0xFF;

                     ch9GetDescription_host(handle, app_test_struct.APP_PIPE1, 
                        &tx_get_string_desc, 
                        (uchar_ptr)app_test_struct.RX_TEST_BUFFER);

                     while (!app_test_struct.TRANSACTION_COMPLETE) {
                        /* Wait until transaction is complete */
                        if (app_test_struct.DEVICE_DETACHED) {
                           return;
                        } /* Endif */
                     
                        if (DEVICE_RETURNED_STALL) {
                           _usb_host_cancel_transfer(handle, app_test_struct.APP_PIPE1);
                           DEVICE_RETURNED_STALL = FALSE;
                           printf("\nDevice does not support Get Product String command");
                           fflush(stdout);
                           break;
                        } /* Endif */
                     } /* EndWhile */
                

                     total_len = (app_test_struct.RX_TEST_BUFFER[0] & 0x00FF);
                
                     if (total_len) {
                        printf("\nThe Device's Product string:");
                
                        for (i=2;i<total_len;i++) {
                           printf("%c", app_test_struct.RX_TEST_BUFFER[i]);
                           /* Unicode is 2 bytes */
                           i++;
                        } /* Endfor */
                     } /* Endif */
                  } else {
                     printf("\nProduct string index is zero");
                  } /* Endif */
                  
                  if (app_test_struct.RX_DESC_DEVICE.BSERIALNUMBER) {
                     /* Get the Serial number string */
                     app_test_struct.TRANSACTION_COMPLETE = FALSE;
                
                     tx_get_string_desc.WVALUE |= 
                        app_test_struct.RX_DESC_DEVICE.BSERIALNUMBER;
                
                     tx_get_string_desc.WLENGTH = 0xFF;

                     ch9GetDescription_host(handle, app_test_struct.APP_PIPE1, 
                        &tx_get_string_desc, 
                      (uchar_ptr)app_test_struct.RX_TEST_BUFFER);

                     while (!app_test_struct.TRANSACTION_COMPLETE) {
                        /* Wait until transaction is complete */
                        if (app_test_struct.DEVICE_DETACHED) {
                           return;
                        } /* Endif */
                     
                        if (DEVICE_RETURNED_STALL) {
                           _usb_host_cancel_transfer(handle, app_test_struct.APP_PIPE1);
                           DEVICE_RETURNED_STALL = FALSE;
                           printf("\nDevice does not support Get Serial Number String command");
                           fflush(stdout);
                           break;
                        } /* Endif */
                     } /* EndWhile */
                

                     total_len = (app_test_struct.RX_TEST_BUFFER[0] & 0x00FF);
                
                     if (total_len) {
                        printf("\nThe Device's Serial number:");
                
                        for (i=2;i<total_len;i++) {
                           printf("%c", app_test_struct.RX_TEST_BUFFER[i]);
                           /* Unicode is 2 bytes */
                           i++;
                        } /* Endfor */
                     } /* Endif */
                  } else {
                     printf("\nSerial number string index is zero");
                  } /* Endif */
                  
                  printf("\nThe device driver is not installed");
                  fflush(stdout);
                
                  while (!app_test_struct.DEVICE_DETACHED) {
                  } /* Endwhile */
                
                  return;
            } /* Endif */
            
            app_test_struct.TRANSACTION_COMPLETE = FALSE;

            /* Set new configuration */
            ch9SetConfig_host(handle, app_test_struct.APP_PIPE1, &tx_set_config, 
               (uchar_ptr)app_test_struct.RX_TEST_BUFFER);

            while (!app_test_struct.TRANSACTION_COMPLETE) {
               /* Wait until transaction is complete */
               if (app_test_struct.DEVICE_DETACHED) {
                  return;
               } /* Endif */
            } /* EndWhile */

            app_test_struct.TRANSACTION_COMPLETE = FALSE;
         } /* Endif */
         
         /* Enable endpoint 1 by using Vendor specific request */
         tx_enable_echo.WVALUE = BIDIRECTIONAL;
         tx_enable_echo.WINDEX = 1;
         
         ch9Vendor_host(handle, app_test_struct.APP_PIPE1, &tx_enable_echo, 
               (uchar_ptr)app_test_struct.RX_TEST_BUFFER);
               
         while (!app_test_struct.TRANSACTION_COMPLETE) {
            /* Wait until transaction is complete */
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */
         
         app_test_struct.TRANSACTION_COMPLETE = FALSE;
         
         
         tx_enable_echo.WINDEX = 2;

         ch9Vendor_host(handle, app_test_struct.APP_PIPE1, &tx_enable_echo, 
               (uchar_ptr)app_test_struct.RX_TEST_BUFFER);
  
         while (!app_test_struct.TRANSACTION_COMPLETE) {
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */
         
         app_test_struct.TRANSACTION_COMPLETE = FALSE;

         tx_enable_echo.WINDEX = 3;

         ch9Vendor_host(handle, app_test_struct.APP_PIPE1, &tx_enable_echo, 
               (uchar_ptr)app_test_struct.RX_TEST_BUFFER);
  
         while (!app_test_struct.TRANSACTION_COMPLETE) {
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */
         
         app_test_struct.TRANSACTION_COMPLETE = FALSE;
         
         app_test_struct.PIPE_INT_OUT = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 2, USB_SEND, 0x00, 
            USB_INTERRUPT_PIPE, 64, 0, 0);
         app_test_struct.PIPE_INT_IN  = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 2, USB_RECV, 0x00, 
            USB_INTERRUPT_PIPE, 64, 0, 0);
         app_test_struct.PIPE_BULK_OUT = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 1, USB_SEND, 0x00, 
            USB_BULK_PIPE, 64, 0, 0);
         app_test_struct.PIPE_BULK_IN  = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 1, USB_RECV, 0x00, 
            USB_BULK_PIPE, 64, 0, 0);
         app_test_struct.PIPE_ISO_OUT = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 3, USB_SEND, 0x00, 
            USB_ISOCHRONOUS_PIPE, 1023, 0, 0);
         app_test_struct.PIPE_ISO_IN  = _usb_host_open_pipe(handle, 
            app_test_struct.USB_CURRENT_ADDRESS, 3, USB_RECV, 0x00, 
            USB_ISOCHRONOUS_PIPE, 1023, 0, 0);
            
         if (HOST_REGISTER_SERVICES) {
      
            error = _usb_host_register_service(handle, app_test_struct.PIPE_INT_OUT, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */      
      
            error = _usb_host_register_service(handle, app_test_struct.PIPE_INT_IN, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */      
      
            error = _usb_host_register_service(handle, app_test_struct.PIPE_BULK_OUT, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */      
            
            error = _usb_host_register_service(handle, app_test_struct.PIPE_BULK_IN, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */      
      
            error = _usb_host_register_service(handle, app_test_struct.PIPE_ISO_OUT, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */      
            
            error = _usb_host_register_service(handle, app_test_struct.PIPE_ISO_IN, 
               app_process_pipe_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
               fflush(stdout);
            } /* Endif */
            HOST_REGISTER_SERVICES = FALSE;
         } /* Endif */
         
         printf("\nEcho Test Start");
         fflush(stdout);
      } /* Endif */
      
      if (app_test_struct.ENUMERATION_DONE) {

         for (i=0; i<DATA_SIZE; i++) {
            app_test_struct.INTDATAOUT[i] = (uchar)i;
            app_test_struct.INTDATAIN[i] = 0;
            app_test_struct.BULKDATAOUT[i] = (uchar)i;
            app_test_struct.BULKDATAIN[i] = 0;
            app_test_struct.ISODATAOUT[i] = (uchar)i;
            app_test_struct.ISODATAIN[i] = 0;
         } /* Endfor */

         _usb_host_send_data(handle, app_test_struct.PIPE_ISO_OUT, 
            (uchar_ptr)app_test_struct.ISODATAOUT, 64);
      
         _usb_host_send_data(handle, app_test_struct.PIPE_INT_OUT, 
            (uchar_ptr)app_test_struct.INTDATAOUT, DATA_SIZE);      

         _usb_host_send_data(handle, app_test_struct.PIPE_BULK_OUT, 
            (uchar_ptr)app_test_struct.BULKDATAOUT, DATA_SIZE);
            
            
         while ((_usb_host_get_transfer_status(handle, 
            app_test_struct.PIPE_BULK_OUT)) != USB_STATUS_IDLE) 
         {
            /* Wait until transaction is complete */
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */
      
         _usb_host_recv_data(handle, app_test_struct.PIPE_ISO_IN, 
            (uchar_ptr)app_test_struct.ISODATAIN, 64);
      
         _usb_host_recv_data(handle, app_test_struct.PIPE_INT_IN, 
            (uchar_ptr)app_test_struct.INTDATAIN, DATA_SIZE);      
            
         _usb_host_recv_data(handle, app_test_struct.PIPE_BULK_IN, 
            (uchar_ptr)app_test_struct.BULKDATAIN, DATA_SIZE);
            
         while ((_usb_host_get_transfer_status(handle, 
            app_test_struct.PIPE_BULK_IN)) != USB_STATUS_IDLE) 
         {
            if (app_test_struct.DEVICE_DETACHED) {
               return;
            } /* Endif */
         } /* EndWhile */

         for (i=0; ((i<DATA_SIZE) && (!app_test_struct.DEVICE_DETACHED)); i++) {
         
            if ((app_test_struct.INTDATAOUT[i] != 
               app_test_struct.INTDATAIN[i]) || 
               (app_test_struct.BULKDATAOUT[i] != 
               app_test_struct.BULKDATAIN[i])) 
            {
               printf("\nEcho Test Failed");
               fflush(stdout);
               return;
            } /* Endif */
         } /* Endfor */
         
         putchar('.');
         fflush(stdout);
         loop_count++;
         if (loop_count == 100) {
            loop_count = 0;
            printf("\nAre you Done being host (y/n)?:");
            fflush(stdout);
            c = getchar();
            fflush(stdin);
            if ((c == 'y') || (c == 'Y')) {
#ifdef   HOSTLINK_IO
               c = getchar();
               fflush(stdin);
#endif         
               _usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
               
               if (state >= B_IDLE) {
                  _usb_otg_set_status(app_otg_handle, USB_OTG_B_BUS_REQ, FALSE);
               } /* Endif */      
               
               app_test_struct.DO_SRP = FALSE;
               if (state == A_HOST) {
                  printf("\nWould you like to start HNP (y/n)?:");
                  fflush(stdout);
                  c = getchar();
                  fflush(stdin);
                  if ((c == 'y') || (c == 'Y')) {
#ifdef   HOSTLINK_IO
                     c = getchar();
                     fflush(stdin);
#endif                     
                     app_test_struct.DO_HNP = TRUE;
                     app_process_HNP(handle);
                     return;
                  } else {
#ifdef   HOSTLINK_IO
                     c = getchar();
                     fflush(stdin);
#endif                     
                     app_test_struct.DO_HNP = FALSE;
                  } /* Endif */
               } /* Endif */
            } else {
#ifdef   HOSTLINK_IO
               c = getchar();
               fflush(stdin);
#endif               
            } /* Endif */
         } /* Endif */
      } /* Endif */
   } /* EndWhile */
} /* Endbody */

/* EOF */