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 00028 //_____ M A C R O S ________________________________________________________ 00029 00030 //_____ D E F I N I T I O N ________________________________________________ 00031 bit ms_multiple_drive; 00032 //_____ P R I V A T E D E C L A R A T I O N ______________________________ 00033 00034 #ifdef AVRGCC 00035 extern PGM_VOID_P pbuffer; 00036 #else 00037 extern U8 code *pbuffer; 00038 #endif 00039 extern U8 data_to_transfer; 00040 00041 extern code S_usb_hid_report_descriptor_mouse usb_hid_report_descriptor_mouse; 00042 00043 extern U16 wInterface; 00044 00045 00046 //_____ D E C L A R A T I O N ______________________________________________ 00047 00062 Bool usb_user_read_request(U8 type, U8 request) 00063 { 00064 U8 descriptor_type ; 00065 U8 string_type ; 00066 00067 string_type = Usb_read_byte(); 00068 descriptor_type = Usb_read_byte(); 00069 switch(request) 00070 { 00071 case GET_DESCRIPTOR: 00072 switch (descriptor_type) 00073 { 00074 case REPORT: 00075 hid_get_report(); 00076 return TRUE; 00077 break; 00078 case HID: 00079 hid_get_hid_descriptor(); 00080 return TRUE; 00081 break; 00082 default: 00083 return FALSE; 00084 break; 00085 } 00086 break; 00087 00088 case SET_CONFIGURATION: 00089 switch (descriptor_type) 00090 { 00091 case SET_REPORT: 00092 hid_set_report(); 00093 return TRUE; 00094 break; 00095 default: 00096 return FALSE; 00097 break; 00098 } 00099 break; 00100 00101 case GET_INTERFACE: 00102 // usb_hid_set_idle(); 00103 usb_hid_get_interface(); 00104 return TRUE; 00105 break; 00106 default: 00107 return FALSE; 00108 break; 00109 } 00110 return FALSE; 00111 } 00112 00113 00114 00125 void usb_user_endpoint_init(U8 conf_nb) 00126 { 00127 usb_configure_endpoint(EP_SPIDER_IN, \ 00128 TYPE_INTERRUPT, \ 00129 DIRECTION_IN, \ 00130 SIZE_8, \ 00131 ONE_BANK, \ 00132 NYET_ENABLED); 00133 } 00134 00135 00147 Bool usb_user_get_descriptor(U8 type, U8 string) 00148 { 00149 switch(type) 00150 { 00151 case STRING_DESCRIPTOR: 00152 switch (string) 00153 { 00154 case LANG_ID: 00155 data_to_transfer = sizeof (usb_user_language_id); 00156 pbuffer = &(usb_user_language_id.bLength); 00157 return TRUE; 00158 break; 00159 case MAN_INDEX: 00160 data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor); 00161 pbuffer = &(usb_user_manufacturer_string_descriptor.bLength); 00162 return TRUE; 00163 break; 00164 case PROD_INDEX: 00165 data_to_transfer = sizeof (usb_user_product_string_descriptor); 00166 pbuffer = &(usb_user_product_string_descriptor.bLength); 00167 return TRUE; 00168 break; 00169 case SN_INDEX: 00170 data_to_transfer = sizeof (usb_user_serial_number); 00171 pbuffer = &(usb_user_serial_number.bLength); 00172 return TRUE; 00173 break; 00174 default: 00175 return FALSE; 00176 } 00177 default: 00178 return FALSE; 00179 } 00180 00181 return FALSE; 00182 } 00183 00184 00195 void hid_get_report(void) 00196 { 00197 00198 U16 wLength; 00199 U8 nb_byte; 00200 bit zlp=0; 00201 00202 LSB(wInterface)=Usb_read_byte(); 00203 MSB(wInterface)=Usb_read_byte(); 00204 00205 data_to_transfer = sizeof(usb_hid_report_descriptor_mouse); 00206 pbuffer = &(usb_hid_report_descriptor_mouse.report[0]); 00207 00208 LSB(wLength) = Usb_read_byte(); 00209 MSB(wLength) = Usb_read_byte(); 00210 Usb_ack_receive_setup() ; 00211 00212 if (wLength > data_to_transfer) 00213 { 00214 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; } 00215 else { zlp = FALSE; } 00216 } 00217 else 00218 { 00219 data_to_transfer = (U8)wLength; 00220 } 00221 00222 while((data_to_transfer != 0) && (!Is_usb_receive_out())) 00223 { 00224 while(!Is_usb_read_control_enabled()); 00225 00226 nb_byte=0; 00227 while(data_to_transfer != 0) 00228 { 00229 if(nb_byte++==EP_CONTROL_LENGTH) 00230 { 00231 break; 00232 } 00233 #ifndef AVRGCC 00234 Usb_write_byte(*pbuffer++); 00235 #else // AVRGCC does not support point to PGM space 00236 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory 00237 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++)); 00238 #endif 00239 data_to_transfer --; 00240 } 00241 Usb_send_control_in(); 00242 } 00243 00244 Usb_send_control_in(); 00245 00246 if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 00247 if(zlp == TRUE) { Usb_send_control_in(); } 00248 00249 while(!Is_usb_receive_out()); 00250 Usb_ack_receive_out(); 00251 } 00252 00253 00254 00265 void hid_set_report (void) 00266 { 00267 Usb_ack_receive_setup(); 00268 Usb_send_control_in(); 00269 00270 while(!Is_usb_receive_out()); 00271 Usb_ack_receive_out(); 00272 Usb_send_control_in(); 00273 00274 } 00275 00276 00287 void usb_hid_set_idle (void) 00288 { 00289 U8 dummy; 00290 dummy = Usb_read_byte(); 00291 dummy = Usb_read_byte(); 00292 LSB(wInterface)=Usb_read_byte(); 00293 MSB(wInterface)=Usb_read_byte(); 00294 00295 Usb_ack_receive_setup(); 00296 00297 Usb_send_control_in(); // send a ZLP for STATUS phase 00298 while(!Is_usb_in_ready()); 00299 } 00300 00301 00312 void usb_hid_get_interface (void) 00313 { 00314 U8 dummy; 00315 dummy = Usb_read_byte(); 00316 dummy = Usb_read_byte(); 00317 LSB(wInterface)=Usb_read_byte(); 00318 MSB(wInterface)=Usb_read_byte(); 00319 00320 Usb_ack_receive_setup(); 00321 00322 Usb_send_control_in(); // send a ZLP for STATUS phase 00323 while(!Is_usb_in_ready()); 00324 } 00325 00336 void hid_get_hid_descriptor(void) 00337 { 00338 U16 wLength; 00339 U8 nb_byte; 00340 bit zlp=0; 00341 00342 00343 00344 LSB(wInterface)=Usb_read_byte(); 00345 MSB(wInterface)=Usb_read_byte(); 00346 00347 data_to_transfer = sizeof(usb_conf_desc.hid_mouse); 00348 pbuffer = &(usb_conf_desc.hid_mouse.bLength); 00349 00350 LSB(wLength) = Usb_read_byte(); 00351 MSB(wLength) = Usb_read_byte(); 00352 Usb_ack_receive_setup() ; 00353 00354 if (wLength > data_to_transfer) 00355 { 00356 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; } 00357 else { zlp = FALSE; } 00358 } 00359 else 00360 { 00361 data_to_transfer = (U8)wLength; 00362 } 00363 00364 while((data_to_transfer != 0) && (!Is_usb_receive_out())) 00365 { 00366 while(!Is_usb_read_control_enabled()); 00367 00368 nb_byte=0; 00369 while(data_to_transfer != 0) 00370 { 00371 if(nb_byte++==EP_CONTROL_LENGTH) 00372 { 00373 break; 00374 } 00375 #ifndef AVRGCC 00376 Usb_write_byte(*pbuffer++); 00377 #else // AVRGCC does not support point to PGM space 00378 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory 00379 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++)); 00380 #endif 00381 data_to_transfer --; 00382 } 00383 Usb_send_control_in(); 00384 } 00385 00386 Usb_send_control_in(); 00387 00388 if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; } 00389 if(zlp == TRUE) { Usb_send_control_in(); } 00390 00391 while(!Is_usb_receive_out()); 00392 Usb_ack_receive_out(); 00393 } 00394 00395
1.5.1-p1