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