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 | /** @addtogroup drvusbohci
|
---|
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 "ohci_batch.h"
|
---|
41 | #include "ohci_endpoint.h"
|
---|
42 | #include "utils/malloc32.h"
|
---|
43 |
|
---|
44 | static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t);
|
---|
45 | /*----------------------------------------------------------------------------*/
|
---|
46 | /** Safely destructs ohci_transfer_batch_t structure
|
---|
47 | *
|
---|
48 | * @param[in] ohci_batch Instance to destroy.
|
---|
49 | */
|
---|
50 | static void ohci_transfer_batch_dispose(ohci_transfer_batch_t *ohci_batch)
|
---|
51 | {
|
---|
52 | if (!ohci_batch)
|
---|
53 | return;
|
---|
54 | unsigned i = 0;
|
---|
55 | if (ohci_batch->tds) {
|
---|
56 | for (; i< ohci_batch->td_count; ++i) {
|
---|
57 | if (i != ohci_batch->leave_td)
|
---|
58 | free32(ohci_batch->tds[i]);
|
---|
59 | }
|
---|
60 | free(ohci_batch->tds);
|
---|
61 | }
|
---|
62 | usb_transfer_batch_dispose(ohci_batch->usb_batch);
|
---|
63 | free32(ohci_batch->device_buffer);
|
---|
64 | free(ohci_batch);
|
---|
65 | }
|
---|
66 | /*----------------------------------------------------------------------------*/
|
---|
67 | void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
|
---|
68 | {
|
---|
69 | assert(ohci_batch);
|
---|
70 | assert(ohci_batch->usb_batch);
|
---|
71 | usb_transfer_batch_finish(ohci_batch->usb_batch,
|
---|
72 | ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size,
|
---|
73 | ohci_batch->usb_batch->buffer_size);
|
---|
74 | ohci_transfer_batch_dispose(ohci_batch);
|
---|
75 | }
|
---|
76 | /*----------------------------------------------------------------------------*/
|
---|
77 | ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
|
---|
78 | {
|
---|
79 | assert(usb_batch);
|
---|
80 | #define CHECK_NULL_DISPOSE_RET(ptr, message...) \
|
---|
81 | if (ptr == NULL) { \
|
---|
82 | usb_log_error(message); \
|
---|
83 | ohci_transfer_batch_dispose(ohci_batch); \
|
---|
84 | return NULL; \
|
---|
85 | } else (void)0
|
---|
86 |
|
---|
87 | ohci_transfer_batch_t *ohci_batch =
|
---|
88 | calloc(1, sizeof(ohci_transfer_batch_t));
|
---|
89 | CHECK_NULL_DISPOSE_RET(ohci_batch,
|
---|
90 | "Failed to allocate OHCI batch data.\n");
|
---|
91 | link_initialize(&ohci_batch->link);
|
---|
92 | ohci_batch->td_count =
|
---|
93 | (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
|
---|
94 | / OHCI_TD_MAX_TRANSFER;
|
---|
95 | /* Control transfer need Setup and Status stage */
|
---|
96 | if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
97 | ohci_batch->td_count += 2;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* We need an extra place for TD that was left at ED */
|
---|
101 | ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
|
---|
102 | CHECK_NULL_DISPOSE_RET(ohci_batch->tds,
|
---|
103 | "Failed to allocate OHCI transfer descriptors.\n");
|
---|
104 |
|
---|
105 | /* Add TD left over by the previous transfer */
|
---|
106 | ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
|
---|
107 | ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
|
---|
108 | ohci_batch->leave_td = 0;
|
---|
109 | unsigned i = 1;
|
---|
110 | for (; i <= ohci_batch->td_count; ++i) {
|
---|
111 | ohci_batch->tds[i] = malloc32(sizeof(td_t));
|
---|
112 | CHECK_NULL_DISPOSE_RET(ohci_batch->tds[i],
|
---|
113 | "Failed to allocate TD %d.\n", i );
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
|
---|
118 | * it is, however, not capable of handling buffer that occupies more
|
---|
119 | * than two pages (the first page is computed using start pointer, the
|
---|
120 | * other using the end pointer) */
|
---|
121 | if (usb_batch->setup_size + usb_batch->buffer_size > 0) {
|
---|
122 | /* Use one buffer for setup and data stage */
|
---|
123 | ohci_batch->device_buffer =
|
---|
124 | malloc32(usb_batch->setup_size + usb_batch->buffer_size);
|
---|
125 | CHECK_NULL_DISPOSE_RET(ohci_batch->device_buffer,
|
---|
126 | "Failed to allocate device accessible buffer.\n");
|
---|
127 | /* Copy setup data */
|
---|
128 | memcpy(ohci_batch->device_buffer, usb_batch->setup_buffer,
|
---|
129 | usb_batch->setup_size);
|
---|
130 | /* Copy generic data */
|
---|
131 | if (usb_batch->ep->direction != USB_DIRECTION_IN)
|
---|
132 | memcpy(
|
---|
133 | ohci_batch->device_buffer + usb_batch->setup_size,
|
---|
134 | usb_batch->buffer, usb_batch->buffer_size);
|
---|
135 | }
|
---|
136 | ohci_batch->usb_batch = usb_batch;
|
---|
137 |
|
---|
138 | const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
|
---|
139 | assert(batch_setup[usb_batch->ep->transfer_type]);
|
---|
140 | batch_setup[usb_batch->ep->transfer_type](ohci_batch, dir);
|
---|
141 |
|
---|
142 | return ohci_batch;
|
---|
143 | #undef CHECK_NULL_DISPOSE_RET
|
---|
144 | }
|
---|
145 | /*----------------------------------------------------------------------------*/
|
---|
146 | /** Check batch TDs' status.
|
---|
147 | *
|
---|
148 | * @param[in] ohci_batch Batch structure to use.
|
---|
149 | * @return False, if there is an active TD, true otherwise.
|
---|
150 | *
|
---|
151 | * Walk all TDs (usually there is just one). Stop with false if there is an
|
---|
152 | * active TD. Stop with true if an error is found. Return true if the walk
|
---|
153 | * completes with the last TD.
|
---|
154 | */
|
---|
155 | bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *ohci_batch)
|
---|
156 | {
|
---|
157 | assert(ohci_batch);
|
---|
158 | assert(ohci_batch->usb_batch);
|
---|
159 |
|
---|
160 | usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
|
---|
161 | ohci_batch->usb_batch, ohci_batch->td_count);
|
---|
162 | usb_log_debug2("ED: %x:%x:%x:%x.\n",
|
---|
163 | ohci_batch->ed->status, ohci_batch->ed->td_head,
|
---|
164 | ohci_batch->ed->td_tail, ohci_batch->ed->next);
|
---|
165 |
|
---|
166 | size_t i = 0;
|
---|
167 | for (; i < ohci_batch->td_count; ++i) {
|
---|
168 | assert(ohci_batch->tds[i] != NULL);
|
---|
169 | usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
|
---|
170 | ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
|
---|
171 | ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
|
---|
172 | if (!td_is_finished(ohci_batch->tds[i])) {
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 | ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
|
---|
176 | if (ohci_batch->usb_batch->error != EOK) {
|
---|
177 | usb_log_debug("Batch %p found error TD(%zu):%x.\n",
|
---|
178 | ohci_batch->usb_batch, i,
|
---|
179 | ohci_batch->tds[i]->status);
|
---|
180 | /* Make sure TD queue is empty (one TD),
|
---|
181 | * ED should be marked as halted */
|
---|
182 | ohci_batch->ed->td_tail =
|
---|
183 | (ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK);
|
---|
184 | ++i;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | assert(i <= ohci_batch->td_count);
|
---|
190 | ohci_batch->leave_td = i;
|
---|
191 |
|
---|
192 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
|
---|
193 | assert(ohci_ep);
|
---|
194 | ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
|
---|
195 | assert(i > 0);
|
---|
196 | ohci_batch->usb_batch->transfered_size =
|
---|
197 | ohci_batch->usb_batch->buffer_size;
|
---|
198 | for (--i;i < ohci_batch->td_count; ++i)
|
---|
199 | ohci_batch->usb_batch->transfered_size
|
---|
200 | -= td_remain_size(ohci_batch->tds[i]);
|
---|
201 |
|
---|
202 | /* Clear possible ED HALT */
|
---|
203 | ohci_batch->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
|
---|
204 | /* just make sure that we are leaving the right TD behind */
|
---|
205 | const uint32_t pa = addr_to_phys(ohci_ep->td);
|
---|
206 | assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
|
---|
207 | assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
|
---|
208 |
|
---|
209 | return true;
|
---|
210 | }
|
---|
211 | /*----------------------------------------------------------------------------*/
|
---|
212 | /** Starts execution of the TD list
|
---|
213 | *
|
---|
214 | * @param[in] ohci_batch Batch structure to use
|
---|
215 | */
|
---|
216 | void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
|
---|
217 | {
|
---|
218 | assert(ohci_batch);
|
---|
219 | ed_set_end_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
|
---|
220 | }
|
---|
221 | /*----------------------------------------------------------------------------*/
|
---|
222 | /** Prepare generic control transfer
|
---|
223 | *
|
---|
224 | * @param[in] ohci_batch Batch structure to use.
|
---|
225 | * @param[in] dir Communication direction
|
---|
226 | *
|
---|
227 | * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
|
---|
228 | * Data stage with alternating toggle and direction supplied by parameter.
|
---|
229 | * Status stage with toggle 1 and direction supplied by parameter.
|
---|
230 | */
|
---|
231 | static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
|
---|
232 | {
|
---|
233 | assert(ohci_batch);
|
---|
234 | assert(ohci_batch->usb_batch);
|
---|
235 | assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
|
---|
236 | usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", ohci_batch->ed,
|
---|
237 | ohci_batch->ed->status, ohci_batch->ed->td_tail,
|
---|
238 | ohci_batch->ed->td_head, ohci_batch->ed->next);
|
---|
239 | static const usb_direction_t reverse_dir[] = {
|
---|
240 | [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
|
---|
241 | [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
|
---|
242 | };
|
---|
243 |
|
---|
244 | int toggle = 0;
|
---|
245 | const char* buffer = ohci_batch->device_buffer;
|
---|
246 | const usb_direction_t data_dir = dir;
|
---|
247 | const usb_direction_t status_dir = reverse_dir[dir];
|
---|
248 |
|
---|
249 | /* setup stage */
|
---|
250 | td_init(ohci_batch->tds[0], USB_DIRECTION_BOTH, buffer,
|
---|
251 | ohci_batch->usb_batch->setup_size, toggle);
|
---|
252 | td_set_next(ohci_batch->tds[0], ohci_batch->tds[1]);
|
---|
253 | usb_log_debug("Created CONTROL SETUP TD: %x:%x:%x:%x.\n",
|
---|
254 | ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
|
---|
255 | ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
|
---|
256 | buffer += ohci_batch->usb_batch->setup_size;
|
---|
257 |
|
---|
258 | /* data stage */
|
---|
259 | size_t td_current = 1;
|
---|
260 | size_t remain_size = ohci_batch->usb_batch->buffer_size;
|
---|
261 | while (remain_size > 0) {
|
---|
262 | const size_t transfer_size =
|
---|
263 | remain_size > OHCI_TD_MAX_TRANSFER ?
|
---|
264 | OHCI_TD_MAX_TRANSFER : remain_size;
|
---|
265 | toggle = 1 - toggle;
|
---|
266 |
|
---|
267 | td_init(ohci_batch->tds[td_current], data_dir, buffer,
|
---|
268 | transfer_size, toggle);
|
---|
269 | td_set_next(ohci_batch->tds[td_current],
|
---|
270 | ohci_batch->tds[td_current + 1]);
|
---|
271 | usb_log_debug("Created CONTROL DATA TD: %x:%x:%x:%x.\n",
|
---|
272 | ohci_batch->tds[td_current]->status,
|
---|
273 | ohci_batch->tds[td_current]->cbp,
|
---|
274 | ohci_batch->tds[td_current]->next,
|
---|
275 | ohci_batch->tds[td_current]->be);
|
---|
276 |
|
---|
277 | buffer += transfer_size;
|
---|
278 | remain_size -= transfer_size;
|
---|
279 | assert(td_current < ohci_batch->td_count - 1);
|
---|
280 | ++td_current;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* status stage */
|
---|
284 | assert(td_current == ohci_batch->td_count - 1);
|
---|
285 | td_init(ohci_batch->tds[td_current], status_dir, NULL, 0, 1);
|
---|
286 | td_set_next(ohci_batch->tds[td_current],
|
---|
287 | ohci_batch->tds[td_current + 1]);
|
---|
288 | usb_log_debug("Created CONTROL STATUS TD: %x:%x:%x:%x.\n",
|
---|
289 | ohci_batch->tds[td_current]->status,
|
---|
290 | ohci_batch->tds[td_current]->cbp,
|
---|
291 | ohci_batch->tds[td_current]->next,
|
---|
292 | ohci_batch->tds[td_current]->be);
|
---|
293 | usb_log_debug2(
|
---|
294 | "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
|
---|
295 | ohci_batch->usb_batch,
|
---|
296 | usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
|
---|
297 | usb_str_direction(dir),
|
---|
298 | USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
|
---|
299 | }
|
---|
300 | /*----------------------------------------------------------------------------*/
|
---|
301 | /** Prepare generic data transfer
|
---|
302 | *
|
---|
303 | * @param[in] ohci_batch Batch structure to use.
|
---|
304 | * @paramp[in] dir Communication direction.
|
---|
305 | *
|
---|
306 | * Direction is supplied by the associated ep and toggle is maintained by the
|
---|
307 | * OHCI hw in ED.
|
---|
308 | */
|
---|
309 | static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
|
---|
310 | {
|
---|
311 | assert(ohci_batch);
|
---|
312 | assert(ohci_batch->usb_batch);
|
---|
313 | assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
|
---|
314 | usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", ohci_batch->ed,
|
---|
315 | ohci_batch->ed->status, ohci_batch->ed->td_tail,
|
---|
316 | ohci_batch->ed->td_head, ohci_batch->ed->next);
|
---|
317 |
|
---|
318 | size_t td_current = 0;
|
---|
319 | size_t remain_size = ohci_batch->usb_batch->buffer_size;
|
---|
320 | char *buffer = ohci_batch->device_buffer;
|
---|
321 | while (remain_size > 0) {
|
---|
322 | const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
|
---|
323 | ? OHCI_TD_MAX_TRANSFER : remain_size;
|
---|
324 |
|
---|
325 | td_init(ohci_batch->tds[td_current], dir, buffer,
|
---|
326 | transfer_size, -1);
|
---|
327 | td_set_next(ohci_batch->tds[td_current],
|
---|
328 | ohci_batch->tds[td_current + 1]);
|
---|
329 |
|
---|
330 | usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
|
---|
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);
|
---|
339 | ++td_current;
|
---|
340 | }
|
---|
341 | usb_log_debug2(
|
---|
342 | "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
|
---|
343 | ohci_batch->usb_batch,
|
---|
344 | usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
|
---|
345 | usb_str_direction(dir),
|
---|
346 | USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
|
---|
347 | }
|
---|
348 | /*----------------------------------------------------------------------------*/
|
---|
349 | static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
|
---|
350 | {
|
---|
351 | [USB_TRANSFER_CONTROL] = batch_control,
|
---|
352 | [USB_TRANSFER_BULK] = batch_data,
|
---|
353 | [USB_TRANSFER_INTERRUPT] = batch_data,
|
---|
354 | [USB_TRANSFER_ISOCHRONOUS] = NULL,
|
---|
355 | };
|
---|
356 | /**
|
---|
357 | * @}
|
---|
358 | */
|
---|