usbdevice.c 25.9 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
/*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.12 $
*** $Date: 2004/08/23 19:43:19 $
***
*** Description:      
***  This file contains the VUSB32 device specific Echo example code.
***                                                               
**************************************************************************
*END*********************************************************************/

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

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;

DEV_GLOBAL_STRUCT dev_global_struct;

uchar USB_IF_ALT[4] = {0, 0, 0, 0};
#define MAX_USB_IF_ALT ((sizeof(USB_IF_ALT)/sizeof(USB_IF_ALT[0]))-1)

void dev_bus_suspend(_usb_device_handle,boolean,uint_8,uint_8_ptr,uint_32);

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

//printf(">>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;

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

/*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*--------------------------------------------------------------------*/
static void
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(">>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;
            if (dev_global_struct.funcs->stall_ep)
                dev_global_struct.funcs->stall_ep(handle, endpoint, 0);
            _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*--------------------------------------------------------------------*/
static void
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(">>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);
            if (dev_global_struct.funcs->stall_ep)
                dev_global_struct.funcs->stall_ep(handle, 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);
      }
   }
   return;
}

/*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*--------------------------------------------------------------------*/
static void
ch9SetAddress
   (
      /* 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 new_address;

//printf(">>ch9SetAddress setup %d addr 0x%x\n", setup, swab16(setup_ptr->VALUE));

   if (setup) {
      new_address = swab16(setup_ptr->VALUE) & 0xFF;
      /* status phase */
      _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);
//printf(">>ch9SetAddress state = ADDRESS with addr 0x%x\n", new_address);   
      dev_global_struct.funcs->initeps(handle);
   }
   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
ch9GetDescription
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
//printf(">>ch9GetDescription\n");   
   dev_global_struct.funcs->get_desc(handle, setup, setup_ptr);
   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*--------------------------------------------------------------------*/
static void
ch9SetDescription
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
//printf(">>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*--------------------------------------------------------------------*/
static void
ch9GetConfig
   (
      /* USB handle */
      _usb_device_handle handle,
      
      /* Is it a Setup phase? */
      boolean setup,
            
      /* The setup packet pointer */
      SETUP_STRUCT_PTR setup_ptr
   )
{
   uint_16 current_config;
//printf(">>ch9GetConfig\n");   
   /* 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));
      /* status phase */
      _usb_device_recv_data(handle, 0, 0, 0);
   }
   return;
}

/*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*--------------------------------------------------------------------*/
static void
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;
   uint_16 value;
//printf(">>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) & 0xFF;
//printf(">>ch9SetConfig new value 0x%x\n", value);   
         /* 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;
}

/*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*--------------------------------------------------------------------*/
static void 
ch9GetInterface
   (
      /* 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;
   uint_16 index, length;

//printf(">>ch9GetInterface\n");   
   
   _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;
   }

   if (setup) {
      index = swab16(setup_ptr->INDEX) & 0x00FF;
      index = MIN(index, MAX_USB_IF_ALT);
      length = MIN(swab16(setup_ptr->LENGTH), sizeof(uint_8));
//printf(">>ch9GetInterface index %d, length %d\n", index, length);   
      _usb_device_send_data(handle, 0, &USB_IF_ALT[index], length);
      /* status phase */      
      _usb_device_recv_data(handle, 0, 0, 0);
   }
   return;
}

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

//printf(">>ch9SetInterface\n");   
   if (setup) {
      if (setup_ptr->REQUESTTYPE != 0x01) {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      }

      /*
      ** 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
      */
      index = swab16(setup_ptr->INDEX) & 0x00FF;
      index = MIN(index, MAX_USB_IF_ALT);
      value = swab16(setup_ptr->VALUE) & 0x00FF;
      if (USB_IF_ALT[index] != value)
      {
         USB_IF_ALT[index] = value;
         /* Reconfigure endpoints here. */
         
      }

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

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

//printf(">>ch9SynchFrame\n");   
   
   if (setup) {
      if (setup_ptr->REQUESTTYPE != 0x02) {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      }

      index = swab16(setup_ptr->INDEX) & 0x00FF;
      if (index >= dev_global_struct.num_ifcs)
      {
         _usb_device_stall_endpoint(handle, 0, 0);
         return;
      }

      length = MIN(swab16(setup_ptr->LENGTH), sizeof(sof_count));
      _usb_device_get_status(handle, USB_STATUS_SOF_COUNT, &sof_count);
      _usb_device_send_data(handle, 0, (uchar_ptr)&sof_count, length);
      /* status phase */      
      _usb_device_recv_data(handle, 0, 0, 0);
   }
   return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9Vendor
* Returned Value : 
* Comments       :
*     Chapter 9 SynchFrame command
*     See section 9.4.11 (page 195) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
static void 
ch9Vendor
   (
      _usb_device_handle handle,
      boolean setup,
      SETUP_STRUCT_PTR setup_ptr
   )
{
//printf(">>ch9Vendor\n");   
	dev_global_struct.funcs->vendor(handle, setup, setup_ptr);
	return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : service_ep0
* Returned Value : None
* Comments       :
*     Called upon a completed endpoint 0 (USB 1.1 Chapter 9) transfer
* 
*END*--------------------------------------------------------------------*/
static void 
dev_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_32              length      
   )
{

//printf(">>dev_service_ep0: setup %d, dir %d, len %d\n", setup, direction, length);   
   if (setup) {
      _usb_device_read_setup_data(handle, 0, (uchar_ptr)&local_setup_packet);
   }
   
//printf(">>dev_service_ep0: setup packet REQT 0x%x, REQ 0x%x\n", local_setup_packet.REQUESTTYPE, local_setup_packet.REQUEST);
   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;

         }
         
         break;

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

      case 0x40:
         /* vendor specific request */
         ch9Vendor(handle, setup, &local_setup_packet);
         break;
      
      default:
         _usb_device_stall_endpoint(handle, 0, 0);
         break;
         
   }
   
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : reset_ep0
* Returned Value : None
* Comments       :
*     Called upon a bus reset event.  Initialises the control endpoint.
* 
*END*--------------------------------------------------------------------*/
static void
dev_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      
   )
{
	dev_global_struct.FIRST_SOF = TRUE;
//printf(">>dev_reset_ep0\n");
	/* Call device specific reset_ep0 */
	dev_global_struct.funcs->reset_ep0(handle, setup, direction, buffer, length);
	if (dev_global_struct.dev_state != USB_DEV_STATE_ATTACHED)
	    dev_global_struct.dev_state = USB_DEV_STATE_RESET;
	return;
}


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : bus_suspend
* Returned Value : None
* Comments       :
*     Called upon a SLEEP interrupt.
* 
*END*--------------------------------------------------------------------*/
void
dev_bus_suspend
   (
      /* [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      
   )
{
   return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : bus_sof
* Returned Value : None
* Comments       :
*     Called upon a Start of Frame Interrupt.
* 
*END*--------------------------------------------------------------------*/
static void
dev_bus_sof
   (
      /* [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      
   )
{
    /* XXX this is useless overhead most of the time */
    /* XXX figure out a way to unregister this when we don't need it */
    if (dev_global_struct.FIRST_SOF) {
        _usb_device_register_service(handle, USB_SERVICE_SLEEP, dev_bus_suspend);
	dev_global_struct.FIRST_SOF = FALSE;
    }

}

/*
 * USB device state machine
 */
void
_usb_device_state_machine(_usb_device_handle handle)
{
#if 0
#ifdef	USB_ECHO_DEVICE
extern void echo_device_poll(_usb_device_handle);

	echo_device_poll(handle);
#endif
#endif
	return;
}

/*
 * Initialize the generic USB device layer.
 */
void
__usb_arc_device_setup(int which, _usb_device_handle *handlep)
{
#ifdef	USB_ECHO_DEVICE
extern void echo_device_init(_usb_device_handle);
extern struct usbdevfuncs echo_dev_funcs;
#endif
#ifdef	USB_RDBS_DEVICE
extern void rdbs_device_init(_usb_device_handle);
extern struct usbdevfuncs rdbs_dev_funcs;
#endif
	uint_8 error;
printf("arc_device_setup%d at ticks = %d\n", which, osGetCount());

	if (_usb_device_init(which, 0, handlep, 16)) {
		printf("_usb_device_init fails\n");
		return;
	}
   
	error = _usb_device_register_service(*handlep, USB_SERVICE_EP0, 
		dev_service_ep0);
	if (error != USB_OK) {
		printf("USB Device EP0 Registration failed: %x\n", error);
	}
   
	error = _usb_device_register_service(*handlep, USB_SERVICE_BUS_RESET, 
		dev_reset_ep0);
	if (error != USB_OK) {
		printf("USB Device RESET Registration failed: %x\n", error);
	}
   
	error = _usb_device_register_service(*handlep, USB_SERVICE_SOF,
		dev_bus_sof);
	if (error != USB_OK) {
		printf("USB Device SOF Registration failed: %x\n", error);
	}

	/*
	 * Device-specific initialization routines
	 */
#ifdef	USB_ECHO_DEVICE
	echo_device_init(*handlep);
	dev_global_struct.funcs = &echo_dev_funcs;
#endif
#ifdef	USB_RDBS_DEVICE
	rdbs_device_init(*handlep);
	dev_global_struct.funcs = &rdbs_dev_funcs;
#endif
	dev_global_struct.dev_state = USB_DEV_STATE_INIT;
	return;
}