source: mainline/uspace/drv/ohci/batch.c@ 28d9c95

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

Cleanup, add normal and doxygen comments

  • Property mode set to 100644
File size: 15.0 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 */
[2cc6e97]68static void ohci_transfer_batch_dispose(void *ohci_batch)
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/*----------------------------------------------------------------------------*/
[28d9c95]85/** Allocate memory initialize internal structures
86 *
87 * @param[in] fun DDF function to pass to callback.
88 * @param[in] ep Communication target
89 * @param[in] buffer Data source/destination.
90 * @param[in] buffer_size Size of the buffer.
91 * @param[in] setup_buffer Setup data source (if not NULL)
92 * @param[in] setup_size Size of setup_buffer (should be always 8)
93 * @param[in] func_in function to call on inbound transfer completion
94 * @param[in] func_out function to call on outbound transfer completion
95 * @param[in] arg additional parameter to func_in or func_out
96 * @return Valid pointer if all structures were successfully created,
97 * NULL otherwise.
98 *
99 * Allocates and initializes structures needed by the OHCI hw for the transfer.
100 */
[6bec59b]101usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
102 char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
[be7950e8]103 usbhc_iface_transfer_in_callback_t func_in,
[6bec59b]104 usbhc_iface_transfer_out_callback_t func_out, void *arg)
[be7950e8]105{
[2bf8f8c]106#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
107 if (ptr == NULL) { \
108 usb_log_error(message); \
109 if (instance) { \
[2cc6e97]110 usb_transfer_batch_dispose(instance); \
[2bf8f8c]111 } \
112 return NULL; \
113 } else (void)0
114
[1387692]115 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
[2bf8f8c]116 CHECK_NULL_DISPOSE_RETURN(instance,
117 "Failed to allocate batch instance.\n");
[d017cea]118 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
[2cc6e97]119 NULL, setup_size, func_in, func_out, arg, fun, NULL,
120 ohci_transfer_batch_dispose);
[2bf8f8c]121
[9a6fde4]122 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
123 assert(hcd_ep);
124
125 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
[06c552c]126 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
127 instance->private_data = data;
128
[9a6fde4]129 data->td_count =
[b854e56]130 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
[06c552c]131 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
132 data->td_count += 2;
133 }
134
[28d9c95]135 /* We need an extra place for TD that is currently assigned to hcd_ep*/
[9a6fde4]136 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
[06c552c]137 CHECK_NULL_DISPOSE_RETURN(data->tds,
138 "Failed to allocate transfer descriptors.\n");
139
[28d9c95]140 /* Add TD left over by the previous transfer */
[9a6fde4]141 data->tds[0] = hcd_ep->td;
142 data->leave_td = 0;
143 unsigned i = 1;
144 for (; i <= data->td_count; ++i) {
145 data->tds[i] = malloc32(sizeof(td_t));
146 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
147 "Failed to allocate TD %d.\n", i );
148 }
149
150 data->ed = hcd_ep->ed;
[06c552c]151
[28d9c95]152 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
153 * it is, however, not capable of handling buffer that accupies more
154 * than two pages (the first page is computete using start pointer, the
155 * other using end pointer)*/
[9a6fde4]156 if (setup_size + buffer_size > 0) {
157 data->device_buffer = malloc32(setup_size + buffer_size);
158 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
[2bf8f8c]159 "Failed to allocate device accessible buffer.\n");
[9a6fde4]160 instance->setup_buffer = data->device_buffer;
161 instance->data_buffer = data->device_buffer + setup_size;
[2bf8f8c]162 memcpy(instance->setup_buffer, setup_buffer, setup_size);
163 }
164
165 return instance;
[be7950e8]166}
167/*----------------------------------------------------------------------------*/
[28d9c95]168/** Check batch TDs' status.
169 *
170 * @param[in] instance Batch structure to use.
171 * @return False, if there is an active TD, true otherwise.
172 *
173 * Walk all TDs (usually there is just one). Stop with false if there is an
174 * active TD. Stop with true if an error is found. Return true if the walk
175 * completes with the last TD.
176 */
[f98b8269]177bool batch_is_complete(usb_transfer_batch_t *instance)
178{
[f1be95c8]179 assert(instance);
[2cc6e97]180 ohci_transfer_batch_t *data = instance->private_data;
[f1be95c8]181 assert(data);
[9a6fde4]182 size_t tds = data->td_count;
[4125b7d]183 usb_log_debug("Batch(%p) checking %zu td(s) for completion.\n",
[f1be95c8]184 instance, tds);
[78d4e1f]185 usb_log_debug("ED: %x:%x:%x:%x.\n",
186 data->ed->status, data->ed->td_head, data->ed->td_tail,
187 data->ed->next);
[f1be95c8]188 size_t i = 0;
[ad86349]189 instance->transfered_size = instance->buffer_size;
[f1be95c8]190 for (; i < tds; ++i) {
[9a6fde4]191 assert(data->tds[i] != NULL);
[4125b7d]192 usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
[9a6fde4]193 data->tds[i]->status, data->tds[i]->cbp, data->tds[i]->next,
194 data->tds[i]->be);
195 if (!td_is_finished(data->tds[i])) {
[f1be95c8]196 return false;
[aa9ccf7]197 }
[9a6fde4]198 instance->error = td_error(data->tds[i]);
[f1be95c8]199 if (instance->error != EOK) {
[4125b7d]200 usb_log_debug("Batch(%p) found error TD(%zu):%x.\n",
[9a6fde4]201 instance, i, data->tds[i]->status);
[c9dc471]202 /* Make sure TD queue is empty (one TD),
203 * ED should be marked as halted */
204 data->ed->td_tail =
205 (data->ed->td_head & ED_TDTAIL_PTR_MASK);
206 ++i;
[9a6fde4]207 break;
[f1be95c8]208 }
209 }
[d6522dd]210 data->leave_td = i;
[9a6fde4]211 assert(data->leave_td <= data->td_count);
[d6522dd]212 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
213 assert(hcd_ep);
214 hcd_ep->td = data->tds[i];
[ad86349]215 if (i > 0)
216 instance->transfered_size -= td_remain_size(data->tds[i - 1]);
217
[c9dc471]218 /* Clear possible ED HALT */
219 data->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
[28d9c95]220 const uint32_t pa = addr_to_phys(hcd_ep->td);
[c9dc471]221 assert(pa == (data->ed->td_head & ED_TDHEAD_PTR_MASK));
222 assert(pa == (data->ed->td_tail & ED_TDTAIL_PTR_MASK));
[d6522dd]223
[f1be95c8]224 return true;
[f98b8269]225}
226/*----------------------------------------------------------------------------*/
[28d9c95]227/** Starts execution of the TD list
228 *
229 * @param[in] instance Batch structure to use
230 */
[7013b14]231void batch_commit(usb_transfer_batch_t *instance)
232{
233 assert(instance);
234 ohci_transfer_batch_t *data = instance->private_data;
235 assert(data);
236 ed_set_end_td(data->ed, data->tds[data->td_count]);
237}
238/*----------------------------------------------------------------------------*/
[28d9c95]239/** Prepares control write transfer.
240 *
241 * @param[in] instance Batch structure to use.
242 *
243 * Uses generic control transfer using direction OUT(data stage) and
244 * IN(status stage).
245 */
[1387692]246void batch_control_write(usb_transfer_batch_t *instance)
[be7950e8]247{
[2bf8f8c]248 assert(instance);
[d328172]249 /* We are data out, we are supposed to provide data */
[d017cea]250 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[2cc6e97]251 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[e42dd32]252 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
[d328172]253 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[be7950e8]254}
255/*----------------------------------------------------------------------------*/
[28d9c95]256/** Prepares control read transfer.
257 *
258 * @param[in] instance Batch structure to use.
259 *
260 * Uses generic control transfer using direction IN(data stage) and
261 * OUT(status stage).
262 */
[1387692]263void batch_control_read(usb_transfer_batch_t *instance)
[be7950e8]264{
[2bf8f8c]265 assert(instance);
[2cc6e97]266 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[e42dd32]267 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
[81001f6]268 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[be7950e8]269}
270/*----------------------------------------------------------------------------*/
[28d9c95]271/** Prepare interrupt in transfer.
272 *
273 * @param[in] instance Batch structure to use.
274 *
275 * Data transfer.
276 */
[1387692]277void batch_interrupt_in(usb_transfer_batch_t *instance)
[be7950e8]278{
[2bf8f8c]279 assert(instance);
[2cc6e97]280 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c6fe469]281 batch_data(instance);
[d328172]282 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[be7950e8]283}
284/*----------------------------------------------------------------------------*/
[28d9c95]285/** Prepare interrupt out transfer.
286 *
287 * @param[in] instance Batch structure to use.
288 *
289 * Data transfer.
290 */
[1387692]291void batch_interrupt_out(usb_transfer_batch_t *instance)
[be7950e8]292{
[2bf8f8c]293 assert(instance);
[d328172]294 /* We are data out, we are supposed to provide data */
[d017cea]295 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[2cc6e97]296 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c6fe469]297 batch_data(instance);
[d328172]298 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
[be7950e8]299}
300/*----------------------------------------------------------------------------*/
[28d9c95]301/** Prepare bulk in transfer.
302 *
303 * @param[in] instance Batch structure to use.
304 *
305 * Data transfer.
306 */
[1387692]307void batch_bulk_in(usb_transfer_batch_t *instance)
[be7950e8]308{
[2bf8f8c]309 assert(instance);
[2cc6e97]310 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c6fe469]311 batch_data(instance);
[d328172]312 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
[be7950e8]313}
314/*----------------------------------------------------------------------------*/
[28d9c95]315/** Prepare bulk out transfer.
316 *
317 * @param[in] instance Batch structure to use.
318 *
319 * Data transfer.
320 */
[1387692]321void batch_bulk_out(usb_transfer_batch_t *instance)
[be7950e8]322{
[2bf8f8c]323 assert(instance);
[6b082a1b]324 /* We are data out, we are supposed to provide data */
325 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[a13ed97]326 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c6fe469]327 batch_data(instance);
[a13ed97]328 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
[d328172]329}
330/*----------------------------------------------------------------------------*/
[28d9c95]331/** Prepare generic control transfer
332 *
333 * @param[in] instance Batch structure to use.
334 * @param[in] data_dir Direction to use for data stage.
335 * @param[in] status_dir Direction to use for status stage.
336 *
337 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
338 * Data stage with alternating toggle and direction supplied by parameter.
339 * Status stage with toggle 1 and direction supplied by parameter.
340 */
[e42dd32]341void batch_control(usb_transfer_batch_t *instance,
342 usb_direction_t data_dir, usb_direction_t status_dir)
[7786cea]343{
344 assert(instance);
[2cc6e97]345 ohci_transfer_batch_t *data = instance->private_data;
[7786cea]346 assert(data);
[d6522dd]347 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
[8790650]348 data->ed->status, data->ed->td_tail, data->ed->td_head,
349 data->ed->next);
[e42dd32]350 int toggle = 0;
351 /* setup stage */
[9a6fde4]352 td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
[e42dd32]353 instance->setup_size, toggle);
[9a6fde4]354 td_set_next(data->tds[0], data->tds[1]);
355 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0]->status,
356 data->tds[0]->cbp, data->tds[0]->next, data->tds[0]->be);
[e42dd32]357
358 /* data stage */
359 size_t td_current = 1;
360 size_t remain_size = instance->buffer_size;
[d017cea]361 char *buffer = instance->data_buffer;
[e42dd32]362 while (remain_size > 0) {
363 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
364 OHCI_TD_MAX_TRANSFER : remain_size;
365 toggle = 1 - toggle;
366
[9a6fde4]367 td_init(data->tds[td_current], data_dir, buffer,
[e42dd32]368 transfer_size, toggle);
[9a6fde4]369 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[e42dd32]370 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
[9a6fde4]371 data->tds[td_current]->status, data->tds[td_current]->cbp,
372 data->tds[td_current]->next, data->tds[td_current]->be);
[e42dd32]373
[d017cea]374 buffer += transfer_size;
[e42dd32]375 remain_size -= transfer_size;
[9a6fde4]376 assert(td_current < data->td_count - 1);
[e42dd32]377 ++td_current;
378 }
379
380 /* status stage */
[9a6fde4]381 assert(td_current == data->td_count - 1);
382 td_init(data->tds[td_current], status_dir, NULL, 0, 1);
383 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[e42dd32]384 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
[9a6fde4]385 data->tds[td_current]->status, data->tds[td_current]->cbp,
386 data->tds[td_current]->next, data->tds[td_current]->be);
[f98b8269]387}
388/*----------------------------------------------------------------------------*/
[28d9c95]389/** Prepare generic data transfer
390 *
391 * @param[in] instance Batch structure to use.
392 *
393 * Direction is supplied by the associated ep and toggle is maintained by the
394 * OHCI hw in ED.
395 */
[c6fe469]396void batch_data(usb_transfer_batch_t *instance)
397{
398 assert(instance);
[2cc6e97]399 ohci_transfer_batch_t *data = instance->private_data;
[c6fe469]400 assert(data);
[d6522dd]401 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
[c6fe469]402 data->ed->status, data->ed->td_tail, data->ed->td_head,
403 data->ed->next);
404
[aa9ccf7]405 size_t td_current = 0;
[c6fe469]406 size_t remain_size = instance->buffer_size;
[d017cea]407 char *buffer = instance->data_buffer;
[c6fe469]408 while (remain_size > 0) {
409 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
410 OHCI_TD_MAX_TRANSFER : remain_size;
411
[9a6fde4]412 td_init(data->tds[td_current], instance->ep->direction,
[d017cea]413 buffer, transfer_size, -1);
[9a6fde4]414 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
[c6fe469]415 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
[9a6fde4]416 data->tds[td_current]->status, data->tds[td_current]->cbp,
417 data->tds[td_current]->next, data->tds[td_current]->be);
[c6fe469]418
[d017cea]419 buffer += transfer_size;
[c6fe469]420 remain_size -= transfer_size;
421 assert(td_current < data->td_count);
422 ++td_current;
423 }
424}
[41b96b4]425/**
426 * @}
427 */
Note: See TracBrowser for help on using the repository browser.