usb_standard_request.c
Go to the documentation of this file.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
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 #include "config.h"
00052 #include "usb_drv.h"
00053 #include "usb_descriptors.h"
00054 #include "usb_specific_request.h"
00055 #include <string.h>
00056
00057
00058
00059
00060
00061 #define PRINTF printf
00062 #define PRINTF_P printf_P
00063
00064
00065
00066 static void usb_get_descriptor( void);
00067 static void usb_set_address( void);
00068 static void usb_set_configuration(void);
00069 static void usb_clear_feature( void);
00070 static void usb_set_feature( void);
00071 static void usb_get_status( void);
00072 static void usb_get_configuration(void);
00073 static void usb_get_interface (void);
00074 static void usb_set_interface (void);
00075
00076
00077
00078
00079 static bit zlp;
00080 static U8 endpoint_status[NB_ENDPOINTS];
00081
00082 #ifdef AVRGCC
00083 PGM_VOID_P pbuffer;
00084 #else
00085 U8 FLASH *pbuffer;
00086 #endif
00087 U8 data_to_transfer;
00088
00089 U16 wInterface;
00090
00091 static U8 bmRequestType;
00092
00093 U8 usb_configuration_nb;
00094
00095 usb_mode_t usb_mode = rndis_debug;
00096
00097 extern bit usb_connected;
00098 extern FLASH S_usb_device_descriptor usb_user_device_descriptor_network;
00099 extern FLASH S_usb_user_configuration_descriptor_network usb_user_configuration_descriptor_network;
00100 extern FLASH S_usb_device_descriptor usb_user_device_descriptor_composite;
00101 extern FLASH S_usb_user_configuration_descriptor_composite usb_user_configuration_descriptor_composite;
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 void usb_process_request(void)
00119 {
00120 U8 bmRequest;
00121
00122 bmRequestType = Usb_read_byte();
00123 bmRequest = Usb_read_byte();
00124
00125 switch (bmRequest)
00126 {
00127 case GET_DESCRIPTOR:
00128 if (0x80 == bmRequestType) { usb_get_descriptor(); }
00129 else goto user_read;
00130 break;
00131
00132 case GET_CONFIGURATION:
00133 if (0x80 == bmRequestType) { usb_get_configuration(); }
00134 else goto user_read;
00135 break;
00136
00137 case SET_ADDRESS:
00138 if (0x00 == bmRequestType) { usb_set_address(); }
00139 else goto user_read;
00140 break;
00141
00142 case SET_CONFIGURATION:
00143 if (0x00 == bmRequestType) { usb_set_configuration(); }
00144 else goto user_read;
00145 break;
00146
00147 case CLEAR_FEATURE:
00148 if (0x02 >= bmRequestType) { usb_clear_feature(); }
00149 else goto user_read;
00150 break;
00151
00152 case SET_FEATURE:
00153 if (0x02 >= bmRequestType) { usb_set_feature(); }
00154 else goto user_read;
00155 break;
00156
00157 case GET_STATUS:
00158 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
00159 { usb_get_status(); }
00160 else goto user_read;
00161 break;
00162
00163 case GET_INTERFACE:
00164 if (bmRequestType == 0x81) { usb_get_interface(); }
00165 else goto user_read;
00166 break;
00167
00168
00169 case SET_INTERFACE:
00170 if (bmRequestType == 0x01) {usb_set_interface();}
00171 break;
00172
00173 case SET_DESCRIPTOR:
00174 case SYNCH_FRAME:
00175 default:
00176 user_read:
00177 usb_user_read_request(bmRequestType, bmRequest);
00178 break;
00179 }
00180
00181 Usb_select_endpoint(EP_CONTROL);
00182
00183
00184
00185
00186 if(Is_usb_receive_setup())
00187 Usb_enable_stall_handshake();
00188
00189
00190 Usb_ack_receive_setup();
00191 Usb_ack_receive_out();
00192 Usb_ack_in_ready();
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202 void usb_set_address(void)
00203 {
00204 Usb_configure_address(Usb_read_byte());
00205
00206 Usb_ack_receive_setup();
00207
00208 Usb_send_control_in();
00209 while(!Is_usb_in_ready());
00210
00211 Usb_enable_address();
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 void usb_set_configuration( void )
00224 {
00225 U8 configuration_number;
00226
00227 configuration_number = Usb_read_byte();
00228
00229
00230 Usb_ack_receive_setup();
00231 usb_configuration_nb = configuration_number;
00232
00233 Usb_send_control_in();
00234 while(!Is_usb_in_ready());
00235
00236 usb_user_endpoint_init(usb_configuration_nb);
00237 Usb_set_configuration_action();
00238 }
00239
00240
00241
00242
00243
00244 void usb_get_string_descriptor_sram(U8 string_type) {
00245 U16 requested_length;
00246 U8 dummy;
00247 const char* user_str;
00248
00249 user_str = usb_user_get_string_sram(string_type);
00250
00251 dummy = Usb_read_byte();
00252 dummy = Usb_read_byte();
00253 requested_length = Usb_read_byte();
00254 requested_length |= Usb_read_byte()<<8;
00255
00256 if(!user_str)
00257 return;
00258
00259 const U8 actual_descriptor_size = 2+strlen(user_str)*2;
00260
00261 if (requested_length > actual_descriptor_size) {
00262 zlp = ((actual_descriptor_size % EP_CONTROL_LENGTH) == 0);
00263 requested_length = actual_descriptor_size;
00264 }
00265
00266 Usb_ack_receive_setup() ;
00267
00268 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00269 Usb_enable_stall_handshake();
00270 return;
00271 }
00272
00273
00274 Usb_write_byte(actual_descriptor_size);
00275
00276
00277 Usb_write_byte(STRING_DESCRIPTOR);
00278
00279 requested_length -= 2;
00280 U8 nb_byte = 2;
00281
00282 if(!requested_length) {
00283 Usb_send_control_in();
00284 }
00285
00286 while((requested_length != 0) && (!Is_usb_receive_out()))
00287 {
00288 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00289 Usb_enable_stall_handshake();
00290 break;
00291 }
00292
00293 while(requested_length != 0)
00294 {
00295 if(nb_byte==EP_CONTROL_LENGTH)
00296 {
00297 nb_byte=0;
00298 break;
00299 }
00300
00301 Usb_write_byte(*user_str);
00302 Usb_write_byte(0);
00303 user_str++;
00304 requested_length -=2;
00305 nb_byte+=2;
00306 }
00307 Usb_send_control_in();
00308 }
00309
00310
00311 if(Is_usb_receive_out()) {
00312
00313 Usb_ack_receive_out();
00314 return;
00315 }
00316
00317 if(zlp == TRUE) {
00318 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00319 Usb_enable_stall_handshake();
00320 return;
00321 }
00322 Usb_send_control_in();
00323 }
00324
00325 usb_endpoint_wait_for_receive_out();
00326 Usb_ack_receive_out();
00327 }
00328
00329
00330 void usb_get_string_descriptor(U8 string_type) {
00331 U16 requested_length;
00332 U8 dummy;
00333 PGM_P user_str;
00334
00335 user_str = usb_user_get_string(string_type);
00336
00337 if(!user_str) {
00338 usb_get_string_descriptor_sram(string_type);
00339 return;
00340 }
00341
00342 dummy = Usb_read_byte();
00343 dummy = Usb_read_byte();
00344 requested_length = Usb_read_byte();
00345 requested_length |= Usb_read_byte()<<8;
00346
00347
00348 const U8 actual_descriptor_size = 2+strlen_P(user_str)*2;
00349
00350 if (requested_length > actual_descriptor_size) {
00351 zlp = ((actual_descriptor_size % EP_CONTROL_LENGTH) == 0);
00352 requested_length = actual_descriptor_size;
00353 }
00354
00355 Usb_ack_receive_setup() ;
00356
00357 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00358 Usb_enable_stall_handshake();
00359 return;
00360 }
00361 U8 nb_byte = 0;
00362
00363
00364 if(requested_length) {
00365 Usb_write_byte(actual_descriptor_size);
00366 requested_length--;
00367 nb_byte++;
00368 }
00369
00370
00371 if(requested_length) {
00372 Usb_write_byte(STRING_DESCRIPTOR);
00373 requested_length--;
00374 nb_byte++;
00375 }
00376
00377 if(!requested_length) {
00378 Usb_send_control_in();
00379 }
00380
00381 while((requested_length != 0) && (!Is_usb_receive_out()))
00382 {
00383 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00384 Usb_enable_stall_handshake();
00385 break;
00386 }
00387
00388 while(requested_length != 0)
00389 {
00390 if(nb_byte==EP_CONTROL_LENGTH) {
00391 nb_byte=0;
00392 break;
00393 }
00394
00395 Usb_write_byte(pgm_read_byte_near((unsigned int)user_str++));
00396 requested_length--;
00397 nb_byte++;
00398 if(requested_length) {
00399 Usb_write_byte(0);
00400 requested_length--;
00401 nb_byte++;
00402 }
00403 }
00404 Usb_send_control_in();
00405 }
00406
00407
00408
00409 if(Is_usb_receive_out()) {
00410
00411 Usb_ack_receive_out();
00412 return;
00413 }
00414
00415 if(zlp == TRUE) {
00416 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00417 Usb_enable_stall_handshake();
00418 return;
00419 }
00420 Usb_send_control_in();
00421 }
00422
00423 usb_endpoint_wait_for_receive_out();
00424 Usb_ack_receive_out();
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 void usb_get_descriptor(void)
00439 {
00440 U8 LSBwLength, MSBwLength;
00441 U8 descriptor_type ;
00442 U8 string_type ;
00443 U8 dummy;
00444 U8 byteswereread;
00445
00446 zlp = FALSE;
00447 string_type = Usb_read_byte();
00448 descriptor_type = Usb_read_byte();
00449 byteswereread = 0;
00450
00451 switch (descriptor_type)
00452 {
00453 case DEVICE_DESCRIPTOR:
00454 data_to_transfer = Usb_get_dev_desc_length();
00455 pbuffer = Usb_get_dev_desc_pointer();
00456 break;
00457 case CONFIGURATION_DESCRIPTOR:
00458 data_to_transfer = Usb_get_conf_desc_length(string_type);
00459 pbuffer = Usb_get_conf_desc_pointer(string_type);
00460 break;
00461 #if 1
00462 case STRING_DESCRIPTOR:
00463 if(string_type!=LANG_ID) {
00464 usb_get_string_descriptor(string_type);
00465 return;
00466 }
00467 #endif
00468 default:
00469 dummy = Usb_read_byte();
00470 dummy = Usb_read_byte();
00471 LSBwLength = Usb_read_byte();
00472 MSBwLength = Usb_read_byte();
00473 byteswereread=1;
00474 if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE ) {
00475 Usb_enable_stall_handshake();
00476 Usb_ack_receive_setup();
00477 return;
00478 }
00479 break;
00480 }
00481 if (byteswereread==0) {
00482 dummy = Usb_read_byte();
00483 dummy = Usb_read_byte();
00484 LSBwLength = Usb_read_byte();
00485 MSBwLength = Usb_read_byte();
00486 }
00487
00488 Usb_ack_receive_setup() ;
00489
00490 if ((LSBwLength > data_to_transfer) || (MSBwLength)) {
00491 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00492 else { zlp = FALSE; }
00493
00494 LSBwLength = data_to_transfer;
00495 MSBwLength = 0x00;
00496 } else {
00497 data_to_transfer = LSBwLength;
00498 }
00499
00500 while((data_to_transfer != 0) && (!Is_usb_receive_out())) {
00501 U8 nb_byte = 0;
00502 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00503 Usb_enable_stall_handshake();
00504 break;
00505 }
00506
00507
00508 while(data_to_transfer != 0) {
00509
00510 if(nb_byte++==EP_CONTROL_LENGTH)
00511 break;
00512
00513 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00514 data_to_transfer --;
00515
00516 }
00517 Usb_send_control_in();
00518 }
00519
00520 if(Is_usb_receive_out()) {
00521
00522 Usb_ack_receive_out();
00523 return;
00524 }
00525
00526 if(zlp == TRUE) {
00527 if(usb_endpoint_wait_for_read_control_enabled()!=0) {
00528 Usb_enable_stall_handshake();
00529 return;
00530 }
00531 Usb_send_control_in();
00532 }
00533
00534 usb_endpoint_wait_for_receive_out();
00535 Usb_ack_receive_out();
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545 void usb_get_configuration(void)
00546 {
00547 Usb_ack_receive_setup();
00548
00549 Usb_write_byte(usb_configuration_nb);
00550 Usb_send_control_in();
00551
00552 usb_endpoint_wait_for_receive_out();
00553 Usb_ack_receive_out();
00554 }
00555
00556
00557
00558
00559
00560
00561
00562
00563 void usb_get_status(void)
00564 {
00565 U8 wIndex;
00566 U8 dummy;
00567
00568 dummy = Usb_read_byte();
00569 dummy = Usb_read_byte();
00570 wIndex = Usb_read_byte();
00571
00572 switch(bmRequestType)
00573 {
00574 case REQUEST_DEVICE_STATUS: Usb_ack_receive_setup();
00575 Usb_write_byte(DEVICE_STATUS);
00576 break;
00577
00578 case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
00579 Usb_write_byte(INTERFACE_STATUS);
00580 break;
00581
00582 case REQUEST_ENDPOINT_STATUS: Usb_ack_receive_setup();
00583 wIndex = wIndex & MSK_EP_DIR;
00584 Usb_write_byte(endpoint_status[wIndex]);
00585 break;
00586 default:
00587 Usb_enable_stall_handshake();
00588 Usb_ack_receive_setup();
00589 return;
00590 }
00591
00592 Usb_write_byte(0x00);
00593 Usb_send_control_in();
00594
00595 usb_endpoint_wait_for_receive_out();
00596 Usb_ack_receive_out();
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606 void usb_set_feature(void)
00607 {
00608 U8 wValue;
00609 U8 wIndex;
00610 U8 dummy;
00611
00612 if (bmRequestType == INTERFACE_TYPE)
00613 {
00614 return;
00615 }
00616 else if (bmRequestType == ENDPOINT_TYPE)
00617 {
00618 wValue = Usb_read_byte();
00619 dummy = Usb_read_byte();
00620
00621 if (wValue == FEATURE_ENDPOINT_HALT)
00622 {
00623 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00624
00625 if (wIndex == EP_CONTROL)
00626 {
00627 return;
00628 }
00629
00630 Usb_select_endpoint(wIndex);
00631 if(Is_usb_endpoint_enabled())
00632 {
00633 Usb_enable_stall_handshake();
00634 Usb_select_endpoint(EP_CONTROL);
00635 endpoint_status[wIndex] = 0x01;
00636 Usb_ack_receive_setup();
00637 Usb_send_control_in();
00638 }
00639 else
00640 {
00641 Usb_select_endpoint(EP_CONTROL);
00642 return;
00643 }
00644 }
00645 else
00646 {
00647 return;
00648 }
00649 }
00650 }
00651
00652
00653
00654
00655
00656
00657
00658 void usb_clear_feature(void)
00659 {
00660 U8 wValue;
00661 U8 wIndex;
00662 U8 dummy;
00663
00664 if (bmRequestType == ZERO_TYPE)
00665 {
00666 return;
00667 }
00668 else if (bmRequestType == INTERFACE_TYPE)
00669 {
00670 return;
00671 }
00672 else if (bmRequestType == ENDPOINT_TYPE)
00673 {
00674 wValue = Usb_read_byte();
00675 dummy = Usb_read_byte();
00676
00677 if (wValue == FEATURE_ENDPOINT_HALT)
00678 {
00679 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00680
00681 Usb_select_endpoint(wIndex);
00682 if(Is_usb_endpoint_enabled())
00683 {
00684 if(wIndex != EP_CONTROL)
00685 {
00686 Usb_disable_stall_handshake();
00687 Usb_reset_endpoint(wIndex);
00688 Usb_reset_data_toggle();
00689 }
00690 Usb_select_endpoint(EP_CONTROL);
00691 endpoint_status[wIndex] = 0x00;
00692 Usb_ack_receive_setup();
00693 Usb_send_control_in();
00694
00695 }
00696 else
00697 {
00698 return;
00699 }
00700 }
00701 else
00702 {
00703 return;
00704 }
00705 }
00706 }
00707
00708
00709
00710
00711
00712
00713
00714 void usb_get_interface (void)
00715 {
00716
00717 }
00718
00719
00720
00721
00722
00723
00724
00725 void usb_set_interface (void)
00726 {
00727 U8 alt_setting;
00728 U8 dummy;
00729 U8 interface;
00730
00731 alt_setting = Usb_read_byte();
00732 dummy = Usb_read_byte();
00733 interface = Usb_read_byte();
00734
00735 if(usb_user_set_alt_interface(interface, alt_setting)) {
00736 Usb_ack_receive_setup();
00737 Usb_send_control_in();
00738 while(!Is_usb_in_ready());
00739
00740 usb_endpoint_wait_for_receive_out();
00741 Usb_ack_receive_out();
00742 }
00743
00744 }