echo_h.c 7.63 KB
/*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.c$
*** $Revision: 1.11 $
*** $Date: 2003/08/04 23:30:12 $
***
*** Description:      
***   This file contains the host specific code of the echo example.
***                                                               
**************************************************************************
*END*********************************************************************/

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

volatile ECHO_GLOBAL_STRUCT    echo_host_struct;

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_host_detach
* Returned Value : 
* Comments       :
*     Callback function to process the device detach event
* 
*END*--------------------------------------------------------------------*/
static void
echo_host_detach
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
    USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
    HOST_GLOBAL_STRUCT_PTR hgp;

    hgp = &host_global_struct[hsp->CTLR_NUM];
    PRINTF("echo_host_detach USB%d\n", hsp->CTLR_NUM);

    _usb_host_cancel_transfer(handle, hgp->DEFAULT_PIPE);
    _usb_host_cancel_transfer(handle, echo_host_struct.PIPE_INT_IN);
    _usb_host_cancel_transfer(handle, echo_host_struct.PIPE_INT_OUT);
    _usb_host_cancel_transfer(handle, echo_host_struct.PIPE_BULK_IN);
    _usb_host_cancel_transfer(handle, echo_host_struct.PIPE_BULK_OUT);
    return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_host_send_transaction_done
* Returned Value : 
* Comments       :
*     Callback function to process the transaction done event on an
*     output data pipe
* 
*END*--------------------------------------------------------------------*/
static void
echo_host_send_transaction_done
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];

printf("<<<<echo_host_send_transaction_done, length %d\n", length); /* XXX */

	hgp->TRANSACTION_COMPLETE = TRUE;
	return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_host_recv_transaction_done
* Returned Value : 
* Comments       :
*     Callback function to process the transaction done event on an
*     input data pipe
* 
*END*--------------------------------------------------------------------*/
static void
echo_host_recv_transaction_done
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];

printf("<<<<echo_host_recv_transaction_done, length %d\n", length); /* XXX */

	hgp->TRANSACTION_COMPLETE = TRUE;
	return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : echo_host_chap9
* Returned Value : 
* Comments       :
*     Device specific chapter 9 processing
* 
*END*--------------------------------------------------------------------*/
static void
echo_host_chapter9(_usb_host_handle handle)
{
}

static void
echo_host_open(_usb_host_handle  handle)
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;
	uint_8 error;

	hgp = &host_global_struct[hsp->CTLR_NUM];

         echo_host_struct.PIPE_INT_OUT = _usb_host_open_pipe(handle, 
            hgp->USB_CURRENT_ADDRESS, 2, USB_SEND, 0x00, 
            USB_INTERRUPT_PIPE, 64, 0, 1);
         echo_host_struct.PIPE_INT_IN  = _usb_host_open_pipe(handle, 
            hgp->USB_CURRENT_ADDRESS, 2, USB_RECV, 0x00, 
            USB_INTERRUPT_PIPE, 64, 0, 1);
         echo_host_struct.PIPE_BULK_OUT = _usb_host_open_pipe(handle, 
            hgp->USB_CURRENT_ADDRESS, 1, USB_SEND, 0x00, 
            USB_BULK_PIPE, 64, 0, 1);
         echo_host_struct.PIPE_BULK_IN  = _usb_host_open_pipe(handle, 
            hgp->USB_CURRENT_ADDRESS, 1, USB_RECV, 0x00, 
            USB_BULK_PIPE, 64, 0, 1);
            
            error = _usb_host_register_service(handle, echo_host_struct.PIPE_INT_OUT, 
               echo_host_send_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
            }
      
            error = _usb_host_register_service(handle, echo_host_struct.PIPE_INT_IN, 
               echo_host_recv_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
            }
      
            error = _usb_host_register_service(handle, echo_host_struct.PIPE_BULK_OUT, 
               echo_host_send_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
            }
            
            error = _usb_host_register_service(handle, echo_host_struct.PIPE_BULK_IN, 
               echo_host_recv_transaction_done);
      
            if (error != USB_OK) {
               printf("\nUSB Service Registration failed. Error: %x", error);
            }

       return;	    
}

/*
 * Hardwire the device descriptor for the echo device
 */
usb_dev_desc echo_dev_desc = {
	18,
	1,
#ifdef BIG_ENDIAN
	0x0110,
#else
	0x1001,
#endif
	0x00,
	0x00,
	0x00,
	64,
#ifdef BIG_ENDIAN
	0x05a3,
	0x0001,
	0x0002,
#else
	0xa305,
	0x0100,
	0x0200,
#endif
	1,
	2,
	0,
	1
};

/*
 * Compare Device Descriptor to see if it is the Echo Device
 */
int
echo_host_match(usb_dev_desc *uddp)
{
//printf("<<<<echo_host_match &echo_dev_desc 0x%x, &IDVENDOR 0x%x\n", &echo_dev_desc, &(echo_dev_desc.IDVENDOR));
	if (swab16(uddp->IDVENDOR) != echo_dev_desc.IDVENDOR) {
		printf("Vendor id 0x%x not echo 0x%x\n", swab16(uddp->IDVENDOR), echo_dev_desc.IDVENDOR);
		return(0);
	}

	if (swab16(uddp->IDPRODUCT) != echo_dev_desc.IDPRODUCT) {
		printf("Prod id 0x%x not echo\n", swab16(uddp->IDPRODUCT));
		return(0);
	}

	if (swab16(uddp->BCDDEVICE) != echo_dev_desc.BCDDEVICE) {
		printf("Dev version 0x%x not echo\n", swab16(uddp->BCDDEVICE));
		return(0);
	}

	return(1);
}

static void
echo_host_poll(_usb_host_handle handle)
{
	return;
}

static void
echo_host_send(_usb_host_handle handle, u8 *buffer, s32 len, u64 offset)
{
printf("---echo_host_send buf 0x%x, len %d, pipe %d\n", buffer, len, echo_host_struct.PIPE_BULK_OUT);
	/*
	 * For Echo device, there is no position, so offset is ignored
	 */
        _usb_host_send_data(handle, echo_host_struct.PIPE_BULK_OUT, 
            (uchar_ptr)buffer, len);
	return;
}

static void
echo_host_recv(_usb_host_handle handle, u8 *buffer, s32 len, u64 offset)
{
printf("---echo_host_recv buf 0x%x, len %d, pipe %d\n", buffer, len, echo_host_struct.PIPE_BULK_IN);

	_usb_host_recv_data(handle, echo_host_struct.PIPE_BULK_IN, 
            (uchar_ptr)buffer, len);
	return;
}

usb_host_driver_funcs echo_host_funcs = {
	echo_host_match,
	echo_host_open,
	echo_host_poll,
	echo_host_send,
	echo_host_recv,
	echo_host_detach,
	echo_host_chapter9,
};

usb_host_driver echo_host_driver = {
	&echo_dev_desc,
	&echo_host_funcs,
	"Echo loopback"
};