Changes in uspace/drv/ohci/batch.c [d6522dd:aa9ccf7] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/ohci/batch.c
rd6522dd raa9ccf7 39 39 40 40 #include "batch.h" 41 #include "hcd_endpoint.h"42 41 #include "utils/malloc32.h" 43 42 #include "hw_struct/endpoint_descriptor.h" 44 43 #include "hw_struct/transfer_descriptor.h" 45 44 46 typedef struct ohci_ transfer_batch {45 typedef struct ohci_batch { 47 46 ed_t *ed; 48 td_t * *tds;47 td_t *tds; 49 48 size_t td_count; 50 size_t leave_td; 51 char *device_buffer; 52 } ohci_transfer_batch_t; 53 54 static void ohci_transfer_batch_dispose(void *ohci_batch) 55 { 56 ohci_transfer_batch_t *instance = ohci_batch; 57 if (!instance) 58 return; 59 free32(instance->device_buffer); 60 unsigned i = 0; 61 if (instance->tds) { 62 for (; i< instance->td_count; ++i) { 63 if (i != instance->leave_td) 64 free32(instance->tds[i]); 65 } 66 free(instance->tds); 67 } 68 free(instance); 69 } 70 /*----------------------------------------------------------------------------*/ 49 } ohci_batch_t; 50 71 51 static void batch_control(usb_transfer_batch_t *instance, 72 52 usb_direction_t data_dir, usb_direction_t status_dir); 73 53 static void batch_data(usb_transfer_batch_t *instance); 74 /*----------------------------------------------------------------------------*/ 54 static void batch_call_in_and_dispose(usb_transfer_batch_t *instance); 55 static void batch_call_out_and_dispose(usb_transfer_batch_t *instance); 56 57 #define DEFAULT_ERROR_COUNT 3 75 58 usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep, 76 59 char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size, … … 82 65 usb_log_error(message); \ 83 66 if (instance) { \ 84 usb_transfer_batch_dispose(instance); \67 batch_dispose(instance); \ 85 68 } \ 86 69 return NULL; \ … … 90 73 CHECK_NULL_DISPOSE_RETURN(instance, 91 74 "Failed to allocate batch instance.\n"); 92 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size, 93 NULL, setup_size, func_in, func_out, arg, fun, NULL, 94 ohci_transfer_batch_dispose); 95 96 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep); 97 assert(hcd_ep); 98 99 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1); 75 usb_target_t target = 76 { .address = ep->address, .endpoint = ep->endpoint }; 77 usb_transfer_batch_init(instance, target, ep->transfer_type, ep->speed, 78 ep->max_packet_size, buffer, NULL, buffer_size, NULL, setup_size, 79 func_in, func_out, arg, fun, ep, NULL); 80 81 ohci_batch_t *data = malloc(sizeof(ohci_batch_t)); 100 82 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n"); 83 bzero(data, sizeof(ohci_batch_t)); 101 84 instance->private_data = data; 102 85 103 data->td_count = 86 /* we needs + 1 transfer descriptor as the last one won't be executed */ 87 data->td_count = 1 + 104 88 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER); 105 89 if (ep->transfer_type == USB_TRANSFER_CONTROL) { … … 107 91 } 108 92 109 /* we need one extra place for td that is currently assigned to hcd_ep*/ 110 data->tds = calloc(sizeof(td_t*), data->td_count + 1); 93 data->tds = malloc32(sizeof(td_t) * data->td_count); 111 94 CHECK_NULL_DISPOSE_RETURN(data->tds, 112 95 "Failed to allocate transfer descriptors.\n"); 113 114 data->tds[0] = hcd_ep->td; 115 data->leave_td = 0; 116 unsigned i = 1; 117 for (; i <= data->td_count; ++i) { 118 data->tds[i] = malloc32(sizeof(td_t)); 119 CHECK_NULL_DISPOSE_RETURN(data->tds[i], 120 "Failed to allocate TD %d.\n", i ); 121 } 122 123 data->ed = hcd_ep->ed; 124 125 if (setup_size + buffer_size > 0) { 126 data->device_buffer = malloc32(setup_size + buffer_size); 127 CHECK_NULL_DISPOSE_RETURN(data->device_buffer, 96 bzero(data->tds, sizeof(td_t) * data->td_count); 97 98 data->ed = malloc32(sizeof(ed_t)); 99 CHECK_NULL_DISPOSE_RETURN(data->ed, 100 "Failed to allocate endpoint descriptor.\n"); 101 102 if (buffer_size > 0) { 103 instance->transport_buffer = malloc32(buffer_size); 104 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer, 128 105 "Failed to allocate device accessible buffer.\n"); 129 instance->setup_buffer = data->device_buffer; 130 instance->data_buffer = data->device_buffer + setup_size; 106 } 107 108 if (setup_size > 0) { 109 instance->setup_buffer = malloc32(setup_size); 110 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer, 111 "Failed to allocate device accessible setup buffer.\n"); 131 112 memcpy(instance->setup_buffer, setup_buffer, setup_size); 132 113 } … … 135 116 } 136 117 /*----------------------------------------------------------------------------*/ 118 void batch_dispose(usb_transfer_batch_t *instance) 119 { 120 assert(instance); 121 ohci_batch_t *data = instance->private_data; 122 assert(data); 123 free32(data->ed); 124 free32(data->tds); 125 free32(instance->setup_buffer); 126 free32(instance->transport_buffer); 127 free(data); 128 free(instance); 129 } 130 /*----------------------------------------------------------------------------*/ 137 131 bool batch_is_complete(usb_transfer_batch_t *instance) 138 132 { 139 133 assert(instance); 140 ohci_ transfer_batch_t *data = instance->private_data;141 assert(data); 142 size_t tds = data->td_count ;134 ohci_batch_t *data = instance->private_data; 135 assert(data); 136 size_t tds = data->td_count - 1; 143 137 usb_log_debug("Batch(%p) checking %d td(s) for completion.\n", 144 138 instance, tds); … … 148 142 size_t i = 0; 149 143 for (; i < tds; ++i) { 150 assert(data->tds[i] != NULL);151 144 usb_log_debug("TD %d: %x:%x:%x:%x.\n", i, 152 data->tds[i] ->status, data->tds[i]->cbp, data->tds[i]->next,153 data->tds[i] ->be);154 if (!td_is_finished( data->tds[i])) {145 data->tds[i].status, data->tds[i].cbp, data->tds[i].next, 146 data->tds[i].be); 147 if (!td_is_finished(&data->tds[i])) { 155 148 return false; 156 149 } 157 instance->error = td_error( data->tds[i]);150 instance->error = td_error(&data->tds[i]); 158 151 /* FIXME: calculate real transfered size */ 159 152 instance->transfered_size = instance->buffer_size; 160 153 if (instance->error != EOK) { 161 154 usb_log_debug("Batch(%p) found error TD(%d):%x.\n", 162 instance, i, data->tds[i]->status); 163 break; 155 instance, i, data->tds[i].status); 156 return true; 157 // endpoint_toggle_set(instance->ep, 164 158 } 165 159 } 166 data->leave_td = i;167 assert(data->leave_td <= data->td_count);168 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);169 assert(hcd_ep);170 hcd_ep->td = data->tds[i];171 172 160 return true; 173 161 } 174 162 /*----------------------------------------------------------------------------*/ 175 void batch_commit(usb_transfer_batch_t *instance)176 {177 assert(instance);178 ohci_transfer_batch_t *data = instance->private_data;179 assert(data);180 ed_set_end_td(data->ed, data->tds[data->td_count]);181 }182 /*----------------------------------------------------------------------------*/183 163 void batch_control_write(usb_transfer_batch_t *instance) 184 164 { 185 165 assert(instance); 186 166 /* We are data out, we are supposed to provide data */ 187 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size); 188 instance->next_step = usb_transfer_batch_call_out_and_dispose; 167 memcpy(instance->transport_buffer, instance->buffer, 168 instance->buffer_size); 169 instance->next_step = batch_call_out_and_dispose; 189 170 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN); 190 171 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance); … … 194 175 { 195 176 assert(instance); 196 instance->next_step = usb_transfer_batch_call_in_and_dispose;177 instance->next_step = batch_call_in_and_dispose; 197 178 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT); 198 179 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance); … … 202 183 { 203 184 assert(instance); 204 instance->next_step = usb_transfer_batch_call_in_and_dispose; 185 assert(instance->direction == USB_DIRECTION_IN); 186 instance->next_step = batch_call_in_and_dispose; 205 187 batch_data(instance); 206 188 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance); … … 210 192 { 211 193 assert(instance); 194 assert(instance->direction == USB_DIRECTION_OUT); 212 195 /* We are data out, we are supposed to provide data */ 213 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size); 214 instance->next_step = usb_transfer_batch_call_out_and_dispose; 196 memcpy(instance->transport_buffer, instance->buffer, 197 instance->buffer_size); 198 instance->next_step = batch_call_out_and_dispose; 215 199 batch_data(instance); 216 200 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance); … … 220 204 { 221 205 assert(instance); 222 instance->next_step = usb_transfer_batch_call_in_and_dispose; 206 instance->direction = USB_DIRECTION_IN; 207 instance->next_step = batch_call_in_and_dispose; 223 208 batch_data(instance); 224 209 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance); … … 228 213 { 229 214 assert(instance); 230 instance->next_step = usb_transfer_batch_call_in_and_dispose; 215 instance->direction = USB_DIRECTION_IN; 216 instance->next_step = batch_call_in_and_dispose; 231 217 batch_data(instance); 232 218 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance); … … 236 222 { 237 223 assert(instance); 238 ohci_ transfer_batch_t *data = instance->private_data;224 ohci_batch_t *data = instance->private_data; 239 225 assert(data); 240 226 return data->ed; … … 245 231 { 246 232 assert(instance); 247 ohci_transfer_batch_t *data = instance->private_data; 248 assert(data); 249 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed, 233 ohci_batch_t *data = instance->private_data; 234 assert(data); 235 ed_init(data->ed, instance->ep); 236 ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]); 237 usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed, 250 238 data->ed->status, data->ed->td_tail, data->ed->td_head, 251 239 data->ed->next); 252 240 int toggle = 0; 253 241 /* setup stage */ 254 td_init( data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,242 td_init(&data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer, 255 243 instance->setup_size, toggle); 256 td_set_next( data->tds[0],data->tds[1]);257 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0] ->status,258 data->tds[0] ->cbp, data->tds[0]->next, data->tds[0]->be);244 td_set_next(&data->tds[0], &data->tds[1]); 245 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0].status, 246 data->tds[0].cbp, data->tds[0].next, data->tds[0].be); 259 247 260 248 /* data stage */ 261 249 size_t td_current = 1; 262 250 size_t remain_size = instance->buffer_size; 263 char * buffer = instance->data_buffer;251 char *transfer_buffer = instance->transport_buffer; 264 252 while (remain_size > 0) { 265 253 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ? … … 267 255 toggle = 1 - toggle; 268 256 269 td_init( data->tds[td_current], data_dir,buffer,257 td_init(&data->tds[td_current], data_dir, transfer_buffer, 270 258 transfer_size, toggle); 271 td_set_next( data->tds[td_current],data->tds[td_current + 1]);259 td_set_next(&data->tds[td_current], &data->tds[td_current + 1]); 272 260 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n", 273 data->tds[td_current] ->status, data->tds[td_current]->cbp,274 data->tds[td_current] ->next, data->tds[td_current]->be);275 276 buffer += transfer_size;261 data->tds[td_current].status, data->tds[td_current].cbp, 262 data->tds[td_current].next, data->tds[td_current].be); 263 264 transfer_buffer += transfer_size; 277 265 remain_size -= transfer_size; 278 assert(td_current < data->td_count - 1);266 assert(td_current < data->td_count - 2); 279 267 ++td_current; 280 268 } 281 269 282 270 /* status stage */ 283 assert(td_current == data->td_count - 1); 284 td_init(data->tds[td_current], status_dir, NULL, 0, 1); 285 td_set_next(data->tds[td_current], data->tds[td_current + 1]); 271 assert(td_current == data->td_count - 2); 272 td_init(&data->tds[td_current], status_dir, NULL, 0, 1); 286 273 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n", 287 data->tds[td_current] ->status, data->tds[td_current]->cbp,288 data->tds[td_current] ->next, data->tds[td_current]->be);274 data->tds[td_current].status, data->tds[td_current].cbp, 275 data->tds[td_current].next, data->tds[td_current].be); 289 276 } 290 277 /*----------------------------------------------------------------------------*/ … … 292 279 { 293 280 assert(instance); 294 ohci_transfer_batch_t *data = instance->private_data; 295 assert(data); 296 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed, 281 ohci_batch_t *data = instance->private_data; 282 assert(data); 283 ed_init(data->ed, instance->ep); 284 ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]); 285 usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed, 297 286 data->ed->status, data->ed->td_tail, data->ed->td_head, 298 287 data->ed->next); 299 288 289 /* data stage */ 300 290 size_t td_current = 0; 301 291 size_t remain_size = instance->buffer_size; 302 char * buffer = instance->data_buffer;292 char *transfer_buffer = instance->transport_buffer; 303 293 while (remain_size > 0) { 304 294 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ? 305 295 OHCI_TD_MAX_TRANSFER : remain_size; 306 296 307 td_init( data->tds[td_current], instance->ep->direction,308 buffer, transfer_size, -1);309 td_set_next( data->tds[td_current],data->tds[td_current + 1]);297 td_init(&data->tds[td_current], instance->ep->direction, 298 transfer_buffer, transfer_size, -1); 299 td_set_next(&data->tds[td_current], &data->tds[td_current + 1]); 310 300 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n", 311 data->tds[td_current] ->status, data->tds[td_current]->cbp,312 data->tds[td_current] ->next, data->tds[td_current]->be);313 314 buffer += transfer_size;301 data->tds[td_current].status, data->tds[td_current].cbp, 302 data->tds[td_current].next, data->tds[td_current].be); 303 304 transfer_buffer += transfer_size; 315 305 remain_size -= transfer_size; 316 306 assert(td_current < data->td_count); … … 318 308 } 319 309 } 310 /*----------------------------------------------------------------------------*/ 311 /** Helper function calls callback and correctly disposes of batch structure. 312 * 313 * @param[in] instance Batch structure to use. 314 */ 315 void batch_call_in_and_dispose(usb_transfer_batch_t *instance) 316 { 317 assert(instance); 318 usb_transfer_batch_call_in(instance); 319 batch_dispose(instance); 320 } 321 /*----------------------------------------------------------------------------*/ 322 /** Helper function calls callback and correctly disposes of batch structure. 323 * 324 * @param[in] instance Batch structure to use. 325 */ 326 void batch_call_out_and_dispose(usb_transfer_batch_t *instance) 327 { 328 assert(instance); 329 usb_transfer_batch_call_out(instance); 330 batch_dispose(instance); 331 } 320 332 /** 321 333 * @}
Note:
See TracChangeset
for help on using the changeset viewer.