Changeset 361e61b in mainline for uspace/drv/uhci-hcd/batch.c
- Timestamp:
- 2011-03-21T14:23:15Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 55e388a1
- Parents:
- c32688d (diff), 48fe0c9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/uhci-hcd/batch.c
rc32688d r361e61b 26 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 27 */ 28 /** @addtogroup usb28 /** @addtogroup drvusbuhcihc 29 29 * @{ 30 30 */ 31 31 /** @file 32 * @brief UHCI driver 32 * @brief UHCI driver USB transaction structure 33 33 */ 34 34 #include <errno.h> … … 40 40 #include "batch.h" 41 41 #include "transfer_list.h" 42 #include "uhci .h"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 int batch_schedule(batch_t *instance); 48 typedef struct uhci_batch { 49 qh_t *qh; 50 td_t *tds; 51 size_t packets; 52 device_keeper_t *manager; 53 } uhci_batch_t; 48 54 49 55 static void batch_control(batch_t *instance, 50 56 usb_packet_id data_stage, usb_packet_id status_stage); 51 57 static void batch_data(batch_t *instance, usb_packet_id pid); 52 static void batch_call_in(batch_t *instance);53 static void batch_call_out(batch_t *instance);54 58 static void batch_call_in_and_dispose(batch_t *instance); 55 59 static void batch_call_out_and_dispose(batch_t *instance); 56 static void batch_dispose(batch_t *instance); 57 58 59 /** Allocates memory and initializes internal data structures. 60 61 62 /** Allocate memory and initialize internal data structure. 60 63 * 61 64 * @param[in] fun DDF function to pass to callback. … … 72 75 * @param[in] arg additional parameter to func_in or func_out 73 76 * @param[in] manager Pointer to toggle management structure. 74 * @return False, if there is an active TD, true otherwise. 77 * @return Valid pointer if all substructures were successfully created, 78 * NULL otherwise. 79 * 80 * Determines the number of needed packets (TDs). Prepares a transport buffer 81 * (that is accessible by the hardware). Initializes parameters needed for the 82 * transaction and callback. 75 83 */ 76 84 batch_t * batch_get(ddf_fun_t *fun, usb_target_t target, 77 85 usb_transfer_type_t transfer_type, size_t max_packet_size, 78 usb_speed_t speed, char *buffer, size_t size,86 usb_speed_t speed, char *buffer, size_t buffer_size, 79 87 char* setup_buffer, size_t setup_size, 80 88 usbhc_iface_transfer_in_callback_t func_in, … … 98 106 CHECK_NULL_DISPOSE_RETURN(instance, 99 107 "Failed to allocate batch instance.\n"); 100 bzero(instance, sizeof(batch_t)); 101 102 instance->qh = malloc32(sizeof(queue_head_t)); 103 CHECK_NULL_DISPOSE_RETURN(instance->qh, 108 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, 104 132 "Failed to allocate batch queue head.\n"); 105 queue_head_init(instance->qh); 106 107 instance->packets = (size + max_packet_size - 1) / max_packet_size; 108 if (transfer_type == USB_TRANSFER_CONTROL) { 109 instance->packets += 2; 110 } 111 112 instance->tds = malloc32(sizeof(td_t) * instance->packets); 113 CHECK_NULL_DISPOSE_RETURN( 114 instance->tds, "Failed to allocate transfer descriptors.\n"); 115 bzero(instance->tds, sizeof(td_t) * instance->packets); 116 117 // const size_t transport_size = max_packet_size * 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 146 if (func_out)147 instance->callback_out = func_out;148 if (func_in)149 instance->callback_in = func_in;150 151 queue_head_set_element_td(instance->qh, addr_to_phys(instance->tds));152 153 149 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n", 154 150 instance, target.address, target.endpoint); … … 156 152 } 157 153 /*----------------------------------------------------------------------------*/ 158 /** Check sbatch TDs for activity.154 /** Check batch TDs for activity. 159 155 * 160 156 * @param[in] instance Batch structure to use. 161 157 * @return False, if there is an active TD, true otherwise. 158 * 159 * Walk all TDs. Stop with false if there is an active one (it is to be 160 * processed). Stop with true if an error is found. Return true if the last TS 161 * is reached. 162 162 */ 163 163 bool batch_is_complete(batch_t *instance) 164 164 { 165 165 assert(instance); 166 uhci_batch_t *data = instance->private_data; 167 assert(data); 168 166 169 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n", 167 instance, instance->packets);170 instance, data->packets); 168 171 instance->transfered_size = 0; 169 172 size_t i = 0; 170 for (;i < instance->packets; ++i) {171 if (td_is_active(& instance->tds[i])) {173 for (;i < data->packets; ++i) { 174 if (td_is_active(&data->tds[i])) { 172 175 return false; 173 176 } 174 177 175 instance->error = td_status(& instance->tds[i]);178 instance->error = td_status(&data->tds[i]); 176 179 if (instance->error != EOK) { 177 180 usb_log_debug("Batch(%p) found error TD(%d):%x.\n", 178 instance, i, instance->tds[i].status); 179 180 device_keeper_set_toggle(instance->manager, 181 instance->target, td_toggle(&instance->tds[i])); 181 instance, i, data->tds[i].status); 182 td_print_status(&data->tds[i]); 183 184 device_keeper_set_toggle(data->manager, 185 instance->target, instance->direction, 186 td_toggle(&data->tds[i])); 182 187 if (i > 0) 183 188 goto substract_ret; … … 185 190 } 186 191 187 instance->transfered_size += td_act_size(& instance->tds[i]);188 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])) 189 194 goto substract_ret; 190 195 } … … 197 202 * 198 203 * @param[in] instance Batch structure to use. 204 * 205 * Uses genercir control function with pids OUT and IN. 199 206 */ 200 207 void batch_control_write(batch_t *instance) 201 208 { 202 209 assert(instance); 203 /* we are data out, we are supposed to provide data */210 /* We are data out, we are supposed to provide data */ 204 211 memcpy(instance->transport_buffer, instance->buffer, 205 212 instance->buffer_size); … … 207 214 instance->next_step = batch_call_out_and_dispose; 208 215 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance); 209 batch_schedule(instance);210 216 } 211 217 /*----------------------------------------------------------------------------*/ … … 213 219 * 214 220 * @param[in] instance Batch structure to use. 221 * 222 * Uses generic control with pids IN and OUT. 215 223 */ 216 224 void batch_control_read(batch_t *instance) … … 220 228 instance->next_step = batch_call_in_and_dispose; 221 229 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance); 222 batch_schedule(instance); 223 } 224 /*----------------------------------------------------------------------------*/ 225 /** Prepares interrupt in transaction. 226 * 227 * @param[in] instance Batch structure to use. 230 } 231 /*----------------------------------------------------------------------------*/ 232 /** Prepare interrupt in transaction. 233 * 234 * @param[in] instance Batch structure to use. 235 * 236 * Data transaction with PID_IN. 228 237 */ 229 238 void batch_interrupt_in(batch_t *instance) 230 239 { 231 240 assert(instance); 241 instance->direction = USB_DIRECTION_IN; 232 242 batch_data(instance, USB_PID_IN); 233 243 instance->next_step = batch_call_in_and_dispose; 234 244 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance); 235 batch_schedule(instance); 236 } 237 /*----------------------------------------------------------------------------*/ 238 /** Prepares interrupt out transaction. 239 * 240 * @param[in] instance Batch structure to use. 245 } 246 /*----------------------------------------------------------------------------*/ 247 /** Prepare interrupt out transaction. 248 * 249 * @param[in] instance Batch structure to use. 250 * 251 * Data transaction with PID_OUT. 241 252 */ 242 253 void batch_interrupt_out(batch_t *instance) 243 254 { 244 255 assert(instance); 245 /* we are data out, we are supposed to provide data */ 246 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size); 256 instance->direction = USB_DIRECTION_OUT; 257 /* We are data out, we are supposed to provide data */ 258 memcpy(instance->transport_buffer, instance->buffer, 259 instance->buffer_size); 247 260 batch_data(instance, USB_PID_OUT); 248 261 instance->next_step = batch_call_out_and_dispose; 249 262 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance); 250 batch_schedule(instance); 251 } 252 /*----------------------------------------------------------------------------*/ 253 /** Prepares bulk in transaction. 254 * 255 * @param[in] instance Batch structure to use. 263 } 264 /*----------------------------------------------------------------------------*/ 265 /** Prepare bulk in transaction. 266 * 267 * @param[in] instance Batch structure to use. 268 * 269 * Data transaction with PID_IN. 256 270 */ 257 271 void batch_bulk_in(batch_t *instance) … … 259 273 assert(instance); 260 274 batch_data(instance, USB_PID_IN); 275 instance->direction = USB_DIRECTION_IN; 261 276 instance->next_step = batch_call_in_and_dispose; 262 277 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance); 263 batch_schedule(instance); 264 } 265 /*----------------------------------------------------------------------------*/ 266 /** Prepares bulk out transaction. 267 * 268 * @param[in] instance Batch structure to use. 278 } 279 /*----------------------------------------------------------------------------*/ 280 /** Prepare bulk out transaction. 281 * 282 * @param[in] instance Batch structure to use. 283 * 284 * Data transaction with PID_OUT. 269 285 */ 270 286 void batch_bulk_out(batch_t *instance) 271 287 { 272 288 assert(instance); 273 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size); 289 instance->direction = USB_DIRECTION_OUT; 290 /* We are data out, we are supposed to provide data */ 291 memcpy(instance->transport_buffer, instance->buffer, 292 instance->buffer_size); 274 293 batch_data(instance, USB_PID_OUT); 275 294 instance->next_step = batch_call_out_and_dispose; 276 295 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance); 277 batch_schedule(instance); 278 } 279 /*----------------------------------------------------------------------------*/ 280 /** Prepares generic data transaction 296 } 297 /*----------------------------------------------------------------------------*/ 298 /** Prepare generic data transaction 281 299 * 282 300 * @param[in] instance Batch structure to use. 283 301 * @param[in] pid to use for data packets. 302 * 303 * Packets with alternating toggle bit and supplied pid value. 304 * The last packet is marked with IOC flag. 284 305 */ 285 306 void batch_data(batch_t *instance, usb_packet_id pid) 286 307 { 287 308 assert(instance); 309 uhci_batch_t *data = instance->private_data; 310 assert(data); 311 288 312 const bool low_speed = instance->speed == USB_SPEED_LOW; 289 int toggle = 290 d evice_keeper_get_toggle(instance->manager, instance->target);313 int toggle = device_keeper_get_toggle( 314 data->manager, instance->target, instance->direction); 291 315 assert(toggle == 0 || toggle == 1); 292 316 … … 294 318 size_t remain_size = instance->buffer_size; 295 319 while (remain_size > 0) { 296 char * data =320 char *trans_data = 297 321 instance->transport_buffer + instance->buffer_size 298 322 - remain_size; … … 302 326 remain_size : instance->max_packet_size; 303 327 304 td_t *next_packet = (packet + 1 < instance->packets)305 ? & instance->tds[packet + 1] : NULL;306 307 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); 308 332 assert(packet_size <= remain_size); 309 333 310 334 td_init( 311 & instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,312 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, 313 337 next_packet); 314 338 … … 318 342 ++packet; 319 343 } 320 device_keeper_set_toggle(instance->manager, instance->target, toggle); 321 } 322 /*----------------------------------------------------------------------------*/ 323 /** Prepares generic control transaction 344 td_set_ioc(&data->tds[packet - 1]); 345 device_keeper_set_toggle(data->manager, instance->target, 346 instance->direction, toggle); 347 } 348 /*----------------------------------------------------------------------------*/ 349 /** Prepare generic control transaction 324 350 * 325 351 * @param[in] instance Batch structure to use. 326 352 * @param[in] data_stage to use for data packets. 327 353 * @param[in] status_stage to use for data packets. 354 * 355 * Setup stage with toggle 0 and USB_PID_SETUP. 356 * Data stage with alternating toggle and pid supplied by parameter. 357 * Status stage with toggle 1 and pid supplied by parameter. 358 * The last packet is marked with IOC. 328 359 */ 329 360 void batch_control(batch_t *instance, … … 331 362 { 332 363 assert(instance); 364 uhci_batch_t *data = instance->private_data; 365 assert(data); 366 assert(data->packets >= 2); 333 367 334 368 const bool low_speed = instance->speed == USB_SPEED_LOW; 335 369 int toggle = 0; 336 370 /* setup stage */ 337 td_init(instance->tds, DEFAULT_ERROR_COUNT, 338 instance->setup_size, toggle, false, low_speed, instance->target, 339 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]); 340 375 341 376 /* data stage */ … … 343 378 size_t remain_size = instance->buffer_size; 344 379 while (remain_size > 0) { 345 char * data =380 char *control_data = 346 381 instance->transport_buffer + instance->buffer_size 347 382 - remain_size; … … 354 389 355 390 td_init( 356 & instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,391 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size, 357 392 toggle, false, low_speed, instance->target, data_stage, 358 data, &instance->tds[packet + 1]);393 control_data, &data->tds[packet + 1]); 359 394 360 395 ++packet; 361 assert(packet < instance->packets);396 assert(packet < data->packets); 362 397 assert(packet_size <= remain_size); 363 398 remain_size -= packet_size; … … 365 400 366 401 /* status stage */ 367 assert(packet == instance->packets - 1); 368 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT, 369 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL); 370 371 372 instance->tds[packet].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG; 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 373 409 usb_log_debug2("Control last TD status: %x.\n", 374 instance->tds[packet].status); 375 } 376 /*----------------------------------------------------------------------------*/ 377 /** Prepares data, gets error status and calls callback in. 378 * 379 * @param[in] instance Batch structure to use. 380 */ 381 void batch_call_in(batch_t *instance) 382 { 383 assert(instance); 384 assert(instance->callback_in); 385 386 /* we are data in, we need data */ 387 memcpy(instance->buffer, instance->transport_buffer, 388 instance->buffer_size); 389 390 int err = instance->error; 391 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n", 392 instance, instance->transfer_type, str_error(err), err, 393 instance->transfered_size); 394 395 instance->callback_in( 396 instance->fun, err, instance->transfered_size, instance->arg); 397 } 398 /*----------------------------------------------------------------------------*/ 399 /** Gets error status and calls callback out. 400 * 401 * @param[in] instance Batch structure to use. 402 */ 403 void batch_call_out(batch_t *instance) 404 { 405 assert(instance); 406 assert(instance->callback_out); 407 408 int err = instance->error; 409 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n", 410 instance, instance->transfer_type, str_error(err), err); 411 instance->callback_out(instance->fun, 412 err, instance->arg); 413 } 414 /*----------------------------------------------------------------------------*/ 415 /** Prepares data, gets error status, calls callback in and dispose. 410 data->tds[packet].status); 411 } 412 /*----------------------------------------------------------------------------*/ 413 qh_t * batch_qh(batch_t *instance) 414 { 415 assert(instance); 416 uhci_batch_t *data = instance->private_data; 417 assert(data); 418 return data->qh; 419 } 420 /*----------------------------------------------------------------------------*/ 421 /** Helper function calls callback and correctly disposes of batch structure. 416 422 * 417 423 * @param[in] instance Batch structure to use. … … 424 430 } 425 431 /*----------------------------------------------------------------------------*/ 426 /** Gets error status, calls callback out and dispose.432 /** Helper function calls callback and correctly disposes of batch structure. 427 433 * 428 434 * @param[in] instance Batch structure to use. … … 435 441 } 436 442 /*----------------------------------------------------------------------------*/ 437 /** Correctly dispose sall used data structures.443 /** Correctly dispose all used data structures. 438 444 * 439 445 * @param[in] instance Batch structure to use. … … 442 448 { 443 449 assert(instance); 450 uhci_batch_t *data = instance->private_data; 451 assert(data); 444 452 usb_log_debug("Batch(%p) disposing.\n", instance); 445 453 /* free32 is NULL safe */ 446 free32( instance->tds);447 free32( instance->qh);454 free32(data->tds); 455 free32(data->qh); 448 456 free32(instance->setup_buffer); 449 457 free32(instance->transport_buffer); 458 free(data); 450 459 free(instance); 451 }452 /*----------------------------------------------------------------------------*/453 int batch_schedule(batch_t *instance)454 {455 assert(instance);456 uhci_t *hc = fun_to_uhci(instance->fun);457 assert(hc);458 return uhci_schedule(hc, instance);459 460 } 460 461 /**
Note:
See TracChangeset
for help on using the changeset viewer.