00001
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
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
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_kbd usb_hid_report_descriptor_kbd;
00066
00067 U8 g_u8_report_rate=0;
00068
00069
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
00092 wValue_lsb = Usb_read_byte();
00093 wValue_msb = Usb_read_byte();
00094
00095
00096 if( USB_SETUP_GET_STAND_INTERFACE == type )
00097 {
00098 switch( request )
00099 {
00100 case SETUP_GET_DESCRIPTOR:
00101 switch( wValue_msb )
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
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
00125
00126 switch (wValue_msb)
00127 {
00128 case REPORT_TYPE_INPUT:
00129
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
00148 break;
00149 }
00150 }
00151 if( USB_SETUP_GET_CLASS_INTER == type )
00152 {
00153 switch( request )
00154 {
00155 case SETUP_HID_GET_REPORT:
00156
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
00163 break;
00164 }
00165 }
00166 return FALSE;
00167 }
00168
00169
00174 void usb_user_endpoint_init(U8 conf_nb)
00175 {
00176 usb_configure_endpoint( EP_KBD_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;
00194 }
00195
00196
00202 void usb_user_interface_reset(U16 wInterface, U8 alternate_setting)
00203 {
00204
00205 if( INTERFACE_NB_KBD == wInterface )
00206 {
00207
00208 Usb_select_endpoint(EP_KBD_IN);
00209 Usb_disable_stall_handshake();
00210 Usb_reset_endpoint(EP_KBD_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_kbd);
00279 pbuffer = &(usb_hid_report_descriptor_kbd.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;
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)
00301 {
00302 if(nb_byte++==EP_CONTROL_LENGTH)
00303 {
00304 break;
00305 }
00306 #ifndef __GNUC__
00307 Usb_write_byte(*pbuffer++);
00308 #else // AVRGCC does not support point to PGM space
00309
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
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
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
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_kbd);
00408 pbuffer = &(usb_conf_desc.hid_kbd.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; }
00418 }
00419 else
00420 {
00421 data_to_transfer = (U8)wLength;
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)
00430 {
00431 if(nb_byte++==EP_CONTROL_LENGTH)
00432 {
00433 break;
00434 }
00435 #ifndef __GNUC__
00436 Usb_write_byte(*pbuffer++);
00437 #else // AVRGCC does not support point to PGM space
00438
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
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