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

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

ohci: calloc parameter order fix

  • Property mode set to 100644
File size: 13.1 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/** @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
44void (*const batch_setup[4][3])(ohci_transfer_batch_t*);
45/*----------------------------------------------------------------------------*/
46/** Safely destructs ohci_transfer_batch_t structure
47 *
48 * @param[in] ohci_batch Instance to destroy.
49 */
50static 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/*----------------------------------------------------------------------------*/
67void 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/*----------------------------------------------------------------------------*/
77ohci_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...) \
81if (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 assert(
139 batch_setup[usb_batch->ep->transfer_type][usb_batch->ep->direction]);
140 batch_setup[usb_batch->ep->transfer_type][usb_batch->ep->direction](
141 ohci_batch);
142
143 return ohci_batch;
144#undef CHECK_NULL_DISPOSE_RET
145}
146/*----------------------------------------------------------------------------*/
147/** Check batch TDs' status.
148 *
149 * @param[in] ohci_batch Batch structure to use.
150 * @return False, if there is an active TD, true otherwise.
151 *
152 * Walk all TDs (usually there is just one). Stop with false if there is an
153 * active TD. Stop with true if an error is found. Return true if the walk
154 * completes with the last TD.
155 */
156bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *ohci_batch)
157{
158 assert(ohci_batch);
159 assert(ohci_batch->usb_batch);
160
161 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
162 ohci_batch->usb_batch, ohci_batch->td_count);
163 usb_log_debug2("ED: %x:%x:%x:%x.\n",
164 ohci_batch->ed->status, ohci_batch->ed->td_head,
165 ohci_batch->ed->td_tail, ohci_batch->ed->next);
166
167 size_t i = 0;
168 for (; i < ohci_batch->td_count; ++i) {
169 assert(ohci_batch->tds[i] != NULL);
170 usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
171 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
172 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
173 if (!td_is_finished(ohci_batch->tds[i])) {
174 return false;
175 }
176 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
177 if (ohci_batch->usb_batch->error != EOK) {
178 usb_log_debug("Batch %p found error TD(%zu):%x.\n",
179 ohci_batch->usb_batch, i,
180 ohci_batch->tds[i]->status);
181 /* Make sure TD queue is empty (one TD),
182 * ED should be marked as halted */
183 ohci_batch->ed->td_tail =
184 (ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK);
185 ++i;
186 break;
187 }
188 }
189
190 assert(i <= ohci_batch->td_count);
191 ohci_batch->leave_td = i;
192
193 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
194 assert(ohci_ep);
195 ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
196 assert(i > 0);
197 ohci_batch->usb_batch->transfered_size =
198 ohci_batch->usb_batch->buffer_size;
199 for (--i;i < ohci_batch->td_count; ++i)
200 ohci_batch->usb_batch->transfered_size
201 -= td_remain_size(ohci_batch->tds[i]);
202
203 /* Clear possible ED HALT */
204 ohci_batch->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
205 /* just make sure that we are leaving the right TD behind */
206 const uint32_t pa = addr_to_phys(ohci_ep->td);
207 assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
208 assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
209
210 return true;
211}
212/*----------------------------------------------------------------------------*/
213/** Starts execution of the TD list
214 *
215 * @param[in] ohci_batch Batch structure to use
216 */
217void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
218{
219 assert(ohci_batch);
220 ed_set_end_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
221}
222/*----------------------------------------------------------------------------*/
223/** Prepare generic control transfer
224 *
225 * @param[in] ohci_batch Batch structure to use.
226 * @param[in] data_dir Direction to use for data stage.
227 * @param[in] status_dir Direction to use for status stage.
228 *
229 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
230 * Data stage with alternating toggle and direction supplied by parameter.
231 * Status stage with toggle 1 and direction supplied by parameter.
232 */
233static void batch_control(ohci_transfer_batch_t *ohci_batch,
234 usb_direction_t data_dir, usb_direction_t status_dir)
235{
236 assert(ohci_batch);
237 assert(ohci_batch->usb_batch);
238 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", ohci_batch->ed,
239 ohci_batch->ed->status, ohci_batch->ed->td_tail,
240 ohci_batch->ed->td_head, ohci_batch->ed->next);
241
242 int toggle = 0;
243 char* buffer = ohci_batch->device_buffer;
244
245 /* setup stage */
246 td_init(ohci_batch->tds[0], USB_DIRECTION_BOTH, buffer,
247 ohci_batch->usb_batch->setup_size, toggle);
248 td_set_next(ohci_batch->tds[0], ohci_batch->tds[1]);
249 usb_log_debug("Created CONTROL SETUP TD: %x:%x:%x:%x.\n",
250 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
251 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
252 buffer += ohci_batch->usb_batch->setup_size;
253
254 /* data stage */
255 size_t td_current = 1;
256 size_t remain_size = ohci_batch->usb_batch->buffer_size;
257 while (remain_size > 0) {
258 const size_t transfer_size =
259 remain_size > OHCI_TD_MAX_TRANSFER ?
260 OHCI_TD_MAX_TRANSFER : remain_size;
261 toggle = 1 - toggle;
262
263 td_init(ohci_batch->tds[td_current], data_dir, buffer,
264 transfer_size, toggle);
265 td_set_next(ohci_batch->tds[td_current],
266 ohci_batch->tds[td_current + 1]);
267 usb_log_debug("Created CONTROL DATA TD: %x:%x:%x:%x.\n",
268 ohci_batch->tds[td_current]->status,
269 ohci_batch->tds[td_current]->cbp,
270 ohci_batch->tds[td_current]->next,
271 ohci_batch->tds[td_current]->be);
272
273 buffer += transfer_size;
274 remain_size -= transfer_size;
275 assert(td_current < ohci_batch->td_count - 1);
276 ++td_current;
277 }
278
279 /* status stage */
280 assert(td_current == ohci_batch->td_count - 1);
281 td_init(ohci_batch->tds[td_current], status_dir, NULL, 0, 1);
282 td_set_next(ohci_batch->tds[td_current],
283 ohci_batch->tds[td_current + 1]);
284 usb_log_debug("Created CONTROL STATUS TD: %x:%x:%x:%x.\n",
285 ohci_batch->tds[td_current]->status,
286 ohci_batch->tds[td_current]->cbp,
287 ohci_batch->tds[td_current]->next,
288 ohci_batch->tds[td_current]->be);
289}
290/*----------------------------------------------------------------------------*/
291/** Prepare generic data transfer
292 *
293 * @param[in] ohci_batch Batch structure to use.
294 *
295 * Direction is supplied by the associated ep and toggle is maintained by the
296 * OHCI hw in ED.
297 */
298static void batch_data(ohci_transfer_batch_t *ohci_batch)
299{
300 assert(ohci_batch);
301 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", ohci_batch->ed,
302 ohci_batch->ed->status, ohci_batch->ed->td_tail,
303 ohci_batch->ed->td_head, ohci_batch->ed->next);
304
305 const usb_direction_t direction = ohci_batch->usb_batch->ep->direction;
306 size_t td_current = 0;
307 size_t remain_size = ohci_batch->usb_batch->buffer_size;
308 char *buffer = ohci_batch->device_buffer;
309 while (remain_size > 0) {
310 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
311 ? OHCI_TD_MAX_TRANSFER : remain_size;
312
313 td_init(ohci_batch->tds[td_current], direction,
314 buffer, transfer_size, -1);
315 td_set_next(ohci_batch->tds[td_current],
316 ohci_batch->tds[td_current + 1]);
317 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
318 ohci_batch->tds[td_current]->status,
319 ohci_batch->tds[td_current]->cbp,
320 ohci_batch->tds[td_current]->next,
321 ohci_batch->tds[td_current]->be);
322
323 buffer += transfer_size;
324 remain_size -= transfer_size;
325 assert(td_current < ohci_batch->td_count);
326 ++td_current;
327 }
328 usb_log_debug2(
329 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
330 ohci_batch->usb_batch,
331 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
332 usb_str_direction(ohci_batch->usb_batch->ep->direction),
333 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
334}
335/*----------------------------------------------------------------------------*/
336static void setup_control(ohci_transfer_batch_t *ohci_batch)
337{
338 // TODO Find a better way to do this
339 /* Check first bit of the first setup request byte
340 * (it signals hc-> dev or dev->hc communication) */
341 const char *direction;
342 if (ohci_batch->device_buffer[0] & (1 << 7)) {
343 batch_control(ohci_batch, USB_DIRECTION_IN, USB_DIRECTION_OUT);
344 direction = "read";
345 } else {
346 batch_control(ohci_batch, USB_DIRECTION_OUT, USB_DIRECTION_IN);
347 direction = "write";
348 }
349 usb_log_debug2(
350 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
351 ohci_batch->usb_batch,
352 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
353 direction, USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
354}
355/*----------------------------------------------------------------------------*/
356void (*const batch_setup[4][3])(ohci_transfer_batch_t*) =
357{
358 { NULL, NULL, setup_control },
359 { NULL, NULL, NULL },
360 { batch_data, batch_data, NULL },
361 { batch_data, batch_data, NULL },
362};
363/**
364 * @}
365 */
Note: See TracBrowser for help on using the repository browser.