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 bmRequestType = Usb_read_byte();
00100 bmRequest = Usb_read_byte();
00101
00102 switch (bmRequest)
00103 {
00104 case GET_DESCRIPTOR:
00105 if (0x80 == bmRequestType) { usb_get_descriptor(); }
00106 else { usb_user_read_request(bmRequestType, bmRequest); }
00107 break;
00108
00109 case GET_CONFIGURATION:
00110 if (0x80 == bmRequestType) { usb_get_configuration(); }
00111 else { usb_user_read_request(bmRequestType, bmRequest); }
00112 break;
00113
00114 case SET_ADDRESS:
00115 if (0x00 == bmRequestType) { usb_set_address(); }
00116 else { usb_user_read_request(bmRequestType, bmRequest); }
00117 break;
00118
00119 case SET_CONFIGURATION:
00120 if (0x00 == bmRequestType) { usb_set_configuration(); }
00121 else { usb_user_read_request(bmRequestType, bmRequest); }
00122 break;
00123
00124 case CLEAR_FEATURE:
00125 if (0x02 >= bmRequestType) { usb_clear_feature(); }
00126 else { usb_user_read_request(bmRequestType, bmRequest); }
00127 break;
00128
00129 case SET_FEATURE:
00130 if (0x02 >= bmRequestType) { usb_set_feature(); }
00131 else { usb_user_read_request(bmRequestType, bmRequest); }
00132 break;
00133
00134 case GET_STATUS:
00135 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType))
00136 { usb_get_status(); }
00137 else { usb_user_read_request(bmRequestType, bmRequest); }
00138 break;
00139
00140 case GET_INTERFACE:
00141 if (bmRequestType == 0x81) { usb_get_interface(); }
00142 else { usb_user_read_request(bmRequestType, bmRequest); }
00143 break;
00144
00145
00146 case SET_INTERFACE:
00147 if (bmRequestType == 0x01) {usb_set_interface();}
00148 break;
00149
00150 case SET_DESCRIPTOR:
00151 case SYNCH_FRAME:
00152 default:
00153 if(usb_user_read_request(bmRequestType, bmRequest) == FALSE)
00154 {
00155 Usb_enable_stall_handshake();
00156 Usb_ack_receive_setup();
00157 return;
00158 }
00159 break;
00160 }
00161 }
00162
00163
00175 void usb_set_address(void)
00176 {
00177 Usb_configure_address(Usb_read_byte());
00178
00179 Usb_ack_receive_setup();
00180
00181 Usb_send_control_in();
00182 while(!Is_usb_in_ready());
00183
00184 Usb_enable_address();
00185 }
00186
00187
00201 void usb_set_configuration( void )
00202 {
00203 U8 configuration_number;
00204
00205 configuration_number = Usb_read_byte();
00206
00207 if (configuration_number <= NB_CONFIGURATION)
00208 {
00209 Usb_ack_receive_setup();
00210 usb_configuration_nb = configuration_number;
00211 }
00212 else
00213 {
00216 Usb_enable_stall_handshake();
00217 Usb_ack_receive_setup();
00218 return;
00219 }
00220
00221 Usb_send_control_in();
00222
00223 usb_user_endpoint_init(usb_configuration_nb);
00224 Usb_set_configuration_action();
00225 }
00226
00227
00242 void usb_get_descriptor(void)
00243 {
00244 U16 wLength ;
00245 U8 descriptor_type ;
00246 U8 string_type ;
00247 U8 dummy;
00248 U8 nb_byte;
00249
00250 zlp = FALSE;
00251 string_type = Usb_read_byte();
00252 descriptor_type = Usb_read_byte();
00253
00254 switch (descriptor_type)
00255 {
00256 case DEVICE_DESCRIPTOR:
00257 data_to_transfer = Usb_get_dev_desc_length();
00258 pbuffer = Usb_get_dev_desc_pointer();
00259 break;
00260 case CONFIGURATION_DESCRIPTOR:
00261 data_to_transfer = Usb_get_conf_desc_length();
00262 pbuffer = Usb_get_conf_desc_pointer();
00263 break;
00264 default:
00265 if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE )
00266 {
00267 Usb_enable_stall_handshake();
00268 Usb_ack_receive_setup();
00269 return;
00270 }
00271 break;
00272 }
00273
00274 dummy = Usb_read_byte();
00275 dummy = Usb_read_byte();
00276 LSB(wLength) = Usb_read_byte();
00277 MSB(wLength) = Usb_read_byte();
00278 Usb_ack_receive_setup() ;
00279
00280 if (wLength > data_to_transfer)
00281 {
00282 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
00283 else { zlp = FALSE; }
00284 }
00285 else
00286 {
00287 data_to_transfer = (U8)wLength;
00288 }
00289
00290 while((data_to_transfer != 0) && (!Is_usb_receive_out()))
00291 {
00292 while(!Is_usb_read_control_enabled());
00293
00294 nb_byte=0;
00295 while(data_to_transfer != 0)
00296 {
00297 if(nb_byte++==EP_CONTROL_LENGTH)
00298 {
00299 break;
00300 }
00301 #ifndef AVRGCC
00302 Usb_write_byte(*pbuffer++);
00303 #else // AVRGCC does not support point to PGM space
00304 #warning with avrgcc assumes devices descriptors are stored in the lower 64Kbytes of on-chip flash memory
00305 Usb_write_byte(pgm_read_byte_near((unsigned int)pbuffer++));
00306 #endif
00307 data_to_transfer --;
00308 }
00309 Usb_send_control_in();
00310 }
00311
00312 Usb_send_control_in();
00313
00314 if(Is_usb_receive_out()) { Usb_ack_receive_out(); return; }
00315 if(zlp == TRUE)
00316 {
00317 while(!Is_usb_read_control_enabled());
00318 Usb_send_control_in();
00319 }
00320
00321
00322 while(!Is_usb_receive_out());
00323 Usb_ack_receive_out();
00324 }
00325
00326
00338 void usb_get_configuration(void)
00339 {
00340 Usb_ack_receive_setup();
00341
00342 Usb_write_byte(usb_configuration_nb);
00343 Usb_ack_in_ready();
00344
00345 while( !Is_usb_receive_out() );
00346 Usb_ack_receive_out();
00347 }
00348
00360 void usb_get_status(void)
00361 {
00362 U8 wIndex;
00363 U8 dummy;
00364
00365 dummy = Usb_read_byte();
00366 dummy = Usb_read_byte();
00367 wIndex = Usb_read_byte();
00368
00369 switch(bmRequestType)
00370 {
00371 case REQUEST_DEVICE_STATUS: Usb_ack_receive_setup();
00372 Usb_write_byte(DEVICE_STATUS);
00373 break;
00374
00375 case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup();
00376 Usb_write_byte(INTERFACE_STATUS);
00377 break;
00378
00379 case REQUEST_ENDPOINT_STATUS: Usb_ack_receive_setup();
00380 wIndex = wIndex & MSK_EP_DIR;
00381 Usb_write_byte(endpoint_status[wIndex]);
00382 break;
00383 default:
00384 Usb_enable_stall_handshake();
00385 Usb_ack_receive_setup();
00386 return;
00387 }
00388
00389 Usb_write_byte(0x00);
00390 Usb_send_control_in();
00391
00392 while( !Is_usb_receive_out() );
00393 Usb_ack_receive_out();
00394 }
00395
00396
00408 void usb_set_feature(void)
00409 {
00410 U8 wValue;
00411 U8 wIndex;
00412 U8 dummy;
00413
00414 if (bmRequestType == INTERFACE_TYPE)
00415 {
00418 Usb_enable_stall_handshake();
00419 Usb_ack_receive_setup();
00420 return;
00421 }
00422 else if (bmRequestType == ENDPOINT_TYPE)
00423 {
00424 wValue = Usb_read_byte();
00425 dummy = Usb_read_byte();
00426
00427 if (wValue == FEATURE_ENDPOINT_HALT)
00428 {
00429 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00430
00431 if (wIndex == EP_CONTROL)
00432 {
00433 Usb_enable_stall_handshake();
00434 Usb_ack_receive_setup();
00435 return;
00436 }
00437
00438 Usb_select_endpoint(wIndex);
00439 if(Is_usb_endpoint_enabled())
00440 {
00441 Usb_enable_stall_handshake();
00442 Usb_select_endpoint(EP_CONTROL);
00443 endpoint_status[wIndex] = 0x01;
00444 Usb_ack_receive_setup();
00445 Usb_send_control_in();
00446 }
00447 else
00448 {
00449 Usb_select_endpoint(EP_CONTROL);
00450 Usb_enable_stall_handshake();
00451 Usb_ack_receive_setup();
00452 return;
00453 }
00454 }
00455 else
00456 {
00457 Usb_enable_stall_handshake();
00458 Usb_ack_receive_setup();
00459 return;
00460 }
00461 }
00462 }
00463
00464
00475 void usb_clear_feature(void)
00476 {
00477 U8 wValue;
00478 U8 wIndex;
00479 U8 dummy;
00480
00481 if (bmRequestType == ZERO_TYPE)
00482 {
00485 Usb_enable_stall_handshake();
00486 Usb_ack_receive_setup();
00487 return;
00488 }
00489 else if (bmRequestType == INTERFACE_TYPE)
00490 {
00493 Usb_enable_stall_handshake();
00494 Usb_ack_receive_setup();
00495 return;
00496 }
00497 else if (bmRequestType == ENDPOINT_TYPE)
00498 {
00499 wValue = Usb_read_byte();
00500 dummy = Usb_read_byte();
00501
00502 if (wValue == FEATURE_ENDPOINT_HALT)
00503 {
00504 wIndex = (Usb_read_byte() & MSK_EP_DIR);
00505
00506 Usb_select_endpoint(wIndex);
00507 if(Is_usb_endpoint_enabled())
00508 {
00509 if(wIndex != EP_CONTROL)
00510 {
00511 Usb_disable_stall_handshake();
00512 Usb_reset_endpoint(wIndex);
00513 Usb_reset_data_toggle();
00514 }
00515 Usb_select_endpoint(EP_CONTROL);
00516 endpoint_status[wIndex] = 0x00;
00517 Usb_ack_receive_setup();
00518 Usb_send_control_in();
00519 }
00520 else
00521 {
00522 Usb_select_endpoint(EP_CONTROL);
00523 Usb_enable_stall_handshake();
00524 Usb_ack_receive_setup();
00525 return;
00526 }
00527 }
00528 else
00529 {
00530 Usb_enable_stall_handshake();
00531 Usb_ack_receive_setup();
00532 return;
00533 }
00534 }
00535 }
00536
00537
00538
00549 void usb_get_interface (void)
00550 {
00551 Usb_enable_stall_handshake();
00552 Usb_ack_receive_setup();
00553 }
00554
00565 void usb_set_interface (void)
00566 {
00567 Usb_ack_receive_setup();
00568 Usb_send_control_in();
00569 while(!Is_usb_in_ready());
00570 }