00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "config.h"
00026 #include "conf_usb.h"
00027 #include "lib_mcu\usb\usb_drv.h"
00028 #include "usb_descriptors.h"
00029 #include "modules\usb\device_chap9\usb_standard_request.h"
00030 #include "usb_specific_request.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 static void usb_get_descriptor( void);
00041 static void usb_set_address( void);
00042 static void usb_set_configuration(void);
00043 static void usb_clear_feature( void);
00044 static void usb_set_feature( void);
00045 static void usb_get_status( void);
00046 static void usb_get_configuration(void);
00047 static void usb_get_interface (void);
00048 static void usb_set_interface (void);
00049
00050
00051
00052
00053
00054
00055
00056 static bit zlp;
00057 static U8 endpoint_status[NB_ENDPOINTS];
00058
00059 #ifdef AVRGCC
00060 PGM_VOID_P pbuffer;
00061 #else
00062 U8 code *pbuffer;
00063 #endif
00064 U8 data_to_transfer;
00065
00066 U16 wInterface;
00067
00068 static U8 bmRequestType;
00069
00070 U8 usb_configuration_nb;
00071 extern bit usb_connected;
00072 extern code S_usb_device_descriptor usb_user_device_descriptor;
00073 extern code S_usb_user_configuration_descriptor usb_user_configuration_descriptor;
00074
00075
00076
00095 void usb_process_request(void)
00096 {
00097 U8 bmRequest;
00098
00099 UEINTX &= ~(1<<RXOUTI);
00100
00101 bmRequestType = Usb_read_byte();
00102 bmRequest = Usb_read_byte();
00103
00104 switch (bmRequest)
00105 {
00106 case GET_DESCRIPTOR:
00107 if (0x80 == bmRequestType) { usb_get_descriptor(); }
00108 else { usb_user_read_request(bmRequestType, bmRequest); }
00109 break;
00110
00111 case GET_CONFIGURATION:
00112 if (0x80 == bmRequestType) { usb_get_configuration(); }
00113 else { usb_user_read_request(bmRequestType, bmRequest); }
00114 break;
00115
00116 case SET_ADDRESS:
00117 if (0x00 == bmRequestType) { usb_set_address(); }
00118 else { usb_user_read_request(bmRequestType, bmRequest); }
00119 break;
00120
00121 case SET_CONFIGURATION:
00122 if (0x00 == bmRequestType)
00123 {
00124 usb_set_configuration();
00125 #if (USB_OTG_FEATURE == ENABLED)
00126 Otg_send_event(EVT_OTG_DEVICE_CONNECTED);
00127 #endif
00128 }
00129 else { usb_user_read_request(bmRequestType, bmRequest); }
00130 break;
00131
00132 case CLEAR_FEATURE:
00133 if (0x02 >= bmRequestType) { usb_clear_feature(); }
00134 else { usb_user_read_request(bmRequestType, bmRequest); }
00135 break;
00136
00137 case SET_FEATURE:
00138 if (0x02 >= bmRequestType) { usb_set_feature(); }
00139 else { usb_user_read_request(bmRequestType, bmRequest); }
00140 break;
00141
00142 case GET_STATUS:
00143 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
00144 { usb_get_status(); }
00145 else { usb_user_read_request(bmRequestType, bmRequest); }
00146 break;
00147
00148 case GET_INTERFACE:
00149 if (bmRequestType == 0x81) { usb_get_interface(); }
00150 else { usb_user_read_request(bmRequestType, bmRequest); }
00151 break;
00152
00153
00154 case SET_INTERFACE:
00155 if (bmRequestType == 0x01) {usb_set_interface();}
00156 break;
00157
00158 case SET_DESCRIPTOR:
00159 case SYNCH_FRAME:
00160 default:
00161 if(usb_user_read_request(bmRequestType, bmRequest) == FALSE)
00162 {
00163 Usb_enable_stall_handshake();
00164 Usb_ack_receive_setup();
00165 return;
00166 }
00167 break;
00168 }
00169 }
00170
00171
00183 void usb_set_address(void)
00184 {
00185 Usb_configure_address(Usb_read_byte());
00186
00187 Usb_ack_receive_setup();
00188
00189 Usb_send_control_in();
00190 while(!Is_usb_in_ready());
00191
00192 Usb_enable_address();
00193 }
00194
00195
00209 void usb_set_configuration( void )
00210 {
00211 U8 configuration_number;
00212
00213 configuration_number = Usb_read_byte();
00214
00215 if (configuration_number <= NB_CONFIGURATION)
00216 {
00217 Usb_ack_receive_setup();
00218 usb_configuration_nb = configuration_number;
00219 }
00220 else
00221 {
00224 Usb_enable_stall_handshake();
00225 Usb_ack_receive_setup();
00226 return;
00227 }
00228
00229 Usb_send_control_in();
00230
00231 usb_user_endpoint_init(usb_configuration_nb);
00232 Usb_set_configuration_action();
00233 }
00234
00235
00250 void usb_get_descriptor(void)
00251 {
00252 U16 wLength;
00253 U8 descriptor_type ;
00254 U8 string_type;
00255 U8 dummy;
00256 U8 nb_byte;
00257
00258 zlp = FALSE;
00259 string_type = Usb_read_byte();
00260 descriptor_type = Usb_read_byte();
00261
00262 switch (descriptor_type)
00263 {
00264 case DEVICE_DESCRIPTOR:
00265 data_to_transfer = Usb_get_dev_desc_length();
00266 pbuffer = Usb_get_dev_desc_pointer();
00267 break;
00268 case CONFIGURATION_DESCRIPTOR:
00269 data_to_transfer = Usb_get_conf_desc_length();
00270 pbuffer = Usb_get_conf_desc_pointer();
00271 break;
00272 default:
00273 if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE )
00274 {
00275 Usb_enable_stall_handshake();
00276 Usb_ack_receive_setup();
00277 return;
00278 }
00279 break;
00280 }
00281
00282 dummy = Usb_read_byte();
00283 dummy = Usb_read_byte();
00284 LSB(wLength) = Usb_read_byte();
00285 MSB(wLength) = Usb_read_byte();
00286 Usb_ack_receive_setup() ;
00287
00288 if (wLength > data_to_transfer)
00289 {
00290 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00291 else { zlp = FALSE; }
00292 }
00293 else
00294 {
00295 data_to_transfer = (U8)wLength;
00296 }
00297
00298 Usb_ack_nak_out();
00299
00300 while((data_to_transfer != 0) && (!Is_usb_nak_out_sent()))
00301 {
00302 while(!Is_usb_read_control_enabled())
00303 {
00304 if (Is_usb_nak_out_sent())
00305 break;
00306 }
00307
00308 nb_byte=0;
00309 while(data_to_transfer != 0)
00310 {
00311 if(nb_byte++==EP_CONTROL_LENGTH)
00312 {
00313 break;
00314 }
00315 #ifndef AVRGCC
00316 Usb_write_byte(*pbuffer++);
00317 #else // AVRGCC does not support point to PGM space
00318 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00319 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00320 #endif
00321 data_to_transfer --;
00322 }
00323
00324 if (Is_usb_nak_out_sent())
00325 break;
00326 else
00327 Usb_send_control_in();
00328 }
00329
00330 if((zlp == TRUE) && (!Is_usb_nak_out_sent()))
00331 {
00332 while(!Is_usb_read_control_enabled());
00333 Usb_send_control_in();
00334 }
00335
00336 while (!(Is_usb_nak_out_sent()));
00337 Usb_ack_nak_out();
00338 UEINTX &= ~(1<<RXOUTI);
00339 }
00340
00341
00353 void usb_get_configuration(void)
00354 {
00355 Usb_ack_receive_setup();
00356
00357 Usb_write_byte(usb_configuration_nb);
00358 Usb_ack_in_ready();
00359
00360 while( !Is_usb_receive_out() );
00361 Usb_ack_receive_out();
00362 }
00363
00375 void usb_get_status(void)
00376 {
00377 U8 wIndex;
00378 U8 dummy;
00379
00380 dummy = Usb_read_byte();
00381 dummy = Usb_read_byte();
00382 wIndex = Usb_read_byte();
00383
00384 switch(bmRequestType)
00385 {
00386 case REQUEST_DEVICE_STATUS: Usb_ack_receive_setup();
00387 Usb_write_byte(DEVICE_STATUS);
00388 break;
00389
00390 case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
00391 Usb_write_byte(INTERFACE_STATUS);
00392 break;
00393
00394 case REQUEST_ENDPOINT_STATUS: Usb_ack_receive_setup();
00395 wIndex = wIndex & MSK_EP_DIR;
00396 Usb_write_byte(endpoint_status[wIndex]);
00397 break;
00398 default:
00399 Usb_enable_stall_handshake();
00400 Usb_ack_receive_setup();
00401 return;
00402 }
00403
00404 Usb_write_byte(0x00);
00405 Usb_send_control_in();
00406
00407 while( !Is_usb_receive_out() );
00408 Usb_ack_receive_out();
00409 }
00410
00411
00423 void usb_set_feature(void)
00424 {
00425 U8 wValue;
00426 U8 wIndex;
00427 U8 dummy;
00428
00429 switch (bmRequestType)
00430 {
00431 case ZERO_TYPE:
00432 wValue = Usb_read_byte();
00433 switch (wValue)
00434 {
00435 case USB_REMOTE_WAKEUP:
00436 if ((CONF_ATTRIBUTES&USB_CONFIG_REMOTEWAKEUP) == USB_CONFIG_REMOTEWAKEUP)
00437 {
00438 remote_wakeup_feature = ENABLED;
00439 Usb_ack_receive_setup();
00440 Usb_send_control_in();
00441 }
00442 else
00443 {
00444 Usb_enable_stall_handshake();
00445 Usb_ack_receive_setup();
00446 }
00447 break;
00448
00449 #if (USB_OTG_FEATURE == ENABLED)
00450 case OTG_B_HNP_ENABLE:
00451 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))
00452 {
00453 Usb_enable_stall_handshake();
00454 Usb_ack_receive_setup();
00455 }
00456 else
00457 {
00458 otg_features_supported |= OTG_B_HNP_ENABLE;
00459 otg_device_nb_hnp_retry = BDEV_HNP_NB_RETRY;
00460 Usb_ack_receive_setup();
00461 Usb_send_control_in();
00462 }
00463 break;
00464
00465 case OTG_A_HNP_SUPPORT:
00466 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))
00467 {
00468 Usb_enable_stall_handshake();
00469 Usb_ack_receive_setup();
00470 }
00471 else
00472 {
00473 otg_features_supported |= OTG_A_HNP_SUPPORT;
00474 Usb_ack_receive_setup();
00475 Usb_send_control_in();
00476 }
00477 break;
00478
00479 case OTG_A_ALT_HNP_SUPPORT:
00480 if (((OTG_BMATTRIBUTES&HNP_SUPPORT) == 0) || (USB_OTG_FEATURE == DISABLED))
00481 {
00482 Usb_enable_stall_handshake();
00483 Usb_ack_receive_setup();
00484 }
00485 else
00486 {
00487 otg_features_supported |= OTG_A_ALT_HNP_SUPPORT;
00488 Usb_ack_receive_setup();
00489 Usb_send_control_in();
00490 }
00491 break;
00492 #endif
00493
00494 default:
00495 Usb_enable_stall_handshake();
00496 Usb_ack_receive_setup();
00497 break;
00498 }
00499 break;
00500
00501 case INTERFACE_TYPE:
00504 Usb_enable_stall_handshake();
00505 Usb_ack_receive_setup();
00506 break;
00507
00508 case ENDPOINT_TYPE:
00509 wValue = Usb_read_byte();
00510 dummy = Usb_read_byte();
00511
00512 if (wValue == FEATURE_ENDPOINT_HALT)
00513 {
00514 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00515
00516 if (wIndex == EP_CONTROL)
00517 {
00518 Usb_enable_stall_handshake();
00519 Usb_ack_receive_setup();
00520 }
00521
00522 Usb_select_endpoint(wIndex);
00523 if(Is_usb_endpoint_enabled())
00524 {
00525 Usb_enable_stall_handshake();
00526 Usb_select_endpoint(EP_CONTROL);
00527 endpoint_status[wIndex] = 0x01;
00528 Usb_ack_receive_setup();
00529 Usb_send_control_in();
00530 }
00531 else
00532 {
00533 Usb_select_endpoint(EP_CONTROL);
00534 Usb_enable_stall_handshake();
00535 Usb_ack_receive_setup();
00536 }
00537 }
00538 else
00539 {
00540 Usb_enable_stall_handshake();
00541 Usb_ack_receive_setup();
00542 }
00543 break;
00544
00545 default:
00546 Usb_enable_stall_handshake();
00547 Usb_ack_receive_setup();
00548 break;
00549 }
00550 }
00551
00552
00563 void usb_clear_feature(void)
00564 {
00565 U8 wValue;
00566 U8 wIndex;
00567 U8 dummy;
00568
00569 if (bmRequestType == ZERO_TYPE)
00570 {
00573 Usb_enable_stall_handshake();
00574 Usb_ack_receive_setup();
00575 return;
00576 }
00577 else if (bmRequestType == INTERFACE_TYPE)
00578 {
00581 Usb_enable_stall_handshake();
00582 Usb_ack_receive_setup();
00583 return;
00584 }
00585 else if (bmRequestType == ENDPOINT_TYPE)
00586 {
00587 wValue = Usb_read_byte();
00588 dummy = Usb_read_byte();
00589
00590 if (wValue == FEATURE_ENDPOINT_HALT)
00591 {
00592 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00593
00594 Usb_select_endpoint(wIndex);
00595 if(Is_usb_endpoint_enabled())
00596 {
00597 if(wIndex != EP_CONTROL)
00598 {
00599 Usb_disable_stall_handshake();
00600 Usb_reset_endpoint(wIndex);
00601 Usb_reset_data_toggle();
00602 }
00603 Usb_select_endpoint(EP_CONTROL);
00604 endpoint_status[wIndex] = 0x00;
00605 Usb_ack_receive_setup();
00606 Usb_send_control_in();
00607 }
00608 else
00609 {
00610 Usb_select_endpoint(EP_CONTROL);
00611 Usb_enable_stall_handshake();
00612 Usb_ack_receive_setup();
00613 return;
00614 }
00615 }
00616 else
00617 {
00618 Usb_enable_stall_handshake();
00619 Usb_ack_receive_setup();
00620 return;
00621 }
00622 }
00623 }
00624
00625
00626
00637 void usb_get_interface (void)
00638 {
00639 Usb_ack_receive_setup();
00640 Usb_send_control_in();
00641
00642 while( !Is_usb_receive_out() );
00643 Usb_ack_receive_out();
00644 }
00645
00656 void usb_set_interface (void)
00657 {
00658 Usb_ack_receive_setup();
00659 Usb_send_control_in();
00660 while(!Is_usb_in_ready());
00661 }