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
00048
00049
00050 #include "config.h"
00051 #include "conf_usb.h"
00052 #include "lib_mcu/usb/usb_drv.h"
00053 #include "usb_descriptors.h"
00054 #include "modules/usb/device_chap9/usb_standard_request.h"
00055 #include "usb_specific_request.h"
00056 #if ((USB_DEVICE_SN_USE==ENABLE) && (USE_DEVICE_SN_UNIQUE==ENABLE))
00057 #include "lib_mcu/flash/flash_drv.h"
00058 #endif
00059
00060
00061
00062 #ifdef __GNUC__
00063 extern PGM_VOID_P pbuffer;
00064 #else
00065 extern U8 code *pbuffer;
00066 #endif
00067 extern U8 data_to_transfer;
00068 extern code S_usb_hid_report_descriptor usb_hid_report_descriptor;
00069 U8 jump_bootloader=0;
00070
00071 U8 g_u8_report_rate=0;
00072
00073
00074
00075 void hid_get_report_descriptor(void);
00076 void usb_hid_set_report_ouput(void);
00077 void usb_hid_set_idle (U8 u8_report_id, U8 u8_duration );
00078 void usb_hid_get_idle (U8 u8_report_id);
00079 void hid_get_hid_descriptor(void);
00080 void usb_hid_set_report_feature(void);
00081
00082
00091 Bool usb_user_read_request(U8 type, U8 request)
00092 {
00093 U8 wValue_msb;
00094 U8 wValue_lsb;
00095
00096
00097 wValue_lsb = Usb_read_byte();
00098 wValue_msb = Usb_read_byte();
00099
00100
00101 if( USB_SETUP_GET_STAND_INTERFACE == type )
00102 {
00103 switch( request )
00104 {
00105 case SETUP_GET_DESCRIPTOR:
00106 switch( wValue_msb )
00107 {
00108 case DESCRIPTOR_HID:
00109 hid_get_hid_descriptor();
00110 return TRUE;
00111 break;
00112
00113 case DESCRIPTOR_REPORT:
00114 hid_get_report_descriptor();
00115 return TRUE;
00116
00117 case DESCRIPTOR_PHYSICAL:
00118
00119 break;
00120 }
00121 break;
00122 }
00123 }
00124 if( USB_SETUP_SET_CLASS_INTER == type )
00125 {
00126 switch( request )
00127 {
00128 case SETUP_HID_SET_REPORT:
00129
00130
00131 switch (wValue_msb)
00132 {
00133 case REPORT_TYPE_INPUT:
00134
00135 break;
00136
00137 case REPORT_TYPE_OUTPUT:
00138 usb_hid_set_report_ouput();
00139 return TRUE;
00140 break;
00141
00142 case REPORT_TYPE_FEATURE:
00143 usb_hid_set_report_feature();
00144 return TRUE;
00145 break;
00146 }
00147 break;
00148
00149 case SETUP_HID_SET_IDLE:
00150 usb_hid_set_idle(wValue_lsb,wValue_msb);
00151 return TRUE;
00152
00153 case SETUP_HID_SET_PROTOCOL:
00154
00155 break;
00156 }
00157 }
00158 if( USB_SETUP_GET_CLASS_INTER == type )
00159 {
00160 switch( request )
00161 {
00162 case SETUP_HID_GET_REPORT:
00163
00164 break;
00165 case SETUP_HID_GET_IDLE:
00166 usb_hid_get_idle(wValue_lsb);
00167 return TRUE;
00168 case SETUP_HID_GET_PROTOCOL:
00169
00170 break;
00171 }
00172 }
00173 return FALSE;
00174 }
00175
00176
00181 void usb_user_endpoint_init(U8 conf_nb)
00182 {
00183 usb_configure_endpoint( EP_HID_IN, \
00184 TYPE_INTERRUPT,\
00185 DIRECTION_IN, \
00186 SIZE_8, \
00187 ONE_BANK, \
00188 NYET_ENABLED);
00189
00190 usb_configure_endpoint( EP_HID_OUT, \
00191 TYPE_INTERRUPT,\
00192 DIRECTION_OUT, \
00193 SIZE_8, \
00194 ONE_BANK, \
00195 NYET_ENABLED);
00196 }
00197
00198
00205 U8 usb_user_interface_get( U16 wInterface )
00206 {
00207 return 0;
00208 }
00209
00210
00216 void usb_user_interface_reset(U16 wInterface, U8 alternate_setting)
00217 {
00218
00219 if( INTERFACE_NB == wInterface )
00220 {
00221
00222 Usb_select_endpoint(EP_HID_IN);
00223 Usb_disable_stall_handshake();
00224 Usb_reset_endpoint(EP_HID_IN);
00225 Usb_reset_data_toggle();
00226 Usb_select_endpoint(EP_HID_OUT);
00227 Usb_disable_stall_handshake();
00228 Usb_reset_endpoint(EP_HID_OUT);
00229 Usb_reset_data_toggle();
00230 }
00231 }
00232
00233
00241 Bool usb_user_get_descriptor(U8 type, U8 string)
00242 {
00243 switch(type)
00244 {
00245 case DESCRIPTOR_STRING:
00246 switch (string)
00247 {
00248 case LANG_ID:
00249 data_to_transfer = sizeof (usb_user_language_id);
00250 pbuffer = &(usb_user_language_id.bLength);
00251 return TRUE;
00252 break;
00253
00254 case MAN_INDEX:
00255 data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
00256 pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
00257 return TRUE;
00258 break;
00259
00260 case PROD_INDEX:
00261 data_to_transfer = sizeof (usb_user_product_string_descriptor);
00262 pbuffer = &(usb_user_product_string_descriptor.bLength);
00263 return TRUE;
00264 break;
00265
00266 #if (USB_DEVICE_SN_USE==ENABLE)
00267 case SN_INDEX:
00268 data_to_transfer = sizeof (usb_user_serial_number);
00269 pbuffer = &(usb_user_serial_number.bLength);
00270 #if (USE_DEVICE_SN_UNIQUE==ENABLE)
00271 f_get_serial_string=TRUE;
00272 data_to_transfer += (SN_LENGTH*4);
00273 #endif
00274 return TRUE;
00275 break;
00276 #endif
00277 }
00278 break;
00279 }
00280 return FALSE;
00281 }
00282
00283
00286 void hid_get_report_descriptor(void)
00287 {
00288 U16 wLength;
00289 U8 nb_byte;
00290 bit zlp = FALSE;
00291 U16 wInterface;
00292
00293 LSB(wInterface)=Usb_read_byte();
00294 MSB(wInterface)=Usb_read_byte();
00295
00296 data_to_transfer = sizeof(usb_hid_report_descriptor);
00297 pbuffer = &(usb_hid_report_descriptor.report[0]);
00298
00299 LSB(wLength) = Usb_read_byte();
00300 MSB(wLength) = Usb_read_byte();
00301 Usb_ack_receive_setup();
00302
00303 if (wLength > data_to_transfer)
00304 {
00305 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00306 else { zlp = FALSE; }
00307 }
00308 else
00309 {
00310 data_to_transfer = (U8)wLength;
00311 }
00312
00313 while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00314 {
00315 while(!Is_usb_read_control_enabled());
00316
00317 nb_byte=0;
00318 while(data_to_transfer != 0)
00319 {
00320 if(nb_byte++==EP_CONTROL_LENGTH)
00321 {
00322 break;
00323 }
00324 #ifndef __GNUC__
00325 Usb_write_byte(*pbuffer++);
00326 #else // AVRGCC does not support point to PGM space
00327
00328 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00329 #endif
00330 data_to_transfer --;
00331 }
00332 Usb_send_control_in();
00333 }
00334
00335 if(Is_usb_receive_out())
00336 {
00337
00338 Usb_ack_receive_out();
00339 return;
00340 }
00341 if(zlp == TRUE)
00342 {
00343 while(!Is_usb_read_control_enabled());
00344 Usb_send_control_in();
00345 }
00346
00347 while(!Is_usb_receive_out());
00348 Usb_ack_receive_out();
00349 }
00350
00351
00354 void usb_hid_set_report_ouput (void)
00355 {
00356 Usb_ack_receive_setup();
00357 Usb_send_control_in();
00358
00359 while(!Is_usb_receive_out());
00360 Usb_ack_receive_out();
00361 Usb_send_control_in();
00362 }
00363
00364
00370 void usb_hid_set_idle (U8 u8_report_id, U8 u8_duration )
00371 {
00372 U16 wInterface;
00373
00374
00375 LSB(wInterface)=Usb_read_byte();
00376 MSB(wInterface)=Usb_read_byte();
00377 Usb_ack_receive_setup();
00378
00379 g_u8_report_rate = u8_duration;
00380
00381 Usb_send_control_in();
00382 while(!Is_usb_in_ready());
00383 }
00384
00385
00390 void usb_hid_get_idle (U8 u8_report_id)
00391 {
00392 U16 wLength;
00393 U16 wInterface;
00394
00395
00396 LSB(wInterface)= Usb_read_byte();
00397 MSB(wInterface)= Usb_read_byte();
00398 LSB(wLength) = Usb_read_byte();
00399 MSB(wLength) = Usb_read_byte();
00400 Usb_ack_receive_setup();
00401
00402 if( wLength != 0 )
00403 {
00404 Usb_write_byte(g_u8_report_rate);
00405 Usb_send_control_in();
00406 }
00407
00408 while(!Is_usb_receive_out());
00409 Usb_ack_receive_out();
00410 }
00411
00412 void usb_hid_set_report_feature(void)
00413 {
00414
00415 Usb_ack_receive_setup();
00416 Usb_send_control_in();
00417
00418 while(!Is_usb_receive_out());
00419
00420 if(Usb_read_byte()==0x55)
00421 if(Usb_read_byte()==0xAA)
00422 if(Usb_read_byte()==0x55)
00423 if(Usb_read_byte()==0xAA)
00424 {
00425 jump_bootloader=1;
00426 }
00427 Usb_ack_receive_out();
00428 Usb_send_control_in();
00429 while(!Is_usb_in_ready());
00430 }
00431
00434 void hid_get_hid_descriptor(void)
00435 {
00436 U16 wLength;
00437 U8 nb_byte;
00438 bit zlp=FALSE;
00439 U16 wInterface;
00440
00441 LSB(wInterface)=Usb_read_byte();
00442 MSB(wInterface)=Usb_read_byte();
00443
00444 data_to_transfer = sizeof(usb_conf_desc.hid);
00445 pbuffer = &(usb_conf_desc.hid.bLength);
00446
00447 LSB(wLength) = Usb_read_byte();
00448 MSB(wLength) = Usb_read_byte();
00449 Usb_ack_receive_setup();
00450
00451 if (wLength > data_to_transfer)
00452 {
00453 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00454 else { zlp = FALSE; }
00455 }
00456 else
00457 {
00458 data_to_transfer = (U8)wLength;
00459 }
00460
00461 while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00462 {
00463 while(!Is_usb_read_control_enabled());
00464
00465 nb_byte=0;
00466 while(data_to_transfer != 0)
00467 {
00468 if(nb_byte++==EP_CONTROL_LENGTH)
00469 {
00470 break;
00471 }
00472 #ifndef __GNUC__
00473 Usb_write_byte(*pbuffer++);
00474 #else // AVRGCC does not support point to PGM space
00475
00476 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00477 #endif
00478 data_to_transfer --;
00479 }
00480 Usb_send_control_in();
00481 }
00482
00483 if(Is_usb_receive_out())
00484 {
00485
00486 Usb_ack_receive_out();
00487 return;
00488 }
00489 if(zlp == TRUE)
00490 {
00491 while(!Is_usb_read_control_enabled());
00492 Usb_send_control_in();
00493 }
00494
00495 while(!Is_usb_receive_out());
00496 Usb_ack_receive_out();
00497 }
00498