00001
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
00051
00052
00053
00054
00055
00056 #include "conf_usb.h"
00057
00058
00059 #if USB_DEVICE_FEATURE == ENABLED
00060
00061 #include "usb_drv.h"
00062 #include "usb_descriptors.h"
00063 #include "usb_standard_request.h"
00064 #include "usb_specific_request.h"
00065 #include "usb_task.h"
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 static void usb_get_descriptor (void);
00077 static void usb_set_address (void);
00078 static void usb_set_configuration(void);
00079 static void usb_clear_feature (void);
00080 static void usb_set_feature (void);
00081 static void usb_get_status (void);
00082 static void usb_get_configuration(void);
00083 static Bool usb_get_interface (void);
00084 static void usb_set_interface (void);
00085
00086
00087
00088
00089 const void *pbuffer;
00090 U16 data_to_transfer;
00091 static U8 bmRequestType;
00092 volatile U8 usb_configuration_nb;
00093 extern volatile Bool usb_connected;
00094 extern const S_usb_device_descriptor usb_user_device_descriptor;
00095 extern const S_usb_user_configuration_descriptor usb_user_configuration_descriptor;
00096 static U8 usb_interface_status[NB_INTERFACE];
00097
00098
00115 void usb_process_request(void)
00116 {
00117 U8 bRequest;
00118
00119 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00120 bmRequestType = Usb_read_endpoint_data(EP_CONTROL, 8);
00121 bRequest = Usb_read_endpoint_data(EP_CONTROL, 8);
00122
00123 switch (bRequest)
00124 {
00125 case GET_DESCRIPTOR:
00126 if (bmRequestType == 0x80) usb_get_descriptor();
00127 else goto unsupported_request;
00128 break;
00129
00130 case GET_CONFIGURATION:
00131 if (bmRequestType == 0x80) usb_get_configuration();
00132 else goto unsupported_request;
00133 break;
00134
00135 case SET_ADDRESS:
00136 if (bmRequestType == 0x00) usb_set_address();
00137 else goto unsupported_request;
00138 break;
00139
00140 case SET_CONFIGURATION:
00141 if (bmRequestType == 0x00) usb_set_configuration();
00142 else goto unsupported_request;
00143 break;
00144
00145 case CLEAR_FEATURE:
00146 if (bmRequestType <= 0x02) usb_clear_feature();
00147 else goto unsupported_request;
00148 break;
00149
00150 case SET_FEATURE:
00151 if (bmRequestType <= 0x02) usb_set_feature();
00152 else goto unsupported_request;
00153 break;
00154
00155 case GET_STATUS:
00156 if (0x7F < bmRequestType && bmRequestType <= 0x82) usb_get_status();
00157 else goto unsupported_request;
00158 break;
00159
00160 case GET_INTERFACE:
00161 if (bmRequestType == 0x81)
00162 {
00163 if(!usb_get_interface())
00164 {
00165 Usb_enable_stall_handshake(EP_CONTROL);
00166 Usb_ack_setup_received_free();
00167 }
00168 }
00169 else goto unsupported_request;
00170 break;
00171
00172 case SET_INTERFACE:
00173 if (bmRequestType == 0x01) usb_set_interface();
00174 else goto unsupported_request;
00175 break;
00176
00177 case SET_DESCRIPTOR:
00178 case SYNCH_FRAME:
00179 default:
00180 unsupported_request:
00181 if (!usb_user_read_request(bmRequestType, bRequest))
00182 {
00183 Usb_enable_stall_handshake(EP_CONTROL);
00184 Usb_ack_setup_received_free();
00185 }
00186 break;
00187 }
00188 }
00189
00190
00194 void usb_set_address(void)
00195 {
00196 U8 addr = Usb_read_endpoint_data(EP_CONTROL, 8);
00197 Usb_configure_address(addr);
00198
00199 Usb_ack_setup_received_free();
00200
00201 Usb_ack_control_in_ready_send();
00202 while (!Is_usb_control_in_ready());
00203
00204 Usb_enable_address();
00205 }
00206
00207
00213 void usb_set_configuration(void)
00214 {
00215 U8 configuration_number = Usb_read_endpoint_data(EP_CONTROL, 8);
00216 U8 u8_i;
00217
00218 if (configuration_number <= NB_CONFIGURATION)
00219 {
00220 Usb_ack_setup_received_free();
00221 usb_configuration_nb = configuration_number;
00222 for( u8_i=0; u8_i<NB_INTERFACE; u8_i++) usb_interface_status[u8_i]=0;
00223
00224 usb_user_endpoint_init(usb_configuration_nb);
00225 Usb_set_configuration_action();
00226
00227 Usb_ack_control_in_ready_send();
00228 }
00229 else
00230 {
00233 Usb_enable_stall_handshake(EP_CONTROL);
00234 Usb_ack_setup_received_free();
00235 }
00236 }
00237
00238
00245 void usb_get_descriptor(void)
00246 {
00247 Bool zlp;
00248 U16 wLength;
00249 U8 descriptor_type;
00250 U8 string_type;
00251 Union32 temp;
00252 #if (USB_HIGH_SPEED_SUPPORT==ENABLED)
00253 Bool b_first_data = TRUE;
00254 #endif
00255
00256 zlp = FALSE;
00257 string_type = Usb_read_endpoint_data(EP_CONTROL, 8);
00258 descriptor_type = Usb_read_endpoint_data(EP_CONTROL, 8);
00259
00260 switch (descriptor_type)
00261 {
00262 case DEVICE_DESCRIPTOR:
00263 data_to_transfer = Usb_get_dev_desc_length();
00264 pbuffer = Usb_get_dev_desc_pointer();
00265 break;
00266
00267 #if (USB_HIGH_SPEED_SUPPORT==DISABLED)
00268 case CONFIGURATION_DESCRIPTOR:
00269 data_to_transfer = Usb_get_conf_desc_length();
00270 pbuffer = Usb_get_conf_desc_pointer();
00271 break;
00272
00273 #else
00274 case CONFIGURATION_DESCRIPTOR:
00275 if( Is_usb_full_speed_mode() )
00276 {
00277 data_to_transfer = Usb_get_conf_desc_fs_length();
00278 pbuffer = Usb_get_conf_desc_fs_pointer();
00279 }else{
00280 data_to_transfer = Usb_get_conf_desc_hs_length();
00281 pbuffer = Usb_get_conf_desc_hs_pointer();
00282 }
00283 break;
00284
00285 case OTHER_SPEED_CONFIGURATION_DESCRIPTOR:
00286 if( !Is_usb_full_speed_mode() )
00287 {
00288 data_to_transfer = Usb_get_conf_desc_fs_length();
00289 pbuffer = Usb_get_conf_desc_fs_pointer();
00290 }else{
00291 data_to_transfer = Usb_get_conf_desc_hs_length();
00292 pbuffer = Usb_get_conf_desc_hs_pointer();
00293 }
00294 break;
00295
00296 case DEVICE_QUALIFIER_DESCRIPTOR:
00297 data_to_transfer = Usb_get_qualifier_desc_length();
00298 pbuffer = Usb_get_qualifier_desc_pointer();
00299 break;
00300
00301 #endif
00302
00303 default:
00304 if (!usb_user_get_descriptor(descriptor_type, string_type))
00305 {
00306 Usb_enable_stall_handshake(EP_CONTROL);
00307 Usb_ack_setup_received_free();
00308 return;
00309 }
00310 break;
00311 }
00312
00313 temp.u32 = Usb_read_endpoint_data(EP_CONTROL, 32);
00314
00315
00316 wLength = usb_format_usb_to_mcu_data(16, temp.u16[1]);
00317 Usb_ack_setup_received_free();
00318
00319 if (wLength > data_to_transfer)
00320 {
00321 zlp = !(data_to_transfer % EP_CONTROL_LENGTH);
00322 }
00323 else
00324 {
00325
00326
00327 data_to_transfer = wLength;
00328 }
00329
00330 Usb_ack_nak_out(EP_CONTROL);
00331
00332 while (data_to_transfer && !Is_usb_nak_out(EP_CONTROL))
00333 {
00334 while (!Is_usb_control_in_ready() && !Is_usb_nak_out(EP_CONTROL));
00335
00336 if (Is_usb_nak_out(EP_CONTROL))
00337 break;
00338
00339 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00340
00341 #if (USB_HIGH_SPEED_SUPPORT==ENABLED) // To support other descriptors like OTHER_SPEED_CONFIGURATION_DESCRIPTOR
00342 if( b_first_data ) {
00343 b_first_data = FALSE;
00344 if( 0!= data_to_transfer ) {
00345 usb_write_ep_txpacket(EP_CONTROL, pbuffer, 1, &pbuffer);
00346 data_to_transfer--;
00347 }
00348 if( 0!= data_to_transfer ) {
00349 usb_write_ep_txpacket(EP_CONTROL, &descriptor_type, 1, NULL);
00350 pbuffer = ((const U8*)pbuffer)+1;
00351 data_to_transfer--;
00352 }
00353 }
00354 #endif
00355 if( 0!= data_to_transfer ) {
00356 data_to_transfer = usb_write_ep_txpacket(EP_CONTROL, pbuffer,
00357 data_to_transfer, &pbuffer);
00358 }
00359 if (Is_usb_nak_out(EP_CONTROL))
00360 break;
00361
00362 Usb_ack_control_in_ready_send();
00363 }
00364
00365 if (zlp && !Is_usb_nak_out(EP_CONTROL))
00366 {
00367 while (!Is_usb_control_in_ready());
00368 Usb_ack_control_in_ready_send();
00369 }
00370
00371 while (!Is_usb_nak_out(EP_CONTROL));
00372 Usb_ack_nak_out(EP_CONTROL);
00373 while (!Is_usb_control_out_received());
00374 Usb_ack_control_out_received_free();
00375 }
00376
00377
00381 void usb_get_configuration(void)
00382 {
00383 Usb_ack_setup_received_free();
00384
00385 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00386 Usb_write_endpoint_data(EP_CONTROL, 8, usb_configuration_nb);
00387 Usb_ack_control_in_ready_send();
00388
00389 while (!Is_usb_control_out_received());
00390 Usb_ack_control_out_received_free();
00391 }
00392
00393
00397 void usb_get_status(void)
00398 {
00399 U8 wIndex;
00400
00401 switch (bmRequestType)
00402 {
00403 case REQUEST_DEVICE_STATUS:
00404 Usb_ack_setup_received_free();
00405 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00406 Usb_write_endpoint_data(EP_CONTROL, 8, DEVICE_STATUS);
00407 break;
00408
00409 case REQUEST_INTERFACE_STATUS:
00410 Usb_ack_setup_received_free();
00411 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00412 Usb_write_endpoint_data(EP_CONTROL, 8, INTERFACE_STATUS);
00413 break;
00414
00415 case REQUEST_ENDPOINT_STATUS:
00416 Usb_read_endpoint_data(EP_CONTROL, 16);
00417 wIndex = Usb_read_endpoint_data(EP_CONTROL, 8);
00418 wIndex = Get_desc_ep_nbr(wIndex);
00419 Usb_ack_setup_received_free();
00420 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00421 Usb_write_endpoint_data(EP_CONTROL, 8, Is_usb_endpoint_stall_requested(wIndex) );
00422 break;
00423
00424 default:
00425 Usb_enable_stall_handshake(EP_CONTROL);
00426 Usb_ack_setup_received_free();
00427 return;
00428 }
00429
00430 Usb_write_endpoint_data(EP_CONTROL, 8, 0x00);
00431 Usb_ack_control_in_ready_send();
00432
00433 while (!Is_usb_control_out_received());
00434 Usb_ack_control_out_received_free();
00435 }
00436
00437
00441 void usb_set_feature(void)
00442 {
00443 U16 wValue = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00444 U16 wIndex = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00445 U16 wLength = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00446
00447 if (wLength)
00448 goto unsupported_request;
00449
00450 switch (wValue)
00451 {
00452 case FEATURE_ENDPOINT_HALT:
00453 wIndex = Get_desc_ep_nbr(wIndex);
00454 if (bmRequestType != ENDPOINT_TYPE ||
00455 wIndex == EP_CONTROL ||
00456 !Is_usb_endpoint_enabled(wIndex))
00457 goto unsupported_request;
00458
00459 Usb_enable_stall_handshake(wIndex);
00460 Usb_ack_setup_received_free();
00461 Usb_ack_control_in_ready_send();
00462 break;
00463
00464 #if (USB_HIGH_SPEED_SUPPORT==ENABLED)
00465 case FEATURE_TEST_MODE:
00466 if (bmRequestType != DEVICE_TYPE ||
00467 wIndex & 0x00FF)
00468 goto unsupported_request;
00469
00470 switch (wIndex >> 8)
00471 {
00472 case TEST_J:
00473 Usb_ack_setup_received_free();
00474 Usb_ack_control_in_ready_send();
00475 while (!Is_usb_control_in_ready());
00476 Wr_bitfield(AVR32_USBB_udcon, AVR32_USBB_UDCON_SPDCONF_MASK, 2);
00477 Set_bits(AVR32_USBB_udcon, AVR32_USBB_UDCON_TSTJ_MASK);
00478 break;
00479
00480 case TEST_K:
00481 Usb_ack_setup_received_free();
00482 Usb_ack_control_in_ready_send();
00483 while (!Is_usb_control_in_ready());
00484 Wr_bitfield(AVR32_USBB_udcon, AVR32_USBB_UDCON_SPDCONF_MASK, 2);
00485 Set_bits(AVR32_USBB_udcon, AVR32_USBB_UDCON_TSTK_MASK);
00486 break;
00487
00488 case TEST_SE0_NAK:
00489 Usb_ack_setup_received_free();
00490 Usb_ack_control_in_ready_send();
00491 while (!Is_usb_control_in_ready());
00492 Wr_bitfield(AVR32_USBB_udcon, AVR32_USBB_UDCON_SPDCONF_MASK, 2);
00493 break;
00494
00495 case TEST_PACKET:
00496 {
00497 static const U8 test_packet[] =
00498 {
00499
00500 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00501
00502 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00503
00504 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
00505
00506 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
00507
00508 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
00509
00510 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
00511 };
00512
00513 Usb_ack_setup_received_free();
00514 Usb_ack_control_in_ready_send();
00515 while (!Is_usb_control_in_ready());
00516 Wr_bitfield(AVR32_USBB_udcon, AVR32_USBB_UDCON_SPDCONF_MASK, 2);
00517 Usb_disable_endpoint(EP_CONTROL);
00518 Usb_unallocate_memory(EP_CONTROL);
00519 (void)Usb_configure_endpoint(EP_CONTROL,
00520 TYPE_BULK,
00521 DIRECTION_IN,
00522 64,
00523 SINGLE_BANK);
00524 Usb_reset_endpoint(EP_CONTROL);
00525 Set_bits(AVR32_USBB_udcon, AVR32_USBB_UDCON_TSTPCKT_MASK);
00526 usb_write_ep_txpacket(EP_CONTROL, &test_packet, sizeof(test_packet), NULL);
00527 Usb_send_in(EP_CONTROL);
00528 }
00529 break;
00530
00531 case TEST_FORCE_ENABLE:
00532 default:
00533 goto unsupported_request;
00534 }
00535 break;
00536 #endif
00537
00538 case FEATURE_DEVICE_REMOTE_WAKEUP:
00539 default:
00540 goto unsupported_request;
00541 }
00542
00543 return;
00544
00545 unsupported_request:
00546 Usb_enable_stall_handshake(EP_CONTROL);
00547 Usb_ack_setup_received_free();
00548 }
00549
00550
00553 void usb_clear_feature(void)
00554 {
00555 U8 wValue;
00556 U8 wIndex;
00557
00558 if (bmRequestType == DEVICE_TYPE || bmRequestType == INTERFACE_TYPE)
00559 {
00562 Usb_enable_stall_handshake(EP_CONTROL);
00563 Usb_ack_setup_received_free();
00564 }
00565 else if (bmRequestType == ENDPOINT_TYPE)
00566 {
00567 wValue = Usb_read_endpoint_data(EP_CONTROL, 8);
00568
00569 if (wValue == FEATURE_ENDPOINT_HALT)
00570 {
00571 Usb_read_endpoint_data(EP_CONTROL, 8);
00572 wIndex = Usb_read_endpoint_data(EP_CONTROL, 8);
00573 wIndex = Get_desc_ep_nbr(wIndex);
00574
00575 if (Is_usb_endpoint_enabled(wIndex))
00576 {
00577 if (wIndex != EP_CONTROL)
00578 {
00579 Usb_disable_stall_handshake(wIndex);
00580 Usb_reset_endpoint(wIndex);
00581 Usb_reset_data_toggle(wIndex);
00582 }
00583 Usb_ack_setup_received_free();
00584 Usb_ack_control_in_ready_send();
00585 }
00586 else
00587 {
00588 Usb_enable_stall_handshake(EP_CONTROL);
00589 Usb_ack_setup_received_free();
00590 }
00591 }
00592 else
00593 {
00594 Usb_enable_stall_handshake(EP_CONTROL);
00595 Usb_ack_setup_received_free();
00596 }
00597 }
00598 }
00599
00600
00603 Bool usb_get_interface (void)
00604 {
00605 U16 wInterface;
00606 U16 wValue;
00607
00608
00609 wValue = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00610
00611
00612 wInterface=usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00613 if(0!=wValue)
00614 return FALSE;
00615 Usb_ack_setup_received_free();
00616
00617 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00618 Usb_write_endpoint_data(EP_CONTROL, 8, usb_interface_status[wInterface] );
00619 Usb_ack_control_in_ready_send();
00620
00621 while( !Is_usb_control_out_received() );
00622 Usb_ack_control_out_received_free();
00623 return TRUE;
00624 }
00625
00626
00629 void usb_set_interface(void)
00630 {
00631 U8 u8_i;
00632
00633
00634
00635 U16 wValue = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00636 U16 wIndex = usb_format_usb_to_mcu_data(16, Usb_read_endpoint_data(EP_CONTROL, 16));
00637 Usb_ack_setup_received_free();
00638
00639
00640 #if (USB_HIGH_SPEED_SUPPORT==ENABLED)
00641 if( Is_usb_full_speed_mode() )
00642 {
00643 data_to_transfer = Usb_get_conf_desc_fs_length();
00644 pbuffer = Usb_get_conf_desc_fs_pointer();
00645 }else{
00646 data_to_transfer = Usb_get_conf_desc_hs_length();
00647 pbuffer = Usb_get_conf_desc_hs_pointer();
00648 }
00649 #else
00650 data_to_transfer = Usb_get_conf_desc_length();
00651 pbuffer = Usb_get_conf_desc_pointer();
00652 #endif
00653
00654
00655
00656
00657 if( usb_configuration_nb == 0 )
00658 {
00659
00660 Usb_enable_stall_handshake(EP_CONTROL);
00661 Usb_ack_setup_received_free();
00662 return;
00663 }
00664 u8_i = usb_configuration_nb;
00665 while( u8_i != 0 )
00666 {
00667 if( CONFIGURATION_DESCRIPTOR != ((S_usb_configuration_descriptor*)pbuffer)->bDescriptorType )
00668 {
00669 data_to_transfer -= ((S_usb_configuration_descriptor*)pbuffer)->bLength;
00670 pbuffer = (U8*)pbuffer + ((S_usb_configuration_descriptor*)pbuffer)->bLength;
00671 continue;
00672 }
00673 u8_i--;
00674 if( u8_i != 0 )
00675 {
00676 data_to_transfer -= ((S_usb_configuration_descriptor*)pbuffer)->wTotalLength;
00677 pbuffer = (U8*)pbuffer + ((S_usb_configuration_descriptor*)pbuffer)->wTotalLength;
00678 }
00679 }
00680
00681
00682 if( wIndex >= ((S_usb_configuration_descriptor*)pbuffer)->bNumInterfaces )
00683 {
00684
00685 Usb_enable_stall_handshake(EP_CONTROL);
00686 Usb_ack_setup_received_free();
00687 return;
00688 }
00689 while( 1 )
00690 {
00691 if( data_to_transfer <= ((S_usb_interface_descriptor*)pbuffer)->bLength )
00692 {
00693
00694 Usb_enable_stall_handshake(EP_CONTROL);
00695 Usb_ack_setup_received_free();
00696 return;
00697 }
00698 data_to_transfer -= ((S_usb_interface_descriptor*)pbuffer)->bLength;
00699 pbuffer = (U8*)pbuffer + ((S_usb_interface_descriptor*)pbuffer)->bLength;
00700 if( INTERFACE_DESCRIPTOR != ((S_usb_interface_descriptor*)pbuffer)->bDescriptorType )
00701 continue;
00702 if( wIndex != ((S_usb_interface_descriptor*)pbuffer)->bInterfaceNumber )
00703 continue;
00704 if( wValue != ((S_usb_interface_descriptor*)pbuffer)->bAlternateSetting )
00705 continue;
00706 usb_interface_status[wIndex] = wValue;
00707 break;
00708 }
00709
00710
00711 while( 1 )
00712 {
00713 if( data_to_transfer <= ((S_usb_endpoint_descriptor*)pbuffer)->bLength )
00714 break;
00715 data_to_transfer -= ((S_usb_endpoint_descriptor*)pbuffer)->bLength;
00716 pbuffer = (U8*)pbuffer + ((S_usb_endpoint_descriptor*)pbuffer)->bLength;
00717 if( INTERFACE_DESCRIPTOR == ((S_usb_endpoint_descriptor*)pbuffer)->bDescriptorType )
00718 break;
00719 if( ENDPOINT_DESCRIPTOR == ((S_usb_endpoint_descriptor*)pbuffer)->bDescriptorType )
00720 {
00721
00722 u8_i = ((S_usb_endpoint_descriptor*)pbuffer)->bEndpointAddress & (~MSK_EP_DIR);
00723 Usb_disable_stall_handshake(u8_i);
00724 Usb_reset_endpoint(u8_i);
00725 Usb_reset_data_toggle(u8_i);
00726 }
00727 }
00728
00729
00730 Usb_ack_control_in_ready_send();
00731 while (!Is_usb_control_in_ready());
00732 }
00733
00734
00735 #endif // USB_DEVICE_FEATURE == ENABLED