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

- File List
- Globals

# usb_standard_request.c

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008
    00009
    00010
    00011
    00012
    00013
    00014
    00015
    00016
    00017
    00018
    00019
    00020
    00021
    00022
    00023 //_____ I N C L U D E S ____________________________________________________
    00024
    00025 #include "config.h"
    00026 #include "conf_usb.h"
    00027 #include "lib_mcu\usb\usb_drv.h"
    00028 #include "usb_descriptors.h"
    00029 #include "modules\usb\device_chap9\usb_standard_request.h"
    00030 #include "usb_specific_request.h"
    00031
    00032
    00033 //_____ M A C R O S ________________________________________________________
    00034
    00035
    00036 //_____ D E F I N I T I O N ________________________________________________
    00037
    00038 //_____ P R I V A T E   D E C L A R A T I O N ______________________________
    00039
    00040 static  void    usb_get_descriptor(   void);
    00041 static  void    usb_set_address(      void);
    00042 static  void    usb_set_configuration(void);
    00043 static  void    usb_clear_feature(    void);
    00044 static  void    usb_set_feature(      void);
    00045 static  void    usb_get_status(       void);
    00046 static  void    usb_get_configuration(void);
    00047 static  void    usb_get_interface (void);
    00048 static  void    usb_set_interface (void);
    00049
    00050
    00051
    00052
    00053
    00054 //_____ D E C L A R A T I O N ______________________________________________
    00055
    00056 static  bit  zlp;
    00057 static  U8   endpoint_status[NB_ENDPOINTS];
    00058 static  U8   device_status=DEVICE_STATUS;
    00059
    00060 #ifdef AVRGCC
    00061         PGM_VOID_P pbuffer;
    00062 #else
    00063         U8   code *pbuffer;
    00064 #endif
    00065         U8   data_to_transfer;
    00066
    00067         U16  wInterface;
    00068
    00069 static  U8   bmRequestType;
    00070
    00071         U8   usb_configuration_nb;
    00072 extern  bit     usb_connected;
    00073 extern  code    S_usb_device_descriptor             usb_user_device_descriptor;
    00074 extern  code    S_usb_user_configuration_descriptor usb_user_configuration_descriptor;
    00075
    00076 U8      usb_remote_wup_feature;  // Store ENABLED value if a SetFeature(RemoteWakeUp) has been received
    00077
    00078
    00097 void usb_process_request(void)
    00098 {
    00099    U8  bmRequest;
    00100
    00101    UEINTX &= ~(1<<RXOUTI);
    00102
    00103    bmRequestType = Usb_read_byte();
    00104    bmRequest     = Usb_read_byte();
    00105
    00106    switch (bmRequest)
    00107    {
    00108     case GET_DESCRIPTOR:
    00109          if (0x80 == bmRequestType) { usb_get_descriptor(); }
    00110          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00111          break;
    00112
    00113     case GET_CONFIGURATION:
    00114          if (0x80 == bmRequestType) { usb_get_configuration(); }
    00115          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00116          break;
    00117
    00118     case SET_ADDRESS:
    00119          if (0x00 == bmRequestType) { usb_set_address(); }
    00120          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00121          break;
    00122
    00123     case SET_CONFIGURATION:
    00124          if (0x00 == bmRequestType) { usb_set_configuration(); }
    00125          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00126          break;
    00127
    00128     case CLEAR_FEATURE:
    00129          if (0x02 >= bmRequestType) { usb_clear_feature(); }
    00130          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00131          break;
    00132
    00133     case SET_FEATURE:
    00134          if (0x02 >= bmRequestType) { usb_set_feature(); }
    00135          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00136          break;
    00137
    00138     case GET_STATUS:
    00139          if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
    00140                                     { usb_get_status(); }
    00141          else                       { usb_user_read_request(bmRequestType, bmRequest); }
    00142          break;
    00143
    00144     case GET_INTERFACE:
    00145           if (bmRequestType == 0x81) { usb_get_interface(); }
    00146           else { usb_user_read_request(bmRequestType, bmRequest); }
    00147           break;
    00148
    00149
    00150     case SET_INTERFACE:
    00151       if (bmRequestType == 0x01) {usb_set_interface();}
    00152       break;
    00153
    00154     case SET_DESCRIPTOR:
    00155     case SYNCH_FRAME:
    00156     default:
    00157          if(usb_user_read_request(bmRequestType, bmRequest) == FALSE)
    00158          {
    00159             Usb_enable_stall_handshake();
    00160             Usb_ack_receive_setup();
    00161             return;
    00162          }
    00163          break;
    00164   }
    00165 }
    00166
    00167
    00179 void usb_set_address(void)
    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 }
    00190
    00191
    00205 void usb_set_configuration( void )
    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 }
    00230
    00231
    00246 void usb_get_descriptor(void)
    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 }
    00337
    00338
    00350 void usb_get_configuration(void)
    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 }
    00360
    00372 void usb_get_status(void)
    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 }
    00407
    00408
    00420 void usb_set_feature(void)
    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 }
    00492
    00493
    00504 void usb_clear_feature(void)
    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 }
    00574
    00575
    00576
    00587 void usb_get_interface (void)
    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 }
    00598
    00609 void usb_set_interface (void)
    00610 {
    00611   Usb_ack_receive_setup();
    00612   Usb_send_control_in();
    00613   while(!Is_usb_in_ready());
    00614 }