- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

# usb_standard_request.c File Reference

,vProcess 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)
| usb_get_descriptor.
static void | usb_set_address (void)
| usb_set_address.
static void | usb_set_configuration (void)
| usb_set_configuration.
static void | usb_clear_feature (void)
| usb_clear_feature.
static void | usb_set_feature (void)
| usb_set_feature.
static void | usb_get_status (void)
| usb_get_status.
static void | usb_get_configuration (void)
| usb_get_configuration.
static void | usb_get_interface (void)
| usb_get_interface.
static void | usb_set_interface (void)
| usb_set_interface.
void | usb_process_request (void)
| This function reads the SETUP request sent to the default control endpoint and calls the appropriate function.
## Variables
static bit | zlp
static U8 | endpoint_status [4]
static U8 | device_status = 1
U8 code * | pbuffer
U8 | data_to_transfer
U16 | wInterface
static U8 | bmRequestType
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 /.
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 /.
code S_usb_device_descriptor | usb_user_device_descriptor
code S_usb_user_configuration_descriptor | usb_user_configuration_descriptor
U8 | usb_remote_wup_feature

## Detailed Description

,vProcess USB device enumeration requests.

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.8 at90usb162-2enum-mouse_ms-1_0_1
  Id
    usb_standard_request.c,v 1.8 2006/11/30 15:07:56 arobert 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 246 of file usb_standard_request.c.

References CONFIGURATION_DESCRIPTOR, data_to_transfer, DEVICE_DESCRIPTOR, EP_CONTROL_LENGTH, FALSE, Is_usb_nak_out_sent, Is_usb_read_control_enabled, LSB, MSB, pbuffer, RXOUTI, TRUE, Usb_ack_nak_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().

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

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 179 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().

    00180 {
    00181    Usb_configure_address(Usb_read_byte());
    00182
    00183    Usb_ack_receive_setup();
    00184
    00185    Usb_send_control_in();
    00186    while(!Is_usb_in_ready());
    00187
    00188    Usb_enable_address();
    00189 }

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 205 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().

    00206 {
    00207 U8 configuration_number;
    00208
    00209    configuration_number = Usb_read_byte();
    00210
    00211    if (configuration_number <= NB_CONFIGURATION)
    00212    {
    00213       Usb_ack_receive_setup();
    00214       usb_configuration_nb = configuration_number;
    00215    }
    00216    else
    00217    {
    00220       Usb_enable_stall_handshake();
    00221       Usb_ack_receive_setup();
    00222       return;
    00223    }
    00224
    00225    Usb_send_control_in();
    00226
    00227    usb_user_endpoint_init(usb_configuration_nb);
    00228    Usb_set_configuration_action();
    00229 }

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

< dummy read

Definition at line 504 of file usb_standard_request.c.

References bmRequestType, device_status, DISABLED, ENABLED, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_DEVICE_REMOTE_WAKEUP, 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_REMOTE_WAKEUP, usb_remote_wup_feature, Usb_reset_data_toggle, Usb_reset_endpoint, Usb_select_endpoint, Usb_send_control_in, USB_STATUS_REMOTEWAKEUP, and ZERO_TYPE.

Referenced by usb_process_request().

    00505 {
    00506 U8 wValue;
    00507 U8 wIndex;
    00508 U8 dummy;
    00509
    00510    if (bmRequestType == ZERO_TYPE)
    00511    {
    00512      wValue = Usb_read_byte();
    00513      if ((wValue == FEATURE_DEVICE_REMOTE_WAKEUP) && (USB_REMOTE_WAKEUP == ENABLED))
    00514      {
    00515        device_status &= ~USB_STATUS_REMOTEWAKEUP;
    00516        usb_remote_wup_feature = DISABLED;
    00517        Usb_ack_receive_setup();
    00518        Usb_send_control_in();
    00519      }
    00520      else
    00521      {
    00522       Usb_enable_stall_handshake();
    00523       Usb_ack_receive_setup();
    00524      }
    00525       return;
    00526    }
    00527    else if (bmRequestType == INTERFACE_TYPE)
    00528    {
    00531       Usb_enable_stall_handshake();
    00532       Usb_ack_receive_setup();
    00533       return;
    00534    }
    00535    else if (bmRequestType == ENDPOINT_TYPE)
    00536    {
    00537       wValue = Usb_read_byte();
    00538       dummy  = Usb_read_byte();
    00539
    00540       if (wValue == FEATURE_ENDPOINT_HALT)
    00541       {
    00542          wIndex = (Usb_read_byte() & MSK_EP_DIR);
    00543
    00544          Usb_select_endpoint(wIndex);
    00545          if(Is_usb_endpoint_enabled())
    00546          {
    00547             if(wIndex != EP_CONTROL)
    00548             {
    00549                Usb_disable_stall_handshake();
    00550                Usb_reset_endpoint(wIndex);
    00551                Usb_reset_data_toggle();
    00552             }
    00553             Usb_select_endpoint(EP_CONTROL);
    00554             endpoint_status[wIndex] = 0x00;
    00555             Usb_ack_receive_setup();
    00556             Usb_send_control_in();
    00557          }
    00558          else
    00559          {
    00560             Usb_select_endpoint(EP_CONTROL);
    00561             Usb_enable_stall_handshake();
    00562             Usb_ack_receive_setup();
    00563             return;
    00564          }
    00565       }
    00566       else
    00567       {
    00568          Usb_enable_stall_handshake();
    00569          Usb_ack_receive_setup();
    00570          return;
    00571       }
    00572    }
    00573 }

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

References bmRequestType, device_status, ENABLED, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_DEVICE_REMOTE_WAKEUP, FEATURE_ENDPOINT_HALT, INTERFACE_TYPE, Is_usb_endpoint_enabled, MSK_EP_DIR, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_read_byte, USB_REMOTE_WAKEUP, usb_remote_wup_feature, Usb_select_endpoint, Usb_send_control_in, USB_STATUS_REMOTEWAKEUP, and ZERO_TYPE.

Referenced by usb_process_request().

    00421 {
    00422 U8 wValue;
    00423 U8 wIndex;
    00424 U8 dummy;
    00425
    00426    if (bmRequestType == ZERO_TYPE)
    00427    {
    00428      wValue = Usb_read_byte();
    00429      if ((wValue == FEATURE_DEVICE_REMOTE_WAKEUP) && (USB_REMOTE_WAKEUP == ENABLED))
    00430      {
    00431        device_status |= USB_STATUS_REMOTEWAKEUP;
    00432        usb_remote_wup_feature = ENABLED;
    00433        Usb_ack_receive_setup();
    00434        Usb_send_control_in();
    00435      }
    00436      else
    00437      {
    00438        Usb_enable_stall_handshake();
    00439        Usb_ack_receive_setup();
    00440      }
    00441      return;
    00442    }
    00443    else if (bmRequestType == INTERFACE_TYPE)
    00444    {
    00447       Usb_enable_stall_handshake();
    00448       Usb_ack_receive_setup();
    00449       return;
    00450    }
    00451    else if (bmRequestType == ENDPOINT_TYPE)
    00452    {
    00453       wValue = Usb_read_byte();
    00454       dummy    = Usb_read_byte();
    00455
    00456       if (wValue == FEATURE_ENDPOINT_HALT)
    00457       {
    00458          wIndex = (Usb_read_byte() & MSK_EP_DIR);
    00459
    00460          if (wIndex == EP_CONTROL)
    00461          {
    00462             Usb_enable_stall_handshake();
    00463             Usb_ack_receive_setup();
    00464             return;
    00465          }
    00466
    00467          Usb_select_endpoint(wIndex);
    00468          if(Is_usb_endpoint_enabled())
    00469          {
    00470             Usb_enable_stall_handshake();
    00471             Usb_select_endpoint(EP_CONTROL);
    00472             endpoint_status[wIndex] = 0x01;
    00473             Usb_ack_receive_setup();
    00474             Usb_send_control_in();
    00475          }
    00476          else
    00477          {
    00478             Usb_select_endpoint(EP_CONTROL);
    00479             Usb_enable_stall_handshake();
    00480             Usb_ack_receive_setup();
    00481             return;
    00482          }
    00483       }
    00484       else
    00485       {
    00486          Usb_enable_stall_handshake();
    00487          Usb_ack_receive_setup();
    00488          return;
    00489       }
    00490    }
    00491 }

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 372 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().

    00373 {
    00374 U8 wIndex;
    00375 U8 dummy;
    00376
    00377    dummy    = Usb_read_byte();
    00378    dummy    = Usb_read_byte();
    00379    wIndex = Usb_read_byte();
    00380
    00381    switch(bmRequestType)
    00382    {
    00383     case REQUEST_DEVICE_STATUS:    Usb_ack_receive_setup();
    00384                                    Usb_write_byte(device_status);
    00385                                    break;
    00386
    00387     case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
    00388                                    Usb_write_byte(INTERFACE_STATUS);
    00389                                    break;
    00390
    00391     case REQUEST_ENDPOINT_STATUS:  Usb_ack_receive_setup();
    00392                                    wIndex = wIndex & MSK_EP_DIR;
    00393                                    Usb_write_byte(endpoint_status[wIndex]);
    00394                                    break;
    00395     default:
    00396                                    Usb_enable_stall_handshake();
    00397                                    Usb_ack_receive_setup();
    00398                                    return;
    00399    }
    00400
    00401    Usb_write_byte(0x00);
    00402    Usb_send_control_in();
    00403
    00404    while( !Is_usb_receive_out() );
    00405    Usb_ack_receive_out();
    00406 }

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 350 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().

    00351 {
    00352    Usb_ack_receive_setup();
    00353
    00354    Usb_write_byte(usb_configuration_nb);
    00355    Usb_ack_in_ready();
    00356
    00357    while( !Is_usb_receive_out() );
    00358    Usb_ack_receive_out();
    00359 }

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

References Is_usb_receive_out, Usb_ack_receive_out, Usb_ack_receive_setup, and Usb_send_control_in.

Referenced by usb_process_request().

    00588 {
    00589    Usb_ack_receive_setup();
    00590    Usb_send_control_in();
    00591
    00592    while( !Is_usb_receive_out() );
    00593    Usb_ack_receive_out();
    00594
    00595             //Usb_enable_stall_handshake();
    00596             //Usb_ack_receive_setup();
    00597 }

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 609 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().

    00610 {
    00611   Usb_ack_receive_setup();
    00612   Usb_send_control_in();
    00613   while(!Is_usb_in_ready());
    00614 }

## Variable Documentation

bit zlp`[static]`

Definition at line 56 of file usb_standard_request.c.

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

U8 endpoint_status[4]`[static]`

Definition at line 57 of file usb_standard_request.c.

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

U8 device_status = 1`[static]`

Definition at line 58 of file usb_standard_request.c.

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

U8 code* pbuffer

Definition at line 63 of file usb_standard_request.c.

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

U8 data_to_transfer

Definition at line 65 of file usb_standard_request.c.

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

U16 wInterface

Definition at line 67 of file usb_standard_request.c.

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

U8 bmRequestType`[static]`

Definition at line 69 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 42 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