Changes in uspace/drv/ohci/root_hub.c [f35b294:361fcec] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/ohci/root_hub.c
rf35b294 r361fcec 149 149 150 150 151 static void usb_create_serialized_hub_descriptor(rh_t *instance, 152 uint8_t ** out_result, 153 size_t * out_size); 154 155 static void rh_init_descriptors(rh_t *instance); 151 static int create_serialized_hub_descriptor(rh_t *instance); 152 153 static int rh_init_descriptors(rh_t *instance); 156 154 157 155 static int process_get_port_status_request(rh_t *instance, uint16_t port, … … 164 162 usb_transfer_batch_t * request); 165 163 166 static void create_interrupt_mask(rh_t *instance, void ** buffer, 167 size_t * buffer_size); 164 static void create_interrupt_mask_in_instance(rh_t *instance); 168 165 169 166 static int process_get_descriptor_request(rh_t *instance, … … 198 195 static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request); 199 196 200 197 static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request); 198 199 static bool is_zeros(void * buffer, size_t size); 201 200 202 201 … … 205 204 * @return Error code. 206 205 */ 207 int rh_init(rh_t *instance, ddf_dev_t *dev,ohci_regs_t *regs) {206 int rh_init(rh_t *instance, ohci_regs_t *regs) { 208 207 assert(instance); 209 //instance->address = -1;210 208 instance->registers = regs; 211 instance->device = dev; 212 instance->port_count = instance->registers->rh_desc_a & 0xff; 213 rh_init_descriptors(instance); 209 instance->port_count = 210 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK; 211 int opResult = rh_init_descriptors(instance); 212 if(opResult != EOK){ 213 return opResult; 214 } 214 215 // set port power mode to no-power-switching 215 instance->registers->rh_desc_a = 216 instance->registers->rh_desc_a | (1<<9); 216 instance->registers->rh_desc_a |= RHDA_NPS_FLAG; 217 instance->unfinished_interrupt_transfer = NULL; 218 instance->interrupt_mask_size = (instance->port_count + 8)/8; 219 instance->interrupt_buffer = malloc(instance->interrupt_mask_size); 220 if(!instance->interrupt_buffer) 221 return ENOMEM; 222 217 223 218 224 usb_log_info("OHCI root hub with %d ports.\n", instance->port_count); 219 220 //start generic usb hub driver221 222 /* TODO: implement */223 225 return EOK; 224 226 } … … 236 238 assert(request); 237 239 int opResult; 238 if (request-> transfer_type == USB_TRANSFER_CONTROL) {240 if (request->ep->transfer_type == USB_TRANSFER_CONTROL) { 239 241 usb_log_info("Root hub got CONTROL packet\n"); 240 242 opResult = process_ctrl_request(instance, request); 241 } else if (request->transfer_type == USB_TRANSFER_INTERRUPT) { 243 usb_transfer_batch_finish_error(request, opResult); 244 } else if (request->ep->transfer_type == USB_TRANSFER_INTERRUPT) { 242 245 usb_log_info("Root hub got INTERRUPT packet\n"); 243 void * buffer; 244 create_interrupt_mask(instance, &buffer, 245 &(request->transfered_size)); 246 memcpy(request->transport_buffer, buffer, 247 request->transfered_size); 246 create_interrupt_mask_in_instance(instance); 247 if(is_zeros(instance->interrupt_buffer, 248 instance->interrupt_mask_size)){ 249 usb_log_debug("no changes..\n"); 250 instance->unfinished_interrupt_transfer = request; 251 //will be finished later 252 }else{ 253 usb_log_debug("processing changes..\n"); 254 process_interrupt_mask_in_instance(instance, request); 255 } 248 256 opResult = EOK; 249 257 } else { 250 258 opResult = EINVAL; 251 } 252 usb_transfer_batch_finish_error(request, opResult); 253 return EOK; 254 } 255 256 /*----------------------------------------------------------------------------*/ 257 258 259 usb_transfer_batch_finish_error(request, opResult); 260 } 261 return EOK; 262 } 263 264 /*----------------------------------------------------------------------------*/ 265 266 /** 267 * process interrupt on a hub 268 * 269 * If there is no pending interrupt transfer, nothing happens. 270 * @param instance 271 */ 259 272 void rh_interrupt(rh_t *instance) { 260 usb_log_info("Whoa whoa wait, I`m not supposed to receive any " 261 "interrupts, am I?\n"); 262 /* TODO: implement? */ 273 if(!instance->unfinished_interrupt_transfer){ 274 return; 275 } 276 usb_log_debug("finalizing interrupt transfer\n"); 277 create_interrupt_mask_in_instance(instance); 278 process_interrupt_mask_in_instance(instance, 279 instance->unfinished_interrupt_transfer); 263 280 } 264 281 /*----------------------------------------------------------------------------*/ … … 271 288 * 272 289 * @param instance root hub instance 273 * @param@out out_result pointer to resultant serialized descriptor 274 * @param@out out_size size of serialized descriptor 275 */ 276 static void usb_create_serialized_hub_descriptor(rh_t *instance, 277 uint8_t ** out_result, 278 size_t * out_size) { 279 //base size 280 size_t size = 7; 281 //variable size according to port count 282 size_t var_size = instance->port_count / 8 + 283 ((instance->port_count % 8 > 0) ? 1 : 0); 284 size += 2 * var_size; 290 * @return error code 291 */ 292 static int create_serialized_hub_descriptor(rh_t *instance) { 293 size_t size = 7 + 294 ((instance->port_count +7 )/ 8) * 2; 295 size_t var_size = (instance->port_count +7 )/ 8; 285 296 uint8_t * result = (uint8_t*) malloc(size); 297 if(!result) return ENOMEM; 298 286 299 bzero(result, size); 287 300 //size … … 312 325 result[7 + var_size + i] = 255; 313 326 } 314 (*out_result) = result; 315 (*out_size) = size; 327 instance->hub_descriptor = result; 328 instance->descriptor_size = size; 329 return EOK; 316 330 } 317 331 /*----------------------------------------------------------------------------*/ … … 322 336 * be initialized only once per hub. 323 337 * @instance root hub instance 324 */ 325 static void rh_init_descriptors(rh_t *instance) { 338 * @return error code 339 */ 340 static int rh_init_descriptors(rh_t *instance) { 326 341 memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor, 327 342 sizeof (ohci_rh_device_descriptor) … … 330 345 memcpy(&descriptor, &ohci_rh_conf_descriptor, 331 346 sizeof (ohci_rh_conf_descriptor)); 332 uint8_t * hub_descriptor; 333 size_t hub_desc_size;334 usb_create_serialized_hub_descriptor(instance, &hub_descriptor,335 &hub_desc_size);336 347 348 int opResult = create_serialized_hub_descriptor(instance); 349 if(opResult != EOK){ 350 return opResult; 351 } 337 352 descriptor.total_length = 338 353 sizeof (usb_standard_configuration_descriptor_t) + 339 354 sizeof (usb_standard_endpoint_descriptor_t) + 340 355 sizeof (usb_standard_interface_descriptor_t) + 341 hub_desc_size;356 instance->descriptor_size; 342 357 343 358 uint8_t * full_config_descriptor = 344 359 (uint8_t*) malloc(descriptor.total_length); 360 if(!full_config_descriptor){ 361 return ENOMEM; 362 } 345 363 memcpy(full_config_descriptor, &descriptor, sizeof (descriptor)); 346 364 memcpy(full_config_descriptor + sizeof (descriptor), … … 352 370 sizeof (ohci_rh_iface_descriptor) + 353 371 sizeof (ohci_rh_ep_descriptor), 354 hub_descriptor, hub_desc_size);355 372 instance->hub_descriptor, instance->descriptor_size); 373 356 374 instance->descriptors.configuration = full_config_descriptor; 357 375 instance->descriptors.configuration_size = descriptor.total_length; 376 return EOK; 358 377 } 359 378 /*----------------------------------------------------------------------------*/ … … 373 392 if (port < 1 || port > instance->port_count) 374 393 return EINVAL; 375 uint32_t * uint32_buffer = (uint32_t*) request-> transport_buffer;394 uint32_t * uint32_buffer = (uint32_t*) request->data_buffer; 376 395 request->transfered_size = 4; 377 396 uint32_buffer[0] = instance->registers->rh_port_status[port - 1]; … … 399 418 static int process_get_hub_status_request(rh_t *instance, 400 419 usb_transfer_batch_t * request) { 401 uint32_t * uint32_buffer = (uint32_t*) request-> transport_buffer;420 uint32_t * uint32_buffer = (uint32_t*) request->data_buffer; 402 421 request->transfered_size = 4; 403 422 //bits, 0,1,16,17 … … 446 465 * bit i indicates change on i`th port (i>0). For more info see 447 466 * Hub and Port status bitmap specification in USB specification 448 * (chapter 11.13.4) 449 * @param instance root hub instance 450 * @param@out buffer pointer to created interrupt mas 451 * @param@out buffer_size size of created interrupt mask 452 */ 453 static void create_interrupt_mask(rh_t *instance, void ** buffer, 454 size_t * buffer_size) { 455 int bit_count = instance->port_count + 1; 456 (*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1); 457 458 (*buffer) = malloc(*buffer_size); 459 uint8_t * bitmap = (uint8_t*) (*buffer); 467 * (chapter 11.13.4). 468 * Uses instance`s interrupt buffer to store the interrupt information. 469 * @param instance root hub instance 470 */ 471 static void create_interrupt_mask_in_instance(rh_t * instance) { 472 uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer); 460 473 uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16)) 461 474 | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16)); 462 bzero(bitmap, (*buffer_size));475 bzero(bitmap, instance->interrupt_mask_size); 463 476 if (instance->registers->rh_status & mask) { 464 477 bitmap[0] = 1; … … 491 504 const uint16_t setup_request_value = setup_request->value_high; 492 505 //(setup_request->value_low << 8); 493 bool del = false;494 506 switch (setup_request_value) { 495 507 case USB_DESCTYPE_HUB: 496 508 { 497 uint8_t * descriptor; 498 usb_create_serialized_hub_descriptor( 499 instance, &descriptor, &size); 500 result_descriptor = descriptor; 501 if (result_descriptor) del = true; 509 usb_log_debug("USB_DESCTYPE_HUB\n"); 510 result_descriptor = instance->hub_descriptor; 511 size = instance->descriptor_size; 502 512 break; 503 513 } … … 549 559 } 550 560 request->transfered_size = size; 551 memcpy(request->transport_buffer, result_descriptor, size); 552 if (del) 553 free(result_descriptor); 561 memcpy(request->data_buffer, result_descriptor, size); 554 562 return EOK; 555 563 } … … 570 578 if (request->buffer_size != 1) 571 579 return EINVAL; 572 request-> transport_buffer[0] = 1;580 request->data_buffer[0] = 1; 573 581 request->transfered_size = 1; 574 582 return EOK; … … 865 873 return opResult; 866 874 } 875 /*----------------------------------------------------------------------------*/ 876 877 /** 878 * process hanging interrupt request 879 * 880 * If an interrupt transfer has been received and there was no change, 881 * the driver stores the transfer information and waits for change to occcur. 882 * This routine is called when that happens and it finalizes the interrupt 883 * transfer. 884 * 885 * @param instance hub instance 886 * @param request batch request to be processed 887 * 888 * @return 889 */ 890 static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request){ 891 memcpy(request->data_buffer, instance->interrupt_buffer, 892 instance->interrupt_mask_size); 893 request->transfered_size = instance->interrupt_mask_size; 894 instance->unfinished_interrupt_transfer = NULL; 895 usb_transfer_batch_finish_error(request, EOK); 896 return EOK; 897 } 898 899 /*----------------------------------------------------------------------------*/ 900 901 /** 902 * return whether the buffer is full of zeros 903 * 904 * Convenience function. 905 * @param buffer 906 * @param size 907 * @return 908 */ 909 static bool is_zeros(void * buffer, size_t size){ 910 if(!buffer) return true; 911 if(!size) return true; 912 size_t i; 913 for(i=0;i<size;++i){ 914 if(((char*)buffer)[i]) 915 return false; 916 } 917 return true; 918 } 867 919 868 920 /**
Note:
See TracChangeset
for help on using the changeset viewer.