source: mainline/uspace/drv/uhci-hcd/hw_struct/transfer_descriptor.c@ 44b1674

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44b1674 was 4125b7d, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

usb_log_printf() checks for printf correctness

It is surprising how many printf warnings simple check could
produce ;-).

Next time, it won't compile. Bad, huh?

  • Property mode set to 100644
File size: 6.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
33 */
34#include <errno.h>
35#include <usb/debug.h>
36
37#include "transfer_descriptor.h"
38#include "utils/malloc32.h"
39
40/** Initialize Transfer Descriptor
41 *
42 * @param[in] instance Memory place to initialize.
43 * @param[in] err_count Number of retries hc should attempt.
44 * @param[in] size Size of data source.
45 * @param[in] toggle Value of toggle bit.
46 * @param[in] iso True if TD represents Isochronous transfer.
47 * @param[in] low_speed Target device's speed.
48 * @param[in] target Address and endpoint receiving the transfer.
49 * @param[in] pid Packet identification (SETUP, IN or OUT).
50 * @param[in] buffer Source of data.
51 * @param[in] next Net TD in transaction.
52 * @return Error code.
53 *
54 * Uses a mix of supplied and default values.
55 * Implicit values:
56 * - all TDs have vertical flag set (makes transfers to endpoints atomic)
57 * - in the error field only active it is set
58 * - if the packet uses PID_IN and is not isochronous SPD is set
59 *
60 * Dumps 8 bytes of buffer if PID_SETUP is used.
61 */
62void td_init(td_t *instance, int err_count, size_t size, bool toggle, bool iso,
63 bool low_speed, usb_target_t target, usb_packet_id pid, void *buffer,
64 td_t *next)
65{
66 assert(instance);
67 assert(size < 1024);
68 assert((pid == USB_PID_SETUP) || (pid == USB_PID_IN)
69 || (pid == USB_PID_OUT));
70
71 const uint32_t next_pa = addr_to_phys(next);
72 assert((next_pa & LINK_POINTER_ADDRESS_MASK) == next_pa);
73
74 instance->next = 0
75 | LINK_POINTER_VERTICAL_FLAG
76 | (next_pa ? next_pa : LINK_POINTER_TERMINATE_FLAG);
77
78 instance->status = 0
79 | ((err_count & TD_STATUS_ERROR_COUNT_MASK)
80 << TD_STATUS_ERROR_COUNT_POS)
81 | (low_speed ? TD_STATUS_LOW_SPEED_FLAG : 0)
82 | (iso ? TD_STATUS_ISOCHRONOUS_FLAG : 0)
83 | TD_STATUS_ERROR_ACTIVE;
84
85 if (pid == USB_PID_IN && !iso) {
86 instance->status |= TD_STATUS_SPD_FLAG;
87 }
88
89 instance->device = 0
90 | (((size - 1) & TD_DEVICE_MAXLEN_MASK) << TD_DEVICE_MAXLEN_POS)
91 | (toggle ? TD_DEVICE_DATA_TOGGLE_ONE_FLAG : 0)
92 | ((target.address & TD_DEVICE_ADDRESS_MASK)
93 << TD_DEVICE_ADDRESS_POS)
94 | ((target.endpoint & TD_DEVICE_ENDPOINT_MASK)
95 << TD_DEVICE_ENDPOINT_POS)
96 | ((pid & TD_DEVICE_PID_MASK) << TD_DEVICE_PID_POS);
97
98 instance->buffer_ptr = addr_to_phys(buffer);
99
100 usb_log_debug2("Created TD(%p): %X:%X:%X:%X(%p).\n",
101 instance, instance->next, instance->status, instance->device,
102 instance->buffer_ptr, buffer);
103 td_print_status(instance);
104 if (pid == USB_PID_SETUP) {
105 usb_log_debug("SETUP BUFFER: %s\n",
106 usb_debug_str_buffer(buffer, 8, 8));
107 }
108}
109/*----------------------------------------------------------------------------*/
110/** Convert TD status into standard error code
111 *
112 * @param[in] instance TD structure to use.
113 * @return Error code.
114 */
115int td_status(td_t *instance)
116{
117 assert(instance);
118
119 /* This is hc internal error it should never be reported. */
120 if ((instance->status & TD_STATUS_ERROR_BIT_STUFF) != 0)
121 return EAGAIN;
122
123 /* CRC or timeout error, like device not present or bad data,
124 * it won't be reported unless err count reached zero */
125 if ((instance->status & TD_STATUS_ERROR_CRC) != 0)
126 return EBADCHECKSUM;
127
128 /* HC does not end transactions on these, it should never be reported */
129 if ((instance->status & TD_STATUS_ERROR_NAK) != 0)
130 return EAGAIN;
131
132 /* Buffer overrun or underrun */
133 if ((instance->status & TD_STATUS_ERROR_BUFFER) != 0)
134 return ERANGE;
135
136 /* Device babble is something serious */
137 if ((instance->status & TD_STATUS_ERROR_BABBLE) != 0)
138 return EIO;
139
140 /* Stall might represent err count reaching zero or stall response from
141 * the device. If err count reached zero, one of the above is reported*/
142 if ((instance->status & TD_STATUS_ERROR_STALLED) != 0)
143 return ESTALL;
144
145 return EOK;
146}
147/*----------------------------------------------------------------------------*/
148/** Print values in status field (dw1) in a human readable way.
149 *
150 * @param[in] instance TD structure to use.
151 */
152void td_print_status(td_t *instance)
153{
154 assert(instance);
155 const uint32_t s = instance->status;
156 usb_log_debug2("TD(%p) status(%#" PRIx32 "):%s %d,%s%s%s%s%s%s%s%s%s%s%s %zu.\n",
157 instance, instance->status,
158 (s & TD_STATUS_SPD_FLAG) ? " SPD," : "",
159 (s >> TD_STATUS_ERROR_COUNT_POS) & TD_STATUS_ERROR_COUNT_MASK,
160 (s & TD_STATUS_LOW_SPEED_FLAG) ? " LOW SPEED," : "",
161 (s & TD_STATUS_ISOCHRONOUS_FLAG) ? " ISOCHRONOUS," : "",
162 (s & TD_STATUS_IOC_FLAG) ? " IOC," : "",
163 (s & TD_STATUS_ERROR_ACTIVE) ? " ACTIVE," : "",
164 (s & TD_STATUS_ERROR_STALLED) ? " STALLED," : "",
165 (s & TD_STATUS_ERROR_BUFFER) ? " BUFFER," : "",
166 (s & TD_STATUS_ERROR_BABBLE) ? " BABBLE," : "",
167 (s & TD_STATUS_ERROR_NAK) ? " NAK," : "",
168 (s & TD_STATUS_ERROR_CRC) ? " CRC/TIMEOUT," : "",
169 (s & TD_STATUS_ERROR_BIT_STUFF) ? " BIT_STUFF," : "",
170 (s & TD_STATUS_ERROR_RESERVED) ? " RESERVED," : "",
171 td_act_size(instance)
172 );
173}
174/**
175 * @}
176 */
Note: See TracBrowser for help on using the repository browser.