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
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 "batch.h"
41#include "hcd_endpoint.h"
42#include "utils/malloc32.h"
43#include "hw_struct/endpoint_descriptor.h"
44#include "hw_struct/transfer_descriptor.h"
45
46/** OHCI specific data required for USB transfer */
47typedef struct ohci_transfer_batch {
48 /** Endpoint descriptor of the target endpoint. */
49 ed_t *ed;
50 /** List of TDs needed for the transfer */
51 td_t **tds;
52 /** Number of TDs used by the transfer */
53 size_t td_count;
54 /** Dummy TD to be left at the ED and used by the next transfer */
55 size_t leave_td;
56 /** Data buffer, must be accessible byb the OHCI hw. */
57 void *device_buffer;
58} ohci_transfer_batch_t;
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 */
68static void ohci_transfer_batch_dispose(void *ohci_batch)
69{
70 ohci_transfer_batch_t *instance = ohci_batch;
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);
83}
84/*----------------------------------------------------------------------------*/
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 */
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,
103 usbhc_iface_transfer_in_callback_t func_in,
104 usbhc_iface_transfer_out_callback_t func_out, void *arg)
105{
106#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
107 if (ptr == NULL) { \
108 usb_log_error(message); \
109 if (instance) { \
110 usb_transfer_batch_dispose(instance); \
111 } \
112 return NULL; \
113 } else (void)0
114
115 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
116 CHECK_NULL_DISPOSE_RETURN(instance,
117 "Failed to allocate batch instance.\n");
118 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
119 NULL, setup_size, func_in, func_out, arg, fun, NULL,
120 ohci_transfer_batch_dispose);
121
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);
126 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
127 instance->private_data = data;
128
129 data->td_count =
130 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
131 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
132 data->td_count += 2;
133 }
134
135 /* We need an extra place for TD that is currently assigned to hcd_ep*/
136 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
137 CHECK_NULL_DISPOSE_RETURN(data->tds,
138 "Failed to allocate transfer descriptors.\n");
139
140 /* Add TD left over by the previous transfer */
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;
151
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)*/
156 if (setup_size + buffer_size > 0) {
157 data->device_buffer = malloc32(setup_size + buffer_size);
158 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
159 "Failed to allocate device accessible buffer.\n");
160 instance->setup_buffer = data->device_buffer;
161 instance->data_buffer = data->device_buffer + setup_size;
162 memcpy(instance->setup_buffer, setup_buffer, setup_size);
163 }
164
165 return instance;
166}
167/*----------------------------------------------------------------------------*/
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 */
177bool batch_is_complete(usb_transfer_batch_t *instance)
178{
179 assert(instance);
180 ohci_transfer_batch_t *data = instance->private_data;
181 assert(data);
182 size_t tds = data->td_count;
183 usb_log_debug("Batch(%p) checking %zu td(s) for completion.\n",
184 instance, tds);
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);
188 size_t i = 0;
189 instance->transfered_size = instance->buffer_size;
190 for (; i < tds; ++i) {
191 assert(data->tds[i] != NULL);
192 usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
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])) {
196 return false;
197 }
198 instance->error = td_error(data->tds[i]);
199 if (instance->error != EOK) {
200 usb_log_debug("Batch(%p) found error TD(%zu):%x.\n",
201 instance, i, data->tds[i]->status);
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;
207 break;
208 }
209 }
210 data->leave_td = i;
211 assert(data->leave_td <= data->td_count);
212 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
213 assert(hcd_ep);
214 hcd_ep->td = data->tds[i];
215 if (i > 0)
216 instance->transfered_size -= td_remain_size(data->tds[i - 1]);
217
218 /* Clear possible ED HALT */
219 data->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
220 const uint32_t pa = addr_to_phys(hcd_ep->td);
221 assert(pa == (data->ed->td_head & ED_TDHEAD_PTR_MASK));
222 assert(pa == (data->ed->td_tail & ED_TDTAIL_PTR_MASK));
223
224 return true;
225}
226/*----------------------------------------------------------------------------*/
227/** Starts execution of the TD list
228 *
229 * @param[in] instance Batch structure to use
230 */
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/*----------------------------------------------------------------------------*/
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 */
246void batch_control_write(usb_transfer_batch_t *instance)
247{
248 assert(instance);
249 /* We are data out, we are supposed to provide data */
250 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
251 instance->next_step = usb_transfer_batch_call_out_and_dispose;
252 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
253 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
254}
255/*----------------------------------------------------------------------------*/
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 */
263void batch_control_read(usb_transfer_batch_t *instance)
264{
265 assert(instance);
266 instance->next_step = usb_transfer_batch_call_in_and_dispose;
267 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
268 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
269}
270/*----------------------------------------------------------------------------*/
271/** Prepare interrupt in transfer.
272 *
273 * @param[in] instance Batch structure to use.
274 *
275 * Data transfer.
276 */
277void batch_interrupt_in(usb_transfer_batch_t *instance)
278{
279 assert(instance);
280 instance->next_step = usb_transfer_batch_call_in_and_dispose;
281 batch_data(instance);
282 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
283}
284/*----------------------------------------------------------------------------*/
285/** Prepare interrupt out transfer.
286 *
287 * @param[in] instance Batch structure to use.
288 *
289 * Data transfer.
290 */
291void batch_interrupt_out(usb_transfer_batch_t *instance)
292{
293 assert(instance);
294 /* We are data out, we are supposed to provide data */
295 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
296 instance->next_step = usb_transfer_batch_call_out_and_dispose;
297 batch_data(instance);
298 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
299}
300/*----------------------------------------------------------------------------*/
301/** Prepare bulk in transfer.
302 *
303 * @param[in] instance Batch structure to use.
304 *
305 * Data transfer.
306 */
307void batch_bulk_in(usb_transfer_batch_t *instance)
308{
309 assert(instance);
310 instance->next_step = usb_transfer_batch_call_in_and_dispose;
311 batch_data(instance);
312 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
313}
314/*----------------------------------------------------------------------------*/
315/** Prepare bulk out transfer.
316 *
317 * @param[in] instance Batch structure to use.
318 *
319 * Data transfer.
320 */
321void batch_bulk_out(usb_transfer_batch_t *instance)
322{
323 assert(instance);
324 /* We are data out, we are supposed to provide data */
325 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
326 instance->next_step = usb_transfer_batch_call_out_and_dispose;
327 batch_data(instance);
328 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
329}
330/*----------------------------------------------------------------------------*/
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 */
341void batch_control(usb_transfer_batch_t *instance,
342 usb_direction_t data_dir, usb_direction_t status_dir)
343{
344 assert(instance);
345 ohci_transfer_batch_t *data = instance->private_data;
346 assert(data);
347 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
348 data->ed->status, data->ed->td_tail, data->ed->td_head,
349 data->ed->next);
350 int toggle = 0;
351 /* setup stage */
352 td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
353 instance->setup_size, toggle);
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);
357
358 /* data stage */
359 size_t td_current = 1;
360 size_t remain_size = instance->buffer_size;
361 char *buffer = instance->data_buffer;
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
367 td_init(data->tds[td_current], data_dir, buffer,
368 transfer_size, toggle);
369 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
370 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
371 data->tds[td_current]->status, data->tds[td_current]->cbp,
372 data->tds[td_current]->next, data->tds[td_current]->be);
373
374 buffer += transfer_size;
375 remain_size -= transfer_size;
376 assert(td_current < data->td_count - 1);
377 ++td_current;
378 }
379
380 /* status stage */
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]);
384 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
385 data->tds[td_current]->status, data->tds[td_current]->cbp,
386 data->tds[td_current]->next, data->tds[td_current]->be);
387}
388/*----------------------------------------------------------------------------*/
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 */
396void batch_data(usb_transfer_batch_t *instance)
397{
398 assert(instance);
399 ohci_transfer_batch_t *data = instance->private_data;
400 assert(data);
401 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
402 data->ed->status, data->ed->td_tail, data->ed->td_head,
403 data->ed->next);
404
405 size_t td_current = 0;
406 size_t remain_size = instance->buffer_size;
407 char *buffer = instance->data_buffer;
408 while (remain_size > 0) {
409 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
410 OHCI_TD_MAX_TRANSFER : remain_size;
411
412 td_init(data->tds[td_current], instance->ep->direction,
413 buffer, transfer_size, -1);
414 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
415 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
416 data->tds[td_current]->status, data->tds[td_current]->cbp,
417 data->tds[td_current]->next, data->tds[td_current]->be);
418
419 buffer += transfer_size;
420 remain_size -= transfer_size;
421 assert(td_current < data->td_count);
422 ++td_current;
423 }
424}
425/**
426 * @}
427 */
Note: See TracBrowser for help on using the repository browser.