usb_standard_request.c File Reference

#include "config.h"
#include "conf_usb.h"
#include "lib_mcu/usb/usb_drv.h"
#include "lib_mcu/pll/pll_drv.h"
#include "usb_descriptors.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "usb_specific_request.h"
#include "lib_mcu/flash/flash_drv.h"

Include dependency graph for usb_standard_request.c:

Go to the source code of this file.

Defines

#define Usb_write_PGM_byte(byte)   (Usb_write_byte(*byte))

Functions

static U8 bin_to_ascii (U8 b)
 This function is used to convert a 4 bit number into an ascii character 5 => '5' 10 => 'A'.
static Bool usb_get_descriptor (void)
 This function manages the GET DESCRIPTOR request.
static void usb_set_address (void)
 This function manages the SET ADDRESS request.
static Bool usb_set_configuration (void)
 This function manages the SET CONFIGURATION request.
static void usb_get_configuration (void)
 This function manages the GET CONFIGURATION request.
static Bool usb_get_status (U8 bmRequestType)
 This function manages the GET STATUS request.
static Bool usb_set_feature (U8 bmRequestType)
 This function manages the SET FEATURE request.
static Bool usb_clear_feature (U8 bmRequestType)
 This function manages the SET FEATURE request.
static Bool usb_get_interface (void)
 This function manages the SETUP_GET_INTERFACE request.
static Bool usb_set_interface (void)
 This function manages the SETUP_SET_INTERFACE request.
void usb_process_request (void)
void usb_generate_remote_wakeup (void)
 This function manages the remote wake up generation.

Variables

U8 f_get_serial_string = FALSE
U8 code * pbuffer
U8 endpoint_status [MAX_EP_NB]
U8 data_to_transfer
U8 usb_configuration_nb
 Public : (U8) usb_configuration_nb Store the number of the USB configuration used by the USB device when its value is different from zero, it means the device mode is enumerated Used with USB_DEVICE_FEATURE == ENABLED only /.
U8 remote_wakeup_feature = DISABLE
 Public : (U8) remote_wakeup_feature Store a host request for remote wake up (set feature received) /.
static U8 device_status = DEVICE_STATUS


Detailed Description

Process USB device enumeration requests.

This file contains the USB endpoint 0 management routines corresponding to the standard enumeration process (refer to chapter 9 of the USB specification. This file calls routines of the usb_specific_request.c file for non-standard request management. The enumeration parameters (descriptor tables) are contained in the usb_descriptors.c file.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usb_standard_request.c.


Define Documentation

#define Usb_write_PGM_byte ( byte   )     (Usb_write_byte(*byte))

Definition at line 92 of file usb_standard_request.c.

Referenced by usb_get_descriptor().


Function Documentation

U8 bin_to_ascii ( U8  b  )  [static]

This function is used to convert a 4 bit number into an ascii character 5 => '5' 10 => 'A'.

Parameters:
binary value to convert
Returns:
converted character

Definition at line 663 of file usb_standard_request.c.

Referenced by usb_get_descriptor().

00664 {
00665 return ( (b <= 0x09) ? (b+'0') : (b+'A'-10) );
00666 }

Here is the caller graph for this function:

Bool usb_get_descriptor ( void   )  [static]

This function manages the GET DESCRIPTOR request.

The device descriptor, the configuration descriptor and the device qualifier are supported. All other descriptors must be supported by the usb_user_get_descriptor function. Only 1 configuration is supported.

< sizeof (usb_user_device_descriptor);

< sizeof (usb_user_configuration_descriptor);

< don't care of wIndex field

< read wLength

< clear the receive setup flag

< send only requested number of data

< Send data until necessary

< Check endpoint 0 size

Definition at line 243 of file usb_standard_request.c.

References bin_to_ascii(), data_to_transfer, DESCRIPTOR_CONFIGURATION, DESCRIPTOR_DEVICE, EP_CONTROL_LENGTH, f_get_serial_string, FALSE, Is_usb_nak_out_sent, Is_usb_read_control_enabled, LSB, MSB, pbuffer, TRUE, Usb_ack_control_out, Usb_ack_nak_out, Usb_ack_receive_setup, Usb_get_conf_desc_length, Usb_get_conf_desc_pointer, Usb_get_dev_desc_length, Usb_get_dev_desc_pointer, Usb_read_byte, Usb_send_control_in, usb_user_get_descriptor(), Usb_write_byte, and Usb_write_PGM_byte.

Referenced by usb_process_request().

00244 {
00245    Bool zlp;
00246    U16  wLength;
00247    U8   descriptor_type ;
00248    U8   string_type;
00249    U8   dummy;
00250    U8   nb_byte;
00251    U8   byte_to_send;
00252 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00253    U16  sn_index=0;
00254    U8   initial_data_to_transfer;
00255 #endif
00256     
00257    zlp             = FALSE;                  /* no zero length packet */
00258    string_type     = Usb_read_byte();        /* read LSB of wValue    */
00259    descriptor_type = Usb_read_byte();        /* read MSB of wValue    */
00260 
00261    switch (descriptor_type)
00262    {
00263       case DESCRIPTOR_DEVICE:
00264       data_to_transfer = Usb_get_dev_desc_length(); 
00265       pbuffer          = Usb_get_dev_desc_pointer();
00266       break;
00267       
00268       case DESCRIPTOR_CONFIGURATION:
00269       data_to_transfer = Usb_get_conf_desc_length(); 
00270       pbuffer          = Usb_get_conf_desc_pointer();
00271       break;
00272       
00273       default:
00274       if( !usb_user_get_descriptor(descriptor_type, string_type))
00275          return FALSE;  // Unknow descriptor then stall request
00276       break;
00277    }
00278 
00279    dummy = Usb_read_byte();                     
00280    dummy = Usb_read_byte();
00281    LSB(wLength) = Usb_read_byte();              
00282    MSB(wLength) = Usb_read_byte();
00283    Usb_ack_receive_setup() ;                  
00284 
00285    if (wLength > data_to_transfer)
00286    {
00287       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00288       else { zlp = FALSE; }                   
00289    }
00290    else
00291    {
00292       data_to_transfer = (U8)wLength;         
00293    }
00294 
00295    Usb_ack_nak_out();
00296     
00297    byte_to_send=0;
00298 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00299    initial_data_to_transfer = data_to_transfer;
00300 #endif
00301    while((data_to_transfer != 0) && (!Is_usb_nak_out_sent()))
00302    {
00303       while(!Is_usb_read_control_enabled())
00304       {
00305          if (Is_usb_nak_out_sent())
00306             break;    // don't clear the flag now, it will be cleared after
00307       }
00308               
00309       nb_byte=0;
00310       while(data_to_transfer != 0)        
00311       {
00312          if(nb_byte++==EP_CONTROL_LENGTH) 
00313             break;         
00314          
00315 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00316 
00317          if(f_get_serial_string && (data_to_transfer < (initial_data_to_transfer-1)))    //if we are sending the signature characters (third byte and more...)
00318          {                                                                              //(The first two bytes are the length and the descriptor)
00319             
00320             switch (byte_to_send)
00321             {
00322                case 0:
00323                Usb_write_byte(bin_to_ascii((Flash_read_sn(sn_index)>>4) & 0x0F)); //sends the fist part (MSB) of the signature hex number, converted in ascii
00324                break;
00325               
00326                case 1:
00327                Usb_write_byte(0);                                                   //then, sends a null character (Usb_unicode)                    
00328                break;
00329     
00330                case 2:
00331                Usb_write_byte(bin_to_ascii(Flash_read_sn(sn_index) & 0x0F));      //sends the second part (LSB) of the signature hex number, converted in ascii 
00332                break;
00333     
00334                case 3:
00335                Usb_write_byte(0);                                                   //then, sends a null character (Usb_unicode)  
00336                sn_index++;                                                         //increments the signature address pointer.
00337                break;
00338             }
00339             byte_to_send = (byte_to_send+1)%4;     
00340          }
00341          else
00342          {
00343             Usb_write_PGM_byte(pbuffer++);                                                 //Write a flash byte to USB        
00344          }        
00345 #else          
00346          Usb_write_PGM_byte(pbuffer++);          
00347 #endif
00348          data_to_transfer --;                                                             //decrements the number of bytes to transmit.
00349       }
00350       
00351       if (Is_usb_nak_out_sent())
00352          break;
00353       else
00354          Usb_send_control_in();
00355    }
00356    
00357 #if (USE_DEVICE_SN_UNIQUE==ENABLE)      
00358    f_get_serial_string=FALSE;                                                   //end of signature transmission    
00359 #endif   
00360 
00361    if((zlp == TRUE) && (!Is_usb_nak_out_sent()))
00362    {
00363       while(!Is_usb_read_control_enabled());
00364       Usb_send_control_in();
00365    }
00366 
00367    while (!(Is_usb_nak_out_sent()));
00368    Usb_ack_nak_out();
00369    Usb_ack_control_out();
00370    return TRUE;
00371 }

Here is the call graph for this function:

Here is the caller graph for this function:

void usb_set_address ( void   )  [static]

This function manages the SET ADDRESS request.

When complete, the device will filter the requests using the new address.

Definition at line 200 of file usb_standard_request.c.

References Is_usb_in_ready, Usb_ack_receive_setup, Usb_configure_address, Usb_enable_address, Usb_read_byte, and Usb_send_control_in.

Referenced by usb_process_request().

00201 {
00202    U8 addr = Usb_read_byte();
00203    Usb_configure_address(addr);
00204 
00205    Usb_ack_receive_setup();
00206 
00207    Usb_send_control_in();                    // send a ZLP for STATUS phase
00208    while(!Is_usb_in_ready());                // waits for status phase done
00209                                              // before using the new address
00210    Usb_enable_address();
00211 }

Here is the caller graph for this function:

Bool usb_set_configuration ( void   )  [static]

This function manages the SET CONFIGURATION request.

If the selected configuration is valid, this function call the usb_user_endpoint_init() function that will configure the endpoints following the configuration number.

Definition at line 219 of file usb_standard_request.c.

References FALSE, NB_CONFIGURATION, TRUE, Usb_ack_receive_setup, usb_configuration_nb, Usb_read_byte, Usb_send_control_in, Usb_set_configuration_action, and usb_user_endpoint_init().

Referenced by usb_process_request().

00220 {
00221    U8 configuration_number;
00222 
00223    // Get/Check new configuration
00224    configuration_number = Usb_read_byte();
00225    if (configuration_number > NB_CONFIGURATION)
00226       return FALSE;  //  Bad configuration number then stall request
00227    Usb_ack_receive_setup();
00228    usb_configuration_nb = configuration_number;
00229    
00230    Usb_send_control_in();                          // send a ZLP for STATUS phase
00231    usb_user_endpoint_init(usb_configuration_nb);   // endpoint configuration
00232    Usb_set_configuration_action();
00233    return TRUE;
00234 }

Here is the call graph for this function:

Here is the caller graph for this function:

void usb_get_configuration ( void   )  [static]

This function manages the GET CONFIGURATION request.

The current configuration number is returned.

Definition at line 377 of file usb_standard_request.c.

References Is_usb_receive_out, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, usb_configuration_nb, and Usb_write_byte.

Referenced by usb_process_request().

00378 {
00379    Usb_ack_receive_setup();
00380 
00381    Usb_write_byte(usb_configuration_nb);
00382    Usb_ack_in_ready();
00383 
00384    while( !Is_usb_receive_out() );
00385    Usb_ack_receive_out();
00386 }

Here is the caller graph for this function:

Bool usb_get_status ( U8  bmRequestType  )  [static]

This function manages the GET STATUS request.

The device, interface or endpoint status is returned.

< dummy read

< dummy read

Definition at line 392 of file usb_standard_request.c.

References device_status, endpoint_status, FALSE, Is_usb_receive_out, MSK_EP_DIR, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, Usb_send_control_in, USB_SETUP_GET_STAND_DEVICE, USB_SETUP_GET_STAND_ENDPOINT, USB_SETUP_GET_STAND_INTERFACE, and Usb_write_byte.

Referenced by usb_process_request().

00393 {
00394    U8 wIndex;
00395    U8 dummy;
00396 
00397    dummy    = Usb_read_byte();                 
00398    dummy    = Usb_read_byte();                 
00399    wIndex   = Usb_read_byte();
00400 
00401    switch(bmRequestType)
00402    {
00403       case USB_SETUP_GET_STAND_DEVICE:
00404       Usb_ack_receive_setup();
00405       Usb_write_byte(device_status);
00406       break;
00407 
00408       case USB_SETUP_GET_STAND_INTERFACE:
00409       Usb_ack_receive_setup();
00410       Usb_write_byte(0);      // Reserved - always 0
00411       break;
00412 
00413       case USB_SETUP_GET_STAND_ENDPOINT:
00414       Usb_ack_receive_setup();
00415       wIndex = wIndex & MSK_EP_DIR;
00416       Usb_write_byte( endpoint_status[wIndex] );
00417       break;
00418     
00419       default:
00420       return FALSE;
00421    }
00422    Usb_write_byte(0);
00423 
00424    Usb_send_control_in();
00425    while( !Is_usb_receive_out() );
00426    Usb_ack_receive_out();
00427    return TRUE;
00428 }

Here is the caller graph for this function:

Bool usb_set_feature ( U8  bmRequestType  )  [static]

This function manages the SET FEATURE request.

The USB test modes are supported by this function.

< dummy read

Definition at line 434 of file usb_standard_request.c.

References BDEV_HNP_NB_RETRY, device_status, DISABLED, ENABLED, endpoint_status, EP_CONTROL, FALSE, FEATURE_DEVICE_REMOTE_WAKEUP, FEATURE_ENDPOINT_HALT, HNP_SUPPORT, Is_usb_endpoint_enabled, MSK_EP_DIR, OTG_A_ALT_HNP_SUPPORT, OTG_A_HNP_SUPPORT, OTG_B_HNP_ENABLE, otg_features_supported, remote_wakeup_feature, TRUE, Usb_ack_receive_setup, USB_DEVICE_STATUS_REMOTEWAKEUP, Usb_enable_stall_handshake, Usb_read_byte, USB_REMOTE_WAKEUP, USB_REMOTE_WAKEUP_FEATURE, Usb_select_endpoint, Usb_send_control_in, USB_SETUP_SET_STAND_DEVICE, USB_SETUP_SET_STAND_ENDPOINT, and USB_SETUP_SET_STAND_INTERFACE.

Referenced by usb_process_request().

00435 {
00436    U8 wValue;
00437    U8 wIndex;
00438    U8 dummy;
00439 
00440    switch (bmRequestType)
00441    {
00442       case USB_SETUP_SET_STAND_DEVICE:
00443       wValue = Usb_read_byte();
00444       switch (wValue)
00445       {
00446          case USB_REMOTE_WAKEUP:
00447          if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP)
00448          ||  (USB_REMOTE_WAKEUP_FEATURE != ENABLED))
00449             return FALSE;              // Invalid request
00450          device_status |= USB_DEVICE_STATUS_REMOTEWAKEUP;
00451          remote_wakeup_feature = ENABLED;
00452          Usb_ack_receive_setup();
00453          Usb_send_control_in();
00454          break;
00455 
00456 #if (USB_OTG_FEATURE == ENABLED)
00457          case OTG_B_HNP_ENABLE:
00458          if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))   // see usb_descriptors.h
00459             return FALSE;              // Invalid request
00460          otg_features_supported |= OTG_B_HNP_ENABLE;
00461          otg_device_nb_hnp_retry = BDEV_HNP_NB_RETRY;
00462          Usb_ack_receive_setup();
00463          Usb_send_control_in();
00464          break;
00465 
00466          case OTG_A_HNP_SUPPORT:
00467          if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))
00468             return FALSE;              // Invalid request
00469          otg_features_supported |= OTG_A_HNP_SUPPORT;
00470          Usb_ack_receive_setup();
00471          Usb_send_control_in();
00472          break;
00473 
00474          case OTG_A_ALT_HNP_SUPPORT:
00475          if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))
00476             return FALSE;              // Invalid request
00477          otg_features_supported |= OTG_A_ALT_HNP_SUPPORT;
00478          Usb_ack_receive_setup();
00479          Usb_send_control_in();
00480          break;
00481 #endif
00482             
00483          default:
00484          return FALSE;                 // Unknow request
00485          break;
00486       }
00487       break;
00488 
00489       case USB_SETUP_SET_STAND_INTERFACE:
00490       return FALSE;                    // Unknow request
00491       break;
00492 
00493       case USB_SETUP_SET_STAND_ENDPOINT:
00494       wValue   = Usb_read_byte();
00495       dummy    = Usb_read_byte();                
00496       if (wValue != FEATURE_ENDPOINT_HALT)
00497          return FALSE;                 // Unknow request
00498       wIndex = (Usb_read_byte() & MSK_EP_DIR);
00499       if (wIndex == EP_CONTROL)
00500       {
00501          Usb_enable_stall_handshake();
00502          Usb_ack_receive_setup();
00503       }
00504       Usb_select_endpoint(wIndex);
00505       if( !Is_usb_endpoint_enabled())
00506       {
00507          Usb_select_endpoint(EP_CONTROL);
00508          return FALSE;              // Invalid request
00509       }
00510       Usb_enable_stall_handshake();
00511       Usb_select_endpoint(EP_CONTROL);
00512       endpoint_status[wIndex] = 0x01;
00513       Usb_ack_receive_setup();
00514       Usb_send_control_in();
00515       break;
00516 
00517       default:
00518       return FALSE;                    // Unknow request
00519       break;
00520    }
00521    return TRUE;
00522 }

Here is the caller graph for this function:

Bool usb_clear_feature ( U8  bmRequestType  )  [static]

This function manages the SET FEATURE request.

Definition at line 527 of file usb_standard_request.c.

References device_status, DISABLED, ENABLED, endpoint_status, EP_CONTROL, FALSE, FEATURE_DEVICE_REMOTE_WAKEUP, FEATURE_ENDPOINT_HALT, Is_usb_endpoint_enabled, MSK_EP_DIR, remote_wakeup_feature, TRUE, Usb_ack_receive_setup, USB_DEVICE_STATUS_REMOTEWAKEUP, Usb_disable_stall_handshake, Usb_read_byte, USB_REMOTE_WAKEUP_FEATURE, Usb_reset_data_toggle, Usb_reset_endpoint, Usb_select_endpoint, Usb_send_control_in, USB_SETUP_SET_STAND_DEVICE, USB_SETUP_SET_STAND_ENDPOINT, and USB_SETUP_SET_STAND_INTERFACE.

Referenced by usb_process_request().

00528 {
00529    U8 wValue;
00530    U8 wIndex;
00531    U8 dummy;
00532 
00533    switch (bmRequestType)
00534    {
00535       case  USB_SETUP_SET_STAND_DEVICE:
00536       wValue = Usb_read_byte();
00537       if ((wValue != FEATURE_DEVICE_REMOTE_WAKEUP) || (USB_REMOTE_WAKEUP_FEATURE != ENABLED))
00538          return FALSE;              // Invalid request
00539       device_status &= ~USB_DEVICE_STATUS_REMOTEWAKEUP;
00540       remote_wakeup_feature = DISABLED;
00541       Usb_ack_receive_setup();
00542       Usb_send_control_in();
00543       break;
00544    
00545       case USB_SETUP_SET_STAND_INTERFACE:
00546       return FALSE;                    // Unknow request
00547       break;
00548       
00549       case USB_SETUP_SET_STAND_ENDPOINT:
00550       wValue = Usb_read_byte();
00551       dummy  = Usb_read_byte();
00552       if (wValue != FEATURE_ENDPOINT_HALT)
00553          return FALSE;                 // Unknow request
00554       wIndex = (Usb_read_byte() & MSK_EP_DIR);
00555       Usb_select_endpoint(wIndex);
00556       if( !Is_usb_endpoint_enabled())
00557       {
00558          Usb_select_endpoint(EP_CONTROL);
00559          return FALSE;              // Invalid request
00560       } 
00561       if(wIndex != EP_CONTROL)
00562       {
00563          Usb_disable_stall_handshake();
00564          Usb_reset_endpoint(wIndex);
00565          Usb_reset_data_toggle();
00566       }
00567       Usb_select_endpoint(EP_CONTROL);
00568       endpoint_status[wIndex] = 0x00;
00569       Usb_ack_receive_setup();
00570       Usb_send_control_in();
00571       break;
00572    
00573       default:
00574       return FALSE;                    // Unknow request
00575       break;
00576    }
00577    return TRUE;
00578 }

Here is the caller graph for this function:

Bool usb_get_interface ( void   )  [static]

This function manages the SETUP_GET_INTERFACE request.

Definition at line 583 of file usb_standard_request.c.

References FALSE, Is_usb_receive_out, LSB, MSB, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_read_byte, Usb_send_control_in, usb_user_interface_get(), and Usb_write_byte.

Referenced by usb_process_request().

00584 {
00585    U16   wInterface;
00586    U8    wValue_msb;
00587    U8    wValue_lsb;
00588 
00589    // Read wValue
00590    wValue_lsb = Usb_read_byte();
00591    wValue_msb = Usb_read_byte();
00592    // wValue = Alternate Setting
00593    // wIndex = Interface
00594    LSB(wInterface)=Usb_read_byte();
00595    MSB(wInterface)=Usb_read_byte();
00596    if( (0!=wValue_msb) || (0!=wValue_msb) )
00597       return FALSE;
00598    Usb_ack_receive_setup();
00599    
00600    Usb_write_byte( usb_user_interface_get(wInterface) );
00601    Usb_send_control_in();
00602 
00603    while( !Is_usb_receive_out() );
00604    Usb_ack_receive_out();
00605    return TRUE;
00606 }

Here is the call graph for this function:

Here is the caller graph for this function:

Bool usb_set_interface ( void   )  [static]

This function manages the SETUP_SET_INTERFACE request.

Definition at line 611 of file usb_standard_request.c.

References EP_CONTROL, FALSE, Is_usb_in_ready, LSB, MSB, TRUE, Usb_ack_receive_setup, Usb_read_byte, Usb_select_endpoint, Usb_send_control_in, and usb_user_interface_reset().

Referenced by usb_process_request().

00612 {
00613    U16   wInterface;
00614    U8    wValue_msb;
00615    U8    wValue_lsb;
00616 
00617    // Read wValue
00618    wValue_lsb = Usb_read_byte();
00619    wValue_msb = Usb_read_byte();
00620    // wValue = Alternate Setting
00621    // wIndex = Interface
00622    LSB(wInterface)=Usb_read_byte();
00623    MSB(wInterface)=Usb_read_byte();
00624    if( 0!=wValue_msb )
00625       return FALSE;
00626    Usb_ack_receive_setup();
00627    
00628    usb_user_interface_reset(wInterface, wValue_lsb);
00629    Usb_select_endpoint(EP_CONTROL);
00630             
00631    Usb_send_control_in();
00632    while(!Is_usb_in_ready());
00633    return TRUE;
00634 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

U8 code* pbuffer

Definition at line 91 of file usb_standard_request.c.

U8 endpoint_status[MAX_EP_NB]

Definition at line 95 of file usb_standard_request.c.

Referenced by usb_clear_feature(), usb_get_status(), usb_process_request(), and usb_set_feature().

U8 data_to_transfer

Definition at line 96 of file usb_standard_request.c.

U8 device_status = DEVICE_STATUS [static]

Definition at line 99 of file usb_standard_request.c.

Referenced by usb_clear_feature(), usb_get_status(), and usb_set_feature().


Generated on Mon Nov 3 10:08:26 2008 for ATMEL by  doxygen 1.5.3