echo_d.c 25.7 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
/*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:echo_d.c$
*** $Revision: 1.13 $
*** $Date: 2004/10/08 22:32:43 $
***
*** Description:      
***  This file contains the VUSB32 device specific Echo example code.
***                                                               
**************************************************************************
*END*********************************************************************/

#include "osint.h"
#include "host.h"
#include "device.h"
#include "echo_d.h"
#include "test_h.h"

#define  CONTROL_MAX_PKT_SIZE             (64)
#define  BULK_MAX_PKT_SIZE                (64)

extern _usb_device_handle                 dev_handle;

BUFFER_STRUCT ep1_buf __attribute__ ((aligned (16)));
BUFFER_STRUCT ep2_buf __attribute__ ((aligned (16)));

volatile ECHO_GLOBAL_STRUCT echo_app_struct;

#define ECHO_VENDOR_ID 0x05A3
#define ECHO_PRODUCT_ID 0x0001
#define ECHO_VERSION_ID 0x0002

/* Device descriptors are always 18 bytes */
static 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 is indicated in the interface descriptors */
   0x00,
   /* Device Subclass is indicated in the interface descriptors */
   0x00,
   /* Mass storage devices do not use class-specific protocols */
   0x00,
   /* Max packet size */
   CONTROL_MAX_PKT_SIZE,
   /* Vendor ID */
   USB_uint_16_low(ECHO_VENDOR_ID), USB_uint_16_high(ECHO_VENDOR_ID),
   /* Product ID */
   USB_uint_16_low(ECHO_PRODUCT_ID), USB_uint_16_high(ECHO_PRODUCT_ID),
   /* BCD Device version */
   USB_uint_16_low(ECHO_VERSION_ID), USB_uint_16_high(ECHO_VERSION_ID),
   /* Manufacturer string index */
   0x1,
   /* Product string index */
   0x2,
   /* Serial number string index */
   0x0,
   /* Number of configurations available */
   0x1
};

#define CONFIG_DESC_NUM_INTERFACES  (4)
/* This must be counted manually and updated with the descriptor */
/* 1*Config(9) + 1*Interface(9) + 2*Endpoint(7) = 32 bytes */
#define CONFIG_DESC_SIZE            (35)

static uint_8 ConfigDesc[CONFIG_DESC_SIZE] =
{
   /* Configuration Descriptor - always 9 bytes */
   9,
   /* "Configuration" type of descriptor */
   2,
   /* Total length of the Configuration descriptor */
   USB_uint_16_low(CONFIG_DESC_SIZE), USB_uint_16_high(CONFIG_DESC_SIZE),
   /* NumInterfaces */
   1,
   /* Configuration Value */
   1,
   /* Configuration Description String Index*/
   4,
   /* Attributes.  Self-powered. */
   0xc0,
   /* Current draw from bus */
   0,
   /* Interface 0 Descriptor - always 9 bytes */
   9,
   /* "Interface" type of descriptor */
   4,
   /* Number of this interface */
   0,
   /* Alternate Setting */
   0,
   /* Number of endpoints on this interface */
   2,
   /* Interface Class */
   0x08,
   /* Interface Subclass: SCSI transparent command set */
   0x06,
   /* Interface Protocol: Bulk only protocol */
   0x50,
   /* Interface Description String Index */
   0,
   /* Endpoint 1 (Bulk In Endpoint), Interface 0 Descriptor - always 7 bytes*/
   7,
   /* "Endpoint" type of descriptor */
   5,
   /*
   ** Endpoint address.  The low nibble contains the endpoint number and the
   ** high bit indicates TX(1) or RX(0).
   */
   0x81,
   /* Attributes.  0=Control 1=Isochronous 2=Bulk 3=Interrupt */
   0x02,
   /* Max Packet Size for this endpoint */
   USB_uint_16_low(BULK_MAX_PKT_SIZE), 
   USB_uint_16_high(BULK_MAX_PKT_SIZE),
   /* Polling Interval (ms) */
   0,
   /* Endpoint 2 (Bulk Out Endpoint), Interface 0 Descriptor - always 7 bytes*/
   7,
   /* "Endpoint" type of descriptor */
   5,
   /*
   ** Endpoint address.  The low nibble contains the endpoint number and the
   ** high bit indicates TX(1) or RX(0).
   */
   0x01,
   /* Attributes.  0=Control 1=Isochronous 2=Bulk 3=Interrupt */
   0x02,
   /* Max Packet Size for this endpoint */
   USB_uint_16_low(BULK_MAX_PKT_SIZE), 
   USB_uint_16_high(BULK_MAX_PKT_SIZE),
   /* Polling Interval (ms) */
   0,
   /* OTG descriptor */
   3,
   9,    /* OTG type */
   3     /* HNP and SRP support */
};

extern uchar USB_IF_ALT[4];
#define MAX_USB_IF_ALT ((sizeof(USB_IF_ALT)/sizeof(USB_IF_ALT[0]))-1)

/* number of strings in the table not including 0 or n. */
#define 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.
*/

/* Languages supported = American English (0x0409) */
static uint_8 USB_STR_0[ 4] = {sizeof(USB_STR_0), 0x03, 0x09, 0x04};
static uint_8 USB_STR_1[56] = {sizeof(USB_STR_1), 0x03,
      'B',0,'r',0,'o',0,'a',0,'d',0,'O',0,'n',0,' ',0,	/* 16 chars per row */
      'C',0,'o',0,'m',0,'m',0,'u',0,'n',0,'i',0,'c',0,
      'a',0,'t',0,'i',0,'o',0,'n',0,'s',0,' ',0,'C',0,
      'o',0,'r',0,'p',0};
static uint_8 USB_STR_2[36] = {sizeof(USB_STR_2), 0x03,
      'E',0,'c',0,'h',0,'o',0,' ',0,'S',0,'l',0,'a',0,
      'v',0,'e',0,' ',0,'D',0,'e',0,'v',0,'i',0,'c',0,
      'e',0};
static uint_8 USB_STR_3[10] = {sizeof(USB_STR_3), 0x03,
      'B',0,'E',0,'T',0,'A',0};
static uint_8 USB_STR_4[ 8] = {sizeof(USB_STR_4), 0x03,
      '#',0,'0',0,'2',0};
static uint_8 USB_STR_5[ 8] = {sizeof(USB_STR_5), 0x03,
      '_',0,'A',0,'1',0};
static uint_8 USB_STR_6[32] = {sizeof(USB_STR_6), 0x03,
      'R',0,'u',0,'f',0,'u',0,'s',0,' ',0,'T',0,' ',0,
      'F',0,'i',0,'r',0,'e',0,'f',0,'l',0,'y',0};
static uint_8 USB_STR_n[34] = {sizeof(USB_STR_n), 0x03,
      'B',0,'A',0,'D',0,' ',0,'S',0,'T',0,'R',0,'I',0,
      'N',0,'G',0,' ',0,'I',0,'n',0,'d',0,'e',0,'x',0};

static 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_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 echo_ch9GetStatus
   (
      /* 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 index;

//printf(">>>echo_ch9GetStatus\n");
   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 */
            index = swab16(setup_ptr->INDEX) & 0x00FF;
	    index = MIN(index, MAX_USB_IF_ALT);
            if_status = USB_IF_ALT[index];
            /* Send the requested data */
            _usb_device_send_data(handle, 0, &if_status, sizeof(if_status));
            break;
      
         case 0x82:
            /* Endpoint request */
            endpoint = swab16(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 echo_ch9ClearFeature
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
   static uint_8           endpoint;
   uint_16                 usb_status;
   uint_16                 value;
   
//printf(">>>echo_ch9ClearFeature\n");
   _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;
   }

   if (setup) {
      value = swab16(setup_ptr->VALUE);
      switch (setup_ptr->REQUESTTYPE) {
      
         case 0:
            /* DEVICE */
            if (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);
            }
            break;
         
         case 2:
            /* ENDPOINT */
            if (value != 0) {
               _usb_device_stall_endpoint(handle, 0, 0);
               return;
            }
            endpoint = swab16(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,
               0);
            break;
         
         default:
            _usb_device_stall_endpoint(handle, 0, 0);
            break;

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


/*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 echo_ch9SetFeature
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
   uint_16           usb_status;
   uint_16           value;
   uint_8            endpoint;

//printf(">>>echo_ch9SetFeature\n");
   value = swab16(setup_ptr->VALUE);

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

         case 0:
            /* DEVICE */
            if (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);
            }
            if (value & 0x04) {
               /* set a_hnp_support */
            }
            if (value & 0x05) {
               /* set a_alt_hnp_support */
            }
            break;
            
         case 2:
            /* ENDPOINT */
            if (value != 0) {
               _usb_device_stall_endpoint(handle, 0, 0);
               return;
            }
            endpoint = swab16(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;

      }
      
      /* status phase */
      _usb_device_send_data(handle, 0, 0, 0);
   } else {
      if (value & 0x03) {
         /* sleep callback should be installed for HNP capable Dual role device */
         _usb_device_register_service(handle, USB_SERVICE_SLEEP, dev_bus_suspend);
         echo_app_struct.ECHO_ENABLED = FALSE;
      }
   }
   return;
}

/*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*--------------------------------------------------------------------*/
static void echo_ch9GetDescription
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
	uint_16 value;

//printf(">>>echo_ch9GetDescription\n");
   if (setup) {
      /* Load the appropriate string depending on the descriptor requested.*/
      value = swab16(setup_ptr->VALUE) & 0xFF00;
      switch (value) {

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

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

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

         default:
            _usb_device_stall_endpoint(handle, 0, 0);
            return;
      }
      /* status phase */
      _usb_device_recv_data(handle, 0, 0, 0);
      dev_global_struct.dev_state = USB_DEV_STATE_ATTACHED;
   }
   return;
}

/*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 echo_ch9SetDescription
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
//printf(">>>echo_ch9SetDescription\n");
   _usb_device_stall_endpoint(handle, 0, 0);
   return;
}

/*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 echo_ch9GetConfig
   (
      /* 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 current_config;
   /* Return the currently selected configuration */
//printf(">>>echo_ch9GetConfig\n");
   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));
      /* status phase */
      _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 echo_ch9SetConfig
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
   uint_16 usb_state, value;
//printf(">>>echo_ch9SetConfig\n");
   
   _usb_device_get_status(handle, USB_STATUS_DEVICE_STATE, &usb_state);
   
   if ((usb_state == USB_STATE_CONFIG) || (usb_state == USB_STATE_ADDRESS)) {
      if (setup) {
	 value = swab16(setup_ptr->VALUE) & 0x00FF;
         /* 0 indicates return to unconfigured state */
         if (value == 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);
            /* status phase */      
            _usb_device_send_data(handle, 0, 0, 0);
            return;
         }

         /*
         ** 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 != value) {
            /* Reconfigure endpoints here */
            switch (value) {
         
               default:
                  break;
            }
            _usb_device_set_status(handle, USB_STATUS_CURRENT_CONFIG, value);
            _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
               USB_STATE_CONFIG);
            /* status phase */      
            _usb_device_send_data(handle, 0, 0, 0);
            return;
         }
         _usb_device_set_status(handle, USB_STATUS_DEVICE_STATE,
            USB_STATE_CONFIG);
         /* ack */
         _usb_device_send_data(handle, 0, 0, 0);
      }
   } else {
      _usb_device_stall_endpoint(handle, 0, 0);
   }
   return;
}

static void echo_service_data_ep1(_usb_device_handle, boolean, uint_8, uint_8_ptr, uint_32);
static void echo_service_data_ep2(_usb_device_handle, boolean, uint_8, uint_8_ptr, uint_32);

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_ch9Vendor
* Returned Value : 
* Comments       :
*     Chapter 9 SynchFrame command
*     See section 9.4.11 (page 195) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
void echo_ch9Vendor
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_reset_ep0
* Returned Value : None
* Comments       :
*     Called upon a bus reset event.  Initialises the control endpoint.
* 
*END*--------------------------------------------------------------------*/
static void
echo_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_32              length      
   )
{
PRINTF("echo_reset_ep0: ECHO_ENABLED %d\n", echo_app_struct.ECHO_ENABLED);
   if (echo_app_struct.ECHO_ENABLED) {
      _usb_device_cancel_transfer(handle, 1, USB_RECV);
      _usb_device_cancel_transfer(handle, 1, USB_SEND);
      _usb_device_cancel_transfer(handle, 2, USB_RECV);
      _usb_device_cancel_transfer(handle, 2, USB_SEND);
      echo_app_struct.ECHO_ENABLED = FALSE;
   }
   
   if (!echo_app_struct.ECHO_ENABLED) { 
      _usb_device_init_endpoint(handle, 0, CONTROL_MAX_PKT_SIZE, 0,
         USB_CONTROL_ENDPOINT, 0);
      _usb_device_init_endpoint(handle, 0, CONTROL_MAX_PKT_SIZE, 1,
         USB_CONTROL_ENDPOINT, 0);
   }
   return;
}

/*
 * Initialize the data endpoints for the echo slave device
 */
static void
echo_init_data_eps(_usb_device_handle handle)
{
    int ep_type;
    int ep_num;
    int max_pkt;
    int status;

    /*
     * Don't bother with the interrupt endpoints for now
     */
    ep_type = USB_BULK_ENDPOINT;
    ep_num = 1;
    max_pkt = BULK_MAX_PKT_SIZE;
    _usb_device_init_endpoint(handle, ep_num, max_pkt, USB_RECV, ep_type, 0);
    _usb_device_init_endpoint(handle, ep_num, max_pkt, USB_SEND, ep_type, 0);
    _usb_device_register_service(handle, ep_num, 
		(ep_num == 1) ? echo_service_data_ep1 : echo_service_data_ep2);
    status = _usb_device_get_transfer_status(handle, ep_num, USB_RECV);
    if (status != USB_STATUS_IDLE)
        printf(">>>echo_init_data_eps: recv already pending!\n"); /* XXX */
    else {
        printf(">>>echo_init_data_eps: post recv buf for ep%d\n", ep_num);
        _usb_device_recv_data(handle, ep_num,
	    (ep_num == 1) ? DBUF(ep1_buf) : DBUF(ep2_buf), BUFFERSIZE);
    }

    echo_app_struct.ECHO_ENABLED = TRUE;
    return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_service_data_ep1
* Returned Value : None
* Comments       : Callback to implement the actual echo data functions
* 
*END*--------------------------------------------------------------------*/
static void
echo_service_data_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_32              length      
   )
{
printf(">>>echo_service_data_ep1: setup %d, dir %d, buf 0x%x, len %d\n", setup, direction, buffer, length);
	/*
	 * Reset buffer pointers and post next buffer
	 */
	if (direction == USB_RECV) {
		ADD_BUF(ep1_buf, length);
printf(">>>echo_service_data_ep1: recv (buflen now %d)\n", DATA_BUF(ep1_buf));
		/*
		 * If the buffer is full, then send it back
		 */
		if (DATA_BUF(ep1_buf) == BUFFERSIZE) {
			/* Make sure BDTs in consistent state */
      			_usb_device_cancel_transfer(handle, 1, USB_SEND);
            		_usb_device_send_data(handle, 1, &(ep1_buf.buf[0]),
				DATA_BUF(ep1_buf));
		}
		return;
	}

	if (direction == USB_SEND) {
		/*
		 * When send completes, go back to listening
		 */
		DATA_BUF(ep1_buf) = 0;	/* clear buffer */
		bzero((void *)DBUF(ep1_buf), BUFFERSIZE);
		osInvalDCache((void *)DBUF(ep1_buf), BUFFERSIZE);
		/* Make sure BDTs in consistent state */
      		_usb_device_cancel_transfer(handle, 1, USB_RECV);
            	_usb_device_recv_data(handle, 1, DBUF(ep1_buf), BUFFERSIZE);
		return;
	}
	
printf(">>>echo_service_data_ep: invalid dir %d\n", direction);
	return;
}

static void
echo_service_data_ep2
   (
      /* [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_32              length      
   )
{
//printf(">>>echo_service_data_ep2: setup %d, dir %d, buf 0x%x, len %d\n", setup, direction, buffer, length);
	/*
	 * Copy the data as requested
	 */
	if (direction == USB_RECV) {
		return;
	}

	if (direction == USB_SEND) {
		return;
	}
	
printf(">>>echo_service_data_ep2: invalid dir %d???\n", direction);
	return;
}

/*
 * Return info structure for the echo device.
 */
void
echo_query(OSBbUsbInfo *ip)
{
	ip->ua_type = OS_USB_TYPE_DEVICE;
	ip->ua_state = (dev_global_struct.dev_state == USB_DEV_STATE_ATTACHED) ? OS_USB_STATE_ATTACHED : OS_USB_STATE_NULL;
	ip->ua_class = 0;
	ip->ua_subclass = 0;
	ip->ua_protocol = 0;
	ip->ua_vendor = ECHO_VENDOR_ID;
	ip->ua_product = ECHO_PRODUCT_ID;
	ip->ua_cfg = 0;		/* XXX */
	ip->ua_ep = 1;		/* XXX */
	ip->ua_speed = OS_USB_FULL_SPEED;	/* XXX */
	ip->ua_mode = OS_USB_MODE_RW;	/* XXX */
	ip->ua_blksize = 64;
	ip->ua_mfr_str = "BroadOn Communications";
	ip->ua_prod_str = "Echo Slave Device";
	ip->ua_driver_name = "";
	ip->ua_support = TRUE;
}

/*
 * Device-specific initialization for the echo device.
 */
void
echo_device_init(_usb_device_handle handle)
{
//printf(">>>echo_device_init\n");
	INIT_BUF(ep1_buf);
	INIT_BUF(ep2_buf);

	dev_global_struct.num_ifcs = ConfigDesc[CONFIG_DESC_NUM_INTERFACES];
	return;
}

struct usbdevfuncs echo_dev_funcs = {
	echo_reset_ep0,
	echo_ch9GetDescription,
	echo_ch9Vendor,
	echo_init_data_eps,
	echo_query,
    NULL
};