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

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

Hw error handling.

  • Property mode set to 100644
File size: 15.6 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/** Mark batch as failed and continue with next step.
156 *
157 * @param[in] instance Batch structure to use.
158 *
159 */
160void batch_abort(batch_t *instance)
161{
162 assert(instance);
163 instance->error = EIO;
164 instance->next_step(instance);
165}
166/*----------------------------------------------------------------------------*/
167/** Check batch TDs for activity.
168 *
169 * @param[in] instance Batch structure to use.
170 * @return False, if there is an active TD, true otherwise.
171 *
172 * Walk all TDs. Stop with false if there is an active one (it is to be
173 * processed). Stop with true if an error is found. Return true if the last TS
174 * is reached.
175 */
176bool batch_is_complete(batch_t *instance)
177{
178 assert(instance);
179 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
180 instance, instance->packets);
181 instance->transfered_size = 0;
182 size_t i = 0;
183 for (;i < instance->packets; ++i) {
184 if (td_is_active(&instance->tds[i])) {
185 return false;
186 }
187
188 instance->error = td_status(&instance->tds[i]);
189 if (instance->error != EOK) {
190 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
191 instance, i, instance->tds[i].status);
192 td_print_status(&instance->tds[i]);
193
194 device_keeper_set_toggle(instance->manager,
195 instance->target, td_toggle(&instance->tds[i]));
196 if (i > 0)
197 goto substract_ret;
198 return true;
199 }
200
201 instance->transfered_size += td_act_size(&instance->tds[i]);
202 if (td_is_short(&instance->tds[i]))
203 goto substract_ret;
204 }
205substract_ret:
206 instance->transfered_size -= instance->setup_size;
207 return true;
208}
209/*----------------------------------------------------------------------------*/
210/** Prepares control write transaction.
211 *
212 * @param[in] instance Batch structure to use.
213 *
214 * Uses genercir control function with pids OUT and IN.
215 */
216void batch_control_write(batch_t *instance)
217{
218 assert(instance);
219 /* We are data out, we are supposed to provide data */
220 memcpy(instance->transport_buffer, instance->buffer,
221 instance->buffer_size);
222 batch_control(instance, USB_PID_OUT, USB_PID_IN);
223 instance->next_step = batch_call_out_and_dispose;
224 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
225}
226/*----------------------------------------------------------------------------*/
227/** Prepares control read transaction.
228 *
229 * @param[in] instance Batch structure to use.
230 *
231 * Uses generic control with pids IN and OUT.
232 */
233void batch_control_read(batch_t *instance)
234{
235 assert(instance);
236 batch_control(instance, USB_PID_IN, USB_PID_OUT);
237 instance->next_step = batch_call_in_and_dispose;
238 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
239}
240/*----------------------------------------------------------------------------*/
241/** Prepare interrupt in transaction.
242 *
243 * @param[in] instance Batch structure to use.
244 *
245 * Data transaction with PID_IN.
246 */
247void batch_interrupt_in(batch_t *instance)
248{
249 assert(instance);
250 batch_data(instance, USB_PID_IN);
251 instance->next_step = batch_call_in_and_dispose;
252 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
253}
254/*----------------------------------------------------------------------------*/
255/** Prepare interrupt out transaction.
256 *
257 * @param[in] instance Batch structure to use.
258 *
259 * Data transaction with PID_OUT.
260 */
261void batch_interrupt_out(batch_t *instance)
262{
263 assert(instance);
264 /* We are data out, we are supposed to provide data */
265 memcpy(instance->transport_buffer, instance->buffer,
266 instance->buffer_size);
267 batch_data(instance, USB_PID_OUT);
268 instance->next_step = batch_call_out_and_dispose;
269 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
270}
271/*----------------------------------------------------------------------------*/
272/** Prepare bulk in transaction.
273 *
274 * @param[in] instance Batch structure to use.
275 *
276 * Data transaction with PID_IN.
277 */
278void batch_bulk_in(batch_t *instance)
279{
280 assert(instance);
281 batch_data(instance, USB_PID_IN);
282 instance->next_step = batch_call_in_and_dispose;
283 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
284}
285/*----------------------------------------------------------------------------*/
286/** Prepare bulk out transaction.
287 *
288 * @param[in] instance Batch structure to use.
289 *
290 * Data transaction with PID_OUT.
291 */
292void batch_bulk_out(batch_t *instance)
293{
294 assert(instance);
295 /* We are data out, we are supposed to provide data */
296 memcpy(instance->transport_buffer, instance->buffer,
297 instance->buffer_size);
298 batch_data(instance, USB_PID_OUT);
299 instance->next_step = batch_call_out_and_dispose;
300 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
301}
302/*----------------------------------------------------------------------------*/
303/** Prepare generic data transaction
304 *
305 * @param[in] instance Batch structure to use.
306 * @param[in] pid to use for data packets.
307 *
308 * Packets with alternating toggle bit and supplied pid value.
309 * The last packet is marked with IOC flag.
310 */
311void batch_data(batch_t *instance, usb_packet_id pid)
312{
313 assert(instance);
314 const bool low_speed = instance->speed == USB_SPEED_LOW;
315 int toggle =
316 device_keeper_get_toggle(instance->manager, instance->target);
317 assert(toggle == 0 || toggle == 1);
318
319 size_t packet = 0;
320 size_t remain_size = instance->buffer_size;
321 while (remain_size > 0) {
322 char *data =
323 instance->transport_buffer + instance->buffer_size
324 - remain_size;
325
326 const size_t packet_size =
327 (instance->max_packet_size > remain_size) ?
328 remain_size : instance->max_packet_size;
329
330 td_t *next_packet = (packet + 1 < instance->packets)
331 ? &instance->tds[packet + 1] : NULL;
332
333 assert(packet < instance->packets);
334 assert(packet_size <= remain_size);
335
336 td_init(
337 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
338 toggle, false, low_speed, instance->target, pid, data,
339 next_packet);
340
341
342 toggle = 1 - toggle;
343 remain_size -= packet_size;
344 ++packet;
345 }
346 td_set_ioc(&instance->tds[packet - 1]);
347 device_keeper_set_toggle(instance->manager, instance->target, toggle);
348}
349/*----------------------------------------------------------------------------*/
350/** Prepare generic control transaction
351 *
352 * @param[in] instance Batch structure to use.
353 * @param[in] data_stage to use for data packets.
354 * @param[in] status_stage to use for data packets.
355 *
356 * Setup stage with toggle 0 and USB_PID_SETUP.
357 * Data stage with alternating toggle and pid supplied by parameter.
358 * Status stage with toggle 1 and pid supplied by parameter.
359 * The last packet is marked with IOC.
360 */
361void batch_control(batch_t *instance,
362 usb_packet_id data_stage, usb_packet_id status_stage)
363{
364 assert(instance);
365
366 const bool low_speed = instance->speed == USB_SPEED_LOW;
367 int toggle = 0;
368 /* setup stage */
369 td_init(instance->tds, DEFAULT_ERROR_COUNT,
370 instance->setup_size, toggle, false, low_speed, instance->target,
371 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
372
373 /* data stage */
374 size_t packet = 1;
375 size_t remain_size = instance->buffer_size;
376 while (remain_size > 0) {
377 char *data =
378 instance->transport_buffer + instance->buffer_size
379 - remain_size;
380
381 toggle = 1 - toggle;
382
383 const size_t packet_size =
384 (instance->max_packet_size > remain_size) ?
385 remain_size : instance->max_packet_size;
386
387 td_init(
388 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
389 toggle, false, low_speed, instance->target, data_stage,
390 data, &instance->tds[packet + 1]);
391
392 ++packet;
393 assert(packet < instance->packets);
394 assert(packet_size <= remain_size);
395 remain_size -= packet_size;
396 }
397
398 /* status stage */
399 assert(packet == instance->packets - 1);
400 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
401 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
402
403 td_set_ioc(&instance->tds[packet]);
404 usb_log_debug2("Control last TD status: %x.\n",
405 instance->tds[packet].status);
406}
407/*----------------------------------------------------------------------------*/
408/** Prepare data, get error status and call callback in.
409 *
410 * @param[in] instance Batch structure to use.
411 * Copies data from transport buffer, and calls callback with appropriate
412 * parameters.
413 */
414void batch_call_in(batch_t *instance)
415{
416 assert(instance);
417 assert(instance->callback_in);
418
419 /* We are data in, we need data */
420 memcpy(instance->buffer, instance->transport_buffer,
421 instance->buffer_size);
422
423 int err = instance->error;
424 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
425 instance, instance->transfer_type, str_error(err), err,
426 instance->transfered_size);
427
428 instance->callback_in(
429 instance->fun, err, instance->transfered_size, instance->arg);
430}
431/*----------------------------------------------------------------------------*/
432/** Get error status and call callback out.
433 *
434 * @param[in] instance Batch structure to use.
435 */
436void batch_call_out(batch_t *instance)
437{
438 assert(instance);
439 assert(instance->callback_out);
440
441 int err = instance->error;
442 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
443 instance, instance->transfer_type, str_error(err), err);
444 instance->callback_out(instance->fun,
445 err, instance->arg);
446}
447/*----------------------------------------------------------------------------*/
448/** Helper function calls callback and correctly disposes of batch structure.
449 *
450 * @param[in] instance Batch structure to use.
451 */
452void batch_call_in_and_dispose(batch_t *instance)
453{
454 assert(instance);
455 batch_call_in(instance);
456 batch_dispose(instance);
457}
458/*----------------------------------------------------------------------------*/
459/** Helper function calls callback and correctly disposes of batch structure.
460 *
461 * @param[in] instance Batch structure to use.
462 */
463void batch_call_out_and_dispose(batch_t *instance)
464{
465 assert(instance);
466 batch_call_out(instance);
467 batch_dispose(instance);
468}
469/*----------------------------------------------------------------------------*/
470/** Correctly dispose all used data structures.
471 *
472 * @param[in] instance Batch structure to use.
473 */
474void batch_dispose(batch_t *instance)
475{
476 assert(instance);
477 usb_log_debug("Batch(%p) disposing.\n", instance);
478 /* free32 is NULL safe */
479 free32(instance->tds);
480 free32(instance->qh);
481 free32(instance->setup_buffer);
482 free32(instance->transport_buffer);
483 free(instance);
484}
485/**
486 * @}
487 */
Note: See TracBrowser for help on using the repository browser.