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 
00059 #ifdef AVRGCC
00060         PGM_VOID_P pbuffer;
00061 #else
00062         U8   code *pbuffer;
00063 #endif
00064         U8   data_to_transfer;
00065 
00066         U16  wInterface;
00067 
00068 static  U8   bmRequestType;
00069 
00070         U8   usb_configuration_nb;
00071 extern  bit     usb_connected;
00072 extern  code    S_usb_device_descriptor             usb_user_device_descriptor;
00073 extern  code    S_usb_user_configuration_descriptor usb_user_configuration_descriptor;
00074 
00075 
00076 
00077 
00088 void usb_enum_var_init (void)
00089 {
00090 U8 ep_num ;
00091 
00092    for( ep_num=0 ; ep_num<NB_ENDPOINTS ; ep_num++ )
00093    {
00094       endpoint_status[ep_num] = 0;
00095    }
00096    usb_connected        = FALSE;         // USB is not connected
00097    usb_configuration_nb = 0    ;         // Default configuration number is 0
00098 }
00099 
00100 
00119 void usb_process_request(void)
00120 {
00121    U8  bmRequest;
00122 
00123    bmRequestType = Usb_read_byte();
00124    bmRequest     = Usb_read_byte();
00125 
00126    switch (bmRequest)
00127    {
00128     case GET_DESCRIPTOR:
00129          if (0x80 == bmRequestType) { usb_get_descriptor(); }
00130          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00131          break;
00132 
00133     case GET_CONFIGURATION:
00134          if (0x80 == bmRequestType) { usb_get_configuration(); }
00135          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00136          break;
00137 
00138     case SET_ADDRESS:
00139          if (0x00 == bmRequestType) { usb_set_address(); }
00140          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00141          break;
00142 
00143     case SET_CONFIGURATION:
00144          if (0x00 == bmRequestType) { usb_set_configuration(); }
00145          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00146          break;
00147 
00148     case CLEAR_FEATURE:
00149          if (0x02 >= bmRequestType) { usb_clear_feature(); }
00150          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00151          break;
00152 
00153     case SET_FEATURE:
00154          if (0x02 >= bmRequestType) { usb_set_feature(); }
00155          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00156          break;
00157 
00158     case GET_STATUS:
00159          if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
00160                                     { usb_get_status(); }
00161          else                       { usb_user_read_request(bmRequestType, bmRequest); }
00162          break;
00163 
00164     case GET_INTERFACE:
00165           if (bmRequestType == 0x81) { usb_get_interface(); }
00166           else { usb_user_read_request(bmRequestType, bmRequest); }
00167           break;
00168 
00169 
00170     case SET_INTERFACE:
00171       if (bmRequestType == 0x01) {usb_set_interface();}
00172       break;
00173 
00174     case SET_DESCRIPTOR:
00175     case SYNCH_FRAME:
00176     default: 
00177          if(usb_user_read_request(bmRequestType, bmRequest) == FALSE)
00178          {
00179             Usb_enable_stall_handshake();
00180             Usb_ack_receive_setup();
00181             return;
00182          }
00183          break;
00184   }
00185 }
00186 
00187 
00199 void usb_set_address(void)
00200 {
00201    Usb_configure_address(Usb_read_byte());
00202 
00203    Usb_ack_receive_setup();
00204 
00205    Usb_send_control_in();                    
00206    while(!Is_usb_in_ready());                
00207 
00208    Usb_enable_address();
00209 }
00210 
00211 
00225 void usb_set_configuration( void )
00226 {
00227 U8 configuration_number;
00228 
00229    configuration_number = Usb_read_byte();
00230 
00231    if (configuration_number <= NB_CONFIGURATION)
00232    {
00233       Usb_ack_receive_setup();
00234       usb_configuration_nb = configuration_number;
00235    }
00236    else
00237    {
00240       Usb_enable_stall_handshake();
00241       Usb_ack_receive_setup();
00242       return;
00243    }
00244 
00245    Usb_send_control_in();                    
00246 
00247    usb_user_endpoint_init(usb_configuration_nb);  
00248    Usb_set_configuration_action();
00249 }
00250 
00251 
00266 void usb_get_descriptor(void)
00267 {
00268 U16  wLength         ;
00269 U8  descriptor_type ;
00270 U8  string_type     ;
00271 U8  dummy;
00272 U8  nb_byte;
00273 
00274    zlp             = FALSE;                  /* no zero length packet */
00275    string_type     = Usb_read_byte();        /* read LSB of wValue    */
00276    descriptor_type = Usb_read_byte();        /* read MSB of wValue    */
00277 
00278    switch (descriptor_type)
00279    {
00280     case DEVICE_DESCRIPTOR:
00281       data_to_transfer = Usb_get_dev_desc_length(); 
00282       pbuffer          = Usb_get_dev_desc_pointer();
00283       break;
00284     case CONFIGURATION_DESCRIPTOR:
00285       data_to_transfer = Usb_get_conf_desc_length(); 
00286       pbuffer          = Usb_get_conf_desc_pointer();
00287       break;
00288     default:
00289       if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE )
00290       {
00291          Usb_enable_stall_handshake();
00292          Usb_ack_receive_setup();
00293          return;
00294       }
00295       break;
00296    }
00297 
00298    dummy = Usb_read_byte();                     
00299    dummy = Usb_read_byte();
00300    LSB(wLength) = Usb_read_byte();              
00301    MSB(wLength) = Usb_read_byte();
00302    Usb_ack_receive_setup() ;                  
00303 
00304    if (wLength > data_to_transfer)
00305    {
00306       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00307       else { zlp = FALSE; }                   
00308    }
00309    else
00310    {
00311       data_to_transfer = (U8)wLength;         
00312    }
00313 
00314    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00315    {
00316       while(!Is_usb_read_control_enabled());
00317 
00318       nb_byte=0;
00319       while(data_to_transfer != 0)        
00320       {
00321          if(nb_byte++==EP_CONTROL_LENGTH) 
00322          {
00323             break;
00324          }
00325 #ifndef AVRGCC
00326          Usb_write_byte(*pbuffer++);
00327 #else    // AVRGCC does not support point to PGM space
00328 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00329          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00330 #endif
00331          data_to_transfer --;
00332       }
00333       Usb_send_control_in();
00334    }
00335 
00336    Usb_send_control_in();
00337 
00338    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 
00339    if(zlp == TRUE)
00340    {
00341      while(!Is_usb_read_control_enabled());
00342      Usb_send_control_in();
00343    }
00344 
00345 
00346    while(!Is_usb_receive_out());
00347    Usb_ack_receive_out();
00348 }
00349 
00350 
00362 void usb_get_configuration(void)
00363 {
00364    Usb_ack_receive_setup();
00365 
00366    Usb_write_byte(usb_configuration_nb);
00367    Usb_ack_in_ready();
00368 
00369    while( !Is_usb_receive_out() );
00370    Usb_ack_receive_out();
00371 }
00372 
00384 void usb_get_status(void)
00385 {
00386 U8 wIndex;
00387 U8 dummy;
00388 
00389    dummy    = Usb_read_byte();                 
00390    dummy    = Usb_read_byte();                 
00391    wIndex = Usb_read_byte();
00392 
00393    switch(bmRequestType)
00394    {
00395     case REQUEST_DEVICE_STATUS:    Usb_ack_receive_setup();
00396                                    Usb_write_byte(DEVICE_STATUS);
00397                                    break;
00398 
00399     case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
00400                                    Usb_write_byte(INTERFACE_STATUS);
00401                                    break;
00402 
00403     case REQUEST_ENDPOINT_STATUS:  Usb_ack_receive_setup();
00404                                    wIndex = wIndex & MSK_EP_DIR;
00405                                    Usb_write_byte(endpoint_status[wIndex]);
00406                                    break;
00407     default:
00408                                    Usb_enable_stall_handshake();
00409                                    Usb_ack_receive_setup();
00410                                    return;
00411    }
00412 
00413    Usb_write_byte(0x00);
00414    Usb_send_control_in();
00415 
00416    while( !Is_usb_receive_out() );
00417    Usb_ack_receive_out();
00418 }
00419 
00420 
00432 void usb_set_feature(void)
00433 {
00434 U8 wValue;
00435 U8 wIndex;
00436 U8 dummy;
00437 
00438    if (bmRequestType == INTERFACE_TYPE)
00439    {
00442       Usb_enable_stall_handshake();
00443       Usb_ack_receive_setup();
00444       return;
00445    }
00446    else if (bmRequestType == ENDPOINT_TYPE)
00447    {
00448       wValue = Usb_read_byte();
00449       dummy    = Usb_read_byte();                
00450 
00451       if (wValue == FEATURE_ENDPOINT_HALT)
00452       {
00453          wIndex = (Usb_read_byte() & MSK_EP_DIR);
00454 
00455          if (wIndex == EP_CONTROL)
00456          {
00457             Usb_enable_stall_handshake();
00458             Usb_ack_receive_setup();
00459             return;
00460          }
00461 
00462          Usb_select_endpoint(wIndex);
00463          if(Is_usb_endpoint_enabled())
00464          {
00465             Usb_enable_stall_handshake();
00466             Usb_select_endpoint(EP_CONTROL);
00467             endpoint_status[wIndex] = 0x01;
00468             Usb_ack_receive_setup();
00469             Usb_send_control_in();
00470          }
00471          else
00472          {
00473             Usb_enable_stall_handshake();
00474             Usb_ack_receive_setup();
00475             return;
00476          }
00477       }
00478       else
00479       {
00480          Usb_enable_stall_handshake();
00481          Usb_ack_receive_setup();
00482          return;
00483       }
00484    }
00485 }
00486 
00487 
00498 void usb_clear_feature(void)
00499 {
00500 U8 wValue;
00501 U8 wIndex;
00502 U8 dummy;
00503 
00504    if (bmRequestType == ZERO_TYPE)
00505    {
00508       Usb_enable_stall_handshake();
00509       Usb_ack_receive_setup();
00510       return;
00511    }
00512    else if (bmRequestType == INTERFACE_TYPE)
00513    {
00516       Usb_enable_stall_handshake();
00517       Usb_ack_receive_setup();
00518       return;
00519    }
00520    else if (bmRequestType == ENDPOINT_TYPE)
00521    {
00522       wValue = Usb_read_byte();
00523       dummy  = Usb_read_byte();                
00524 
00525       if (wValue == FEATURE_ENDPOINT_HALT)
00526       {
00527          wIndex = (Usb_read_byte() & MSK_EP_DIR);
00528 
00529          Usb_select_endpoint(wIndex);
00530          if(Is_usb_endpoint_enabled())
00531          {
00532             if(wIndex != EP_CONTROL)
00533             {
00534                Usb_disable_stall_handshake();
00535                Usb_reset_endpoint(wIndex);
00536                Usb_reset_data_toggle();
00537             }
00538             Usb_select_endpoint(EP_CONTROL);
00539             endpoint_status[wIndex] = 0x00;
00540             Usb_ack_receive_setup();
00541             Usb_send_control_in();
00542          }
00543          else
00544          {
00545             Usb_enable_stall_handshake();
00546             Usb_ack_receive_setup();
00547             return;
00548          }
00549       }
00550       else
00551       {
00552          Usb_enable_stall_handshake();
00553          Usb_ack_receive_setup();
00554          return;
00555       }
00556    }
00557 }
00558 
00559 
00560 
00571 void usb_get_interface (void)
00572 {
00573   Usb_enable_stall_handshake();
00574   Usb_ack_receive_setup();
00575 }
00576 
00587 void usb_set_interface (void)
00588 {
00589   Usb_ack_receive_setup();
00590   Usb_send_control_in();                    
00591   while(!Is_usb_in_ready());
00592 }

Generated on Fri Mar 17 16:02:03 2006 for Atmel by  doxygen 1.4.6-NO