source: mainline/uspace/drv/bus/usb/ohci/ohci_batch.c@ cd3fa47

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd3fa47 was d60115a, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

ohci: implement transfer abort on endpoint unregister

  • Property mode set to 100644
File size: 13.5 KB
Line 
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 */
28
29/** @addtogroup drvusbohci
30 * @{
31 */
32/** @file
33 * @brief OHCI driver USB transaction structure
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <macros.h>
39#include <mem.h>
40#include <stdbool.h>
41
42#include <usb/usb.h>
43#include <usb/debug.h>
44#include <usb/host/utils/malloc32.h>
45
46#include "ohci_batch.h"
47#include "ohci_bus.h"
48
49static void (*const batch_setup[])(ohci_transfer_batch_t*);
50
51/** Safely destructs ohci_transfer_batch_t structure
52 *
53 * @param[in] ohci_batch Instance to destroy.
54 */
55void ohci_transfer_batch_destroy(ohci_transfer_batch_t *ohci_batch)
56{
57 assert(ohci_batch);
58 if (ohci_batch->tds) {
59 const ohci_endpoint_t *ohci_ep =
60 ohci_endpoint_get(ohci_batch->base.ep);
61 assert(ohci_ep);
62 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
63 if (ohci_batch->tds[i] != ohci_ep->td)
64 free32(ohci_batch->tds[i]);
65 }
66 free(ohci_batch->tds);
67 }
68 free32(ohci_batch->device_buffer);
69 free(ohci_batch);
70}
71
72/** Allocate memory and initialize internal data structure.
73 *
74 * @param[in] ep Endpoint for which the batch will be created
75 * @return Valid pointer if all structures were successfully created,
76 * NULL otherwise.
77 */
78ohci_transfer_batch_t * ohci_transfer_batch_create(endpoint_t *ep)
79{
80 assert(ep);
81
82 ohci_transfer_batch_t *ohci_batch =
83 calloc(1, sizeof(ohci_transfer_batch_t));
84 if (!ohci_batch) {
85 usb_log_error("Failed to allocate OHCI batch data.");
86 return NULL;
87 }
88
89 usb_transfer_batch_init(&ohci_batch->base, ep);
90
91 return ohci_batch;
92}
93
94/** Prepares a batch to be sent.
95 *
96 * Determines the number of needed transfer descriptors (TDs).
97 * Prepares a transport buffer (that is accessible by the hardware).
98 * Initializes parameters needed for the transfer and callback.
99 */
100int ohci_transfer_batch_prepare(ohci_transfer_batch_t *ohci_batch)
101{
102 assert(ohci_batch);
103
104 const size_t setup_size = (ohci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
105 ? USB_SETUP_PACKET_SIZE
106 : 0;
107
108 usb_transfer_batch_t *usb_batch = &ohci_batch->base;
109
110 ohci_batch->td_count = (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
111 / OHCI_TD_MAX_TRANSFER;
112 /* Control transfer need Setup and Status stage */
113 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
114 ohci_batch->td_count += 2;
115 }
116
117 /* We need an extra place for TD that was left at ED */
118 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
119 if (!ohci_batch->tds) {
120 usb_log_error("Failed to allocate OHCI transfer descriptors.");
121 return ENOMEM;
122 }
123
124 /* Add TD left over by the previous transfer */
125 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
126 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
127
128 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
129 ohci_batch->tds[i] = malloc32(sizeof(td_t));
130 if (!ohci_batch->tds[i]) {
131 usb_log_error("Failed to allocate TD %d.", i);
132 return ENOMEM;
133 }
134 }
135
136 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
137 * it is, however, not capable of handling buffer that occupies more
138 * than two pages (the first page is computed using start pointer, the
139 * other using the end pointer) */
140 if (setup_size + usb_batch->buffer_size > 0) {
141 /* Use one buffer for setup and data stage */
142 ohci_batch->device_buffer =
143 malloc32(setup_size + usb_batch->buffer_size);
144 if (!ohci_batch->device_buffer) {
145 usb_log_error("Failed to allocate device buffer");
146 return ENOMEM;
147 }
148 /* Copy setup data */
149 memcpy(ohci_batch->device_buffer, usb_batch->setup.buffer, setup_size);
150 /* Copy generic data */
151 if (usb_batch->ep->direction != USB_DIRECTION_IN)
152 memcpy(
153 ohci_batch->device_buffer + setup_size,
154 usb_batch->buffer, usb_batch->buffer_size);
155 }
156
157 assert(batch_setup[usb_batch->ep->transfer_type]);
158 batch_setup[usb_batch->ep->transfer_type](ohci_batch);
159
160 return EOK;
161}
162
163/** Check batch TDs' status.
164 *
165 * @param[in] ohci_batch Batch structure to use.
166 * @return False, if there is an active TD, true otherwise.
167 *
168 * Walk all TDs (usually there is just one). Stop with false if there is an
169 * active TD. Stop with true if an error is found. Return true if the walk
170 * completes with the last TD.
171 */
172bool ohci_transfer_batch_check_completed(ohci_transfer_batch_t *ohci_batch)
173{
174 assert(ohci_batch);
175
176 usb_log_debug("Batch %p checking %zu td(s) for completion.",
177 &ohci_batch->base, ohci_batch->td_count);
178 usb_log_debug2("ED: %08x:%08x:%08x:%08x.",
179 ohci_batch->ed->status, ohci_batch->ed->td_head,
180 ohci_batch->ed->td_tail, ohci_batch->ed->next);
181
182 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_batch->ed))
183 return false;
184
185 /* Now we may be sure that either the ED is inactive because of errors
186 * or all transfer descriptors completed successfully */
187
188 /* Assume all data got through */
189 ohci_batch->base.transfered_size = ohci_batch->base.buffer_size;
190
191 /* Assume we will leave the last(unused) TD behind */
192 unsigned leave_td = ohci_batch->td_count;
193
194 /* Check all TDs */
195 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
196 assert(ohci_batch->tds[i] != NULL);
197 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.", i,
198 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
199 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
200
201 ohci_batch->base.error = td_error(ohci_batch->tds[i]);
202 if (ohci_batch->base.error == EOK) {
203 /* If the TD got all its data through, it will report
204 * 0 bytes remain, the sole exception is INPUT with
205 * data rounding flag (short), i.e. every INPUT.
206 * Nice thing is that short packets will correctly
207 * report remaining data, thus making this computation
208 * correct (short packets need to be produced by the
209 * last TD)
210 * NOTE: This also works for CONTROL transfer as
211 * the first TD will return 0 remain.
212 * NOTE: Short packets don't break the assumption that
213 * we leave the very last(unused) TD behind.
214 */
215 ohci_batch->base.transfered_size
216 -= td_remain_size(ohci_batch->tds[i]);
217 } else {
218 usb_log_debug("Batch %p found error TD(%zu):%08x.",
219 &ohci_batch->base, i,
220 ohci_batch->tds[i]->status);
221
222 /* ED should be stopped because of errors */
223 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
224
225 /* Now we have a problem: we don't know what TD
226 * the head pointer points to, the retiring rules
227 * described in specs say it should be the one after
228 * the failed one so set the tail pointer accordingly.
229 * It will be the one TD we leave behind.
230 */
231 leave_td = i + 1;
232
233 /* Check TD assumption */
234 assert(ed_head_td(ohci_batch->ed) ==
235 addr_to_phys(ohci_batch->tds[leave_td]));
236
237 /* Set tail to the same TD */
238 ed_set_tail_td(ohci_batch->ed,
239 ohci_batch->tds[leave_td]);
240
241 /* Clear possible ED HALT */
242 ed_clear_halt(ohci_batch->ed);
243 break;
244 }
245 }
246 assert(ohci_batch->base.transfered_size <=
247 ohci_batch->base.buffer_size);
248
249 const size_t setup_size = (ohci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
250 ? USB_SETUP_PACKET_SIZE
251 : 0;
252
253 if (ohci_batch->base.dir == USB_DIRECTION_IN)
254 memcpy(ohci_batch->base.buffer,
255 ohci_batch->device_buffer + setup_size,
256 ohci_batch->base.transfered_size);
257
258 /* Store the remaining TD */
259 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->base.ep);
260 assert(ohci_ep);
261 ohci_ep->td = ohci_batch->tds[leave_td];
262
263 /* Make sure that we are leaving the right TD behind */
264 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
265 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
266
267 return true;
268}
269
270/** Starts execution of the TD list
271 *
272 * @param[in] ohci_batch Batch structure to use
273 */
274void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
275{
276 assert(ohci_batch);
277 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
278}
279
280/** Prepare generic control transfer
281 *
282 * @param[in] ohci_batch Batch structure to use.
283 * @param[in] dir Communication direction
284 *
285 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
286 * Data stage with alternating toggle and direction supplied by parameter.
287 * Status stage with toggle 1 and direction supplied by parameter.
288 */
289static void batch_control(ohci_transfer_batch_t *ohci_batch)
290{
291 assert(ohci_batch);
292
293 usb_direction_t dir = ohci_batch->base.dir;
294 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
295
296 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.", ohci_batch->ed,
297 ohci_batch->ed->status, ohci_batch->ed->td_tail,
298 ohci_batch->ed->td_head, ohci_batch->ed->next);
299 static const usb_direction_t reverse_dir[] = {
300 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
301 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
302 };
303
304 int toggle = 0;
305 const char* buffer = ohci_batch->device_buffer;
306 const usb_direction_t data_dir = dir;
307 const usb_direction_t status_dir = reverse_dir[dir];
308
309 /* Setup stage */
310 td_init(
311 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
312 buffer, USB_SETUP_PACKET_SIZE, toggle);
313 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.",
314 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
315 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
316 buffer += USB_SETUP_PACKET_SIZE;
317
318 /* Data stage */
319 size_t td_current = 1;
320 size_t remain_size = ohci_batch->base.buffer_size;
321 while (remain_size > 0) {
322 const size_t transfer_size =
323 min(remain_size, OHCI_TD_MAX_TRANSFER);
324 toggle = 1 - toggle;
325
326 td_init(ohci_batch->tds[td_current],
327 ohci_batch->tds[td_current + 1],
328 data_dir, buffer, transfer_size, toggle);
329 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.",
330 ohci_batch->tds[td_current]->status,
331 ohci_batch->tds[td_current]->cbp,
332 ohci_batch->tds[td_current]->next,
333 ohci_batch->tds[td_current]->be);
334
335 buffer += transfer_size;
336 remain_size -= transfer_size;
337 assert(td_current < ohci_batch->td_count - 1);
338 ++td_current;
339 }
340
341 /* Status stage */
342 assert(td_current == ohci_batch->td_count - 1);
343 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
344 status_dir, NULL, 0, 1);
345 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.",
346 ohci_batch->tds[td_current]->status,
347 ohci_batch->tds[td_current]->cbp,
348 ohci_batch->tds[td_current]->next,
349 ohci_batch->tds[td_current]->be);
350 usb_log_debug2(
351 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.", \
352 &ohci_batch->base,
353 usb_str_transfer_type(ohci_batch->base.ep->transfer_type),
354 usb_str_direction(dir),
355 USB_TRANSFER_BATCH_ARGS(ohci_batch->base));
356}
357
358/** Prepare generic data transfer
359 *
360 * @param[in] ohci_batch Batch structure to use.
361 * @paramp[in] dir Communication direction.
362 *
363 * Direction is supplied by the associated ep and toggle is maintained by the
364 * OHCI hw in ED.
365 */
366static void batch_data(ohci_transfer_batch_t *ohci_batch)
367{
368 assert(ohci_batch);
369
370 usb_direction_t dir = ohci_batch->base.dir;
371 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
372 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.", ohci_batch->ed,
373 ohci_batch->ed->status, ohci_batch->ed->td_tail,
374 ohci_batch->ed->td_head, ohci_batch->ed->next);
375
376 size_t td_current = 0;
377 size_t remain_size = ohci_batch->base.buffer_size;
378 char *buffer = ohci_batch->device_buffer;
379 while (remain_size > 0) {
380 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
381 ? OHCI_TD_MAX_TRANSFER : remain_size;
382
383 td_init(
384 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
385 dir, buffer, transfer_size, -1);
386
387 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.",
388 ohci_batch->tds[td_current]->status,
389 ohci_batch->tds[td_current]->cbp,
390 ohci_batch->tds[td_current]->next,
391 ohci_batch->tds[td_current]->be);
392
393 buffer += transfer_size;
394 remain_size -= transfer_size;
395 assert(td_current < ohci_batch->td_count);
396 ++td_current;
397 }
398 usb_log_debug2(
399 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.", \
400 &ohci_batch->base,
401 usb_str_transfer_type(ohci_batch->base.ep->transfer_type),
402 usb_str_direction(dir),
403 USB_TRANSFER_BATCH_ARGS(ohci_batch->base));
404}
405
406/** Transfer setup table. */
407static void (*const batch_setup[])(ohci_transfer_batch_t*) =
408{
409 [USB_TRANSFER_CONTROL] = batch_control,
410 [USB_TRANSFER_BULK] = batch_data,
411 [USB_TRANSFER_INTERRUPT] = batch_data,
412 [USB_TRANSFER_ISOCHRONOUS] = NULL,
413};
414/**
415 * @}
416 */
Note: See TracBrowser for help on using the repository browser.