Changes in uspace/drv/usbhub/utils.c [98d06b8:b5ec347] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbhub/utils.c
r98d06b8 rb5ec347 58 58 59 59 //hub descriptor utils 60 61 60 void * usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor) { 62 61 //base size … … 108 107 109 108 //control transactions 110 111 109 int usb_drv_sync_control_read( 112 113 114 115 ){110 int phone, usb_target_t target, 111 usb_device_request_setup_packet_t * request, 112 void * rcvd_buffer, size_t rcvd_size, size_t * actual_size 113 ){ 116 114 usb_handle_t handle; 117 115 int opResult; 118 116 //setup 119 117 opResult = usb_drv_async_control_read_setup(phone, target, 120 request, sizeof(usb_device_request_setup_packet_t),121 122 if (opResult != EOK){123 return opResult; 124 } 125 opResult = usb_drv_async_wait_for(handle); 126 if (opResult != EOK){118 request, sizeof(usb_device_request_setup_packet_t), 119 &handle); 120 if(opResult!=EOK){ 121 return opResult; 122 } 123 opResult = usb_drv_async_wait_for(handle); 124 if(opResult!=EOK){ 127 125 return opResult; 128 126 } 129 127 //read 130 128 opResult = usb_drv_async_control_read_data(phone, target, 131 rcvd_buffer,rcvd_size, actual_size,132 133 if (opResult != EOK){134 return opResult; 135 } 136 opResult = usb_drv_async_wait_for(handle); 137 if (opResult != EOK){129 rcvd_buffer, rcvd_size, actual_size, 130 &handle); 131 if(opResult!=EOK){ 132 return opResult; 133 } 134 opResult = usb_drv_async_wait_for(handle); 135 if(opResult!=EOK){ 138 136 return opResult; 139 137 } 140 138 //finalize 141 139 opResult = usb_drv_async_control_read_status(phone, target, 142 143 if (opResult != EOK){144 return opResult; 145 } 146 opResult = usb_drv_async_wait_for(handle); 147 if (opResult != EOK){140 &handle); 141 if(opResult!=EOK){ 142 return opResult; 143 } 144 opResult = usb_drv_async_wait_for(handle); 145 if(opResult!=EOK){ 148 146 return opResult; 149 147 } … … 151 149 } 152 150 151 153 152 int usb_drv_sync_control_write( 154 155 156 157 ){153 int phone, usb_target_t target, 154 usb_device_request_setup_packet_t * request, 155 void * sent_buffer, size_t sent_size 156 ){ 158 157 usb_handle_t handle; 159 158 int opResult; 160 159 //setup 161 160 opResult = usb_drv_async_control_write_setup(phone, target, 162 request, sizeof(usb_device_request_setup_packet_t),163 164 if (opResult != EOK){165 return opResult; 166 } 167 opResult = usb_drv_async_wait_for(handle); 168 if (opResult != EOK){161 request, sizeof(usb_device_request_setup_packet_t), 162 &handle); 163 if(opResult!=EOK){ 164 return opResult; 165 } 166 opResult = usb_drv_async_wait_for(handle); 167 if(opResult!=EOK){ 169 168 return opResult; 170 169 } 171 170 //write 172 171 opResult = usb_drv_async_control_write_data(phone, target, 173 174 175 if (opResult != EOK){176 return opResult; 177 } 178 opResult = usb_drv_async_wait_for(handle); 179 if (opResult != EOK){172 sent_buffer, sent_size, 173 &handle); 174 if(opResult!=EOK){ 175 return opResult; 176 } 177 opResult = usb_drv_async_wait_for(handle); 178 if(opResult!=EOK){ 180 179 return opResult; 181 180 } 182 181 //finalize 183 182 opResult = usb_drv_async_control_write_status(phone, target, 184 185 if (opResult != EOK){186 return opResult; 187 } 188 opResult = usb_drv_async_wait_for(handle); 189 if (opResult != EOK){183 &handle); 184 if(opResult!=EOK){ 185 return opResult; 186 } 187 opResult = usb_drv_async_wait_for(handle); 188 if(opResult!=EOK){ 190 189 return opResult; 191 190 } … … 195 194 //list implementation 196 195 197 usb_general_list_t * usb_lst_create(void) 196 usb_general_list_t * usb_lst_create(void){ 198 197 usb_general_list_t* result = usb_new(usb_general_list_t); 199 198 usb_lst_init(result); … … 201 200 } 202 201 203 void usb_lst_init(usb_general_list_t * lst) 202 void usb_lst_init(usb_general_list_t * lst){ 204 203 lst->prev = lst; 205 204 lst->next = lst; … … 207 206 } 208 207 209 void usb_lst_prepend(usb_general_list_t* item, void* data) 208 void usb_lst_prepend(usb_general_list_t* item, void* data){ 210 209 usb_general_list_t* appended = usb_new(usb_general_list_t); 211 appended->data =data;212 appended->next =item;213 appended->prev =item->prev;210 appended->data=data; 211 appended->next=item; 212 appended->prev=item->prev; 214 213 item->prev->next = appended; 215 214 item->prev = appended; 216 215 } 217 216 218 void usb_lst_append(usb_general_list_t* item, void* data) 219 usb_general_list_t* appended = 220 appended->data =data;221 appended->next =item->next;222 appended->prev =item;217 void usb_lst_append(usb_general_list_t* item, void* data){ 218 usb_general_list_t* appended =usb_new(usb_general_list_t); 219 appended->data=data; 220 appended->next=item->next; 221 appended->prev=item; 223 222 item->next->prev = appended; 224 223 item->next = appended; 225 224 } 226 225 227 void usb_lst_remove(usb_general_list_t* item) { 226 227 void usb_lst_remove(usb_general_list_t* item){ 228 228 item->next->prev = item->prev; 229 229 item->prev->next = item->next; 230 230 } 231 231 232 static void usb_hub_test_port_status(void) { 233 printf("[usb_hub] -------------port status test---------\n"); 234 usb_port_status_t status = 0; 235 236 //printf("original status %d (should be 0)\n",(uint32_t)status); 237 usb_port_set_bit(&status, 1, 1); 238 //printf("%d =?= 2\n",(uint32_t)status); 239 if (status != 2) { 240 printf("[usb_port_status] test failed: wrong set of bit 1\n"); 241 return; 242 } 243 usb_port_set_bit(&status, 3, 1); 244 if (status != 10) { 245 printf("[usb_port_status] test failed: wrong set of bit 3\n"); 246 return; 247 } 248 249 usb_port_set_bit(&status, 15, 1); 250 if (status != 10 + (1 << 15)) { 251 printf("[usb_port_status] test failed: wrong set of bit 15\n"); 252 return; 253 } 254 usb_port_set_bit(&status, 1, 0); 255 if (status != 8 + (1 << 15)) { 256 printf("[usb_port_status] test failed: wrong unset of bit 1\n"); 257 return; 258 } 259 int i; 260 for (i = 0; i < 32; ++i) { 261 if (i == 3 || i == 15) { 262 if (!usb_port_get_bit(&status, i)) { 263 printf("[usb_port_status] test failed: wrong bit at %d\n", i); 264 } 265 } else { 266 if (usb_port_get_bit(&status, i)) { 267 printf("[usb_port_status] test failed: wrong bit at %d\n", i); 268 } 269 } 270 } 271 272 printf("test ok\n"); 273 274 275 //printf("%d =?= 10\n",(uint32_t)status); 276 277 //printf("this should be 0: %d \n",usb_port_get_bit(&status,0)); 278 //printf("this should be 1: %d \n",usb_port_get_bit(&status,1)); 279 //printf("this should be 0: %d \n",usb_port_get_bit(&status,2)); 280 //printf("this should be 1: %d \n",usb_port_get_bit(&status,3)); 281 //printf("this should be 0: %d \n",usb_port_get_bit(&status,4)); 282 283 284 285 286 } 232 287 233 288 234 //********************************************* 289 235 // 290 // hub driver code , initialization236 // hub driver code 291 237 // 292 238 //********************************************* 293 239 294 usb_hub_info_t * usb_create_hub_info(device_t * device , int hc) {240 usb_hub_info_t * usb_create_hub_info(device_t * device) { 295 241 usb_hub_info_t* result = usb_new(usb_hub_info_t); 296 242 //result->device = device; 297 243 result->port_count = -1; 298 244 299 300 printf("[usb_hub] phone to hc = %d\n", hc); 245 //get hc connection 246 int hc = usb_drv_hc_connect(NULL, 0); 247 printf("[usb_hub] phone to hc = %d\n",hc); 301 248 if (hc < 0) { 302 249 return result; 303 250 } 304 251 //get some hub info 305 /// \TODO get correct params 306 usb_address_t addr = usb_drv_get_my_address(hc, device); 307 addr = 7; 308 printf("[usb_hub] addres of newly created hub = %d\n", addr); 252 253 usb_address_t addr = usb_drv_get_my_address(hc,device); 254 printf("[usb_hub] addres of newly created hub = %d\n",addr); 309 255 /*if(addr<0){ 310 256 //return result; 311 257 312 258 }*/ 313 314 259 result->device = usb_new(usb_hcd_attached_device_info_t); 315 result->device->address = addr; 316 317 // get hub descriptor 318 usb_target_t target; 319 target.address = addr; 320 target.endpoint = 0; 321 usb_device_request_setup_packet_t request; 322 usb_hub_get_descriptor_request(&request); 323 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE); 324 usb_hub_descriptor_t * descriptor; 325 size_t received_size; 326 int opResult; 327 328 opResult = usb_drv_sync_control_read( 329 hc, target, &request, serialized_descriptor, 330 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size); 331 if (opResult != EOK) { 332 printf("[usb_hub] failed when receiving hub descriptor \n"); 333 } 334 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor); 335 result->port_count = descriptor->ports_count; 336 free(serialized_descriptor); 337 free(descriptor); 338 339 //finish 340 260 result->device->address=addr; 261 //hub configuration? 341 262 printf("[usb_hub] hub info created\n"); 342 263 264 265 343 266 return result; 344 267 } … … 360 283 361 284 //create the hub structure 362 //get hc connection 363 /// \TODO correct params 364 int hc = usb_drv_hc_connect(NULL, 0); 365 366 usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc); 367 int port; 368 int opResult; 369 usb_device_request_setup_packet_t request; 370 usb_target_t target; 371 target.address = hub_info->device->address; 372 target.endpoint = 0; 373 for (port = 0; port < hub_info->port_count; ++port) { 374 usb_hub_set_power_port_request(&request, port); 375 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0); 376 if (opResult != EOK) { 377 printf("[usb_hub]something went wrong when setting hub`s %dth port\n", port); 378 } 379 } 380 //ports powered, hub seems to be enabled 381 382 ipc_hangup(hc); 383 384 //add the hub to list 285 usb_hub_info_t * hub_info = usb_create_hub_info(dev); 385 286 usb_lst_append(&usb_hub_list, hub_info); 386 287 printf("[usb_hub] hub info added to list\n"); 387 288 //(void)hub_info; 388 289 check_hub_changes(); 389 390 /// \TODO start the check loop, if not already started...391 392 //this is just a test for port status bitmap type393 usb_hub_test_port_status();394 395 290 printf("[usb_hub] hub dev added\n"); 291 //test port status type... 396 292 397 293 return EOK; … … 399 295 } 400 296 401 //********************************************* 402 // 403 // hub driver code, main loop 404 // 405 //********************************************* 406 407 /** 408 * reset the port with new device and reserve the default address 409 * @param hc 410 * @param port 411 * @param target 412 */ 413 414 static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) { 415 usb_device_request_setup_packet_t request; 416 int opResult; 417 printf("[usb_hub] some connection changed\n"); 418 opResult = usb_drv_reserve_default_address(hc); 419 if (opResult != EOK) { 420 printf("[usb_hub] cannot assign default address, it is probably used\n"); 421 } 422 //reset port 423 usb_hub_set_reset_port_request(&request, port); 424 opResult = usb_drv_sync_control_write( 425 hc, target, 426 &request, 427 NULL, 0 428 ); 429 if (opResult != EOK) { 430 //continue; 431 printf("[usb_hub] something went wrong when reseting a port\n"); 432 } 433 } 434 435 /** 436 * finalize adding new device after port reset 437 * @param hc 438 * @param port 439 * @param target 440 */ 441 static void usb_hub_finalize_add_device( 442 int hc, uint16_t port, usb_target_t target) { 443 444 usb_device_request_setup_packet_t request; 445 int opResult; 446 printf("[usb_hub] finalizing add device\n"); 447 usb_address_t new_device_address = 448 usb_drv_request_address(hc); 449 usb_hub_set_set_address_request 450 (&request, new_device_address); 451 opResult = usb_drv_sync_control_write( 452 hc, target, 453 &request, 454 NULL, 0 455 ); 456 if (opResult != EOK) { 457 printf("[usb_hub] could not set address for new device\n"); 458 } 459 usb_drv_release_default_address(hc); 460 461 462 /// \TODO driver work 463 } 464 465 /** 466 * unregister device address in hc, close the port 467 * @param hc 468 * @param port 469 * @param target 470 */ 471 static void usb_hub_removed_device(int hc, uint16_t port, usb_target_t target) { 472 usb_device_request_setup_packet_t request; 473 int opResult; 474 //disable port 475 usb_hub_set_disable_port_request(&request, port); 476 opResult = usb_drv_sync_control_write( 477 hc, target, 478 &request, 479 NULL, 0 480 ); 481 if (opResult != EOK) { 482 //continue; 483 printf("[usb_hub] something went wrong when disabling a port\n"); 484 } 485 //remove device 486 //close address 487 // 488 489 ///\TODO this code is not complete 490 } 491 492 /** 493 * process interrupts on given hub port 494 * @param hc 495 * @param port 496 * @param target 497 */ 498 static void usb_hub_process_interrupt(int hc, uint16_t port, usb_target_t target) { 499 printf("[usb_hub] interrupt at port %d\n", port); 500 //determine type of change 501 usb_port_status_t status; 502 size_t rcvd_size; 503 usb_device_request_setup_packet_t request; 504 int opResult; 505 usb_hub_set_port_status_request(&request, port); 506 507 opResult = usb_drv_sync_control_read( 508 hc, target, 509 &request, 510 &status, 4, &rcvd_size 511 ); 512 if (opResult != EOK) { 513 printf("[usb_hub] ERROR: could not get port status\n"); 514 return; 515 } 516 if (rcvd_size != sizeof (usb_port_status_t)) { 517 printf("[usb_hub] ERROR: received status has incorrect size\n"); 518 return; 519 } 520 //something connected/disconnected 521 if (usb_port_connect_change(&status)) { 522 if (usb_port_dev_connected(&status)) { 523 printf("[usb_hub] some connection changed\n"); 524 usb_hub_init_add_device(hc, port, target); 525 } else { 526 usb_hub_removed_device(hc, port, target); 527 } 528 } 529 //port reset 530 if (usb_port_reset_completed(&status)) { 531 printf("[usb_hub] finalizing add device\n"); 532 if (usb_port_enabled(&status)) { 533 usb_hub_finalize_add_device(hc, port, target); 534 } else { 535 printf("[usb_hub] ERROR: port reset, but port still not enabled\n"); 536 } 537 } 538 539 usb_port_set_connect_change(&status, false); 540 usb_port_set_reset(&status, false); 541 usb_port_set_reset_completed(&status, false); 542 usb_port_set_dev_connected(&status, false); 543 if (status) { 544 printf("[usb_hub]there was some unsupported change on port\n"); 545 } 546 /// \TODO handle other changes 547 /// \TODO debug log for various situations 548 549 550 551 /* 552 //configure device 553 usb_drv_reserve_default_address(hc); 554 555 usb_address_t new_device_address = usb_drv_request_address(hc); 556 557 558 usb_drv_release_default_address(hc); 559 * */ 560 } 297 298 561 299 562 300 /** Check changes on all known hubs. … … 568 306 usb_general_list_t * lst_item; 569 307 for (lst_item = usb_hub_list.next; 570 571 308 lst_item != &usb_hub_list; 309 lst_item = lst_item->next) { 572 310 printf("[usb_hub] checking hub changes\n"); 573 311 /* 574 312 * Check status change pipe of this hub. 575 313 */ 576 314 577 315 usb_target_t target = { 578 316 .address = 5, … … 589 327 /// \FIXME this is incorrect code: here 590 328 /// must be used particular device instead of NULL 591 //which one?592 329 int hc = usb_drv_hc_connect(NULL, 0); 593 330 if (hc < 0) { … … 611 348 usb_drv_async_wait_for(handle); 612 349 613 if (opResult != EOK){350 if(opResult!=EOK){ 614 351 printf("[usb_hub] something went wrong while getting status of hub\n"); 615 352 continue; 616 353 } 617 354 unsigned int port; 618 for (port = 0; port < port_count; ++port) { 619 bool interrupt = (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2; 620 if (interrupt) { 621 usb_hub_process_interrupt(hc, port, target); 355 for(port=0;port<port_count;++port){ 356 bool interrupt = (((uint8_t*)change_bitmap)[port/8]>>(port%8))%2; 357 if(interrupt){ 358 printf("[usb_hub] interrupt at port %d\n",port); 359 //determine type of change 360 usb_port_status_t status; 361 size_t rcvd_size; 362 usb_device_request_setup_packet_t request; 363 usb_hub_set_port_status_request(&request,port); 364 365 opResult = usb_drv_sync_control_read( 366 hc, target, 367 &request, 368 &status, 4, &rcvd_size 369 ); 370 if(opResult!=EOK){ 371 continue; 372 } 373 if(rcvd_size!=sizeof(usb_port_status_t)){ 374 continue; 375 } 376 377 if(usb_port_connect_change(&status)){ 378 printf("[usb_hub] some connectionchanged\n"); 379 usb_drv_reserve_default_address(hc); 380 //enable port 381 usb_hub_set_enable_port_request(&request,port); 382 opResult = usb_drv_sync_control_write( 383 hc, target, 384 &request, 385 NULL, 0 386 ); 387 if(opResult!=EOK){ 388 continue; 389 } 390 //set address 391 usb_address_t new_device_address = 392 usb_drv_request_address(hc); 393 usb_hub_set_set_address_request 394 (&request,new_device_address); 395 opResult = usb_drv_sync_control_write( 396 hc, target, 397 &request, 398 NULL, 0 399 ); 400 //some other work with drivers 401 /// \TODO do the work with drivers 402 403 404 usb_drv_release_default_address(hc); 405 }else{ 406 printf("[usb_hub] no supported event occured\n"); 407 } 408 /// \TODO handle other changes 409 /// \TODO debug log for various situations 410 411 412 413 /* 414 //configure device 415 usb_drv_reserve_default_address(hc); 416 417 usb_address_t new_device_address = usb_drv_request_address(hc); 418 419 420 usb_drv_release_default_address(hc); 421 * */ 622 422 } 623 423 }
Note:
See TracChangeset
for help on using the changeset viewer.