00001
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
00052 #include "conf_usb.h"
00053
00054
00055 #if USB_HOST_FEATURE == ENABLED
00056
00057 #include "compiler.h"
00058 #include "usb_drv.h"
00059 #include "usb_host_enum.h"
00060 #include "usb_host_task.h"
00061 #include "usb_task.h"
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00073 static const U16 registered_VID_PID[] = VID_PID_TABLE;
00074 #define REG_VID_PID_CNT (sizeof(registered_VID_PID) / sizeof(registered_VID_PID[0]))
00075
00077 static const U8 registered_class[] = CLASS_SUBCLASS_PROTOCOL;
00078 #define REG_CLASS_CNT (sizeof(registered_class) / sizeof(registered_class[0]))
00079
00080
00081
00082
00084 volatile U16 device_VID;
00085
00087 volatile U16 device_PID;
00088
00090 volatile U8 bmattributes;
00091
00093 volatile U8 maxpower;
00094
00096 volatile U8 nb_interface_supported = 0;
00097
00099 volatile S_interface interface_supported[MAX_INTERFACE_SUPPORTED];
00100
00101 extern U32 host_get_timeout( void );
00102
00108 Bool host_check_VID_PID(void)
00109 {
00110 U8 c, d;
00111
00112
00113 device_VID = usb_format_usb_to_mcu_data(16, *(U16 *)(data_stage + OFFSET_FIELD_VID));
00114 device_PID = usb_format_usb_to_mcu_data(16, *(U16 *)(data_stage + OFFSET_FIELD_PID));
00115
00116
00117 for (c = 0; c < REG_VID_PID_CNT; )
00118 {
00119 if (registered_VID_PID[c] == device_VID)
00120 {
00121 for (c += 2, d = c + registered_VID_PID[c - 1]; c < d; c++)
00122 {
00123 if (registered_VID_PID[c] == device_PID) return TRUE;
00124 }
00125 }
00126 else c += 2 + registered_VID_PID[c + 1];
00127 }
00128
00129 return FALSE;
00130 }
00131
00132
00141 Bool host_check_class(void)
00142 {
00143 U8 *descriptor, *conf_end;
00144 U8 device_class, device_subclass, device_protocol;
00145 U8 c;
00146 #if HOST_AUTO_CFG_ENDPOINT == ENABLE
00147 U8 nb_endpoint_to_configure = 0;
00148 U8 ep_index = 0;
00149 U8 physical_pipe = P_1;
00150
00151
00152 Host_set_configured();
00153 #endif
00154
00155
00156 nb_interface_supported = 0;
00157
00158
00159 if (data_stage[OFFSET_FIELD_DESCRIPTOR_TYPE] != CONFIGURATION_DESCRIPTOR) return FALSE;
00160
00161 bmattributes = data_stage[OFFSET_FIELD_BMATTRIBUTES];
00162 maxpower = data_stage[OFFSET_FIELD_MAXPOWER];
00163
00164 conf_end = data_stage +
00165 min(usb_format_usb_to_mcu_data(16, *(U16 *)(data_stage + OFFSET_FIELD_TOTAL_LENGTH)),
00166 SIZEOF_DATA_STAGE - OFFSET_FIELD_PROTOCOL);
00167
00168
00169 for (descriptor = data_stage + data_stage[OFFSET_DESCRIPTOR_LENGTH]; descriptor < conf_end;
00170 descriptor += descriptor[OFFSET_DESCRIPTOR_LENGTH])
00171 {
00172
00173 switch (descriptor[OFFSET_FIELD_DESCRIPTOR_TYPE])
00174 {
00175 case INTERFACE_DESCRIPTOR:
00176
00177 if (nb_interface_supported >= MAX_INTERFACE_SUPPORTED) return TRUE;
00178
00179 #if HOST_AUTO_CFG_ENDPOINT == ENABLE
00180
00181 if (nb_endpoint_to_configure)
00182 {
00183
00184 Host_clear_configured();
00185
00186
00187 nb_endpoint_to_configure = 0;
00188 }
00189 #endif
00190
00191
00192
00193 device_class = descriptor[OFFSET_FIELD_CLASS];
00194 device_subclass = descriptor[OFFSET_FIELD_SUB_CLASS];
00195 device_protocol = descriptor[OFFSET_FIELD_PROTOCOL];
00196
00197
00198 for (c = 0; c < REG_CLASS_CNT; c += 3)
00199 {
00200 if (registered_class[c] == device_class &&
00201 registered_class[c + 1] == device_subclass &&
00202 registered_class[c + 2] == device_protocol)
00203 {
00204
00205
00206 interface_supported[nb_interface_supported].interface_nb = descriptor[OFFSET_FIELD_INTERFACE_NB];
00207
00208 interface_supported[nb_interface_supported].altset_nb = descriptor[OFFSET_FIELD_ALT];
00209
00210 interface_supported[nb_interface_supported].uclass = device_class;
00211
00212 interface_supported[nb_interface_supported].subclass = device_subclass;
00213
00214 interface_supported[nb_interface_supported].protocol = device_protocol;
00215
00216 #if HOST_AUTO_CFG_ENDPOINT == ENABLE
00217 ep_index = 0;
00218 nb_endpoint_to_configure =
00219 #endif
00220 interface_supported[nb_interface_supported].nb_ep = min(descriptor[OFFSET_FIELD_NB_OF_EP], MAX_EP_PER_INTERFACE);
00221
00222
00223 nb_interface_supported++;
00224
00225
00226 break;
00227 }
00228 }
00229 break;
00230
00231 #if HOST_AUTO_CFG_ENDPOINT == ENABLE
00232 case ENDPOINT_DESCRIPTOR:
00233
00234 if (physical_pipe < MAX_PEP_NB && nb_endpoint_to_configure)
00235 {
00236 nb_endpoint_to_configure--;
00237
00238
00239 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00240 Disable_global_interrupt();
00241 #endif
00242 Host_disable_pipe(physical_pipe);
00243 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00244 (void)Is_host_pipe_enabled(physical_pipe);
00245 Enable_global_interrupt();
00246 #endif
00247 Host_unallocate_memory(physical_pipe);
00248 Host_enable_pipe(physical_pipe);
00249
00250
00251 if( TYPE_INTERRUPT != descriptor[OFFSET_FIELD_EP_TYPE] ) descriptor[OFFSET_FIELD_EP_INTERVAL] = 0;
00252
00253
00254 (void)Host_configure_pipe(
00255 physical_pipe,
00256 descriptor[OFFSET_FIELD_EP_INTERVAL],
00257 Get_desc_ep_nbr(descriptor[OFFSET_FIELD_EP_ADDR]),
00258 descriptor[OFFSET_FIELD_EP_TYPE],
00259 Get_pipe_token(descriptor[OFFSET_FIELD_EP_ADDR]),
00260 descriptor[OFFSET_FIELD_EP_SIZE] |
00261 descriptor[OFFSET_FIELD_EP_SIZE + 1] << 8,
00262 (TYPE_BULK == descriptor[OFFSET_FIELD_EP_TYPE]) ? DOUBLE_BANK : SINGLE_BANK
00263 );
00264
00265 #if (USB_HIGH_SPEED_SUPPORT==ENABLED)
00266 if( (TYPE_BULK == Host_get_pipe_type(physical_pipe))
00267 && (TOKEN_OUT == Host_get_pipe_token(physical_pipe)) )
00268 {
00269 if( !Is_usb_full_speed_mode() )
00270 {
00271
00272 Host_configure_pipe_int_req_freq(physical_pipe,0);
00273 Host_enable_ping(physical_pipe);
00274 }
00275 }
00276 #endif
00277
00278
00279 interface_supported[nb_interface_supported - 1].ep_pipe[ep_index++] = physical_pipe++;
00280 }
00281 break;
00282 #endif
00283 }
00284
00285
00286 Host_user_check_class_action(descriptor);
00287 }
00288
00289 #if HOST_AUTO_CFG_ENDPOINT == ENABLE
00290
00291 if (nb_endpoint_to_configure)
00292 {
00293
00294 Host_clear_configured();
00295 }
00296 #endif
00297
00298 return (nb_interface_supported > 0);
00299 }
00300
00301
00314 Status_t host_transfer_control(void *data_pointer)
00315 {
00316 Bool sav_int_sof_enable;
00317 Bool sav_glob_int_en;
00318 U16 data_length;
00319 U8 c;
00320
00321 Usb_ack_event(EVT_HOST_SOF);
00322 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00323 Host_enable_sof_interrupt();
00324 while (!Is_usb_event(EVT_HOST_SOF))
00325 {
00326 if (Is_host_emergency_exit())
00327 {
00328 Host_freeze_pipe(P_CONTROL);
00329 Host_reset_pipe(P_CONTROL);
00330 return CONTROL_TIMEOUT;
00331 }
00332 }
00333 if (!sav_int_sof_enable)
00334 {
00335 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00336 Host_disable_sof_interrupt();
00337 (void)Is_host_sof_interrupt_enabled();
00338 if (sav_glob_int_en) Enable_global_interrupt();
00339 }
00340
00341 Host_configure_pipe_token(P_CONTROL, TOKEN_SETUP);
00342 Host_ack_setup_ready();
00343 Host_unfreeze_pipe(P_CONTROL);
00344
00345
00346 Host_reset_pipe_fifo_access(P_CONTROL);
00347 Host_write_pipe_data(P_CONTROL, 8, usb_request.bmRequestType);
00348 Host_write_pipe_data(P_CONTROL, 8, usb_request.bRequest);
00349 Host_write_pipe_data(P_CONTROL, 16, usb_format_mcu_to_usb_data(16, usb_request.wValue));
00350 Host_write_pipe_data(P_CONTROL, 16, usb_format_mcu_to_usb_data(16, usb_request.wIndex));
00351 Host_write_pipe_data(P_CONTROL, 16, usb_format_mcu_to_usb_data(16, usb_request.wLength));
00352 Host_send_setup();
00353
00354 while (!Is_host_setup_ready())
00355 {
00356 if (Is_host_emergency_exit())
00357 {
00358 Host_freeze_pipe(P_CONTROL);
00359 Host_reset_pipe(P_CONTROL);
00360 return CONTROL_TIMEOUT;
00361 }
00362 if (Is_host_pipe_error(P_CONTROL))
00363 {
00364 c = Host_error_status(P_CONTROL);
00365 Host_ack_all_errors(P_CONTROL);
00366 return c;
00367 }
00368 }
00369
00370
00371
00372 Usb_ack_event(EVT_HOST_SOF);
00373 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00374 Host_enable_sof_interrupt();
00375 Host_freeze_pipe(P_CONTROL);
00376 data_length = usb_request.wLength;
00377 while (!Is_usb_event(EVT_HOST_SOF))
00378 {
00379 if (Is_host_emergency_exit())
00380 {
00381 Host_freeze_pipe(P_CONTROL);
00382 Host_reset_pipe(P_CONTROL);
00383 return CONTROL_TIMEOUT;
00384 }
00385 }
00386 if (!sav_int_sof_enable)
00387 {
00388 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00389 Host_disable_sof_interrupt();
00390 (void)Is_host_sof_interrupt_enabled();
00391 if (sav_glob_int_en) Enable_global_interrupt();
00392 }
00393
00394
00395 if (usb_request.bmRequestType & 0x80)
00396 {
00397 Host_disable_continuous_in_mode(P_CONTROL);
00398 Host_configure_pipe_token(P_CONTROL, TOKEN_IN);
00399 Host_ack_control_in_received_free();
00400 while (data_length)
00401 {
00402 Host_unfreeze_pipe(P_CONTROL);
00403 private_sof_counter = 0;
00404 while (!Is_host_control_in_received())
00405 {
00406 if (Is_host_emergency_exit())
00407 {
00408 Host_freeze_pipe(P_CONTROL);
00409 Host_reset_pipe(P_CONTROL);
00410 return CONTROL_TIMEOUT;
00411 }
00412 if (Is_host_pipe_error(P_CONTROL))
00413 {
00414 c = Host_error_status(P_CONTROL);
00415 Host_ack_all_errors(P_CONTROL);
00416 return c;
00417 }
00418 if (Is_host_stall(P_CONTROL))
00419 {
00420 Host_ack_stall(P_CONTROL);
00421 return CONTROL_STALL;
00422 }
00423 #if TIMEOUT_DELAY_ENABLE == ENABLE
00424 if (1000 < host_get_timeout())
00425 {
00426 Host_freeze_pipe(P_CONTROL);
00427 Host_reset_pipe(P_CONTROL);
00428 return CONTROL_TIMEOUT;
00429 }
00430 #endif
00431 }
00432 Host_reset_pipe_fifo_access(P_CONTROL);
00433 c = Host_get_pipe_size(P_CONTROL) - Host_byte_count(P_CONTROL);
00434 data_length = host_read_p_rxpacket(P_CONTROL, data_pointer, data_length, &data_pointer);
00435 if (usb_request.incomplete_read || c) data_length = 0;
00436 Host_freeze_pipe(P_CONTROL);
00437 Host_ack_control_in_received_free();
00438
00439
00440
00441
00442
00443
00444 if (Is_usb_low_speed_mode())
00445 {
00446 Usb_ack_event(EVT_HOST_SOF);
00447 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00448 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00449 Host_ack_sof();
00450 (void)Is_host_sof_interrupt_enabled();
00451 if (sav_glob_int_en) Enable_global_interrupt();
00452 Host_enable_sof_interrupt();
00453 while (!Is_usb_event(EVT_HOST_SOF))
00454 {
00455 if (Is_host_emergency_exit())
00456 {
00457 Host_freeze_pipe(P_CONTROL);
00458 Host_reset_pipe(P_CONTROL);
00459 return CONTROL_TIMEOUT;
00460 }
00461 }
00462 if (!sav_int_sof_enable)
00463 {
00464 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00465 Host_disable_sof_interrupt();
00466 (void)Is_host_sof_interrupt_enabled();
00467 if (sav_glob_int_en) Enable_global_interrupt();
00468 }
00469 }
00470 }
00471
00472 Host_configure_pipe_token(P_CONTROL, TOKEN_OUT);
00473 Host_ack_control_out_ready_send();
00474 Host_unfreeze_pipe(P_CONTROL);
00475 while (!Is_host_control_out_ready())
00476 {
00477 if (Is_host_emergency_exit())
00478 {
00479 Host_freeze_pipe(P_CONTROL);
00480 Host_reset_pipe(P_CONTROL);
00481 return CONTROL_TIMEOUT;
00482 }
00483 if (Is_host_pipe_error(P_CONTROL))
00484 {
00485 c = Host_error_status(P_CONTROL);
00486 Host_ack_all_errors(P_CONTROL);
00487 return c;
00488 }
00489 if (Is_host_stall(P_CONTROL))
00490 {
00491 Host_ack_stall(P_CONTROL);
00492 return CONTROL_STALL;
00493 }
00494 }
00495 Host_ack_control_out_ready();
00496 }
00497
00498
00499 else
00500 {
00501 Host_configure_pipe_token(P_CONTROL, TOKEN_OUT);
00502 Host_ack_control_out_ready();
00503 while (data_length)
00504 {
00505 Host_unfreeze_pipe(P_CONTROL);
00506 Host_reset_pipe_fifo_access(P_CONTROL);
00507 data_length = host_write_p_txpacket(P_CONTROL, data_pointer, data_length, (const void **)&data_pointer);
00508 Host_send_control_out();
00509 while (!Is_host_control_out_ready())
00510 {
00511 if (Is_host_emergency_exit())
00512 {
00513 Host_freeze_pipe(P_CONTROL);
00514 Host_reset_pipe(P_CONTROL);
00515 return CONTROL_TIMEOUT;
00516 }
00517 if (Is_host_pipe_error(P_CONTROL))
00518 {
00519 c = Host_error_status(P_CONTROL);
00520 Host_ack_all_errors(P_CONTROL);
00521 return c;
00522 }
00523 if (Is_host_stall(P_CONTROL))
00524 {
00525 Host_ack_stall(P_CONTROL);
00526 return CONTROL_STALL;
00527 }
00528 }
00529 Host_ack_control_out_ready();
00530 }
00531
00532 Host_freeze_pipe(P_CONTROL);
00533 Host_configure_pipe_token(P_CONTROL, TOKEN_IN);
00534 Host_ack_control_in_received_free();
00535 Host_unfreeze_pipe(P_CONTROL);
00536 while (!Is_host_control_in_received())
00537 {
00538 if (Is_host_emergency_exit())
00539 {
00540 Host_freeze_pipe(P_CONTROL);
00541 Host_reset_pipe(P_CONTROL);
00542 return CONTROL_TIMEOUT;
00543 }
00544 if (Is_host_pipe_error(P_CONTROL))
00545 {
00546 c = Host_error_status(P_CONTROL);
00547 Host_ack_all_errors(P_CONTROL);
00548 return c;
00549 }
00550 if (Is_host_stall(P_CONTROL))
00551 {
00552 Host_ack_stall(P_CONTROL);
00553 return CONTROL_STALL;
00554 }
00555 }
00556 Host_ack_control_in_received();
00557 Host_freeze_pipe(P_CONTROL);
00558 Host_free_control_in();
00559 }
00560
00561 return CONTROL_GOOD;
00562 }
00563
00564
00565 #endif // USB_HOST_FEATURE == ENABLED