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

- File List
- Globals

# usb_specific_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 //_____ I N C L U D E S ____________________________________________________
    00020
    00021 #include "config.h"
    00022 #include "conf_usb.h"
    00023 #include "lib_mcu\usb\usb_drv.h"
    00024 #include "usb_descriptors.h"
    00025 #include "modules\usb\device_chap9\usb_standard_request.h"
    00026 #include "usb_specific_request.h"
    00027 #include "modules\control_access\ctrl_access.h"
    00028
    00029 //_____ M A C R O S ________________________________________________________
    00030
    00031 //_____ D E F I N I T I O N ________________________________________________
    00032        bit  ms_multiple_drive;
    00033 //_____ P R I V A T E   D E C L A R A T I O N ______________________________
    00034
    00035 #ifdef AVRGCC
    00036 extern PGM_VOID_P pbuffer;
    00037 #else
    00038 extern U8   code *pbuffer;
    00039 #endif
    00040 extern U8   data_to_transfer;
    00041 extern code S_usb_hid_report_descriptor_mouse usb_hid_report_descriptor_mouse;
    00042 extern U16  wInterface;
    00043
    00044
    00045 //_____ D E C L A R A T I O N ______________________________________________
    00046
    00059 Bool usb_user_read_request(U8 type, U8 request)
    00060 {
    00061 U8  descriptor_type ;
    00062 U8  string_type     ;
    00063
    00064    string_type     = Usb_read_byte();
    00065    descriptor_type = Usb_read_byte();
    00066    switch(request)
    00067    {
    00068       case GET_DESCRIPTOR:
    00069
    00070          switch (descriptor_type)
    00071          {
    00072             case REPORT:
    00073                hid_get_report();
    00074                return TRUE;
    00075                break;
    00076
    00077             case HID:
    00078                                         hid_get_hid_descriptor();
    00079                return TRUE;
    00080                break;
    00081             default:
    00082                return FALSE;
    00083                break;
    00084          }
    00085          break;
    00086
    00087        case SET_CONFIGURATION:
    00088          switch (descriptor_type)
    00089          {
    00090             case SET_REPORT:
    00091                hid_set_report();
    00092                return TRUE;
    00093                break;
    00094
    00095             default:
    00096                return FALSE;
    00097                break;
    00098          }
    00099          break;
    00100    case GET_INTERFACE:
    00101 //      usb_hid_set_idle();
    00102       usb_hid_get_interface();
    00103       return TRUE;
    00104       break;
    00105
    00106     case MASS_STORAGE_RESET:
    00107          Usb_ack_receive_setup();
    00108          Usb_send_control_in();
    00109          return TRUE;
    00110          break;
    00111
    00112     case GET_MAX_LUN:
    00113          Usb_ack_receive_setup();
    00114          Usb_write_byte( (get_nb_lun()-1) );
    00115          Usb_send_control_in();
    00116          ms_multiple_drive = 1;
    00117          return TRUE;
    00118          break;
    00119
    00120       default:
    00121          return FALSE;
    00122          break;
    00123
    00124    }
    00125    return FALSE;
    00126 }
    00127
    00128
    00129
    00138 void usb_user_endpoint_init(U8 conf_nb)
    00139 {
    00140  usb_configure_endpoint(EP_MOUSE_IN,      \
    00141                          TYPE_INTERRUPT,     \
    00142                          DIRECTION_IN,  \
    00143                          SIZE_8,       \
    00144                          ONE_BANK,     \
    00145                          NYET_ENABLED);
    00146
    00147  usb_configure_endpoint(EP_MS_IN,      \
    00148                          TYPE_BULK,     \
    00149                          DIRECTION_IN,  \
    00150                          SIZE_64,       \
    00151                          ONE_BANK,     \
    00152                          NYET_ENABLED);
    00153
    00154  usb_configure_endpoint(EP_MS_OUT,     \
    00155                          TYPE_BULK,     \
    00156                          DIRECTION_OUT, \
    00157                          SIZE_64,       \
    00158                          ONE_BANK,     \
    00159                          NYET_ENABLED);
    00160 }
    00161
    00162
    00172 Bool usb_user_get_descriptor(U8 type, U8 string)
    00173 {
    00174    switch(type)
    00175    {
    00176       case STRING_DESCRIPTOR:
    00177          switch (string)
    00178          {
    00179             case LANG_ID:
    00180                data_to_transfer = sizeof (usb_user_language_id);
    00181                pbuffer = &(usb_user_language_id.bLength);
    00182                return TRUE;
    00183                break;
    00184             case MAN_INDEX:
    00185                data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
    00186                pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
    00187                return TRUE;
    00188                break;
    00189             case PROD_INDEX:
    00190                data_to_transfer = sizeof (usb_user_product_string_descriptor);
    00191                pbuffer = &(usb_user_product_string_descriptor.bLength);
    00192                return TRUE;
    00193                break;
    00194             case SN_INDEX:
    00195                data_to_transfer = sizeof (usb_user_serial_number);
    00196                pbuffer = &(usb_user_serial_number.bLength);
    00197                return TRUE;
    00198                break;
    00199             default:
    00200                return FALSE;
    00201          }
    00202       default:
    00203          return FALSE;
    00204    }
    00205
    00206    return FALSE;
    00207 }
    00208
    00209
    00218 void hid_get_report(void)
    00219 {
    00220
    00221 U16 wLength;
    00222 U8  nb_byte;
    00223 bit zlp=0;
    00224
    00225
    00226
    00227    LSB(wInterface)=Usb_read_byte();
    00228    MSB(wInterface)=Usb_read_byte();
    00229
    00230    data_to_transfer = sizeof(usb_hid_report_descriptor_mouse);
    00231    pbuffer = &(usb_hid_report_descriptor_mouse.report[0]);
    00232
    00233    LSB(wLength) = Usb_read_byte();
    00234    MSB(wLength) = Usb_read_byte();
    00235    Usb_ack_receive_setup() ;
    00236
    00237    if (wLength > data_to_transfer)
    00238    {
    00239       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
    00240       else { zlp = FALSE; }
    00241    }
    00242    else
    00243    {
    00244       data_to_transfer = (U8)wLength;
    00245    }
    00246
    00247     while((data_to_transfer != 0) )
    00248    {
    00249       while(!Is_usb_read_control_enabled());
    00250
    00251       nb_byte=0;
    00252       while(data_to_transfer != 0)
    00253       {
    00254          if(nb_byte++==EP_CONTROL_LENGTH)
    00255          {
    00256             break;
    00257          }
    00258 #ifndef AVRGCC
    00259          Usb_write_byte(*pbuffer++);
    00260 #else    // AVRGCC does not support point to PGM space
    00261 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
    00262          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
    00263 #endif
    00264          data_to_transfer --;
    00265       }
    00266       Usb_send_control_in();
    00267    }
    00268
    00269    Usb_send_control_in();
    00270
    00271    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; }
    00272    if(zlp == TRUE)        { Usb_send_control_in(); }
    00273
    00274    while(!Is_usb_receive_out());
    00275    Usb_ack_receive_out();
    00276 }
    00277
    00278
    00279
    00288 void hid_set_report (void)
    00289 {
    00290    Usb_ack_receive_setup();
    00291    Usb_send_control_in();
    00292
    00293    while(!Is_usb_receive_out());
    00294    Usb_ack_receive_out();
    00295    Usb_send_control_in();
    00296
    00297 }
    00298
    00299
    00308 void usb_hid_set_idle (void)
    00309 {
    00310   U8 dummy;
    00311   dummy = Usb_read_byte();
    00312   dummy = Usb_read_byte();
    00313   LSB(wInterface)=Usb_read_byte();
    00314   MSB(wInterface)=Usb_read_byte();
    00315
    00316   Usb_ack_receive_setup();
    00317
    00318   Usb_send_control_in();                       /* send a ZLP for STATUS phase */
    00319   while(!Is_usb_in_ready());
    00320 }
    00321
    00322
    00331 void usb_hid_get_interface (void)
    00332 {
    00333   U8 dummy;
    00334   dummy = Usb_read_byte();
    00335   dummy = Usb_read_byte();
    00336   LSB(wInterface)=Usb_read_byte();
    00337   MSB(wInterface)=Usb_read_byte();
    00338
    00339   Usb_ack_receive_setup();
    00340
    00341   Usb_send_control_in();                       /* send a ZLP for STATUS phase */
    00342   while(!Is_usb_in_ready());
    00343 }
    00344
    00353 void hid_get_hid_descriptor(void)
    00354 {
    00355
    00356 U16 wLength;
    00357 U8  nb_byte;
    00358 bit zlp=0;
    00359
    00360
    00361
    00362    LSB(wInterface)=Usb_read_byte();
    00363    MSB(wInterface)=Usb_read_byte();
    00364
    00365    data_to_transfer = sizeof(usb_conf_desc.hid_mouse);
    00366    pbuffer = &(usb_conf_desc.hid_mouse.bLength);
    00367
    00368    LSB(wLength) = Usb_read_byte();
    00369    MSB(wLength) = Usb_read_byte();
    00370    Usb_ack_receive_setup() ;
    00371
    00372    if (wLength > data_to_transfer)
    00373    {
    00374       if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
    00375       else { zlp = FALSE; }
    00376    }
    00377    else
    00378    {
    00379       data_to_transfer = (U8)wLength;
    00380    }
    00381
    00382    while((data_to_transfer != 0) && (!Is_usb_receive_out()))
    00383    {
    00384       while(!Is_usb_read_control_enabled());
    00385
    00386       nb_byte=0;
    00387       while(data_to_transfer != 0)
    00388       {
    00389          if(nb_byte++==EP_CONTROL_LENGTH)
    00390          {
    00391             break;
    00392          }
    00393 #ifndef AVRGCC
    00394          Usb_write_byte(*pbuffer++);
    00395 #else    // AVRGCC does not support point to PGM space
    00396 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
    00397          Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
    00398 #endif
    00399          data_to_transfer --;
    00400       }
    00401       Usb_send_control_in();
    00402    }
    00403
    00404    Usb_send_control_in();
    00405
    00406    if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; }
    00407    if(zlp == TRUE)        { Usb_send_control_in(); }
    00408
    00409    while(!Is_usb_receive_out());
    00410    Usb_ack_receive_out();
    00411 }