source: mainline/uspace/drv/uhci-hcd/batch.c@ fcf07e6

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

Doxygen

  • Property mode set to 100644
File size: 15.3 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 drvusbuhcihc
29 * @{
30 */
31/** @file
32 * @brief UHCI 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 "transfer_list.h"
42#include "uhci_hc.h"
43#include "utils/malloc32.h"
44
45#define DEFAULT_ERROR_COUNT 3
46
47static void batch_control(batch_t *instance,
48 usb_packet_id data_stage, usb_packet_id status_stage);
49static void batch_data(batch_t *instance, usb_packet_id pid);
50static void batch_call_in(batch_t *instance);
51static void batch_call_out(batch_t *instance);
52static void batch_call_in_and_dispose(batch_t *instance);
53static void batch_call_out_and_dispose(batch_t *instance);
54
55
56/** Allocate memory and initialize internal data structure.
57 *
58 * @param[in] fun DDF function to pass to callback.
59 * @param[in] target Device and endpoint target of the transaction.
60 * @param[in] transfer_type Interrupt, Control or Bulk.
61 * @param[in] max_packet_size maximum allowed size of data packets.
62 * @param[in] speed Speed of the transaction.
63 * @param[in] buffer Data source/destination.
64 * @param[in] size Size of the buffer.
65 * @param[in] setup_buffer Setup data source (if not NULL)
66 * @param[in] setup_size Size of setup_buffer (should be always 8)
67 * @param[in] func_in function to call on inbound transaction completion
68 * @param[in] func_out function to call on outbound transaction completion
69 * @param[in] arg additional parameter to func_in or func_out
70 * @param[in] manager Pointer to toggle management structure.
71 * @return Valid pointer if all substructures were successfully created,
72 * NULL otherwise.
73 *
74 * Determines the number of needed packets (TDs). Prepares a transport buffer
75 * (that is accessible by the hardware). Initializes parameters needed for the
76 * transaction and callback.
77 */
78batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
79 usb_transfer_type_t transfer_type, size_t max_packet_size,
80 usb_speed_t speed, char *buffer, size_t size,
81 char* setup_buffer, size_t setup_size,
82 usbhc_iface_transfer_in_callback_t func_in,
83 usbhc_iface_transfer_out_callback_t func_out, void *arg,
84 device_keeper_t *manager
85 )
86{
87 assert(func_in == NULL || func_out == NULL);
88 assert(func_in != NULL || func_out != NULL);
89
90#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
91 if (ptr == NULL) { \
92 usb_log_error(message); \
93 if (instance) { \
94 batch_dispose(instance); \
95 } \
96 return NULL; \
97 } else (void)0
98
99 batch_t *instance = malloc(sizeof(batch_t));
100 CHECK_NULL_DISPOSE_RETURN(instance,
101 "Failed to allocate batch instance.\n");
102 bzero(instance, sizeof(batch_t));
103
104 instance->qh = malloc32(sizeof(qh_t));
105 CHECK_NULL_DISPOSE_RETURN(instance->qh,
106 "Failed to allocate batch queue head.\n");
107 qh_init(instance->qh);
108
109 instance->packets = (size + max_packet_size - 1) / max_packet_size;
110 if (transfer_type == USB_TRANSFER_CONTROL) {
111 instance->packets += 2;
112 }
113
114 instance->tds = malloc32(sizeof(td_t) * instance->packets);
115 CHECK_NULL_DISPOSE_RETURN(
116 instance->tds, "Failed to allocate transfer descriptors.\n");
117 bzero(instance->tds, sizeof(td_t) * instance->packets);
118
119 if (size > 0) {
120 instance->transport_buffer = malloc32(size);
121 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
122 "Failed to allocate device accessible buffer.\n");
123 }
124
125 if (setup_size > 0) {
126 instance->setup_buffer = malloc32(setup_size);
127 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
128 "Failed to allocate device accessible setup buffer.\n");
129 memcpy(instance->setup_buffer, setup_buffer, setup_size);
130 }
131
132
133 link_initialize(&instance->link);
134
135 instance->max_packet_size = max_packet_size;
136 instance->target = target;
137 instance->transfer_type = transfer_type;
138 instance->buffer = buffer;
139 instance->buffer_size = size;
140 instance->setup_size = setup_size;
141 instance->fun = fun;
142 instance->arg = arg;
143 instance->speed = speed;
144 instance->manager = manager;
145 instance->callback_out = func_out;
146 instance->callback_in = func_in;
147
148 qh_set_element_td(instance->qh, addr_to_phys(instance->tds));
149
150 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
151 instance, target.address, target.endpoint);
152 return instance;
153}
154/*----------------------------------------------------------------------------*/
155/** Check batch TDs for activity.
156 *
157 * @param[in] instance Batch structure to use.
158 * @return False, if there is an active TD, true otherwise.
159 *
160 * Walk all TDs. Stop with false if there is an active one (it is to be
161 * processed). Stop with true if an error is found. Return true if the last TS
162 * is reached.
163 */
164bool batch_is_complete(batch_t *instance)
165{
166 assert(instance);
167 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
168 instance, instance->packets);
169 instance->transfered_size = 0;
170 size_t i = 0;
171 for (;i < instance->packets; ++i) {
172 if (td_is_active(&instance->tds[i])) {
173 return false;
174 }
175
176 instance->error = td_status(&instance->tds[i]);
177 if (instance->error != EOK) {
178 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
179 instance, i, instance->tds[i].status);
180 td_print_status(&instance->tds[i]);
181
182 device_keeper_set_toggle(instance->manager,
183 instance->target, td_toggle(&instance->tds[i]));
184 if (i > 0)
185 goto substract_ret;
186 return true;
187 }
188
189 instance->transfered_size += td_act_size(&instance->tds[i]);
190 if (td_is_short(&instance->tds[i]))
191 goto substract_ret;
192 }
193substract_ret:
194 instance->transfered_size -= instance->setup_size;
195 return true;
196}
197/*----------------------------------------------------------------------------*/
198/** Prepares control write transaction.
199 *
200 * @param[in] instance Batch structure to use.
201 *
202 * Uses genercir control function with pids OUT and IN.
203 */
204void batch_control_write(batch_t *instance)
205{
206 assert(instance);
207 /* We are data out, we are supposed to provide data */
208 memcpy(instance->transport_buffer, instance->buffer,
209 instance->buffer_size);
210 batch_control(instance, USB_PID_OUT, USB_PID_IN);
211 instance->next_step = batch_call_out_and_dispose;
212 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
213}
214/*----------------------------------------------------------------------------*/
215/** Prepares control read transaction.
216 *
217 * @param[in] instance Batch structure to use.
218 *
219 * Uses generic control with pids IN and OUT.
220 */
221void batch_control_read(batch_t *instance)
222{
223 assert(instance);
224 batch_control(instance, USB_PID_IN, USB_PID_OUT);
225 instance->next_step = batch_call_in_and_dispose;
226 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
227}
228/*----------------------------------------------------------------------------*/
229/** Prepare interrupt in transaction.
230 *
231 * @param[in] instance Batch structure to use.
232 *
233 * Data transaction with PID_IN.
234 */
235void batch_interrupt_in(batch_t *instance)
236{
237 assert(instance);
238 batch_data(instance, USB_PID_IN);
239 instance->next_step = batch_call_in_and_dispose;
240 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
241}
242/*----------------------------------------------------------------------------*/
243/** Prepare interrupt out transaction.
244 *
245 * @param[in] instance Batch structure to use.
246 *
247 * Data transaction with PID_OUT.
248 */
249void batch_interrupt_out(batch_t *instance)
250{
251 assert(instance);
252 /* We are data out, we are supposed to provide data */
253 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
254 batch_data(instance, USB_PID_OUT);
255 instance->next_step = batch_call_out_and_dispose;
256 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
257}
258/*----------------------------------------------------------------------------*/
259/** Prepare bulk in transaction.
260 *
261 * @param[in] instance Batch structure to use.
262 *
263 * Data transaction with PID_IN.
264 */
265void batch_bulk_in(batch_t *instance)
266{
267 assert(instance);
268 batch_data(instance, USB_PID_IN);
269 instance->next_step = batch_call_in_and_dispose;
270 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
271}
272/*----------------------------------------------------------------------------*/
273/** Prepare bulk out transaction.
274 *
275 * @param[in] instance Batch structure to use.
276 *
277 * Data transaction with PID_OUT.
278 */
279void batch_bulk_out(batch_t *instance)
280{
281 assert(instance);
282 /* We are data out, we are supposed to provide data */
283 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
284 batch_data(instance, USB_PID_OUT);
285 instance->next_step = batch_call_out_and_dispose;
286 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
287}
288/*----------------------------------------------------------------------------*/
289/** Prepare generic data transaction
290 *
291 * @param[in] instance Batch structure to use.
292 * @param[in] pid to use for data packets.
293 *
294 * Packets with alternating toggle bit and supplied pid value.
295 * The last packet is marked with IOC flag.
296 */
297void batch_data(batch_t *instance, usb_packet_id pid)
298{
299 assert(instance);
300 const bool low_speed = instance->speed == USB_SPEED_LOW;
301 int toggle =
302 device_keeper_get_toggle(instance->manager, instance->target);
303 assert(toggle == 0 || toggle == 1);
304
305 size_t packet = 0;
306 size_t remain_size = instance->buffer_size;
307 while (remain_size > 0) {
308 char *data =
309 instance->transport_buffer + instance->buffer_size
310 - remain_size;
311
312 const size_t packet_size =
313 (instance->max_packet_size > remain_size) ?
314 remain_size : instance->max_packet_size;
315
316 td_t *next_packet = (packet + 1 < instance->packets)
317 ? &instance->tds[packet + 1] : NULL;
318
319 assert(packet < instance->packets);
320 assert(packet_size <= remain_size);
321
322 td_init(
323 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
324 toggle, false, low_speed, instance->target, pid, data,
325 next_packet);
326
327
328 toggle = 1 - toggle;
329 remain_size -= packet_size;
330 ++packet;
331 }
332 td_set_ioc(&instance->tds[packet - 1]);
333 device_keeper_set_toggle(instance->manager, instance->target, toggle);
334}
335/*----------------------------------------------------------------------------*/
336/** Prepare generic control transaction
337 *
338 * @param[in] instance Batch structure to use.
339 * @param[in] data_stage to use for data packets.
340 * @param[in] status_stage to use for data packets.
341 *
342 * Setup stage with toggle 0 and USB_PID_SETUP.
343 * Data stage with alternating toggle and pid supplied by parameter.
344 * Status stage with toggle 1 and pid supplied by parameter.
345 * The last packet is marked with IOC.
346 */
347void batch_control(batch_t *instance,
348 usb_packet_id data_stage, usb_packet_id status_stage)
349{
350 assert(instance);
351
352 const bool low_speed = instance->speed == USB_SPEED_LOW;
353 int toggle = 0;
354 /* setup stage */
355 td_init(instance->tds, DEFAULT_ERROR_COUNT,
356 instance->setup_size, toggle, false, low_speed, instance->target,
357 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
358
359 /* data stage */
360 size_t packet = 1;
361 size_t remain_size = instance->buffer_size;
362 while (remain_size > 0) {
363 char *data =
364 instance->transport_buffer + instance->buffer_size
365 - remain_size;
366
367 toggle = 1 - toggle;
368
369 const size_t packet_size =
370 (instance->max_packet_size > remain_size) ?
371 remain_size : instance->max_packet_size;
372
373 td_init(
374 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
375 toggle, false, low_speed, instance->target, data_stage,
376 data, &instance->tds[packet + 1]);
377
378 ++packet;
379 assert(packet < instance->packets);
380 assert(packet_size <= remain_size);
381 remain_size -= packet_size;
382 }
383
384 /* status stage */
385 assert(packet == instance->packets - 1);
386 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
387 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
388
389 td_set_ioc(&instance->tds[packet]);
390 usb_log_debug2("Control last TD status: %x.\n",
391 instance->tds[packet].status);
392}
393/*----------------------------------------------------------------------------*/
394/** Prepare data, get error status and call callback in.
395 *
396 * @param[in] instance Batch structure to use.
397 * Copies data from transport buffer, and calls callback with appropriate
398 * parameters.
399 */
400void batch_call_in(batch_t *instance)
401{
402 assert(instance);
403 assert(instance->callback_in);
404
405 /* We are data in, we need data */
406 memcpy(instance->buffer, instance->transport_buffer,
407 instance->buffer_size);
408
409 int err = instance->error;
410 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
411 instance, instance->transfer_type, str_error(err), err,
412 instance->transfered_size);
413
414 instance->callback_in(
415 instance->fun, err, instance->transfered_size, instance->arg);
416}
417/*----------------------------------------------------------------------------*/
418/** Get error status and call callback out.
419 *
420 * @param[in] instance Batch structure to use.
421 */
422void batch_call_out(batch_t *instance)
423{
424 assert(instance);
425 assert(instance->callback_out);
426
427 int err = instance->error;
428 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
429 instance, instance->transfer_type, str_error(err), err);
430 instance->callback_out(instance->fun,
431 err, instance->arg);
432}
433/*----------------------------------------------------------------------------*/
434/** Helper function calls callback and correctly disposes of batch structure.
435 *
436 * @param[in] instance Batch structure to use.
437 */
438void batch_call_in_and_dispose(batch_t *instance)
439{
440 assert(instance);
441 batch_call_in(instance);
442 batch_dispose(instance);
443}
444/*----------------------------------------------------------------------------*/
445/** Helper function calls callback and correctly disposes of batch structure.
446 *
447 * @param[in] instance Batch structure to use.
448 */
449void batch_call_out_and_dispose(batch_t *instance)
450{
451 assert(instance);
452 batch_call_out(instance);
453 batch_dispose(instance);
454}
455/*----------------------------------------------------------------------------*/
456/** Correctly dispose all used data structures.
457 *
458 * @param[in] instance Batch structure to use.
459 */
460void batch_dispose(batch_t *instance)
461{
462 assert(instance);
463 usb_log_debug("Batch(%p) disposing.\n", instance);
464 /* free32 is NULL safe */
465 free32(instance->tds);
466 free32(instance->qh);
467 free32(instance->setup_buffer);
468 free32(instance->transport_buffer);
469 free(instance);
470}
471/**
472 * @}
473 */
Note: See TracBrowser for help on using the repository browser.