source: mainline/uspace/drv/bus/usb/ohci/batch.c@ e2976bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2976bb was e2976bb, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

OHCI: Begin move to new hcd arch, plug in batch init hook.

  • Property mode set to 100644
File size: 17.2 KB
RevLine 
[41b96b4]1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
[81dce9f]28/** @addtogroup drvusbohci
[41b96b4]29 * @{
30 */
31/** @file
32 * @brief OHCI driver USB transaction structure
33 */
34#include <errno.h>
35#include <str_error.h>
36
37#include <usb/usb.h>
38#include <usb/debug.h>
39
40#include "batch.h"
[9a6fde4]41#include "hcd_endpoint.h"
[2bf8f8c]42#include "utils/malloc32.h"
[06c552c]43#include "hw_struct/endpoint_descriptor.h"
44#include "hw_struct/transfer_descriptor.h"
45
[28d9c95]46/** OHCI specific data required for USB transfer */
[2cc6e97]47typedef struct ohci_transfer_batch {
[28d9c95]48 /** Endpoint descriptor of the target endpoint. */
[06c552c]49 ed_t *ed;
[28d9c95]50 /** List of TDs needed for the transfer */
[9a6fde4]51 td_t **tds;
[28d9c95]52 /** Number of TDs used by the transfer */
[06c552c]53 size_t td_count;
[28d9c95]54 /** Dummy TD to be left at the ED and used by the next transfer */
[9a6fde4]55 size_t leave_td;
[28d9c95]56 /** Data buffer, must be accessible byb the OHCI hw. */
57 void *device_buffer;
[2cc6e97]58} ohci_transfer_batch_t;
[28d9c95]59/*----------------------------------------------------------------------------*/
60static void batch_control(usb_transfer_batch_t *instance,
61 usb_direction_t data_dir, usb_direction_t status_dir);
62static void batch_data(usb_transfer_batch_t *instance);
63/*----------------------------------------------------------------------------*/
64/** Safely destructs ohci_transfer_batch_t structure
65 *
66 * @param[in] ohci_batch Instance to destroy.
67 */
[e2976bb]68static void ohci_batch_dispose(void *ohci_batch)
[2cc6e97]69{
70 ohci_transfer_batch_t *instance = ohci_batch;
[9a6fde4]71 if (!instance)
72 return;
73 free32(instance->device_buffer);
74 unsigned i = 0;
75 if (instance->tds) {
76 for (; i< instance->td_count; ++i) {
77 if (i != instance->leave_td)
78 free32(instance->tds[i]);
79 }
80 free(instance->tds);
81 }
82 free(instance);
[2cc6e97]83}
[9a6fde4]84/*----------------------------------------------------------------------------*/
[e2976bb]85int batch_init_ohci(usb_transfer_batch_t *batch)
86{
87 assert(batch);
88#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
89 if (ptr == NULL) { \
90 usb_log_error(message); \
91 if (data) { \
92 ohci_batch_dispose(data); \
93 } \
94 return ENOMEM; \
95 } else (void)0
96
97 const hcd_endpoint_t *hcd_ep = hcd_endpoint_get(batch->ep);
98 assert(hcd_ep);
99
100 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
101 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
102
103 data->td_count = ((batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
104 / OHCI_TD_MAX_TRANSFER);
105 /* Control transfer need Setup and Status stage */
106 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
107 data->td_count += 2;
108 }
109
110 /* We need an extra place for TD that is currently assigned to hcd_ep*/
111 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
112 CHECK_NULL_DISPOSE_RETURN(data->tds,
113 "Failed to allocate transfer descriptors.\n");
114
115 /* Add TD left over by the previous transfer */
116 data->tds[0] = hcd_ep->td;
117 data->leave_td = 0;
118 unsigned i = 1;
119 for (; i <= data->td_count; ++i) {
120 data->tds[i] = malloc32(sizeof(td_t));
121 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
122 "Failed to allocate TD %d.\n", i );
123 }
124
125 data->ed = hcd_ep->ed;
126 batch->private_data = data;
127 batch->private_data_dtor = ohci_batch_dispose;
128
129 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
130 * it is, however, not capable of handling buffer that occupies more
131 * than two pages (the first page is computed using start pointer, the
132 * other using the end pointer) */
133 if (batch->setup_size + batch->buffer_size > 0) {
134 data->device_buffer =
135 malloc32(batch->setup_size + batch->buffer_size);
136 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
137 "Failed to allocate device accessible buffer.\n");
138 memcpy(data->device_buffer, batch->setup_buffer,
139 batch->setup_size);
140 batch->data_buffer = data->device_buffer + batch->setup_size;
141 }
142
143 return EOK;
144#undef CHECK_NULL_DISPOSE_RETURN
145}
146/*----------------------------------------------------------------------------*/
[28d9c95]147/** Allocate memory initialize internal structures
148 *
149 * @param[in] fun DDF function to pass to callback.
150 * @param[in] ep Communication target
151 * @param[in] buffer Data source/destination.
152 * @param[in] buffer_size Size of the buffer.
153 * @param[in] setup_buffer Setup data source (if not NULL)
154 * @param[in] setup_size Size of setup_buffer (should be always 8)
155 * @param[in] func_in function to call on inbound transfer completion
156 * @param[in] func_out function to call on outbound transfer completion
157 * @param[in] arg additional parameter to func_in or func_out
158 * @return Valid pointer if all structures were successfully created,
159 * NULL otherwise.
160 *
161 * Allocates and initializes structures needed by the OHCI hw for the transfer.
162 */
[6bec59b]163usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
[02cacce]164 char *buffer, size_t buffer_size,
165 const char *setup_buffer, size_t setup_size,
[be7950e8]166 usbhc_iface_transfer_in_callback_t func_in,
[6bec59b]167 usbhc_iface_transfer_out_callback_t func_out, void *arg)
[be7950e8]168{
[2bf8f8c]169#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
170 if (ptr == NULL) { \
171 usb_log_error(message); \
172 if (instance) { \
[2cc6e97]173 usb_transfer_batch_dispose(instance); \
[2bf8f8c]174 } \
175 return NULL; \
176 } else (void)0
177
[1387692]178 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
[2bf8f8c]179 CHECK_NULL_DISPOSE_RETURN(instance,
180 "Failed to allocate batch instance.\n");
[d017cea]181 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
[2cc6e97]182 NULL, setup_size, func_in, func_out, arg, fun, NULL,
[e2976bb]183 ohci_batch_dispose);
[2bf8f8c]184
[02cacce]185 const hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
[9a6fde4]186 assert(hcd_ep);
187
188 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
[06c552c]189 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
190 instance->private_data = data;
191
[9a6fde4]192 data->td_count =
[b854e56]193 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
[02cacce]194 /* Control transfer need Setup and Status stage */
[06c552c]195 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
196 data->td_count += 2;
197 }
198
[28d9c95]199 /* We need an extra place for TD that is currently assigned to hcd_ep*/
[9a6fde4]200 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
[06c552c]201 CHECK_NULL_DISPOSE_RETURN(data->tds,
202 "Failed to allocate transfer descriptors.\n");
203
[28d9c95]204 /* Add TD left over by the previous transfer */
[9a6fde4]205 data->tds[0] = hcd_ep->td;
206 data->leave_td = 0;
207 unsigned i = 1;
208 for (; i <= data->td_count; ++i) {
209 data->tds[i] = malloc32(sizeof(td_t));
210 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
211 "Failed to allocate TD %d.\n", i );
212 }
213
214 data->ed = hcd_ep->ed;
[06c552c]215
[28d9c95]216 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
[33d19a7]217 * it is, however, not capable of handling buffer that occupies more
218 * than two pages (the first page is computed using start pointer, the
219 * other using the end pointer) */
[9a6fde4]220 if (setup_size + buffer_size > 0) {
221 data->device_buffer = malloc32(setup_size + buffer_size);
222 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
[2bf8f8c]223 "Failed to allocate device accessible buffer.\n");
[9a6fde4]224 instance->setup_buffer = data->device_buffer;
225 instance->data_buffer = data->device_buffer + setup_size;
[2bf8f8c]226 memcpy(instance->setup_buffer, setup_buffer, setup_size);
227 }
228
229 return instance;
[be7950e8]230}
231/*----------------------------------------------------------------------------*/
[28d9c95]232/** Check batch TDs' status.
233 *
234 * @param[in] instance Batch structure to use.
235 * @return False, if there is an active TD, true otherwise.
236 *
237 * Walk all TDs (usually there is just one). Stop with false if there is an
238 * active TD. Stop with true if an error is found. Return true if the walk
239 * completes with the last TD.
240 */
[f98b8269]241bool batch_is_complete(usb_transfer_batch_t *instance)
242{
[f1be95c8]243 assert(instance);
[2cc6e97]244 ohci_transfer_batch_t *data = instance->private_data;
[f1be95c8]245 assert(data);
[4125b7d]246 usb_log_debug("Batch(%p) checking %zu td(s) for completion.\n",
[33d19a7]247 instance, data->td_count);
[78d4e1f]248 usb_log_debug("ED: %x:%x:%x:%x.\n",
249 data->ed->status, data->ed->td_head, data->ed->td_tail,
250 data->ed->next);
[f1be95c8]251 size_t i = 0;
[ad86349]252 instance->transfered_size = instance->buffer_size;
[33d19a7]253 for (; i < data->td_count; ++i) {
[9a6fde4]254 assert(data->tds[i] != NULL);
[4125b7d]255 usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
[9a6fde4]256 data->tds[i]->status, data->tds[i]->cbp, data->tds[i]->next,
257 data->tds[i]->be);
258 if (!td_is_finished(data->tds[i])) {
[f1be95c8]259 return false;
[aa9ccf7]260 }
[9a6fde4]261 instance->error = td_error(data->tds[i]);
[f1be95c8]262 if (instance->error != EOK) {
[4125b7d]263 usb_log_debug("Batch(%p) found error TD(%zu):%x.\n",
[9a6fde4]264 instance, i, data->tds[i]->status);
[c9dc471]265 /* Make sure TD queue is empty (one TD),
266 * ED should be marked as halted */
267 data->ed->td_tail =
268 (data->ed->td_head & ED_TDTAIL_PTR_MASK);
269 ++i;
[9a6fde4]270 break;
[f1be95c8]271 }
272 }
[d6522dd]273 data->leave_td = i;
[9a6fde4]274 assert(data->leave_td <= data->td_count);
[d6522dd]275 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
276 assert(hcd_ep);
277 hcd_ep->td = data->tds[i];
[33d19a7]278 assert(i > 0);
279 for (--i;i < data->td_count; ++i)
280 instance->transfered_size -= td_remain_size(data->tds[i]);
[ad86349]281
[c9dc471]282 /* Clear possible ED HALT */
283 data->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
[28d9c95]284 const uint32_t pa = addr_to_phys(hcd_ep->td);
[c9dc471]285 assert(pa == (data->ed->td_head & ED_TDHEAD_PTR_MASK));
286 assert(pa == (data->ed->td_tail & ED_TDTAIL_PTR_MASK));
[d6522dd]287
[f1be95c8]288 return true;
[f98b8269]289}
290/*----------------------------------------------------------------------------*/
[28d9c95]291/** Starts execution of the TD list
292 *
293 * @param[in] instance Batch structure to use
294 */
[7013b14]295void batch_commit(usb_transfer_batch_t *instance)
296{
297 assert(instance);
298 ohci_transfer_batch_t *data = instance->private_data;
299 assert(data);
300 ed_set_end_td(data->ed, data->tds[data->td_count]);
301}
302/*----------------------------------------------------------------------------*/
[28d9c95]303/** Prepares control write transfer.
304 *
305 * @param[in] instance Batch structure to use.
306 *
307 * Uses generic control transfer using direction OUT(data stage) and
308 * IN(status stage).
309 */
[1387692]310void batch_control_write(usb_transfer_batch_t *instance)
[be7950e8]311{
[2bf8f8c]312 assert(instance);
[d328172]313 /* We are data out, we are supposed to provide data */
[d017cea]314 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[2cc6e97]315 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[e42dd32]316 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
[d328172]317 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[be7950e8]318}
319/*----------------------------------------------------------------------------*/
[28d9c95]320/** Prepares control read transfer.
321 *
322 * @param[in] instance Batch structure to use.
323 *
324 * Uses generic control transfer using direction IN(data stage) and
325 * OUT(status stage).
326 */
[1387692]327void batch_control_read(usb_transfer_batch_t *instance)
[be7950e8]328{
[2bf8f8c]329 assert(instance);
[2cc6e97]330 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[e42dd32]331 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
[81001f6]332 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[be7950e8]333}
334/*----------------------------------------------------------------------------*/
[28d9c95]335/** Prepare interrupt in transfer.
336 *
337 * @param[in] instance Batch structure to use.
338 *
339 * Data transfer.
340 */
[1387692]341void batch_interrupt_in(usb_transfer_batch_t *instance)
[be7950e8]342{
[2bf8f8c]343 assert(instance);
[2cc6e97]344 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c6fe469]345 batch_data(instance);
[d328172]346 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[be7950e8]347}
348/*----------------------------------------------------------------------------*/
[28d9c95]349/** Prepare interrupt out transfer.
350 *
351 * @param[in] instance Batch structure to use.
352 *
353 * Data transfer.
354 */
[1387692]355void batch_interrupt_out(usb_transfer_batch_t *instance)
[be7950e8]356{
[2bf8f8c]357 assert(instance);
[d328172]358 /* We are data out, we are supposed to provide data */
[d017cea]359 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[2cc6e97]360 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c6fe469]361 batch_data(instance);
[d328172]362 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
[be7950e8]363}
364/*----------------------------------------------------------------------------*/
[28d9c95]365/** Prepare bulk in transfer.
366 *
367 * @param[in] instance Batch structure to use.
368 *
369 * Data transfer.
370 */
[1387692]371void batch_bulk_in(usb_transfer_batch_t *instance)
[be7950e8]372{
[2bf8f8c]373 assert(instance);
[2cc6e97]374 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c6fe469]375 batch_data(instance);
[d328172]376 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
[be7950e8]377}
378/*----------------------------------------------------------------------------*/
[28d9c95]379/** Prepare bulk out transfer.
380 *
381 * @param[in] instance Batch structure to use.
382 *
383 * Data transfer.
384 */
[1387692]385void batch_bulk_out(usb_transfer_batch_t *instance)
[be7950e8]386{
[2bf8f8c]387 assert(instance);
[6b082a1b]388 /* We are data out, we are supposed to provide data */
389 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[a13ed97]390 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c6fe469]391 batch_data(instance);
[a13ed97]392 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
[d328172]393}
394/*----------------------------------------------------------------------------*/
[28d9c95]395/** Prepare generic control transfer
396 *
397 * @param[in] instance Batch structure to use.
398 * @param[in] data_dir Direction to use for data stage.
399 * @param[in] status_dir Direction to use for status stage.
400 *
401 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
402 * Data stage with alternating toggle and direction supplied by parameter.
403 * Status stage with toggle 1 and direction supplied by parameter.
404 */
[e42dd32]405void batch_control(usb_transfer_batch_t *instance,
406 usb_direction_t data_dir, usb_direction_t status_dir)
[7786cea]407{
408 assert(instance);
[2cc6e97]409 ohci_transfer_batch_t *data = instance->private_data;
[7786cea]410 assert(data);
[d6522dd]411 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
[8790650]412 data->ed->status, data->ed->td_tail, data->ed->td_head,
413 data->ed->next);
[e42dd32]414 int toggle = 0;
415 /* setup stage */
[9a6fde4]416 td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
[e42dd32]417 instance->setup_size, toggle);
[9a6fde4]418 td_set_next(data->tds[0], data->tds[1]);
419 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0]->status,
420 data->tds[0]->cbp, data->tds[0]->next, data->tds[0]->be);
[e42dd32]421
422 /* data stage */
423 size_t td_current = 1;
424 size_t remain_size = instance->buffer_size;
[d017cea]425 char *buffer = instance->data_buffer;
[e42dd32]426 while (remain_size > 0) {
427 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
428 OHCI_TD_MAX_TRANSFER : remain_size;
429 toggle = 1 - toggle;
430
[9a6fde4]431 td_init(data->tds[td_current], data_dir, buffer,
[e42dd32]432 transfer_size, toggle);
[9a6fde4]433 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[e42dd32]434 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
[9a6fde4]435 data->tds[td_current]->status, data->tds[td_current]->cbp,
436 data->tds[td_current]->next, data->tds[td_current]->be);
[e42dd32]437
[d017cea]438 buffer += transfer_size;
[e42dd32]439 remain_size -= transfer_size;
[9a6fde4]440 assert(td_current < data->td_count - 1);
[e42dd32]441 ++td_current;
442 }
443
444 /* status stage */
[9a6fde4]445 assert(td_current == data->td_count - 1);
446 td_init(data->tds[td_current], status_dir, NULL, 0, 1);
447 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[e42dd32]448 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
[9a6fde4]449 data->tds[td_current]->status, data->tds[td_current]->cbp,
450 data->tds[td_current]->next, data->tds[td_current]->be);
[f98b8269]451}
452/*----------------------------------------------------------------------------*/
[28d9c95]453/** Prepare generic data transfer
454 *
455 * @param[in] instance Batch structure to use.
456 *
457 * Direction is supplied by the associated ep and toggle is maintained by the
458 * OHCI hw in ED.
459 */
[c6fe469]460void batch_data(usb_transfer_batch_t *instance)
461{
462 assert(instance);
[2cc6e97]463 ohci_transfer_batch_t *data = instance->private_data;
[c6fe469]464 assert(data);
[d6522dd]465 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
[c6fe469]466 data->ed->status, data->ed->td_tail, data->ed->td_head,
467 data->ed->next);
468
[aa9ccf7]469 size_t td_current = 0;
[c6fe469]470 size_t remain_size = instance->buffer_size;
[d017cea]471 char *buffer = instance->data_buffer;
[c6fe469]472 while (remain_size > 0) {
[02cacce]473 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
474 ? OHCI_TD_MAX_TRANSFER : remain_size;
[c6fe469]475
[9a6fde4]476 td_init(data->tds[td_current], instance->ep->direction,
[d017cea]477 buffer, transfer_size, -1);
[9a6fde4]478 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[c6fe469]479 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
[9a6fde4]480 data->tds[td_current]->status, data->tds[td_current]->cbp,
481 data->tds[td_current]->next, data->tds[td_current]->be);
[c6fe469]482
[d017cea]483 buffer += transfer_size;
[c6fe469]484 remain_size -= transfer_size;
485 assert(td_current < data->td_count);
486 ++td_current;
487 }
488}
[41b96b4]489/**
490 * @}
491 */
Note: See TracBrowser for help on using the repository browser.