usbms_drv.c 24 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
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *    usbms_drv.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements usb MASS STORAGE class driver
 *
 * Author:
 * -------
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

#ifdef  __USB_MASS_STORAGE_ENABLE__

#include "drv_sw_features_usb.h"
#include "drv_comm.h"
//#include "reg_base.h"
#include "intrCtrl.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h"
//#include "app_buff_alloc.h"
//#include "usb_comm.h"
#include "drvsignals.h"
//#include "gpt_sw.h"

//#include "usb_drv.h"
#include "usb_adap.h"
#include "usb.h"
#include "usb_resource.h"

#include "usbms_drv.h"
#include "usbms_adap.h"
#include "usb_custom.h"
#include "usb_debug_tool.h"
#include "usb_trc.h"
#include "kal_non_specific_general_types.h"
#include "kal_release.h"
#include "stack_config.h"
#include "stack_ltlcom.h"
#include "kal_trace.h"

#include "kal_public_api.h"
#include "kal_general_types.h"



UsbMs_Struct g_UsbMS;


/* static functions */
//static void USB_Ms_DMA_Timeout_hdlr(void *parameter);
static void USB_Ms_Rx_DMA_Callback(void);
static void USB_Ms_Tx_DMA_Callback(void);
static void USB_Ms_Ep0_Command(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static void USB_Ms_BulkOut_Hdlr(void);
static void USB_Ms_BulkIn_Reset(void);
static void USB_Ms_BulkOut_Reset(void);
static void USB_Ms_Cmd_Reset(void);



const kal_uint8 ms_if_dscr[]=
{
	USB_IFDSC_LENGTH,	//bLength;
	USB_INTERFACE,	//bDescriptorType;
	0x00,	//bInterfaceNumber;
	0x00,	//bAlternateSetting;
	USB_MS_EP_NUMBER,	//bNumEndpoints;
	USB_MS_INTERFACE_CLASS_CODE,	//bInterfaceClass;
	USB_MS_INTERFACE_SUBCLASS_CODE,	//bInterfaceSubClass;
	USB_MS_INTERFACE_PROTOCOL_CODE,	//bInterfaceProtocol;
	0x00	//iInterface;
};

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

extern const kal_uint8 usb_epbulk_dscr[];

/************************************************************
	DMA time out handler
*************************************************************/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

/************************************************************
	TX/RX path build functions(use DMA)
*************************************************************/

/* Prepare to receive RX packet to addr and length len */
void USB_Ms_BuildRx(void *addr, kal_uint32 len)
{
//	GPTI_StopItem(g_UsbMS.dma_timeout_handle);

	USB_Dbg_Trace(USB_MS_BUILD_DMA_RX, 0 , 0);

	/* For CBW is 31 */
	if(len == 31)
	{
		ASSERT(0);
//		GPTI_StartItem(g_UsbMS.dma_timeout_handle, USBMS_DMA_TIMEOUT, USB_Ms_DMA_Timeout_hdlr, NULL);
	}

	/* Stop and ack DMA if previous DMA is not yet ready */
	USB_Stop_DMA_Channel(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR);

	/* Initialize rxpipe data */
	g_UsbMS.rxpipe->pData = (kal_uint8 *)addr;
	g_UsbMS.rxpipe->nBytesRecv = 0;
	g_UsbMS.rxpipe->nBuffLen = (kal_int32)len;

	IRQMask(IRQ_USB_CODE);

	/* USB reset and MS ep0 reset cmd will both stop DMA */
	/* If they happen before IRQmask, we will not build DMA in the following if conditions */
	/* If they happen after IRQmask, they will stop DMA */
	/* If before they stop DMA, the DMA already done, this DMA callback message will be ignored by USB task if checking */

	//After USB Reset or USB_EP0_Command_Reset, we must build DMA RX for the first CBW (reset = True)(nState = IDLE)
	//make sure : handle USB Reset message first. (ex. going to build RX + USB_RESET+ Build RX+ USB_RESET_Message(build RX))
	if((g_UsbMS.usb_reset == KAL_FALSE)&&(g_UsbMS.ms_is_reset == KAL_FALSE))
	{
		/* DMA running state is NOT cleared by USB_Ms_Rx_DMA_Callback */
		USB_DMA_Setup(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR, USB_ENDPT_BULK, (kal_uint32)addr, len,
					USB_Ms_Rx_DMA_Callback, KAL_FALSE, KAL_TRUE, USB_DMA1_TYPE);
	}
	
	//USB_UnMask_Irq(USB_MASK_OWNER_MS);
//	if(USB_Get_UnMask_Irq() == KAL_TRUE)
//		IRQUnmask(IRQ_USB_CODE);
	USB_IRQ_Unmask();
		
}


/* Prepare to transmit Tx packet from addr and length len */
void USB_Ms_BuildTx(void *addr, kal_uint32 len)
{
	USB_Dbg_Trace(USB_MS_BUILD_DMA_TX, 0, 0);

	/* Stop and ack DMA if previous DMA is not yet ready */
	USB_Stop_DMA_Channel(g_UsbMS.txpipe->byEP, USB_EP_TX_DIR);

	/* Initialize TX pipe */
	g_UsbMS.txpipe->pData = (kal_uint8 *)addr;
	g_UsbMS.txpipe->nBytesLeft = (kal_int32)len;

	IRQMask(IRQ_USB_CODE);

	/* USB reset and MS ep0 reset cmd will both stop DMA */
	/* If they happen before IRQmask, we will not build DMA in the following if conditions */
	/* If they happen after IRQmask, they will stop DMA */
	/* If before they stop DMA, the DMA already done, this DMA callback message will be ignored by USB task's if checking */
	if ((g_UsbMS.usb_reset == KAL_FALSE)&&(g_UsbMS.ms_is_reset == KAL_FALSE))
	{
		/* For CSW is 13 */
		if(len != 13)
		{
			/* DMA running state is NOT cleared by USB_Ms_Tx_DMA_Callback */
			USB_DMA_Setup(g_UsbMS.txpipe->byEP, USB_EP_TX_DIR, USB_ENDPT_BULK, (kal_uint32)addr, len,
						USB_Ms_Tx_DMA_Callback, KAL_FALSE, KAL_TRUE, USB_DMA1_TYPE);
		}
		else
		{
			/* DMA running state is NOT cleared by USB_Ms_Tx_DMA_Callback */
			USB_DMA_Setup(g_UsbMS.txpipe->byEP, USB_EP_TX_DIR, USB_ENDPT_BULK, (kal_uint32)addr, len,
						NULL, KAL_FALSE, KAL_TRUE, USB_DMA1_TYPE);
		}
	}
	
	//USB_UnMask_Irq(USB_MASK_OWNER_MS);
//	if(USB_Get_UnMask_Irq() == KAL_TRUE)
//		IRQUnmask(IRQ_USB_CODE);
  USB_IRQ_Unmask();
}


/************************************************************
	interface initialization functions
*************************************************************/

/* interface create function, prepare descriptor */
void USB_Ms_If_Create(void *ifname)
{
	kal_uint8 ep_in_id;
	kal_uint8 ep_out_id;
	kal_uint8 if_id;


	/* get resource number and register to gUsbDevice */
	g_UsbMS.ms_if_info = USB_Get_Interface_Number(&if_id);
	g_UsbMS.ms_ep_in_info = USB_Get_Bulk_Tx_Ep(&ep_in_id);
	g_UsbMS.ms_ep_out_info = USB_Get_Bulk_Rx_Ep(&ep_out_id);

	/* record interface name and interface descriptor length */
	g_UsbMS.ms_if_info->interface_name_ptr = (kal_char *)ifname;
	g_UsbMS.ms_if_info->ifdscr_size = USB_IFDSC_LENGTH;
	g_UsbMS.ms_ep_in_info->epdscr_size = USB_EPDSC_LENGTH;
	g_UsbMS.ms_ep_out_info->epdscr_size = USB_EPDSC_LENGTH;

	/* related endpoint info and class specific command handler*/
	g_UsbMS.ms_if_info->ep_info[0] = (Usb_Ep_Info*)g_UsbMS.ms_ep_in_info;
	g_UsbMS.ms_if_info->ep_info[1] = (Usb_Ep_Info*)g_UsbMS.ms_ep_out_info;
	g_UsbMS.ms_if_info->if_class_specific_hdlr = USB_Ms_Ep0_Command;

	kal_mem_cpy(&(g_UsbMS.ms_if_info->ifdscr.stdif), ms_if_dscr, USB_IFDSC_LENGTH);
	kal_mem_cpy(&(g_UsbMS.ms_ep_in_info->epdesc.stdep), usb_epbulk_dscr, USB_EPDSC_LENGTH);
	kal_mem_cpy(&(g_UsbMS.ms_ep_out_info->epdesc.stdep), usb_epbulk_dscr, USB_EPDSC_LENGTH);

	g_UsbMS.ms_if_info->ifdscr.stdif.bInterfaceNumber = if_id;
	g_UsbMS.ms_if_info->ifdscr.stdif.iInterface = USB_Get_String_Number((void *)g_UsbMS.ms_param->interface_string);

	/* endpoint handler */
	USB_Register_Drv_Info(USB_DRV_HDLR_EP_TX, ep_in_id, NULL);
	g_UsbMS.ms_ep_in_info->ep_reset = USB_Ms_BulkIn_Reset;
	USB_Register_Drv_Info(USB_DRV_HDLR_EP_RX, ep_out_id, USB_Ms_BulkOut_Hdlr);
	g_UsbMS.ms_ep_out_info->ep_reset = USB_Ms_BulkOut_Reset;

	g_UsbMS.ms_ep_in_info->epdesc.stdep.bEndpointAddress = (USB_EP_DIR_IN | ep_in_id);	/*InPipe*/
	g_UsbMS.ms_ep_in_info->ep_status.epin_status.byEP = ep_in_id;
	g_UsbMS.ms_ep_in_info->ep_status.epin_status.nBytesLeft = USB_EP_NODATA;
	g_UsbMS.ms_ep_out_info->epdesc.stdep.bEndpointAddress = (USB_EP_DIR_OUT | ep_out_id);	/*OutPipe*/
	g_UsbMS.ms_ep_out_info->ep_status.epout_status.byEP = ep_out_id;

	#if defined(__USB_SW_DMA_NO_SHARE_CHANNELS__)
	USB_Get_DMA_Channel(ep_in_id, ep_out_id, USB_EP_TX_DIR, KAL_FALSE); /*  in & out cannot use the same channel */
	USB_Get_DMA_Channel(ep_in_id, ep_out_id, USB_EP_RX_DIR, KAL_FALSE);
	#else
	USB_Get_DMA_Channel(ep_in_id, ep_out_id, USB_EP_TX_DIR, KAL_TRUE); /*  in & out eps use the same DMA channel */
	#endif

//	if((g_UsbMS.dma_timeout_handle == 0) || (g_UsbMS.dma_timeout_handle == 0x7F))
//		GPTI_GetHandle(&g_UsbMS.dma_timeout_handle);   /* for DMA time out use!!*/
}


/* interface reset function */
void USB_Ms_If_Reset(void)
{
	kal_uint8 index;
	
	/* This must not exist because use this trivky to avoid reset abnormal operation when writing to flash*/
	g_UsbMS.txpipe = &g_UsbMS.ms_ep_in_info->ep_status.epin_status;
	g_UsbMS.rxpipe = &g_UsbMS.ms_ep_out_info->ep_status.epout_status;

	g_UsbMS.usb_reset= KAL_TRUE;
	g_UsbMS.ms_read_write_reset = KAL_TRUE;
	
	for (index = 0;index<MAX_DISK_DRVER;index++)
	{
		g_UsbMS.power_off_state[index] = KAL_FALSE;
	}

	/* Stop DMA */
	USB_Stop_DMA_Channel(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR);
}

/* interface reset function */
void USB_Ms_If_Resume(void)
{
	kal_uint8 index;		
	for (index = 0;index<MAX_DISK_DRVER;index++)
	{
		g_UsbMS.power_off_state[index] = KAL_FALSE;	
	}
}



/* interface enable function, enable EP*/
void USB_Ms_If_Enable(void)
{
//	ilm_struct *usb_ilm = NULL;


	/* Stop DMA */
	/* Compliance test tool will set configuration , but no reset */
	USB_Stop_DMA_Channel(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR);

	/* Configure DMA setting for EP */
	USB_TxEPEn(g_UsbMS.txpipe->byEP, USB_ENDPT_BULK, USB_EP_USE_ONLY_DMA, KAL_TRUE, KAL_FALSE);
	USB_RxEPEn(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK, USB_EP_USE_DMA_ON_DEMAND, KAL_TRUE, KAL_FALSE);

	USB_Dbg_Trace(USB_MS_SWITCH_TO_FIFO_RX_BY_USB_CONFIG, 0, 0);
	/* We do not dynamically switch TX's usage, since if we config TX DMA, and in DMA callback, we switch to FIFO,
	but last packet is sent out after we switch to FIFO, IP will give a ep intr for this DMA "sending" packet, so we will see two ep intrs */

	/* Send reset message to usb task, task handle the ms state machine */
	if(g_UsbMS.is_send_usb_reset_ilm == KAL_TRUE)
	{
		g_UsbMS.is_send_usb_reset_ilm = KAL_FALSE;

		USB_Send_Msg_Ext_Queue(MOD_USB,MSG_ID_USB_MSDRV_RESET_IND, NULL);

//		DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR, MOD_USB,MSG_ID_USB_MSDRV_RESET_IND, NULL);
//		msg_send_ext_queue(usb_ilm);
	}
}


/* interface speed reset function, change EP's speed-specific descriptor */
void USB_Ms_If_Speed_Reset(kal_bool b_other_speed)
{
#ifdef __USB_HS_ENABLE__

	kal_uint32  max_packet;
	max_packet = USB_Speed_Reset_Packet_Size(b_other_speed);	


	g_UsbMS.ms_ep_in_info->epdesc.stdep.wMaxPacketSize[0] = max_packet &0xff;
	g_UsbMS.ms_ep_in_info->epdesc.stdep.wMaxPacketSize[1] = (max_packet>>8) &0xff;
	
	g_UsbMS.ms_ep_out_info->epdesc.stdep.wMaxPacketSize[0] = max_packet &0xff;
	g_UsbMS.ms_ep_out_info->epdesc.stdep.wMaxPacketSize[1] = (max_packet>>8)&0xff;

#endif
}


/************************************************************
	global variable g_UsbMS initialize and release functions
*************************************************************/

/* initialize global variable g_UsbMS */
void USB_Init_Ms_Status(void)
{
	kal_uint8 index;

#ifdef __ZMAEE_APP_TTS__
	{
		extern void zmaee_tts_stop_and_free_mem(void);
		extern void ZMAEE_DebugPrint(const char * pszFormat,...);
		ZMAEE_DebugPrint("USB_Init_Ms_Status: free");
		zmaee_tts_stop_and_free_mem();
	}
#endif
	g_UsbMS.nState = USBMS_IDLE;
	g_UsbMS.txpipe = NULL;
	g_UsbMS.rxpipe = NULL;
	g_UsbMS.max_LUN = USB_Ms_Get_Max_LUN();
	g_UsbMS.current_LUN = 0;
	g_UsbMS.is_send_usb_reset_ilm = KAL_TRUE;
	g_UsbMS.cbw_is_vaild = KAL_TRUE;
	g_UsbMS.ms_is_reset = KAL_FALSE;
	g_UsbMS.usb_reset = KAL_FALSE;
	g_UsbMS.CSWDataResidue = 0;
	g_UsbMS.CSWStatusError = 0x00;
	g_UsbMS.sensePtr = NULL;

	for (index = 0;index<MAX_DISK_DRVER;index++)
	{
		g_UsbMS.power_off_state[index] = KAL_FALSE;
}
}


/* release global variable g_UsbMS */
void USB_Release_Ms_Status(void)
{
	#if defined(__USB_SW_DMA_NO_SHARE_CHANNELS__)
	USB_Free_DMA_Channel(g_UsbMS.txpipe->byEP, g_UsbMS.rxpipe->byEP, USB_EP_TX_DIR, KAL_FALSE);
	USB_Free_DMA_Channel(g_UsbMS.rxpipe->byEP, g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR, KAL_FALSE);
	#else
	USB_Free_DMA_Channel(g_UsbMS.txpipe->byEP, g_UsbMS.rxpipe->byEP, USB_EP_TX_DIR, KAL_TRUE);
	#endif
	USB_Init_Ms_Status();
	g_UsbMS.disk_buffer = NULL;
}


/************************************************************
	Tx/Rx path DMA callback function
*************************************************************/

/* RX DMA callback function*/
static void USB_Ms_Rx_DMA_Callback(void)
{
//	ilm_struct *usb_ilm = NULL;
	usb_ms_rec_done_conf_struct *rec_done_conf;


	/* MS reset cmd must re start from RX CBW */
//	g_UsbMS.ms_is_reset = KAL_FALSE;
//	g_UsbMS.usb_reset = KAL_FALSE;

/*
	if(g_UsbMS.rxpipe->nBuffLen == 31)
	{
		GPTI_StopItem(g_UsbMS.dma_timeout_handle);
	}
*/
	if(USB_Get_Device_State() == DEVSTATE_CONFIG)
	{
		g_UsbMS.rxpipe->nBytesRecv = g_UsbMS.rxpipe->nBuffLen;

		/* Send message to usb task, let ms state machine handle the received data */
		rec_done_conf = (usb_ms_rec_done_conf_struct *)construct_local_para(
							sizeof(usb_ms_rec_done_conf_struct), TD_CTRL);
		rec_done_conf->pData = g_UsbMS.rxpipe->pData;
		rec_done_conf->nBuffLen = g_UsbMS.rxpipe->nBuffLen;
		rec_done_conf->nBytesRecv = g_UsbMS.rxpipe->nBuffLen;

		USB_Send_Msg_Ext_Queue(MOD_USB, MSG_ID_USB_MSDRV_REC_DONE_CONF, rec_done_conf);

//		DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,MOD_USB, MSG_ID_USB_MSDRV_REC_DONE_CONF, rec_done_conf);
//		msg_send_ext_queue(usb_ilm);
	}
}


/* TX DMA callback function */
static void USB_Ms_Tx_DMA_Callback(void)
{
//	ilm_struct *usb_ilm = NULL;


	if(USB_Get_Device_State() == DEVSTATE_CONFIG)
	{
		g_UsbMS.txpipe->nBytesLeft = USB_EP_NODATA;

		/*send message to usb task, let ms state machine handle the transmitted data*/
		USB_Send_Msg_Ext_Queue(MOD_USB, MSG_ID_USB_MSDRV_TRX_DONE_CONF, NULL);

//		DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,MOD_USB, MSG_ID_USB_MSDRV_TRX_DONE_CONF, NULL);
//		msg_send_ext_queue(usb_ilm);
	}
}


/************************************************************
	EP0 command parse functions
*************************************************************/

/* Reset command handler */
static void USB_Ms_Cmd_Reset(void)
{
//	ilm_struct *usb_ilm = NULL;


	/* Stop and ack DMA if previous DMA is not yet ready */
	USB_Stop_DMA_Channel(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR);

	USB_Clear_Tx_EP_FIFO(g_UsbMS.txpipe->byEP, USB_ENDPT_BULK, KAL_TRUE);
	USB_Clear_Rx_EP_FIFO(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK, KAL_TRUE);

	/* Switch endpoint state to use FIFOread */
	if(USB_RxEP_Usage(g_UsbMS.rxpipe->byEP) == USB_EP_STATE_DMA)
		USB_Config_RxEP_Type(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK, KAL_FALSE);

	/* Send reset message to usb task, task handle the state machine */
	if(g_UsbMS.is_send_usb_reset_ilm == KAL_TRUE)
	{
		/* avoid sending too many messages to task when task is busy writing*/
		g_UsbMS.is_send_usb_reset_ilm = KAL_FALSE;

		USB_Send_Msg_Ext_Queue(MOD_USB, MSG_ID_USB_MSDRV_RESET_IND, NULL);

//		DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,MOD_USB, MSG_ID_USB_MSDRV_RESET_IND, NULL);
//		msg_send_ext_queue(usb_ilm);
	}
}


/* parse MS class specific command */
static void USB_Ms_Ep0_Command(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
	kal_bool  bError = KAL_FALSE;


	switch (pcmd->bRequest)
	{
	case USB_MS_RESET:
		if( (pcmd->bmRequestType != USB_CMD_CLASSIFOUT) ||(pcmd->wValue != 0) )
		{
			bError = KAL_TRUE;
		}
		else
		{
			/* In USB compliance tool, PC will send MS ep0 reset cmd, and then clear ep feature, and send CBW */
			USB_Dbg_Trace(USB_MS_EP0_RESET, 0, 0);
			g_UsbMS.cbw_is_vaild = KAL_TRUE;
			g_UsbMS.ms_is_reset = KAL_TRUE;
			/* Build DMA RX here */
			USB_Ms_Cmd_Reset();
			/* Must clear stall status here */
			USB_CtrlEPStall(g_UsbMS.txpipe->byEP, USB_EP_TX_DIR, KAL_FALSE);
			USB_CtrlEPStall(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR, KAL_FALSE);
		}
		break;

	case USB_MS_GETMAXLUN:
		if( (pcmd->bmRequestType != USB_CMD_CLASSIFIN) ||(pcmd->wValue != 0) )
		{
			bError = KAL_TRUE;
		}
		else
		{
			g_UsbMS.max_LUN = USB_Ms_Get_Max_LUN();
			USB_Generate_EP0Data(pep0state, pcmd, (void *)&g_UsbMS.max_LUN, 1);
		}
		break;

	default:
		bError = KAL_TRUE;
		break;
	}

	if(bError == KAL_TRUE)
		drv_trace1(TRACE_FUNC, (kal_uint32)USBMS_ERROR_CMD, (kal_uint32)pcmd->bRequest);

	USB_EP0_Command_Hdlr(bError);
/*
	if(USB_Get_Endpoint0_State() == USB_EP0_IDLE)
	{
		USB_Set_Endpoint0_State(USB_EP0_RX_STATUS);
		USB_Update_EP0_State(USB_EP0_DRV_STATE_READ_END, bError, KAL_TRUE);
	}
	else
	{
		USB_Update_EP0_State(USB_EP0_DRV_STATE_READ_END, bError, KAL_FALSE);
	}
*/	
}


/* EP Bulk Out interrupt handler, called by EP interrupt */
static void USB_Ms_BulkOut_Hdlr(void)
{
	kal_uint32 nCount;
//	ilm_struct *usb_ilm = NULL;
	usb_ms_rec_done_conf_struct *rec_done_conf;


	/* Check received packet length */
	nCount = USB_EP_Rx_Pkt_Len(g_UsbMS.rxpipe->byEP);

	if(nCount == 31)
	{
		USB_EPFIFORead(g_UsbMS.rxpipe->byEP, 31, g_UsbMS.CBWdata, KAL_FALSE);

		USB_Dbg_Trace(USB_MS_FIFO_RX_HDLR, 0, 0);

		g_UsbMS.rxpipe->nBytesRecv = 31;
		g_UsbMS.rxpipe->pData = (kal_uint8 *)g_UsbMS.CBWdata;
		g_UsbMS.rxpipe->nBuffLen = 31;

		/* Send message to usb task, let ms state machine handle the received data */
		rec_done_conf = (usb_ms_rec_done_conf_struct *)construct_local_para(
							sizeof(usb_ms_rec_done_conf_struct), TD_CTRL);
		rec_done_conf->pData = g_UsbMS.rxpipe->pData;
		rec_done_conf->nBuffLen = g_UsbMS.rxpipe->nBuffLen;
		rec_done_conf->nBytesRecv = g_UsbMS.rxpipe->nBytesRecv;


		USB_Send_Msg_Ext_Queue(MOD_USB, MSG_ID_USB_MSDRV_REC_DONE_CONF, rec_done_conf);

//		DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,MOD_USB, MSG_ID_USB_MSDRV_REC_DONE_CONF, rec_done_conf);
//		msg_send_ext_queue(usb_ilm);

		/* Switch endpoint state to use DMA */
		/* Change EP state must before clear RxPktRdy.
		Otherwise, if there are packets arriving after clear RxPktRdy and before switch to DMA,
		then endpoint interrupt should be triggered, and result in an unexpected RX intr */
		USB_Config_RxEP_Type(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK, KAL_TRUE);

		/* Clear RxPktRdy */
		USB_EP_Rx_Ready(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK);
	}
	else
	{
		/* Error handling */
		USB_Dbg_Trace(USB_MS_FIFO_RX_WITH_WRONG_CBW_SIZE, nCount, 0);
		drv_trace1(TRACE_FUNC, (kal_uint32)USBMS_RX_WRONG_CBW_LENGTH, nCount);

		/* Clear RxPktRdy */
//		USB_EP_Rx_Ready(g_UsbMS.rxpipe->byEP, USB_ENDPT_BULK);

		g_UsbMS.cbw_is_vaild = KAL_FALSE;
		USB_CtrlEPStall(g_UsbMS.txpipe->byEP, USB_EP_TX_DIR, KAL_TRUE);
		USB_CtrlEPStall(g_UsbMS.rxpipe->byEP, USB_EP_RX_DIR, KAL_TRUE);
	}
}


/* Bulk In endpoint reset function */
static void USB_Ms_BulkIn_Reset(void)
{
	return;
}


/* Bulk Out endpoint reset function */
static void USB_Ms_BulkOut_Reset(void)
{
	return;
}

#endif //__USB_MASS_STORAGE_ENABLE__