source: mainline/uspace/drv/bus/usb/uhci/uhci_batch.c@ fb28cde

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fb28cde was 2755a622, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

uhci: fix transfer aborting

  • Property mode set to 100644
File size: 10.4 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
29/** @addtogroup drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI driver USB transfer structure
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <macros.h>
39#include <mem.h>
40#include <stdlib.h>
41
42#include <usb/usb.h>
43#include <usb/debug.h>
44#include <usb/host/endpoint.h>
45#include <usb/host/utils/malloc32.h>
46
47#include "uhci_batch.h"
48#include "hw_struct/transfer_descriptor.h"
49
50#define DEFAULT_ERROR_COUNT 3
51
52/** Transfer batch setup table. */
53static void (*const batch_setup[])(uhci_transfer_batch_t*);
54
55/** Destroys uhci_transfer_batch_t structure.
56 *
57 * @param[in] uhci_batch Instance to destroy.
58 */
59void uhci_transfer_batch_destroy(uhci_transfer_batch_t *uhci_batch)
60{
61 assert(uhci_batch);
62 free32(uhci_batch->device_buffer);
63 free(uhci_batch);
64}
65
66/** Allocate memory and initialize internal data structure.
67 *
68 * @param[in] usb_batch Pointer to generic USB batch structure.
69 * @return Valid pointer if all structures were successfully created,
70 * NULL otherwise.
71 */
72uhci_transfer_batch_t * uhci_transfer_batch_create(endpoint_t *ep)
73{
74 uhci_transfer_batch_t *uhci_batch =
75 calloc(1, sizeof(uhci_transfer_batch_t));
76 if (!uhci_batch) {
77 usb_log_error("Failed to allocate UHCI batch.");
78 return NULL;
79 }
80
81 usb_transfer_batch_init(&uhci_batch->base, ep);
82
83 link_initialize(&uhci_batch->link);
84 return uhci_batch;
85}
86
87/* Prepares batch for commiting.
88 *
89 * Determines the number of needed transfer descriptors (TDs).
90 * Prepares a transport buffer (that is accessible by the hardware).
91 * Initializes parameters needed for the transfer and callback.
92 */
93int uhci_transfer_batch_prepare(uhci_transfer_batch_t *uhci_batch)
94{
95 static_assert((sizeof(td_t) % 16) == 0);
96
97 usb_transfer_batch_t *usb_batch = &uhci_batch->base;
98
99 uhci_batch->td_count = (usb_batch->buffer_size + usb_batch->ep->max_packet_size - 1)
100 / usb_batch->ep->max_packet_size;
101
102 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
103 uhci_batch->td_count += 2;
104 }
105
106 const size_t setup_size = (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL)
107 ? USB_SETUP_PACKET_SIZE
108 : 0;
109
110 const size_t total_size = (sizeof(td_t) * uhci_batch->td_count)
111 + sizeof(qh_t) + setup_size + usb_batch->buffer_size;
112 uhci_batch->device_buffer = malloc32(total_size);
113 if (!uhci_batch->device_buffer) {
114 usb_log_error("Failed to allocate UHCI buffer.");
115 return ENOMEM;
116 }
117 memset(uhci_batch->device_buffer, 0, total_size);
118
119 uhci_batch->tds = uhci_batch->device_buffer;
120 uhci_batch->qh =
121 (uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count));
122
123 qh_init(uhci_batch->qh);
124 qh_set_element_td(uhci_batch->qh, &uhci_batch->tds[0]);
125
126 void *dest =
127 uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count)
128 + sizeof(qh_t);
129 /* Copy SETUP packet data to the device buffer */
130 memcpy(dest, usb_batch->setup.buffer, setup_size);
131 dest += setup_size;
132 /* Copy generic data unless they are provided by the device */
133 if (usb_batch->dir != USB_DIRECTION_IN) {
134 memcpy(dest, usb_batch->buffer, usb_batch->buffer_size);
135 }
136 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
137 " memory structures ready.", usb_batch,
138 USB_TRANSFER_BATCH_ARGS(*usb_batch));
139
140 assert(batch_setup[usb_batch->ep->transfer_type]);
141 batch_setup[usb_batch->ep->transfer_type](uhci_batch);
142
143 return EOK;
144}
145
146/** Check batch TDs for activity.
147 *
148 * @param[in] uhci_batch Batch structure to use.
149 * @return False, if there is an active TD, true otherwise.
150 *
151 * Walk all TDs. Stop with false if there is an active one (it is to be
152 * processed). Stop with true if an error is found. Return true if the last TD
153 * is reached.
154 */
155bool uhci_transfer_batch_check_completed(uhci_transfer_batch_t *uhci_batch)
156{
157 assert(uhci_batch);
158 usb_transfer_batch_t *batch = &uhci_batch->base;
159
160 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
161 " checking %zu transfer(s) for completion.",
162 uhci_batch, USB_TRANSFER_BATCH_ARGS(*batch),
163 uhci_batch->td_count);
164 batch->transfered_size = 0;
165
166 for (size_t i = 0;i < uhci_batch->td_count; ++i) {
167 if (td_is_active(&uhci_batch->tds[i])) {
168 return false;
169 }
170
171 batch->error = td_status(&uhci_batch->tds[i]);
172 if (batch->error != EOK) {
173 assert(batch->ep != NULL);
174
175 usb_log_debug("Batch %p found error TD(%zu->%p):%"
176 PRIx32 ".", uhci_batch, i,
177 &uhci_batch->tds[i], uhci_batch->tds[i].status);
178 td_print_status(&uhci_batch->tds[i]);
179
180 batch->ep->toggle = td_toggle(&uhci_batch->tds[i]);
181 goto substract_ret;
182 }
183
184 batch->transfered_size
185 += td_act_size(&uhci_batch->tds[i]);
186 if (td_is_short(&uhci_batch->tds[i]))
187 goto substract_ret;
188 }
189substract_ret:
190 if (batch->transfered_size > 0 && batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
191 assert(batch->transfered_size >= USB_SETUP_PACKET_SIZE);
192 batch->transfered_size -= USB_SETUP_PACKET_SIZE;
193 }
194
195 if (batch->dir == USB_DIRECTION_IN) {
196 assert(batch->transfered_size <= batch->buffer_size);
197 memcpy(batch->buffer,
198 uhci_transfer_batch_data_buffer(uhci_batch),
199 batch->transfered_size);
200 }
201
202 return true;
203}
204
205/** Direction to pid conversion table */
206static const usb_packet_id direction_pids[] = {
207 [USB_DIRECTION_IN] = USB_PID_IN,
208 [USB_DIRECTION_OUT] = USB_PID_OUT,
209};
210
211/** Prepare generic data transfer
212 *
213 * @param[in] uhci_batch Batch structure to use.
214 * @param[in] dir Communication direction.
215 *
216 * Transactions with alternating toggle bit and supplied pid value.
217 * The last transfer is marked with IOC flag.
218 */
219static void batch_data(uhci_transfer_batch_t *uhci_batch)
220{
221 assert(uhci_batch);
222
223 usb_direction_t dir = uhci_batch->base.dir;
224 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
225
226
227 const usb_packet_id pid = direction_pids[dir];
228 const bool low_speed =
229 uhci_batch->base.ep->device->speed == USB_SPEED_LOW;
230 const size_t mps = uhci_batch->base.ep->max_packet_size;
231
232 int toggle = uhci_batch->base.ep->toggle;
233 assert(toggle == 0 || toggle == 1);
234
235 size_t td = 0;
236 size_t remain_size = uhci_batch->base.buffer_size;
237 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
238
239 while (remain_size > 0) {
240 const size_t packet_size = min(remain_size, mps);
241
242 const td_t *next_td = (td + 1 < uhci_batch->td_count)
243 ? &uhci_batch->tds[td + 1] : NULL;
244
245 assert(td < uhci_batch->td_count);
246 td_init(
247 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
248 toggle, false, low_speed, uhci_batch->base.target, pid, buffer, next_td);
249
250 ++td;
251 toggle = 1 - toggle;
252 buffer += packet_size;
253 remain_size -= packet_size;
254 }
255 td_set_ioc(&uhci_batch->tds[td - 1]);
256 uhci_batch->base.ep->toggle = toggle;
257 usb_log_debug2(
258 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.", \
259 uhci_batch,
260 usb_str_transfer_type(uhci_batch->base.ep->transfer_type),
261 usb_str_direction(uhci_batch->base.ep->direction),
262 USB_TRANSFER_BATCH_ARGS(uhci_batch->base));
263}
264
265/** Prepare generic control transfer
266 *
267 * @param[in] uhci_batch Batch structure to use.
268 * @param[in] dir Communication direction.
269 *
270 * Setup stage with toggle 0 and USB_PID_SETUP.
271 * Data stage with alternating toggle and pid determined by the communication
272 * direction.
273 * Status stage with toggle 1 and pid determined by the communication direction.
274 * The last transfer is marked with IOC.
275 */
276static void batch_control(uhci_transfer_batch_t *uhci_batch)
277{
278 assert(uhci_batch);
279
280 usb_direction_t dir = uhci_batch->base.dir;
281 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
282 assert(uhci_batch->td_count >= 2);
283 static const usb_packet_id status_stage_pids[] = {
284 [USB_DIRECTION_IN] = USB_PID_OUT,
285 [USB_DIRECTION_OUT] = USB_PID_IN,
286 };
287
288 const usb_packet_id data_stage_pid = direction_pids[dir];
289 const usb_packet_id status_stage_pid = status_stage_pids[dir];
290 const bool low_speed =
291 uhci_batch->base.ep->device->speed == USB_SPEED_LOW;
292 const size_t mps = uhci_batch->base.ep->max_packet_size;
293 const usb_target_t target = uhci_batch->base.target;
294
295 /* setup stage */
296 td_init(
297 &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
298 USB_SETUP_PACKET_SIZE, 0, false,
299 low_speed, target, USB_PID_SETUP,
300 uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
301
302 /* data stage */
303 size_t td = 1;
304 unsigned toggle = 1;
305 size_t remain_size = uhci_batch->base.buffer_size;
306 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
307
308 while (remain_size > 0) {
309 const size_t packet_size = min(remain_size, mps);
310
311 td_init(
312 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
313 toggle, false, low_speed, target, data_stage_pid,
314 buffer, &uhci_batch->tds[td + 1]);
315
316 ++td;
317 toggle = 1 - toggle;
318 buffer += packet_size;
319 remain_size -= packet_size;
320 assert(td < uhci_batch->td_count);
321 }
322
323 /* status stage */
324 assert(td == uhci_batch->td_count - 1);
325
326 td_init(
327 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
328 target, status_stage_pid, NULL, NULL);
329 td_set_ioc(&uhci_batch->tds[td]);
330
331 usb_log_debug2("Control last TD status: %x.",
332 uhci_batch->tds[td].status);
333}
334
335static void (*const batch_setup[])(uhci_transfer_batch_t*) =
336{
337 [USB_TRANSFER_CONTROL] = batch_control,
338 [USB_TRANSFER_BULK] = batch_data,
339 [USB_TRANSFER_INTERRUPT] = batch_data,
340 [USB_TRANSFER_ISOCHRONOUS] = NULL,
341};
342/**
343 * @}
344 */
Note: See TracBrowser for help on using the repository browser.