Changes in uspace/drv/uhci-hcd/batch.c [a963a68:1387692] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/uhci-hcd/batch.c
ra963a68 r1387692 42 42 #include "uhci_hc.h" 43 43 #include "utils/malloc32.h" 44 #include "uhci_struct/transfer_descriptor.h" 44 45 45 46 #define DEFAULT_ERROR_COUNT 3 46 47 47 static void batch_control(batch_t *instance, 48 typedef struct uhci_batch { 49 qh_t *qh; 50 td_t *tds; 51 size_t packets; 52 usb_device_keeper_t *manager; 53 } uhci_batch_t; 54 55 static void batch_control(usb_transfer_batch_t *instance, 48 56 usb_packet_id data_stage, usb_packet_id status_stage); 49 static void batch_data(batch_t *instance, usb_packet_id pid); 50 static void batch_call_in(batch_t *instance); 51 static void batch_call_out(batch_t *instance); 52 static void batch_call_in_and_dispose(batch_t *instance); 53 static void batch_call_out_and_dispose(batch_t *instance); 57 static void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid); 58 static void batch_call_in_and_dispose(usb_transfer_batch_t *instance); 59 static void batch_call_out_and_dispose(usb_transfer_batch_t *instance); 54 60 55 61 … … 76 82 * transaction and callback. 77 83 */ 78 batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,84 usb_transfer_batch_t * batch_get(ddf_fun_t *fun, usb_target_t target, 79 85 usb_transfer_type_t transfer_type, size_t max_packet_size, 80 usb_speed_t speed, char *buffer, size_t size,86 usb_speed_t speed, char *buffer, size_t buffer_size, 81 87 char* setup_buffer, size_t setup_size, 82 88 usbhc_iface_transfer_in_callback_t func_in, 83 89 usbhc_iface_transfer_out_callback_t func_out, void *arg, 84 device_keeper_t *manager90 usb_device_keeper_t *manager 85 91 ) 86 92 { … … 97 103 } else (void)0 98 104 99 batch_t *instance = malloc(sizeof(batch_t));105 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t)); 100 106 CHECK_NULL_DISPOSE_RETURN(instance, 101 107 "Failed to allocate batch instance.\n"); 102 bzero(instance, sizeof(batch_t)); 103 104 instance->qh = malloc32(sizeof(qh_t)); 105 CHECK_NULL_DISPOSE_RETURN(instance->qh, 108 usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size, 109 buffer, NULL, buffer_size, NULL, setup_size, func_in, 110 func_out, arg, fun, NULL); 111 112 113 uhci_batch_t *data = malloc(sizeof(uhci_batch_t)); 114 CHECK_NULL_DISPOSE_RETURN(instance, 115 "Failed to allocate batch instance.\n"); 116 bzero(data, sizeof(uhci_batch_t)); 117 data->manager = manager; 118 instance->private_data = data; 119 120 data->packets = (buffer_size + max_packet_size - 1) / max_packet_size; 121 if (transfer_type == USB_TRANSFER_CONTROL) { 122 data->packets += 2; 123 } 124 125 data->tds = malloc32(sizeof(td_t) * data->packets); 126 CHECK_NULL_DISPOSE_RETURN( 127 data->tds, "Failed to allocate transfer descriptors.\n"); 128 bzero(data->tds, sizeof(td_t) * data->packets); 129 130 data->qh = malloc32(sizeof(qh_t)); 131 CHECK_NULL_DISPOSE_RETURN(data->qh, 106 132 "Failed to allocate batch queue head.\n"); 107 qh_init(instance->qh); 108 109 instance->packets = (size + max_packet_size - 1) / max_packet_size; 110 if (transfer_type == USB_TRANSFER_CONTROL) { 111 instance->packets += 2; 112 } 113 114 instance->tds = malloc32(sizeof(td_t) * instance->packets); 115 CHECK_NULL_DISPOSE_RETURN( 116 instance->tds, "Failed to allocate transfer descriptors.\n"); 117 bzero(instance->tds, sizeof(td_t) * instance->packets); 118 119 if (size > 0) { 120 instance->transport_buffer = malloc32(size); 133 qh_init(data->qh); 134 qh_set_element_td(data->qh, addr_to_phys(data->tds)); 135 136 if (buffer_size > 0) { 137 instance->transport_buffer = malloc32(buffer_size); 121 138 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer, 122 139 "Failed to allocate device accessible buffer.\n"); … … 130 147 } 131 148 132 133 link_initialize(&instance->link);134 135 instance->max_packet_size = max_packet_size;136 instance->target = target;137 instance->transfer_type = transfer_type;138 instance->buffer = buffer;139 instance->buffer_size = size;140 instance->setup_size = setup_size;141 instance->fun = fun;142 instance->arg = arg;143 instance->speed = speed;144 instance->manager = manager;145 instance->callback_out = func_out;146 instance->callback_in = func_in;147 148 qh_set_element_td(instance->qh, addr_to_phys(instance->tds));149 150 149 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n", 151 150 instance, target.address, target.endpoint); … … 153 152 } 154 153 /*----------------------------------------------------------------------------*/ 155 /** Mark batch as failed and continue with next step.156 *157 * @param[in] instance Batch structure to use.158 *159 */160 void batch_abort(batch_t *instance)161 {162 assert(instance);163 instance->error = EIO;164 instance->next_step(instance);165 }166 /*----------------------------------------------------------------------------*/167 154 /** Check batch TDs for activity. 168 155 * … … 174 161 * is reached. 175 162 */ 176 bool batch_is_complete(batch_t *instance) 177 { 178 assert(instance); 163 bool batch_is_complete(usb_transfer_batch_t *instance) 164 { 165 assert(instance); 166 uhci_batch_t *data = instance->private_data; 167 assert(data); 168 179 169 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n", 180 instance, instance->packets);170 instance, data->packets); 181 171 instance->transfered_size = 0; 182 172 size_t i = 0; 183 for (;i < instance->packets; ++i) {184 if (td_is_active(& instance->tds[i])) {173 for (;i < data->packets; ++i) { 174 if (td_is_active(&data->tds[i])) { 185 175 return false; 186 176 } 187 177 188 instance->error = td_status(& instance->tds[i]);178 instance->error = td_status(&data->tds[i]); 189 179 if (instance->error != EOK) { 190 180 usb_log_debug("Batch(%p) found error TD(%d):%x.\n", 191 instance, i, instance->tds[i].status); 192 td_print_status(&instance->tds[i]); 193 194 device_keeper_set_toggle(instance->manager, 195 instance->target, td_toggle(&instance->tds[i])); 181 instance, i, data->tds[i].status); 182 td_print_status(&data->tds[i]); 183 184 usb_device_keeper_set_toggle(data->manager, 185 instance->target, instance->direction, 186 td_toggle(&data->tds[i])); 196 187 if (i > 0) 197 188 goto substract_ret; … … 199 190 } 200 191 201 instance->transfered_size += td_act_size(& instance->tds[i]);202 if (td_is_short(& instance->tds[i]))192 instance->transfered_size += td_act_size(&data->tds[i]); 193 if (td_is_short(&data->tds[i])) 203 194 goto substract_ret; 204 195 } … … 214 205 * Uses genercir control function with pids OUT and IN. 215 206 */ 216 void batch_control_write( batch_t *instance)207 void batch_control_write(usb_transfer_batch_t *instance) 217 208 { 218 209 assert(instance); … … 231 222 * Uses generic control with pids IN and OUT. 232 223 */ 233 void batch_control_read( batch_t *instance)224 void batch_control_read(usb_transfer_batch_t *instance) 234 225 { 235 226 assert(instance); … … 245 236 * Data transaction with PID_IN. 246 237 */ 247 void batch_interrupt_in(batch_t *instance) 248 { 249 assert(instance); 238 void batch_interrupt_in(usb_transfer_batch_t *instance) 239 { 240 assert(instance); 241 instance->direction = USB_DIRECTION_IN; 250 242 batch_data(instance, USB_PID_IN); 251 243 instance->next_step = batch_call_in_and_dispose; … … 259 251 * Data transaction with PID_OUT. 260 252 */ 261 void batch_interrupt_out(batch_t *instance) 262 { 263 assert(instance); 253 void batch_interrupt_out(usb_transfer_batch_t *instance) 254 { 255 assert(instance); 256 instance->direction = USB_DIRECTION_OUT; 264 257 /* We are data out, we are supposed to provide data */ 265 258 memcpy(instance->transport_buffer, instance->buffer, … … 276 269 * Data transaction with PID_IN. 277 270 */ 278 void batch_bulk_in( batch_t *instance)271 void batch_bulk_in(usb_transfer_batch_t *instance) 279 272 { 280 273 assert(instance); 281 274 batch_data(instance, USB_PID_IN); 275 instance->direction = USB_DIRECTION_IN; 282 276 instance->next_step = batch_call_in_and_dispose; 283 277 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance); … … 290 284 * Data transaction with PID_OUT. 291 285 */ 292 void batch_bulk_out(batch_t *instance) 293 { 294 assert(instance); 286 void batch_bulk_out(usb_transfer_batch_t *instance) 287 { 288 assert(instance); 289 instance->direction = USB_DIRECTION_OUT; 295 290 /* We are data out, we are supposed to provide data */ 296 291 memcpy(instance->transport_buffer, instance->buffer, … … 309 304 * The last packet is marked with IOC flag. 310 305 */ 311 void batch_data(batch_t *instance, usb_packet_id pid) 312 { 313 assert(instance); 306 void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid) 307 { 308 assert(instance); 309 uhci_batch_t *data = instance->private_data; 310 assert(data); 311 314 312 const bool low_speed = instance->speed == USB_SPEED_LOW; 315 int toggle = 316 d evice_keeper_get_toggle(instance->manager, instance->target);313 int toggle = usb_device_keeper_get_toggle( 314 data->manager, instance->target, instance->direction); 317 315 assert(toggle == 0 || toggle == 1); 318 316 … … 320 318 size_t remain_size = instance->buffer_size; 321 319 while (remain_size > 0) { 322 char * data =320 char *trans_data = 323 321 instance->transport_buffer + instance->buffer_size 324 322 - remain_size; … … 328 326 remain_size : instance->max_packet_size; 329 327 330 td_t *next_packet = (packet + 1 < instance->packets)331 ? & instance->tds[packet + 1] : NULL;332 333 assert(packet < instance->packets);328 td_t *next_packet = (packet + 1 < data->packets) 329 ? &data->tds[packet + 1] : NULL; 330 331 assert(packet < data->packets); 334 332 assert(packet_size <= remain_size); 335 333 336 334 td_init( 337 & instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,338 toggle, false, low_speed, instance->target, pid, data,335 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size, 336 toggle, false, low_speed, instance->target, pid, trans_data, 339 337 next_packet); 340 338 … … 344 342 ++packet; 345 343 } 346 td_set_ioc(&instance->tds[packet - 1]); 347 device_keeper_set_toggle(instance->manager, instance->target, toggle); 344 td_set_ioc(&data->tds[packet - 1]); 345 usb_device_keeper_set_toggle(data->manager, instance->target, 346 instance->direction, toggle); 348 347 } 349 348 /*----------------------------------------------------------------------------*/ … … 359 358 * The last packet is marked with IOC. 360 359 */ 361 void batch_control( batch_t *instance,360 void batch_control(usb_transfer_batch_t *instance, 362 361 usb_packet_id data_stage, usb_packet_id status_stage) 363 362 { 364 363 assert(instance); 364 uhci_batch_t *data = instance->private_data; 365 assert(data); 366 assert(data->packets >= 2); 365 367 366 368 const bool low_speed = instance->speed == USB_SPEED_LOW; 367 369 int toggle = 0; 368 370 /* setup stage */ 369 td_init(instance->tds, DEFAULT_ERROR_COUNT, 370 instance->setup_size, toggle, false, low_speed, instance->target, 371 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]); 371 td_init( 372 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false, 373 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer, 374 &data->tds[1]); 372 375 373 376 /* data stage */ … … 375 378 size_t remain_size = instance->buffer_size; 376 379 while (remain_size > 0) { 377 char * data =380 char *control_data = 378 381 instance->transport_buffer + instance->buffer_size 379 382 - remain_size; … … 386 389 387 390 td_init( 388 & instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,391 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size, 389 392 toggle, false, low_speed, instance->target, data_stage, 390 data, &instance->tds[packet + 1]);393 control_data, &data->tds[packet + 1]); 391 394 392 395 ++packet; 393 assert(packet < instance->packets);396 assert(packet < data->packets); 394 397 assert(packet_size <= remain_size); 395 398 remain_size -= packet_size; … … 397 400 398 401 /* status stage */ 399 assert(packet == instance->packets - 1); 400 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT, 401 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL); 402 403 td_set_ioc(&instance->tds[packet]); 402 assert(packet == data->packets - 1); 403 404 td_init( 405 &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed, 406 instance->target, status_stage, NULL, NULL); 407 td_set_ioc(&data->tds[packet]); 408 404 409 usb_log_debug2("Control last TD status: %x.\n", 405 instance->tds[packet].status); 406 } 407 /*----------------------------------------------------------------------------*/ 408 /** Prepare data, get error status and call callback in. 409 * 410 * @param[in] instance Batch structure to use. 411 * Copies data from transport buffer, and calls callback with appropriate 412 * parameters. 413 */ 414 void batch_call_in(batch_t *instance) 415 { 416 assert(instance); 417 assert(instance->callback_in); 418 419 /* We are data in, we need data */ 420 memcpy(instance->buffer, instance->transport_buffer, 421 instance->buffer_size); 422 423 int err = instance->error; 424 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n", 425 instance, instance->transfer_type, str_error(err), err, 426 instance->transfered_size); 427 428 instance->callback_in( 429 instance->fun, err, instance->transfered_size, instance->arg); 430 } 431 /*----------------------------------------------------------------------------*/ 432 /** Get error status and call callback out. 433 * 434 * @param[in] instance Batch structure to use. 435 */ 436 void batch_call_out(batch_t *instance) 437 { 438 assert(instance); 439 assert(instance->callback_out); 440 441 int err = instance->error; 442 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n", 443 instance, instance->transfer_type, str_error(err), err); 444 instance->callback_out(instance->fun, 445 err, instance->arg); 410 data->tds[packet].status); 411 } 412 /*----------------------------------------------------------------------------*/ 413 qh_t * batch_qh(usb_transfer_batch_t *instance) 414 { 415 assert(instance); 416 uhci_batch_t *data = instance->private_data; 417 assert(data); 418 return data->qh; 446 419 } 447 420 /*----------------------------------------------------------------------------*/ … … 450 423 * @param[in] instance Batch structure to use. 451 424 */ 452 void batch_call_in_and_dispose( batch_t *instance)453 { 454 assert(instance); 455 batch_call_in(instance);425 void batch_call_in_and_dispose(usb_transfer_batch_t *instance) 426 { 427 assert(instance); 428 usb_transfer_batch_call_in(instance); 456 429 batch_dispose(instance); 457 430 } … … 461 434 * @param[in] instance Batch structure to use. 462 435 */ 463 void batch_call_out_and_dispose( batch_t *instance)464 { 465 assert(instance); 466 batch_call_out(instance);436 void batch_call_out_and_dispose(usb_transfer_batch_t *instance) 437 { 438 assert(instance); 439 usb_transfer_batch_call_out(instance); 467 440 batch_dispose(instance); 468 441 } … … 472 445 * @param[in] instance Batch structure to use. 473 446 */ 474 void batch_dispose(batch_t *instance) 475 { 476 assert(instance); 447 void batch_dispose(usb_transfer_batch_t *instance) 448 { 449 assert(instance); 450 uhci_batch_t *data = instance->private_data; 451 assert(data); 477 452 usb_log_debug("Batch(%p) disposing.\n", instance); 478 453 /* free32 is NULL safe */ 479 free32( instance->tds);480 free32( instance->qh);454 free32(data->tds); 455 free32(data->qh); 481 456 free32(instance->setup_buffer); 482 457 free32(instance->transport_buffer); 458 free(data); 483 459 free(instance); 484 460 }
Note:
See TracChangeset
for help on using the changeset viewer.