audio.c 34.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 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
/*HEADER******************************************************************
**************************************************************************
*** 
*** Copyright (c) Precise Software Technologies Incorporated
*** All rights reserved
***
*** This software is provided under license and contains proprietary
*** and confidential material which is the property of VAutomation Inc.
*** HTTP://www.vautomation.com
***
*** $Workfile: audio.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:00 $
***
*** Description:      
***  This file contains the USB 1.1 Chapter 9 example application with
***  an Audio Device example.
***                                                               
***
**************************************************************************
*END*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "usbd.h"
#include "devapi.h"
#include "audio.h"

volatile boolean AUDIO_TEST_ENABLED = FALSE;
volatile AUDIO_DATA_RECEIVED = FALSE;
volatile uint_32 bytes_received = 0;

#define DEV_DESC_MAX_PACKET_SIZE    (7)
#define  APP_CONTROL_MAX_PKT_SIZE   (8)
#define TEST_SIZE                   (48)

/* Device descriptors are always 18 bytes */
uint_8  DevDesc[18] =
{
   /* Length of DevDesc */
   sizeof(DevDesc),
   /* "Device" Type of descriptor */
   1,
   /* BCD USB version */
   USB_uint_16_low(0x0110), USB_uint_16_high(0x0110),
   /* Device Class */
   0x00,
   /* Device Subclass */
   0x00,
   /* Device Protocol */
   0x00,
   /* Max packet size */
   APP_CONTROL_MAX_PKT_SIZE,
   /* Vendor ID */
   USB_uint_16_low(0x05A3), USB_uint_16_high(0x05A3),
   /* Product ID */
   USB_uint_16_low(0x1), USB_uint_16_high(0x1),
   /* BCD Device version */
   USB_uint_16_low(0x0001), USB_uint_16_high(0x0001),
   /* Manufacturer string index */
   0x1,
   /* Product string index */
   0x2,
   /* Serial number string index */
   0,
   /* Number of configurations available */
   0x1
};

#define CONFIG_DESC_NUM_INTERFACES  (4)
/* This must be counted manually and updated with the descriptor */
#define  CONFIG_DESC_SIZE           (0x86)
#define  USB_NUM_IFACES             (0x02)
#define  ENDPOINT_SPEAKER           (0x01)
#define  IFC1_ALT0_MAX              (0x0000)
#define  IFC1_ALT1_MAX              (0x0030)
#define  IFC1_ALT0_MAX_LO           (0x00)
#define  IFC1_ALT0_MAX_HI           (0x00)
#define  IFC1_ALT1_MAX_HI           (0x00)
#define  IFC1_ALT1_MAX_LO           (0x30)

uint_8 ConfigDesc[CONFIG_DESC_SIZE] =
{
   /* This table is retrieved by the host after the address has been set.
   ** This table defines the configurations available for the device.
   ** See section 9.6.2 of the Rev 1.0 USB specification (page 184).
   ** These fields are application DEPENDENT. 
   ** Be sure to modify these to meet your specifications.
   */
   9,      /* bLength   Length of this descriptor */
   2,      /* bDescType   2=CONFIGURATION */

   CONFIG_DESC_SIZE & 0xff, CONFIG_DESC_SIZE >> 8,
   USB_NUM_IFACES,      /* bNumInterfaces   Number of interfaces */

   /* USB_CONFIG_VAL: */
   0x01,   /* bConfigValue   Configuration Value */
   0x04,   /* iConfig   String Index for this config = #01 */
   0xc0,   /* bmAttributes   attributes - self powered */
   0,      /* MaxPower   self-powered draws 0 mA from the bus. */
   /*   IIIII  FFFFF   CCCC   000 
   **     I    F      C      0   0
   **     I    FFF    C      0   0
   **     I    F      C      0   0
   **   IIIII  F       CCCC   000 
   */

   /* AUDIO CONTROL: See section 9.6.3, Rev 1.0 USB spec (page 185) */

   0x09,   /* blength   Length of this desriptor */
   4,      /* bDescriptorType   4=INTERFACE */
   0,      /* bIfaceNum   interface number */
   0,      /* bAltSetting   Alternate setting 0 (no others) */
   0,      /* bNumEndpoints   Number of endpoints= use EP0 only */
   0x01,   /* bIfaceClass   Interface class 01=AUDIO */
   0x01,   /* bIfaceSubClass   Audio COntrol */
   0x00,   /* bIfaceProtocol   none */
   0,      /* iInterface */

   /* USB_AC_IFACE: Audio Class Specific Audio Control (AC) I/F desc */
   0x09,      /* bLength */
   0x24,      /* Audio class specific interface descriptor */
   0x01,      /* HEADER type */
   0x09,0x00, /* spec rev = 0009 */
   0x1e,0x00, /* total length=001e */
   0x01,      /* number of streaming interfaces=1 */
   0x01,      /* Interface number=1 */

   0x0c,      /* bLength */
   0x24,      /* Audio class specific interface desc */
   0x02,      /* Input Terminal */
   0x01,      /* TerminalID */
   0x01,0x01, /* TerminalType=0101=Audio Streaming */
   0x00,      /* output AssocTerminal */
   0x02,      /* Number of Channels=2 */
   0x03,0x00, /* wChannelConfig=L,R */
   0x00,      /* iChannelNames=none */
   0x00,      /* iTerminal */

   0x09,      /* bLength */
   0x24,      /* Type=Audio class specific interface desc */
   0x03,      /* Output Terminal */
   0x02,      /* TerminalID */
   0x01,0x03, /* TerminalType=0301=Generic Speakers */
   0x00,      /* bAssocTerminal=terminalID connect to=0? */
   0x01,      /* bSourceID=TerminalID which is the source for this 
              ** terminal=feature unit 
              */
   0x00,      /* iTerminal=string */

   /*   IIIII  FFFFF   CCCC    1  
   **     I    F      C       11  
   **     I    FFF    C        1  
   **     I    F      C        1  
   **   IIIII  F       CCCC  11111
   */

   /* AUDIO STREAM See section 9.6.3, Rev 1.0 USB spec (page 185) */

   /*       AAA   L      TTTTT   000 
   **      A   A  L        T    0   0
   **      AAAAA  L        T    0   0
   **      A   A  L        T    0   0
   **      A   A  LLLLL    T     000 
   */

   0x09,   /* blength   Length of this desriptor */
   4,      /* bDescriptorType   4=INTERFACE */
   1,      /* bIfaceNum   interface number #1 */
   0,      /* bAltSetting   Alternate setting 0 Zero ISO Bandwidth 
           ** version which is required by Win98 
           */
   1,      /* bNumEndpoints   Number of endpoints= use EP0 only */
   0x01,   /* bIfaceClass   Interface class 01=AUDIO */
   0x02,   /* bIfaceSubClass   Audio Streaming */
   0x00,   /* bIfaceProtocol   none */
   4,      /* iInterface   String=#01 */

   0x07,   /* bLength */
   0x24,   /* Audio Class Specific AS Interface Desc. */
   0x01,   /* AS_GENERAL */
   0x01,   /* TermLink connected to terminal #1 */
   0x00,   /* Delay=0 */
   0x00,0x01,   /* FormatTag=PCM */

   0x0b,   /* bLength   Format Specification */
   0x24,   /* Audio Class Specific AS Interface Desc. */
   0x02,   /* FORMAT_TYPE */
   0x01,   /* formatType */
   0x02,   /* NrChannels=2 */
   0x02,   /* SubFrameSize=2 byte */
   16,     /* BitResolution=16 bits */
   0x01,   /* SamFreqType=1 type */
   0x00,0x00, /* ZERO BANDWIDTH */
   0x00,      /* SamFreq MSB (it's a 24 bit number in HZ) */

   9,         /* bLength   Length of this descriptor */
   5,         /* bDescType   5=ENDPOINT */
   ENDPOINT_SPEAKER,   /* bEndpntAddr   End Point address (EP2=out) */
   0x09,      /* bmAttr   ISO adaptive rate */
   IFC1_ALT0_MAX_LO,IFC1_ALT0_MAX_HI, /* wMaxPktSize   Max Packet Size */
   1,         /* bInterval */
   0,         /* bRefresh */
   0,         /* SyncAddr   None */

   /* USB_AS_DESC1: Class Specific Audio AS EP descriptor */
   7,         /* bLength   Length of this descriptor */
   0x25,      /* bDescType   Class specific ENDPOINT */
   1,         /* SubType   GENERAL */
   0,         /* bmAttr   none */
   2,         /* bDelayUnits   PCM samples */
   0x01,0x00, /* bLockDelay   1 sample delay */

   /*       AAA   L      TTTTT    1  
   **      A   A  L        T     11  
   **      AAAAA  L        T      1  
   **      A   A  L        T      1  
   **      A   A  LLLLL    T    11111
   */

   0x09,      /* blength   Length of this desriptor */
   4,         /* bDescriptorType   4=INTERFACE */
   1,         /* bIfaceNum   interface number #1 */
   1,         /* bAltSetting   Alternate setting #1 */
   1,         /* bNumEndpoints   Number of endpoints=1 */
   0x01,      /* bIfaceClass   Interface class 01=AUDIO */
   0x02,      /* bIfaceSubClass   Subclass 02=AUDIO_STREAMING */
   0x00,      /* bIfaceProtocol   protocol */
   05,        /* iInterface   String Index */

   /* USB_AS_IFACE: Audio Class Specific Audio Streaming (AS) Interface 
   ** desc 
   */
   0x07,      /* bLength */
   0x24,      /* Audio Class Specific AS Interface Desc. */
   0x01,      /* AS_GENERAL */
   0x01,      /* TermLink connected to terminal #1 */
   0x00,      /* Delay=0 */
   0x01,0x00, /* FormatTag=PCM */

   0x0b,      /* bLength Format Specification */
   0x24,      /* Audio Class Specific AS Interface Desc. */
   0x02,      /* FORMAT_TYPE */
   0x01,      /* formatType */
   0x02,      /* NrChannels=2 */
   0x02,      /* SubFrameSize=2 byte */
   16,        /* BitResolution=16 bits */
   0x01,      /* SamFreqType=1 type */
   0x11,0x2b, /*  SamFreq=11.025Khz */
   0x00,      /* SamFreq MSB (it's a 24 bit number in HZ) */

   /* USB_EP_DESC1:  table for the USB ENDPOINT Descriptor */
   /* See section 9.6.4 of the Rev 1.0 USB spec (page 187) */
   9,                /* bLength   Length of this descriptor */
   5,                /* bDescType   5=ENDPOINT */
   ENDPOINT_SPEAKER, /* bEndpntAddr   End Point address (EP2=out) */
   0x09,             /* bmAttr   ISO adaptive rate */
   IFC1_ALT1_MAX_LO,IFC1_ALT1_MAX_HI, /* wMaxPktSize   Max Packet Size */
   1,         /* bInterval  */
   0,         /* bRefresh */
   0,         /* SyncAddr   None */

   /* USB_AS_DESC1: Class Specific Audio AS EP descriptor */
   7,         /* bLength   Length of this descriptor */
   0x25,      /* bDescType   Class specific ENDPOINT */
   1,         /* SubType   GENERAL */
   0,         /* bmAttr   none */
   2,         /* bDelayUnits   PCM samples */
   0x01,0x00  /* bLockDelay   1 sample delay */
};

uchar USB_IF_ALT[4] = { 0, 0, 0, 0};

/* number of strings in the table not including 0 or n. */
const uint_8 USB_STR_NUM = 6;

/*
** if the number of strings changes, look for USB_STR_0 everywhere and make 
** the obvious changes.  It should be found in 3 places.
*/

uint_16 USB_STR_0[2] = {0x300 + sizeof(USB_STR_0),0x0409};
uint_16 USB_STR_1[16] = {0x300 + sizeof(USB_STR_1),
      'V','A','u','t','o','m','a','t','i','o','n',' ','I','n','c'};
uint_16 USB_STR_2[29] = {0x300 + sizeof(USB_STR_2),
      'V','U','S','B',' ','S','y','n','t','h','e','s','i','z','a','b','l','e',' ',\
      'C','o','r','e',' ','D','e','m','o'};
uint_16 USB_STR_3[ 5] = {0x300 + sizeof(USB_STR_3),
      'B','E','T','A'};
uint_16 USB_STR_4[ 4] = {0x300 + sizeof(USB_STR_4),
      '#','0','2'};
uint_16 USB_STR_5[ 4] = {0x300 + sizeof(USB_STR_5),
      '_','A','1'};
uint_16 USB_STR_6[15] = {0x300 + sizeof(USB_STR_6),
      'Y','o','u','r',' ','n','a','m','e',' ','h','e','r','e'};
uint_16 USB_STR_n[17] = {0x300 + sizeof(USB_STR_n),
      'B','A','D',' ','S','T','R','I','N','G',' ','I','n','d','e','x'};

const uint_8_ptr USB_STRING_DESC[USB_STR_NUM+2] =
{
   (uint_8_ptr)USB_STR_0,
   (uint_8_ptr)USB_STR_1,
   (uint_8_ptr)USB_STR_2,
   (uint_8_ptr)USB_STR_3,
   (uint_8_ptr)USB_STR_4,
   (uint_8_ptr)USB_STR_5,
   (uint_8_ptr)USB_STR_6,
   (uint_8_ptr)USB_STR_n
};

uint_16        usb_status;
uint_8         endpoint, if_status;
uint_8         data_to_send;
uint_16        sof_count;
uint_8         setup_packet[sizeof(SETUP_STRUCT)];
SETUP_STRUCT   local_setup_packet;

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetStatus
* Returned Value : None
* Comments       :
*     Chapter 9 GetStatus command
*     wValue=Zero
*     wIndex=Zero
*     wLength=1
*     DATA=bmERR_STAT
*     The GET_STATUS command is used to read the bmERR_STAT register.
*     
*     Return the status based on the bRrequestType bits:
*     device (0) = bit 0 = 1 = self powered
*                  bit 1 = 0 = DEVICE_REMOTE_WAKEUP which can be modified
*     with a SET_FEATURE/CLEAR_FEATURE command.
*     interface(1) = 0000.
*     endpoint(2) = bit 0 = stall.
*     static uint_8_ptr pData;
*
*     See section 9.4.5 (page 190) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9GetStatus
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   
   if (setup) {
      switch (setup_ptr->REQUESTTYPE) {

         case 0x80:
            /* Device request */
            _usb_device_get_status(handle, USB_STATUS_DEVICE_STATE, &usb_status);
            /* Send the requested data */
            _usb_device_send_data(handle, 0, (uchar_ptr)&usb_status, sizeof(usb_status));
            break;

         case 0x81:
            /* Interface request */
            if_status = USB_IF_ALT[setup_ptr->INDEX & 0x00FF];
            /* Send the requested data */
            _usb_device_send_data(handle, 0, &if_status, sizeof(if_status));
            break;
      
         case 0x82:
            /* Endpoint request */
            endpoint = setup_ptr->INDEX & USB_STATUS_ENDPOINT_NUMBER_MASK;
            _usb_device_get_status(handle,
               USB_STATUS_ENDPOINT | endpoint, &usb_status);
            /* Send the requested data */
            _usb_device_send_data(handle, 0, (uchar_ptr)&usb_status, sizeof(usb_status));      
            break;
         
         default:
            /* Unknown request */
            _usb_device_stall_endpoint(handle, 0, 0);
            return;

      } /* Endswitch */
      /* status phase */
      _usb_device_recv_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9ClearFeature
* Returned Value : None
* Comments       :
*     Chapter 9 ClearFeature command
*     See section 9.4.1 (page 188) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9ClearFeature
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   static uint_8           endpoint;
   uint_16                 usb_status;

   _usb_device_get_status(handle, USB_STATUS_DEVICE_STATE, &usb_status);

   if ((usb_status != USB_STATE_CONFIG) && (usb_status != USB_STATE_ADDRESS)) {
      _usb_device_stall_endpoint(handle, 0, 0);
      return;
   } /* Endif */

   if (setup) {
      switch (setup_ptr->REQUESTTYPE) {
      
         case 0:
            /* DEVICE */
            if (setup_ptr->VALUE == 1) {
               /* clear remote wakeup */
               _usb_device_get_status(handle, USB_STATUS_DEVICE, &usb_status);
               usb_status &= ~USB_REMOTE_WAKEUP;
               _usb_device_set_status(handle, USB_STATUS_DEVICE, usb_status);
            } /* Endif */
            break;
         
         case 2:
            if (setup_ptr->VALUE != 0) {
               _usb_device_stall_endpoint(handle, 0, 0);
               return;
            }
            
            endpoint = setup_ptr->INDEX & 0x0F;
            /* reset stall */
            _usb_device_set_status(handle, USB_STATUS_ENDPOINT | endpoint,
               0);
            break;
         
         default:
            _usb_device_stall_endpoint(handle, 0, 0);
            break;

      } /* Endswitch */
      /* status phase */
      _usb_device_send_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetFeature
* Returned Value : None
* Comments       :
*     Chapter 9 SetFeature command
*     See section 9.4.9 (page 194) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetFeature
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   uint_16           usb_status;
   uint_8            endpoint;

   if (setup) {
      switch (setup_ptr->REQUESTTYPE) {

         case 0:
            /* DEVICE */
            if (setup_ptr->VALUE == 1) {
               /* set remote wakeup */
               _usb_device_get_status(handle, USB_STATUS_DEVICE, &usb_status);
               usb_status |= USB_REMOTE_WAKEUP;
               _usb_device_set_status(handle, USB_STATUS_DEVICE, usb_status);
            } /* Endif */
            break;
            
         case 2:
            /* ENDPOINT */
            if (setup_ptr->VALUE != 0) {
               _usb_device_stall_endpoint(handle, 0, 0);
               return;
            } /* Endif */
            endpoint =         endpoint = setup_ptr->INDEX & USB_STATUS_ENDPOINT_NUMBER_MASK;
            _usb_device_get_status(handle, USB_STATUS_ENDPOINT | endpoint,
               &usb_status);
            /* set stall */
            _usb_device_set_status(handle, USB_STATUS_ENDPOINT | endpoint,
               1);
            break;

         default:
            _usb_device_stall_endpoint(handle, 0, 0);
            return;

      } /* Endswitch */
      
      /* status phase */
      _usb_device_send_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetAddress
* Returned Value : None
* Comments       :
*     Chapter 9 SetAddress command
*     We setup a TX packet of 0 length ready for the IN token
*     Once we get the TOK_DNE interrupt for the IN token, then
*     we change the ADDR register and go to the ADDRESS state.
*     See section 9.4.6 (page 192) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetAddress
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   static uint_8 new_address;

   if (setup) {
      new_address = setup_ptr->VALUE;
      /* ack */
      _usb_device_send_data(handle, 0, 0, 0);
   } else {
      _usb_device_set_status(handle, USB_STATUS_ADDRESS, new_address);
      _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
         USB_STATE_ADDRESS);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetDescription
* Returned Value : None
* Comments       :
*     Chapter 9 GetDescription command
*     The Device Request can ask for Device/Config/string/interface/endpoint
*     descriptors (via wValue). We then post an IN response to return the
*     requested descriptor.
*     And then wait for the OUT which terminates the control transfer.
*     See section 9.4.3 (page 189) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9GetDescription
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   if (setup) {
      /* Load the appropriate string depending on the descriptor requested.*/
      switch (setup_ptr->VALUE & 0xFF00) {

         case 0x0100:
            _usb_device_send_data(handle, 0, (uchar_ptr)&DevDesc,
               MIN(setup_ptr->LENGTH, sizeof(DevDesc)));
            break;

         case 0x0200:
            _usb_device_send_data(handle, 0, (uchar_ptr)&ConfigDesc,
               MIN(setup_ptr->LENGTH, sizeof(ConfigDesc)));
            break;

         case 0x0300:
            if ((setup_ptr->VALUE & 0x00FF) > USB_STR_NUM) {
               _usb_device_send_data(handle, 0, USB_STRING_DESC[USB_STR_NUM+1],
                  MIN(setup_ptr->LENGTH, USB_STRING_DESC[USB_STR_NUM+1][0]));
            } else {
               _usb_device_send_data(handle, 0,
                  USB_STRING_DESC[setup_ptr->VALUE & 0x00FF],
                  MIN(setup_ptr->LENGTH,
                     USB_STRING_DESC[setup_ptr->VALUE & 0x00FF][0]));
            } /* Endif */      
            break;

         default:
            _usb_device_stall_endpoint(handle, 0, 0);
            return;
      } /* Endswitch */
      _usb_device_recv_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetDescription
* Returned Value : None
* Comments       :
*     Chapter 9 SetDescription command
*     See section 9.4.8 (page 193) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetDescription
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   _usb_device_stall_endpoint(handle, 0, 0);
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetConfig
* Returned Value : None
* Comments       :
*     Chapter 9 GetConfig command
*     See section 9.4.2 (page 189) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9GetConfig
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   uint_16 current_config;
   /* Return the currently selected configuration */
   if (setup){ 
      _usb_device_get_status(handle, USB_STATUS_CURRENT_CONFIG,
         &current_config);
      data_to_send = current_config;      
      _usb_device_send_data(handle, 0, &data_to_send, sizeof(data_to_send));
      _usb_device_recv_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* 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
   (
   _usb_device_handle handle,
   boolean setup,
   SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   uint_16 usb_state;
   
   _usb_device_get_status(handle, USB_STATUS_DEVICE_STATE, &usb_state);
   
   if ((usb_state == USB_STATE_CONFIG) || (usb_state == USB_STATE_ADDRESS)) {
      if (setup) {
         /* 0 indicates return to unconfigured state */
         if ((setup_ptr->VALUE & 0x00FF) == 0) {
            /* clear the currently selected config value */
            _usb_device_set_status(handle, USB_STATUS_CURRENT_CONFIG, 0);
            _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
               USB_STATE_ADDRESS);
            _usb_device_send_data(handle, 0, 0, 0);
            return;
         } /* Endif */

         /*
         ** If the configuration value (setup_ptr->VALUE & 0x00FF) differs
         ** from the current configuration value, then endpoints must be
         ** reconfigured to match the new device configuration
         */
         _usb_device_get_status(handle, USB_STATUS_CURRENT_CONFIG,
            &usb_state);
         if (usb_state != (setup_ptr->VALUE & 0x00FF)) {
            /* Reconfigure endpoints here */
            switch (setup_ptr->VALUE & 0x00FF) {
         
               default:
                  break;
         
            } /* Endswitch */
            _usb_device_set_status(handle, USB_STATUS_CURRENT_CONFIG,
               setup_ptr->VALUE & 0x00FF);
            _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
            USB_STATE_CONFIG);      
            _usb_device_send_data(handle, 0, 0, 0);
            return;
         } /* Endif */
         _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
            USB_STATE_CONFIG);
         /* ack */
         _usb_device_send_data(handle, 0, 0, 0);
      } /* Endif */
   } else {
      _usb_device_stall_endpoint(handle, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetInterface
* Returned Value : None
* Comments       :
*     Chapter 9 GetInterface command
*     See section 9.4.4 (page 190) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9GetInterface
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   uint_16 usb_state;
   
   _usb_device_get_status(handle, USB_STATUS_DEVICE_STATE, &usb_state);
   if (usb_state != USB_STATE_CONFIG) {
      _usb_device_stall_endpoint(handle, 0, 0);
      return;
   } /* Endif */

   if (setup) {
      _usb_device_send_data(handle, 0, &USB_IF_ALT[setup_ptr->INDEX & 0x00FF],
         MIN(setup_ptr->LENGTH, sizeof(uint_8)));
      _usb_device_recv_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetInterface
* Returned Value : None
* Comments       :
*     Chapter 9 SetInterface command
*     See section 9.4.10 (page 195) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SetInterface
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   if (setup) {
      if (setup_ptr->REQUESTTYPE != 0x01) {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      } /* Endif */

      /*
      ** If the alternate value (setup_ptr->VALUE & 0x00FF) differs
      ** from the current alternate value for the specified interface,
      ** then endpoints must be reconfigured to match the new alternate
      */
      if (USB_IF_ALT[setup_ptr->INDEX & 0x00FF]
         != (setup_ptr->VALUE & 0x00FF))
      {
         USB_IF_ALT[setup_ptr->INDEX & 0x00FF] = (setup_ptr->VALUE & 0x00FF);
         /* Reconfigure endpoints here. */
         if (setup_ptr->VALUE & 0x00FF) {
            if (!AUDIO_TEST_ENABLED) {
               AUDIO_TEST_ENABLED = TRUE;
               _usb_device_init_endpoint(handle, ENDPOINT_SPEAKER, 
                  IFC1_ALT1_MAX_LO, USB_RECV, USB_ISOCHRONOUS_ENDPOINT);
            } /* Endif */      
         } /* Endif */
      } /* Endif */
      /* ack */
      _usb_device_send_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SynchFrame
* Returned Value : 
* Comments       :
*     Chapter 9 SynchFrame command
*     See section 9.4.11 (page 195) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void ch9SynchFrame
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{ /* Body */
   
   if (setup) {
      if (setup_ptr->REQUESTTYPE != 0x02) {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      } /* Endif */

      if ((setup_ptr->INDEX & 0x00FF) >=
         ConfigDesc[CONFIG_DESC_NUM_INTERFACES])
      {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      } /* Endif */

      _usb_device_get_status(handle, USB_STATUS_SOF_COUNT, &sof_count);
      _usb_device_send_data(handle, 0, (uchar_ptr)&sof_count,
         MIN(setup_ptr->LENGTH, sizeof(sof_count)));
      _usb_device_recv_data(handle, 0, 0, 0);
   } /* Endif */
   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : service_ep0
* Returned Value : None
* Comments       :
*     Called upon a completed endpoint 0 (USB 1.1 Chapter 9) transfer
* 
*END*--------------------------------------------------------------------*/
void service_ep0
   (
      /* [IN] Handle of the USB device */
      _usb_device_handle handle,
      
      /* [IN] Is it a setup packet? */
      boolean setup,
      
      /* [IN] Direction of the transfer.  Is it transmit? */
      uint_8 direction,
      
      /* [IN] Pointer to the data buffer */
      uint_8_ptr buffer,
      
      /* [IN] Length of the transfer */
      uint_16 length      
   )
{ /* Body */
   boolean  class_request = FALSE;
   
   if (setup) {
      local_setup_packet = *(SETUP_STRUCT_PTR)buffer;
   } else if (class_request) {
      class_request = FALSE;
      /* Finish your class or vendor request here */

      /* The next packet should be a standard setup packet */
      _usb_device_recv_data(handle, 0, setup_packet, sizeof(setup_packet));      
      return;
   } /* Endif */
   
   switch (local_setup_packet.REQUESTTYPE & 0x60) {

      case 0x00:
         switch (local_setup_packet.REQUEST) {

            case 0x0:
               ch9GetStatus(handle, setup, &local_setup_packet);
               break;

            case 0x1:
               ch9ClearFeature(handle, setup, &local_setup_packet);
               break;

            case 0x3:
               ch9SetFeature(handle, setup, &local_setup_packet);
               break;

            case 0x5:
               ch9SetAddress(handle, setup, &local_setup_packet);
               break;

            case 0x6:
               ch9GetDescription(handle, setup, &local_setup_packet);
               break;

            case 0x7:
               ch9SetDescription(handle, setup, &local_setup_packet);
               break;

            case 0x8:
               ch9GetConfig(handle, setup, &local_setup_packet);
               break;

            case 0x9:
               ch9SetConfig(handle, setup, &local_setup_packet);
               break;

            case 0xa:
               ch9GetInterface(handle, setup, &local_setup_packet);
               break;

            case 0xb:
               ch9SetInterface(handle, setup, &local_setup_packet);
               break;

            case 0xc:
               ch9SynchFrame(handle, setup, &local_setup_packet);
               break;

            default:
               _usb_device_stall_endpoint(handle, 0, 0);
               break;

         } /* Endswitch */
         
         break;

      case 0x20:
         /* class specific request */
         class_request = TRUE;
         /* Queue your own packet for class data here */
         return;

      case 0x40:
         /* vendor specific request */
         return;
      
      default:
         _usb_device_stall_endpoint(handle, 0, 0);
         break;
         
   } /* Endswitch */
   
   if (!setup) {
      _usb_device_recv_data(handle, 0, setup_packet, sizeof(setup_packet));
   } /* Endif */
   
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : service_ep1
* Returned Value : None
* Comments       :
*     Called upon a bus reset event.  Initialises the control endpoint.
* 
*END*--------------------------------------------------------------------*/
void service_ep1
   (
      /* [IN] Handle of the USB device */
      _usb_device_handle handle,
   
      /* [IN] Unused */
      boolean setup,
   
      /* [IN] Unused */
      uint_8 direction,
   
      /* [IN] Unused */
      uint_8_ptr buffer,
   
      /* [IN] Unused */
      uint_16 length      
   )
{ /* Body */
   bytes_received += length;
   return;
} /* EndBody */



/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : reset_ep0
* Returned Value : None
* Comments       :
*     Called upon a bus reset event.  Initialises the control endpoint.
* 
*END*--------------------------------------------------------------------*/
void reset_ep0
   (
      /* [IN] Handle of the USB device */
      _usb_device_handle handle,
   
      /* [IN] Unused */
      boolean setup,
   
      /* [IN] Unused */
      uint_8 direction,
   
      /* [IN] Unused */
      uint_8_ptr buffer,
   
      /* [IN] Unused */
      uint_16 length      
   )
{
   _usb_device_init_endpoint(handle, 0, DevDesc[DEV_DESC_MAX_PACKET_SIZE], 0,
      USB_CONTROL_ENDPOINT);
   _usb_device_init_endpoint(handle, 0, DevDesc[DEV_DESC_MAX_PACKET_SIZE], 1,
      USB_CONTROL_ENDPOINT);

   _usb_device_recv_data(handle, 0, setup_packet, sizeof(setup_packet));

  return;
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : main
* Returned Value : None
* Comments       :
*     First function called.  Initialises the USB and registers Chapter 9
*     callback functions.
* 
*END*--------------------------------------------------------------------*/
void main
   (
      void
   )
{ /* Body */
   _usb_device_handle   handle;
   uchar_ptr            ep1_buf;
   uint_32					i=0;
   
   _disable();
   _usb_device_init(0, &handle, 2);
   _usb_device_register_service(handle, USB_SERVICE_EP0, service_ep0);
   _usb_device_register_service(handle, USB_SERVICE_BUS_RESET, reset_ep0);
   _usb_device_register_service(handle, USB_SERVICE_EP1, service_ep1);
   _enable();
   
   printf("\nStart Audio Device Test\n");
   fflush(stdout);

   ep1_buf = (uchar_ptr)malloc(1000);
   if (ep1_buf == NULL) {
      printf("Buffer allocation failed for endpoint 1");
   } /* Endif */
   
   while (TRUE) {
         if (AUDIO_TEST_ENABLED) {
            
            if (_usb_device_get_transfer_status(handle, ENDPOINT_SPEAKER, USB_RECV) 
               == USB_OK) 
            {
               _usb_device_recv_data(handle, ENDPOINT_SPEAKER, ep1_buf, 
                  1000);
            } /* Endif */
            
            while (_usb_device_get_transfer_status(handle, ENDPOINT_SPEAKER, 
               USB_RECV) != USB_STATUS_IDLE) 
            {
               /* Wait until receive is complete */
            } /* Endwhile */
            
            i++;

            if (!(i%10)) {
               printf("\rAudio data received: %d bytes", bytes_received);
               fflush(stdout);
            } /* Endif */

         } /* Endif */    
         
   } /* Endwhile */
} /* Endbody */

/* EOF */