source: mainline/uspace/drv/bus/usb/uhci/hw_struct/transfer_descriptor.c@ 8064c2f6

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

uhci: Sanitize headers.

Include what you use.

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