62 #define PRINTF_P printf_P
66 static void usb_get_descriptor(
void);
67 static void usb_set_address(
void);
68 static void usb_set_configuration(
void);
69 static void usb_clear_feature(
void);
70 static void usb_set_feature(
void);
71 static void usb_get_status(
void);
72 static void usb_get_configuration(
void);
73 static void usb_get_interface (
void);
74 static void usb_set_interface (
void);
80 static U8 endpoint_status[NB_ENDPOINTS];
91 static U8 bmRequestType;
95 usb_mode_t usb_mode = rndis_debug;
99 extern FLASH S_usb_user_configuration_descriptor_network usb_user_configuration_descriptor_network;
101 extern FLASH S_usb_user_configuration_descriptor_composite usb_user_configuration_descriptor_composite;
128 if (0x80 == bmRequestType) { usb_get_descriptor(); }
132 case GET_CONFIGURATION:
133 if (0x80 == bmRequestType) { usb_get_configuration(); }
138 if (0x00 == bmRequestType) { usb_set_address(); }
142 case SET_CONFIGURATION:
143 if (0x00 == bmRequestType) { usb_set_configuration(); }
148 if (0x02 >= bmRequestType) { usb_clear_feature(); }
153 if (0x02 >= bmRequestType) { usb_set_feature(); }
158 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
159 { usb_get_status(); }
164 if (bmRequestType == 0x81) { usb_get_interface(); }
170 if (bmRequestType == 0x01) {usb_set_interface();}
202 void usb_set_address(
void)
223 void usb_set_configuration(
void )
225 U8 configuration_number;
237 Usb_set_configuration_action();
245 U16 requested_length;
247 const char* user_str;
249 user_str = usb_user_get_string_sram(string_type);
260 const U8 actual_descriptor_size = 2+strlen(user_str)*2;
262 if (requested_length > actual_descriptor_size) {
263 zlp = ((actual_descriptor_size % EP_CONTROL_LENGTH) == 0);
264 requested_length = actual_descriptor_size;
269 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
280 requested_length -= 2;
283 if(!requested_length) {
289 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
294 while(requested_length != 0)
296 if(nb_byte==EP_CONTROL_LENGTH)
305 requested_length -=2;
319 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
326 usb_endpoint_wait_for_receive_out();
332 U16 requested_length;
336 user_str = usb_user_get_string(string_type);
350 const U8 actual_descriptor_size = 2+strlen_P(user_str)*2;
352 if (requested_length > actual_descriptor_size) {
353 zlp = ((actual_descriptor_size % EP_CONTROL_LENGTH) == 0);
354 requested_length = actual_descriptor_size;
359 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
366 if(requested_length) {
373 if(requested_length) {
379 if(!requested_length) {
385 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
390 while(requested_length != 0)
392 if(nb_byte==EP_CONTROL_LENGTH) {
400 if(requested_length) {
418 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
425 usb_endpoint_wait_for_receive_out();
440 void usb_get_descriptor(
void)
442 U8 LSBwLength, MSBwLength;
453 switch (descriptor_type)
455 case DEVICE_DESCRIPTOR:
456 data_to_transfer = Usb_get_dev_desc_length();
457 pbuffer = Usb_get_dev_desc_pointer();
459 case CONFIGURATION_DESCRIPTOR:
460 data_to_transfer = Usb_get_conf_desc_length(string_type);
461 pbuffer = Usb_get_conf_desc_pointer(string_type);
464 case STRING_DESCRIPTOR:
465 if(string_type!=LANG_ID) {
484 if (byteswereread==0) {
493 if ((LSBwLength > data_to_transfer) || (MSBwLength)) {
494 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp =
TRUE; }
495 else { zlp =
FALSE; }
497 LSBwLength = data_to_transfer;
500 data_to_transfer = LSBwLength;
505 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
511 while(data_to_transfer != 0) {
513 if(nb_byte++==EP_CONTROL_LENGTH)
530 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
537 usb_endpoint_wait_for_receive_out();
548 void usb_get_configuration(
void)
555 usb_endpoint_wait_for_receive_out();
566 void usb_get_status(
void)
576 switch(bmRequestType)
587 wIndex = wIndex & MSK_EP_DIR;
599 usb_endpoint_wait_for_receive_out();
610 void usb_set_feature(
void)
616 if (bmRequestType == INTERFACE_TYPE)
620 else if (bmRequestType == ENDPOINT_TYPE)
626 if (wValue == FEATURE_ENDPOINT_HALT)
630 if (wIndex == EP_CONTROL)
640 endpoint_status[wIndex] = 0x01;
663 void usb_clear_feature(
void)
669 if (bmRequestType == ZERO_TYPE)
673 else if (bmRequestType == INTERFACE_TYPE)
677 else if (bmRequestType == ENDPOINT_TYPE)
683 if (wValue == FEATURE_ENDPOINT_HALT)
690 if(wIndex != EP_CONTROL)
697 endpoint_status[wIndex] = 0x00;
720 void usb_get_interface (
void)
731 void usb_set_interface (
void)
742 if(usb_user_set_alt_interface(interface, alt_setting)) {
747 usb_endpoint_wait_for_receive_out();