usb_standard_request.c File Reference

Process USB device enumeration requests. More...

#include "config.h"
#include "conf_usb.h"
#include "lib_mcu\usb\usb_drv.h"
#include "usb_descriptors.h"
#include "modules\usb\device_chap9\usb_standard_request.h"
#include "usb_specific_request.h"

Include dependency graph for usb_standard_request.c:

Go to the source code of this file.

Functions

static void usb_get_descriptor (void)
static void usb_set_address (void)
static void usb_set_configuration (void)
static void usb_clear_feature (void)
static void usb_set_feature (void)
static void usb_get_status (void)
static void usb_get_configuration (void)
static void usb_get_interface (void)
static void usb_set_interface (void)
void usb_process_request (void)
 This function reads the SETUP request sent to the default control endpoint and calls the appropriate function. When exiting of the usb_read_request function, the device is ready to manage the next request.

Variables

static bit zlp
static U8 endpoint_status [2]
U8 code * pbuffer
U8 data_to_transfer
U16 wInterface
static U8 bmRequestType
U8 usb_configuration_nb
bit usb_connected
code S_usb_device_descriptor usb_user_device_descriptor
code S_usb_user_configuration_descriptor usb_user_configuration_descriptor


Detailed Description

Process USB device enumeration requests.

,v

Copyright (c) 2004 Atmel.

Use of this program is subject to Atmel's End User License Agreement. Please read file license.txt for copyright notice.

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.

Version:
1.4 at90usb128-demo-hidgen-std-2_0_0
Id
usb_standard_request.c,v 1.4 2006/06/14 16:59:02 rletendu Exp
Todo:
Bug:

Definition in file usb_standard_request.c.


Function Documentation

void usb_get_descriptor ( void   )  [static]

usb_get_descriptor.

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.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< 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 242 of file usb_standard_request.c.

References CONFIGURATION_DESCRIPTOR, data_to_transfer, DEVICE_DESCRIPTOR, EP_CONTROL_LENGTH, FALSE, Is_usb_read_control_enabled, Is_usb_receive_out, LSB, MSB, pbuffer, TRUE, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_enable_stall_handshake, 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 zlp.

Referenced by usb_process_request().

00243 {
00244 U16  wLength         ;
00245 U8  descriptor_type ;
00246 U8  string_type     ;
00247 U8  dummy;
00248 U8  nb_byte;
00249 
00250    zlp             = FALSE;                  /* no zero length packet */
00251    string_type     = Usb_read_byte();        /* read LSB of wValue    */
00252    descriptor_type = Usb_read_byte();        /* read MSB of wValue    */
00253 
00254    switch (descriptor_type)
00255    {
00256     case DEVICE_DESCRIPTOR:
00257       data_to_transfer = Usb_get_dev_desc_length(); 
00258       pbuffer          = Usb_get_dev_desc_pointer();
00259       break;
00260     case CONFIGURATION_DESCRIPTOR:
00261       data_to_transfer = Usb_get_conf_desc_length(); 
00262       pbuffer          = Usb_get_conf_desc_pointer();
00263       break;
00264     default:
00265       if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE )
00266       {
00267          Usb_enable_stall_handshake();
00268          Usb_ack_receive_setup();
00269          return;
00270       }
00271       break;
00272    }
00273 
00274    dummy = Usb_read_byte();                     
00275    dummy = Usb_read_byte();
00276    LSB(wLength) = Usb_read_byte();              
00277    MSB(wLength) = Usb_read_byte();
00278    Usb_ack_receive_setup() ;                  
00279 
00280    if (wLength > data_to_transfer)
00281    {
00282       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00283       else { zlp = FALSE; }                   
00284    }
00285    else
00286    {
00287       data_to_transfer = (U8)wLength;         
00288    }
00289 
00290    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00291    {
00292       while(!Is_usb_read_control_enabled());
00293 
00294       nb_byte=0;
00295       while(data_to_transfer != 0)        
00296       {
00297          if(nb_byte++==EP_CONTROL_LENGTH) 
00298          {
00299             break;
00300          }
00301 #ifndef AVRGCC
00302          Usb_write_byte(*pbuffer++);
00303 #else    // AVRGCC does not support point to PGM space
00304 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00305          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00306 #endif
00307          data_to_transfer --;
00308       }
00309       Usb_send_control_in();
00310    }
00311 
00312    Usb_send_control_in();
00313 
00314    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 
00315    if(zlp == TRUE)
00316    {
00317      while(!Is_usb_read_control_enabled());
00318      Usb_send_control_in();
00319    }
00320 
00321 
00322    while(!Is_usb_receive_out());
00323    Usb_ack_receive_out();
00324 }

Here is the call graph for this function:

void usb_set_address ( void   )  [static]

usb_set_address.

This function manages the SET ADDRESS request. When complete, the device will filter the requests using the new address.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< send a ZLP for STATUS phase

< waits for status phase done before using the new address

Definition at line 175 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().

00176 {
00177    Usb_configure_address(Usb_read_byte());
00178 
00179    Usb_ack_receive_setup();
00180 
00181    Usb_send_control_in();                    
00182    while(!Is_usb_in_ready());                
00183 
00184    Usb_enable_address();
00185 }

void usb_set_configuration ( void   )  [static]

usb_set_configuration.

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.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< keep that order (set StallRq/clear RxSetup) or a OUT request following the SETUP may be acknowledged

< send a ZLP for STATUS phase

< endpoint configuration

Definition at line 201 of file usb_standard_request.c.

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

Referenced by usb_process_request().

00202 {
00203 U8 configuration_number;
00204 
00205    configuration_number = Usb_read_byte();
00206 
00207    if (configuration_number <= NB_CONFIGURATION)
00208    {
00209       Usb_ack_receive_setup();
00210       usb_configuration_nb = configuration_number;
00211    }
00212    else
00213    {
00216       Usb_enable_stall_handshake();
00217       Usb_ack_receive_setup();
00218       return;
00219    }
00220 
00221    Usb_send_control_in();                    
00222 
00223    usb_user_endpoint_init(usb_configuration_nb);  
00224    Usb_set_configuration_action();
00225 }

Here is the call graph for this function:

void usb_clear_feature ( void   )  [static]

usb_clear_feature.

This function manages the SET FEATURE request.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< keep that order (set StallRq/clear RxSetup) or a OUT request following the SETUP may be acknowledged

< keep that order (set StallRq/clear RxSetup) or a OUT request following the SETUP may be acknowledged

< dummy read

Definition at line 475 of file usb_standard_request.c.

References bmRequestType, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_ENDPOINT_HALT, INTERFACE_TYPE, Is_usb_endpoint_enabled, MSK_EP_DIR, Usb_ack_receive_setup, Usb_disable_stall_handshake, Usb_enable_stall_handshake, Usb_read_byte, Usb_reset_data_toggle, Usb_reset_endpoint, Usb_select_endpoint, Usb_send_control_in, and ZERO_TYPE.

Referenced by usb_process_request().

00476 {
00477 U8 wValue;
00478 U8 wIndex;
00479 U8 dummy;
00480 
00481    if (bmRequestType == ZERO_TYPE)
00482    {
00485       Usb_enable_stall_handshake();
00486       Usb_ack_receive_setup();
00487       return;
00488    }
00489    else if (bmRequestType == INTERFACE_TYPE)
00490    {
00493       Usb_enable_stall_handshake();
00494       Usb_ack_receive_setup();
00495       return;
00496    }
00497    else if (bmRequestType == ENDPOINT_TYPE)
00498    {
00499       wValue = Usb_read_byte();
00500       dummy  = Usb_read_byte();                
00501 
00502       if (wValue == FEATURE_ENDPOINT_HALT)
00503       {
00504          wIndex = (Usb_read_byte() & MSK_EP_DIR);
00505 
00506          Usb_select_endpoint(wIndex);
00507          if(Is_usb_endpoint_enabled())
00508          {
00509             if(wIndex != EP_CONTROL)
00510             {
00511                Usb_disable_stall_handshake();
00512                Usb_reset_endpoint(wIndex);
00513                Usb_reset_data_toggle();
00514             }
00515             Usb_select_endpoint(EP_CONTROL);
00516             endpoint_status[wIndex] = 0x00;
00517             Usb_ack_receive_setup();
00518             Usb_send_control_in();
00519          }
00520          else
00521          {
00522             Usb_select_endpoint(EP_CONTROL);
00523             Usb_enable_stall_handshake();
00524             Usb_ack_receive_setup();
00525             return;
00526          }
00527       }
00528       else
00529       {
00530          Usb_enable_stall_handshake();
00531          Usb_ack_receive_setup();
00532          return;
00533       }
00534    }
00535 }

void usb_set_feature ( void   )  [static]

usb_set_feature.

This function manages the SET FEATURE request. The USB test modes are supported by this function.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< keep that order (set StallRq/clear RxSetup) or a OUT request following the SETUP may be acknowledged

< dummy read

Definition at line 408 of file usb_standard_request.c.

References bmRequestType, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_ENDPOINT_HALT, INTERFACE_TYPE, Is_usb_endpoint_enabled, MSK_EP_DIR, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_read_byte, Usb_select_endpoint, and Usb_send_control_in.

Referenced by usb_process_request().

00409 {
00410 U8 wValue;
00411 U8 wIndex;
00412 U8 dummy;
00413 
00414    if (bmRequestType == INTERFACE_TYPE)
00415    {
00418       Usb_enable_stall_handshake();
00419       Usb_ack_receive_setup();
00420       return;
00421    }
00422    else if (bmRequestType == ENDPOINT_TYPE)
00423    {
00424       wValue = Usb_read_byte();
00425       dummy    = Usb_read_byte();                
00426 
00427       if (wValue == FEATURE_ENDPOINT_HALT)
00428       {
00429          wIndex = (Usb_read_byte() & MSK_EP_DIR);
00430 
00431          if (wIndex == EP_CONTROL)
00432          {
00433             Usb_enable_stall_handshake();
00434             Usb_ack_receive_setup();
00435             return;
00436          }
00437 
00438          Usb_select_endpoint(wIndex);
00439          if(Is_usb_endpoint_enabled())
00440          {
00441             Usb_enable_stall_handshake();
00442             Usb_select_endpoint(EP_CONTROL);
00443             endpoint_status[wIndex] = 0x01;
00444             Usb_ack_receive_setup();
00445             Usb_send_control_in();
00446          }
00447          else
00448          {
00449             Usb_select_endpoint(EP_CONTROL);
00450             Usb_enable_stall_handshake();
00451             Usb_ack_receive_setup();
00452             return;
00453          }
00454       }
00455       else
00456       {
00457          Usb_enable_stall_handshake();
00458          Usb_ack_receive_setup();
00459          return;
00460       }
00461    }
00462 }

void usb_get_status ( void   )  [static]

usb_get_status.

This function manages the GET STATUS request. The device, interface or endpoint status is returned.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< dummy read

< dummy read

Definition at line 360 of file usb_standard_request.c.

References bmRequestType, DEVICE_STATUS, endpoint_status, INTERFACE_STATUS, Is_usb_receive_out, MSK_EP_DIR, REQUEST_DEVICE_STATUS, REQUEST_ENDPOINT_STATUS, REQUEST_INTERFACE_STATUS, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_read_byte, Usb_send_control_in, and Usb_write_byte.

Referenced by usb_process_request().

00361 {
00362 U8 wIndex;
00363 U8 dummy;
00364 
00365    dummy    = Usb_read_byte();                 
00366    dummy    = Usb_read_byte();                 
00367    wIndex = Usb_read_byte();
00368 
00369    switch(bmRequestType)
00370    {
00371     case REQUEST_DEVICE_STATUS:    Usb_ack_receive_setup();
00372                                    Usb_write_byte(DEVICE_STATUS);
00373                                    break;
00374 
00375     case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
00376                                    Usb_write_byte(INTERFACE_STATUS);
00377                                    break;
00378 
00379     case REQUEST_ENDPOINT_STATUS:  Usb_ack_receive_setup();
00380                                    wIndex = wIndex & MSK_EP_DIR;
00381                                    Usb_write_byte(endpoint_status[wIndex]);
00382                                    break;
00383     default:
00384                                    Usb_enable_stall_handshake();
00385                                    Usb_ack_receive_setup();
00386                                    return;
00387    }
00388 
00389    Usb_write_byte(0x00);
00390    Usb_send_control_in();
00391 
00392    while( !Is_usb_receive_out() );
00393    Usb_ack_receive_out();
00394 }

void usb_get_configuration ( void   )  [static]

usb_get_configuration.

This function manages the GET CONFIGURATION request. The current configuration number is returned.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

Definition at line 338 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().

00339 {
00340    Usb_ack_receive_setup();
00341 
00342    Usb_write_byte(usb_configuration_nb);
00343    Usb_ack_in_ready();
00344 
00345    while( !Is_usb_receive_out() );
00346    Usb_ack_receive_out();
00347 }

void usb_get_interface ( void   )  [static]

usb_get_interface.

TThis function manages the GET_INTERFACE request.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

Definition at line 549 of file usb_standard_request.c.

References Usb_ack_receive_setup, and Usb_enable_stall_handshake.

Referenced by usb_process_request().

00550 {
00551   Usb_enable_stall_handshake();
00552   Usb_ack_receive_setup();
00553 }

void usb_set_interface ( void   )  [static]

usb_set_interface.

TThis function manages the SET_INTERFACE request.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< send a ZLP for STATUS phase

Definition at line 565 of file usb_standard_request.c.

References Is_usb_in_ready, Usb_ack_receive_setup, and Usb_send_control_in.

Referenced by usb_process_request().

00566 {
00567   Usb_ack_receive_setup();
00568   Usb_send_control_in();                    
00569   while(!Is_usb_in_ready());
00570 }


Variable Documentation

bit zlp [static]

Definition at line 56 of file usb_standard_request.c.

Referenced by hid_get_report(), and usb_get_descriptor().

U8 endpoint_status[2] [static]

Definition at line 57 of file usb_standard_request.c.

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

U8 code* pbuffer

Definition at line 62 of file usb_standard_request.c.

Referenced by hid_get_report(), usb_get_descriptor(), and usb_user_get_descriptor().

U8 data_to_transfer

Definition at line 64 of file usb_standard_request.c.

Referenced by hid_get_report(), usb_get_descriptor(), and usb_user_get_descriptor().

U16 wInterface

Definition at line 66 of file usb_standard_request.c.

Referenced by hid_get_report(), usb_hid_get_interface(), and usb_hid_set_idle().

U8 bmRequestType [static]

Definition at line 68 of file usb_standard_request.c.

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

bit usb_connected

Public : (bit) usb_connected usb_connected is set to TRUE when VBUS has been detected usb_connected is set to FALSE otherwise /

Definition at line 46 of file usb_device_task.c.

code S_usb_device_descriptor usb_user_device_descriptor

code S_usb_user_configuration_descriptor usb_user_configuration_descriptor


Generated on Fri Jan 26 17:33:06 2007 for Atmel by  doxygen 1.5.1-p1