usb_specific_request.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00016 
00017 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms, with or without
00020  * modification, are permitted provided that the following conditions are met:
00021  *
00022  * 1. Redistributions of source code must retain the above copyright notice,
00023  * this list of conditions and the following disclaimer.
00024  *
00025  * 2. Redistributions in binary form must reproduce the above copyright notice,
00026  * this list of conditions and the following disclaimer in the documentation
00027  * and/or other materials provided with the distribution.
00028  *
00029  * 3. The name of ATMEL may not be used to endorse or promote products derived
00030  * from this software without specific prior written permission.
00031  *
00032  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00034  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00042  */
00043 
00044 
00045 //_____ I N C L U D E S ____________________________________________________
00046 
00047 #include "config.h"
00048 #include "conf_usb.h"
00049 #include "lib_mcu/usb/usb_drv.h"
00050 #include "usb_descriptors.h"
00051 #include "modules/usb/device_chap9/usb_standard_request.h"
00052 #include "usb_specific_request.h"
00053 #if ((USB_DEVICE_SN_USE==ENABLE) && (USE_DEVICE_SN_UNIQUE==ENABLE))
00054 #include "lib_mcu/flash/flash_drv.h"
00055 #endif
00056 
00057 //_____ D E F I N I T I O N ________________________________________________
00058 
00059 #ifdef __GNUC__
00060 extern PGM_VOID_P pbuffer;
00061 #else
00062 extern U8   code *pbuffer;
00063 #endif
00064 extern U8   data_to_transfer;
00065 extern code S_usb_hid_report_descriptor_mouse usb_hid_report_descriptor_mouse;
00066 
00067 U8 g_u8_report_rate=0;
00068 
00069 //_____ D E C L A R A T I O N ______________________________________________
00070 
00071 void hid_get_report_descriptor(void);
00072 void usb_hid_set_report_ouput(void);
00073 void usb_hid_set_idle (U8 u8_report_id, U8 u8_duration );
00074 void usb_hid_get_idle (U8 u8_report_id);
00075 void hid_get_hid_descriptor(void);
00076 
00077 
00086 Bool usb_user_read_request(U8 type, U8 request)
00087 {
00088    U8    wValue_msb;
00089    U8    wValue_lsb;
00090 
00091    // Read wValue
00092    wValue_lsb = Usb_read_byte();
00093    wValue_msb = Usb_read_byte();
00094 
00095    //** Specific request from Class HID
00096    if( USB_SETUP_GET_STAND_INTERFACE == type )
00097    {
00098       switch( request )
00099       {
00100          case SETUP_GET_DESCRIPTOR:
00101          switch( wValue_msb ) // Descriptor ID
00102          {
00103             case DESCRIPTOR_HID:
00104             hid_get_hid_descriptor();
00105             return TRUE;
00106             break;
00107    
00108             case DESCRIPTOR_REPORT:
00109             hid_get_report_descriptor();
00110             return TRUE;
00111    
00112             case DESCRIPTOR_PHYSICAL:
00113             // TODO
00114             break;
00115          }
00116          break;
00117       }
00118    }
00119    if( USB_SETUP_SET_CLASS_INTER == type )
00120    {
00121       switch( request )
00122       {
00123          case SETUP_HID_SET_REPORT:
00124          // The MSB wValue field specifies the Report Type
00125          // The LSB wValue field specifies the Report ID
00126          switch (wValue_msb)
00127          {
00128             case REPORT_TYPE_INPUT:
00129             // TODO
00130             break;
00131             
00132             case REPORT_TYPE_OUTPUT:
00133             usb_hid_set_report_ouput();
00134             return TRUE;
00135             break;
00136 
00137             case REPORT_TYPE_FEATURE:
00138             break;
00139          }
00140          break;
00141 
00142          case SETUP_HID_SET_IDLE:
00143          usb_hid_set_idle(wValue_lsb,wValue_msb);
00144          return TRUE;
00145    
00146          case SETUP_HID_SET_PROTOCOL:
00147          // TODO
00148          break;
00149       }
00150    }
00151    if( USB_SETUP_GET_CLASS_INTER == type )
00152    {
00153       switch( request )
00154       {
00155          case SETUP_HID_GET_REPORT:
00156          // TODO
00157          break;
00158          case SETUP_HID_GET_IDLE:
00159          usb_hid_get_idle(wValue_lsb);
00160          return TRUE;
00161          case SETUP_HID_GET_PROTOCOL:
00162          // TODO
00163          break;
00164       }
00165    }
00166    return FALSE;  // No supported request
00167 }
00168 
00169 
00174 void usb_user_endpoint_init(U8 conf_nb)
00175 {
00176    usb_configure_endpoint( EP_MOUSE_IN,   \
00177                            TYPE_INTERRUPT,\
00178                            DIRECTION_IN,  \
00179                            SIZE_8,        \
00180                            ONE_BANK,      \
00181                            NYET_ENABLED);
00182 }
00183 
00184 
00191 U8   usb_user_interface_get( U16 wInterface )
00192 {
00193    return 0;  // Only one alternate setting possible for all interface
00194 }
00195 
00196 
00202 void usb_user_interface_reset(U16 wInterface, U8 alternate_setting)
00203 {  
00204    // default setting selected = reset data toggle
00205    if( INTERFACE_NB_MOUSE == wInterface )
00206    {
00207       // Interface mouse
00208       Usb_select_endpoint(EP_MOUSE_IN);
00209       Usb_disable_stall_handshake();
00210       Usb_reset_endpoint(EP_MOUSE_IN);
00211       Usb_reset_data_toggle();
00212    }
00213 }
00214 
00215    
00223 Bool usb_user_get_descriptor(U8 type, U8 string)
00224 {
00225    switch(type)
00226    {
00227       case DESCRIPTOR_STRING:
00228       switch (string)
00229       {
00230          case LANG_ID:
00231          data_to_transfer = sizeof (usb_user_language_id);
00232          pbuffer = &(usb_user_language_id.bLength);
00233          return TRUE;
00234          break;
00235         
00236          case MAN_INDEX:
00237          data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
00238          pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
00239          return TRUE;
00240          break;
00241         
00242          case PROD_INDEX:
00243          data_to_transfer = sizeof (usb_user_product_string_descriptor);
00244          pbuffer = &(usb_user_product_string_descriptor.bLength);
00245          return TRUE;
00246          break;
00247            
00248 #if (USB_DEVICE_SN_USE==ENABLE)              
00249          case SN_INDEX:
00250          data_to_transfer = sizeof (usb_user_serial_number);
00251          pbuffer = &(usb_user_serial_number.bLength);
00252 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00253          f_get_serial_string=TRUE;
00254          data_to_transfer += (SN_LENGTH*4);
00255 #endif
00256          return TRUE;
00257          break;
00258 #endif
00259       }
00260       break;
00261    }
00262    return FALSE;
00263 }
00264 
00265 
00268 void hid_get_report_descriptor(void)
00269 {
00270    U16 wLength;
00271    U8  nb_byte;
00272    bit zlp = FALSE;
00273    U16 wInterface;
00274 
00275    LSB(wInterface)=Usb_read_byte();
00276    MSB(wInterface)=Usb_read_byte();
00277 
00278    data_to_transfer = sizeof(usb_hid_report_descriptor_mouse);
00279    pbuffer = &(usb_hid_report_descriptor_mouse.report[0]);
00280 
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;           // send only requested number of data
00293    }
00294 
00295    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00296    {
00297       while(!Is_usb_read_control_enabled());
00298 
00299       nb_byte=0;
00300       while(data_to_transfer != 0)              // Send data until necessary
00301       {
00302          if(nb_byte++==EP_CONTROL_LENGTH)       // Check endpoint 0 size
00303          {
00304             break;
00305          }
00306 #ifndef __GNUC__
00307          Usb_write_byte(*pbuffer++);
00308 #else    // AVRGCC does not support point to PGM space
00309 //warning with AVRGCC assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00310          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00311 #endif
00312          data_to_transfer --;
00313       }
00314       Usb_send_control_in();
00315    }
00316 
00317    if(Is_usb_receive_out())
00318    { 
00319       // abort from Host
00320       Usb_ack_receive_out();
00321       return;
00322    }
00323    if(zlp == TRUE)
00324    { 
00325       while(!Is_usb_read_control_enabled());
00326       Usb_send_control_in();
00327    }
00328 
00329    while(!Is_usb_receive_out());
00330    Usb_ack_receive_out();
00331 }
00332 
00333 
00336 void usb_hid_set_report_ouput (void)
00337 {
00338    Usb_ack_receive_setup();
00339    Usb_send_control_in();
00340 
00341    while(!Is_usb_receive_out());
00342    Usb_ack_receive_out();
00343    Usb_send_control_in();
00344 }
00345 
00346 
00352 void usb_hid_set_idle (U8 u8_report_id, U8 u8_duration )
00353 {
00354    U16 wInterface;
00355    
00356    // Get interface number to put in idle mode
00357    LSB(wInterface)=Usb_read_byte();
00358    MSB(wInterface)=Usb_read_byte();
00359    Usb_ack_receive_setup();
00360   
00361    g_u8_report_rate = u8_duration;
00362    
00363    Usb_send_control_in();
00364    while(!Is_usb_in_ready());
00365 }
00366 
00367 
00372 void usb_hid_get_idle (U8 u8_report_id)
00373 {
00374    U16 wLength;
00375    U16 wInterface;
00376 
00377    // Get interface number to put in idle mode
00378    LSB(wInterface)= Usb_read_byte();
00379    MSB(wInterface)= Usb_read_byte();
00380    LSB(wLength)   = Usb_read_byte();
00381    MSB(wLength)   = Usb_read_byte();
00382    Usb_ack_receive_setup();
00383    
00384    if( wLength != 0 )
00385    {
00386       Usb_write_byte(g_u8_report_rate);
00387       Usb_send_control_in();
00388    }
00389    
00390    while(!Is_usb_receive_out());
00391    Usb_ack_receive_out();
00392 }
00393 
00394 
00397 void hid_get_hid_descriptor(void)
00398 {
00399    U16 wLength;
00400    U8  nb_byte;
00401    bit zlp=FALSE;
00402    U16 wInterface;
00403 
00404    LSB(wInterface)=Usb_read_byte();
00405    MSB(wInterface)=Usb_read_byte();
00406 
00407    data_to_transfer = sizeof(usb_conf_desc.hid_mouse);
00408    pbuffer = &(usb_conf_desc.hid_mouse.bLength);
00409 
00410    LSB(wLength) = Usb_read_byte();
00411    MSB(wLength) = Usb_read_byte();
00412    Usb_ack_receive_setup();
00413 
00414    if (wLength > data_to_transfer)
00415    {
00416       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00417       else { zlp = FALSE; }                     // no need of zero length packet
00418    }
00419    else
00420    {
00421       data_to_transfer = (U8)wLength;           // send only requested number of data
00422    }
00423 
00424    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00425    {
00426       while(!Is_usb_read_control_enabled());
00427 
00428       nb_byte=0;
00429       while(data_to_transfer != 0)              // Send data until necessary
00430       {
00431          if(nb_byte++==EP_CONTROL_LENGTH)       // Check endpoint 0 size
00432          {
00433             break;
00434          }
00435 #ifndef __GNUC__
00436          Usb_write_byte(*pbuffer++);
00437 #else    // AVRGCC does not support point to PGM space
00438 //warning with AVRGCC assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00439          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00440 #endif
00441          data_to_transfer --;
00442       }
00443       Usb_send_control_in();
00444    }
00445 
00446    if(Is_usb_receive_out())
00447    { 
00448       // abort from Host
00449       Usb_ack_receive_out();
00450       return;
00451    }
00452    if(zlp == TRUE)
00453    { 
00454       while(!Is_usb_read_control_enabled());
00455       Usb_send_control_in();
00456    }
00457 
00458    while(!Is_usb_receive_out());
00459    Usb_ack_receive_out();
00460 }
00461 

Generated on Fri Oct 31 14:47:07 2008 for ATMEL by  doxygen 1.5.3