blob: eb6dfcdb09e9debed0ae822606d9670c1c1de86f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vivek Gautam4912dcc2013-09-14 14:02:45 +05302/*
3 * USB HOST XHCI Controller stack
4 *
5 * Based on xHCI host controller driver in linux-kernel
6 * by Sarah Sharp.
7 *
8 * Copyright (C) 2008 Intel Corp.
9 * Author: Sarah Sharp
10 *
11 * Copyright (C) 2013 Samsung Electronics Co.Ltd
12 * Authors: Vivek Gautam <gautam.vivek@samsung.com>
13 * Vikas Sajjan <vikas.sajjan@samsung.com>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053014 */
15
16#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053019#include <asm/byteorder.h>
20#include <usb.h>
21#include <asm/unaligned.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060022#include <linux/bug.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090023#include <linux/errno.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053024
Jean-Jacques Hiblotad4142b2019-09-11 11:33:46 +020025#include <usb/xhci.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053026
27/**
28 * Is this TRB a link TRB or was the last TRB the last TRB in this event ring
29 * segment? I.e. would the updated event TRB pointer step off the end of the
30 * event seg ?
31 *
32 * @param ctrl Host controller data structure
33 * @param ring pointer to the ring
34 * @param seg poniter to the segment to which TRB belongs
35 * @param trb poniter to the ring trb
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010036 * Return: 1 if this TRB a link TRB else 0
Vivek Gautam4912dcc2013-09-14 14:02:45 +053037 */
38static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
39 struct xhci_segment *seg, union xhci_trb *trb)
40{
41 if (ring == ctrl->event_ring)
42 return trb == &seg->trbs[TRBS_PER_SEGMENT];
43 else
44 return TRB_TYPE_LINK_LE32(trb->link.control);
45}
46
47/**
48 * Does this link TRB point to the first segment in a ring,
49 * or was the previous TRB the last TRB on the last segment in the ERST?
50 *
51 * @param ctrl Host controller data structure
52 * @param ring pointer to the ring
53 * @param seg poniter to the segment to which TRB belongs
54 * @param trb poniter to the ring trb
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010055 * Return: 1 if this TRB is the last TRB on the last segment else 0
Vivek Gautam4912dcc2013-09-14 14:02:45 +053056 */
57static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl,
58 struct xhci_ring *ring,
59 struct xhci_segment *seg,
60 union xhci_trb *trb)
61{
62 if (ring == ctrl->event_ring)
63 return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
64 (seg->next == ring->first_seg));
65 else
66 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
67}
68
69/**
70 * See Cycle bit rules. SW is the consumer for the event ring only.
71 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
72 *
73 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
74 * chain bit is set), then set the chain bit in all the following link TRBs.
75 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
76 * have their chain bit cleared (so that each Link TRB is a separate TD).
77 *
78 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
79 * set, but other sections talk about dealing with the chain bit set. This was
80 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
81 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
82 *
83 * @param ctrl Host controller data structure
84 * @param ring pointer to the ring
85 * @param more_trbs_coming flag to indicate whether more trbs
86 * are expected or NOT.
87 * Will you enqueue more TRBs before calling
88 * prepare_ring()?
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010089 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053090 */
91static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
92 bool more_trbs_coming)
93{
94 u32 chain;
95 union xhci_trb *next;
96
97 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
98 next = ++(ring->enqueue);
99
100 /*
101 * Update the dequeue pointer further if that was a link TRB or we're at
102 * the end of an event ring segment (which doesn't have link TRBS)
103 */
104 while (last_trb(ctrl, ring, ring->enq_seg, next)) {
105 if (ring != ctrl->event_ring) {
106 /*
107 * If the caller doesn't plan on enqueueing more
108 * TDs before ringing the doorbell, then we
109 * don't want to give the link TRB to the
110 * hardware just yet. We'll give the link TRB
111 * back in prepare_ring() just before we enqueue
112 * the TD at the top of the ring.
113 */
114 if (!chain && !more_trbs_coming)
115 break;
116
117 /*
118 * If we're not dealing with 0.95 hardware or
119 * isoc rings on AMD 0.96 host,
120 * carry over the chain bit of the previous TRB
121 * (which may mean the chain bit is cleared).
122 */
123 next->link.control &= cpu_to_le32(~TRB_CHAIN);
124 next->link.control |= cpu_to_le32(chain);
125
126 next->link.control ^= cpu_to_le32(TRB_CYCLE);
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300127 xhci_flush_cache((uintptr_t)next,
128 sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530129 }
130 /* Toggle the cycle bit after the last ring segment. */
131 if (last_trb_on_last_seg(ctrl, ring,
132 ring->enq_seg, next))
133 ring->cycle_state = (ring->cycle_state ? 0 : 1);
134
135 ring->enq_seg = ring->enq_seg->next;
136 ring->enqueue = ring->enq_seg->trbs;
137 next = ring->enqueue;
138 }
139}
140
141/**
142 * See Cycle bit rules. SW is the consumer for the event ring only.
143 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
144 *
145 * @param ctrl Host controller data structure
146 * @param ring Ring whose Dequeue TRB pointer needs to be incremented.
147 * return none
148 */
149static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
150{
151 do {
152 /*
153 * Update the dequeue pointer further if that was a link TRB or
154 * we're at the end of an event ring segment (which doesn't have
155 * link TRBS)
156 */
157 if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) {
158 if (ring == ctrl->event_ring &&
159 last_trb_on_last_seg(ctrl, ring,
160 ring->deq_seg, ring->dequeue)) {
161 ring->cycle_state = (ring->cycle_state ? 0 : 1);
162 }
163 ring->deq_seg = ring->deq_seg->next;
164 ring->dequeue = ring->deq_seg->trbs;
165 } else {
166 ring->dequeue++;
167 }
168 } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue));
169}
170
171/**
172 * Generic function for queueing a TRB on a ring.
173 * The caller must have checked to make sure there's room on the ring.
174 *
175 * @param more_trbs_coming: Will you enqueue more TRBs before calling
176 * prepare_ring()?
177 * @param ctrl Host controller data structure
178 * @param ring pointer to the ring
179 * @param more_trbs_coming flag to indicate whether more trbs
180 * @param trb_fields pointer to trb field array containing TRB contents
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100181 * Return: pointer to the enqueued trb
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530182 */
183static struct xhci_generic_trb *queue_trb(struct xhci_ctrl *ctrl,
184 struct xhci_ring *ring,
185 bool more_trbs_coming,
186 unsigned int *trb_fields)
187{
188 struct xhci_generic_trb *trb;
189 int i;
190
191 trb = &ring->enqueue->generic;
192
193 for (i = 0; i < 4; i++)
194 trb->field[i] = cpu_to_le32(trb_fields[i]);
195
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300196 xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530197
198 inc_enq(ctrl, ring, more_trbs_coming);
199
200 return trb;
201}
202
203/**
204 * Does various checks on the endpoint ring, and makes it ready
205 * to queue num_trbs.
206 *
207 * @param ctrl Host controller data structure
208 * @param ep_ring pointer to the EP Transfer Ring
209 * @param ep_state State of the End Point
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100210 * Return: error code in case of invalid ep_state, 0 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530211 */
212static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring,
213 u32 ep_state)
214{
215 union xhci_trb *next = ep_ring->enqueue;
216
217 /* Make sure the endpoint has been added to xHC schedule */
218 switch (ep_state) {
219 case EP_STATE_DISABLED:
220 /*
221 * USB core changed config/interfaces without notifying us,
222 * or hardware is reporting the wrong state.
223 */
224 puts("WARN urb submitted to disabled ep\n");
225 return -ENOENT;
226 case EP_STATE_ERROR:
227 puts("WARN waiting for error on ep to be cleared\n");
228 return -EINVAL;
229 case EP_STATE_HALTED:
230 puts("WARN halted endpoint, queueing URB anyway.\n");
231 case EP_STATE_STOPPED:
232 case EP_STATE_RUNNING:
233 debug("EP STATE RUNNING.\n");
234 break;
235 default:
236 puts("ERROR unknown endpoint state for ep\n");
237 return -EINVAL;
238 }
239
240 while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) {
241 /*
242 * If we're not dealing with 0.95 hardware or isoc rings
243 * on AMD 0.96 host, clear the chain bit.
244 */
245 next->link.control &= cpu_to_le32(~TRB_CHAIN);
246
247 next->link.control ^= cpu_to_le32(TRB_CYCLE);
248
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300249 xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530250
251 /* Toggle the cycle bit after the last ring segment. */
252 if (last_trb_on_last_seg(ctrl, ep_ring,
253 ep_ring->enq_seg, next))
254 ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1);
255 ep_ring->enq_seg = ep_ring->enq_seg->next;
256 ep_ring->enqueue = ep_ring->enq_seg->trbs;
257 next = ep_ring->enqueue;
258 }
259
260 return 0;
261}
262
263/**
264 * Generic function for queueing a command TRB on the command ring.
265 * Check to make sure there's room on the command ring for one command TRB.
266 *
267 * @param ctrl Host controller data structure
268 * @param ptr Pointer address to write in the first two fields (opt.)
269 * @param slot_id Slot ID to encode in the flags field (opt.)
270 * @param ep_index Endpoint index to encode in the flags field (opt.)
271 * @param cmd Command type to enqueue
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100272 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530273 */
274void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id,
275 u32 ep_index, trb_type cmd)
276{
277 u32 fields[4];
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100278 u64 val_64 = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530279
280 BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING));
281
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100282 if (ptr)
283 val_64 = xhci_virt_to_bus(ctrl, ptr);
284
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530285 fields[0] = lower_32_bits(val_64);
286 fields[1] = upper_32_bits(val_64);
287 fields[2] = 0;
Bin Meng474b2502017-07-19 21:49:54 +0800288 fields[3] = TRB_TYPE(cmd) | SLOT_ID_FOR_TRB(slot_id) |
289 ctrl->cmd_ring->cycle_state;
290
291 /*
292 * Only 'reset endpoint', 'stop endpoint' and 'set TR dequeue pointer'
293 * commands need endpoint id encoded.
294 */
295 if (cmd >= TRB_RESET_EP && cmd <= TRB_SET_DEQ)
296 fields[3] |= EP_ID_FOR_TRB(ep_index);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530297
298 queue_trb(ctrl, ctrl->cmd_ring, false, fields);
299
300 /* Ring the command ring doorbell */
301 xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST);
302}
303
developer570c2a92020-09-08 18:59:56 +0200304/*
305 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
306 * packets remaining in the TD (*not* including this TRB).
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530307 *
developer570c2a92020-09-08 18:59:56 +0200308 * Total TD packet count = total_packet_count =
309 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
310 *
311 * Packets transferred up to and including this TRB = packets_transferred =
312 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
313 *
314 * TD size = total_packet_count - packets_transferred
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530315 *
developer570c2a92020-09-08 18:59:56 +0200316 * For xHCI 0.96 and older, TD size field should be the remaining bytes
317 * including this TRB, right shifted by 10
318 *
319 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
320 * This is taken care of in the TRB_TD_SIZE() macro
321 *
322 * The last TRB in a TD must have the TD size set to zero.
323 *
324 * @param ctrl host controller data structure
325 * @param transferred total size sent so far
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530326 * @param trb_buff_len length of the TRB Buffer
developer570c2a92020-09-08 18:59:56 +0200327 * @param td_total_len total packet count
328 * @param maxp max packet size of current pipe
329 * @param more_trbs_coming indicate last trb in TD
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100330 * Return: remainder
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530331 */
developer570c2a92020-09-08 18:59:56 +0200332static u32 xhci_td_remainder(struct xhci_ctrl *ctrl, int transferred,
333 int trb_buff_len, unsigned int td_total_len,
334 int maxp, bool more_trbs_coming)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530335{
developer570c2a92020-09-08 18:59:56 +0200336 u32 total_packet_count;
337
developer80390532020-09-08 18:59:57 +0200338 /* MTK xHCI 0.96 contains some features from 1.0 */
339 if (ctrl->hci_version < 0x100 && !(ctrl->quirks & XHCI_MTK_HOST))
developer570c2a92020-09-08 18:59:56 +0200340 return ((td_total_len - transferred) >> 10);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530341
342 /* One TRB with a zero-length data packet. */
developer570c2a92020-09-08 18:59:56 +0200343 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
344 trb_buff_len == td_total_len)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530345 return 0;
346
developer80390532020-09-08 18:59:57 +0200347 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */
348 if ((ctrl->quirks & XHCI_MTK_HOST) && (ctrl->hci_version < 0x100))
349 trb_buff_len = 0;
350
developer570c2a92020-09-08 18:59:56 +0200351 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530352
developer570c2a92020-09-08 18:59:56 +0200353 /* Queueing functions don't count the current TRB into transferred */
354 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530355}
356
357/**
358 * Ring the doorbell of the End Point
359 *
360 * @param udev pointer to the USB device structure
361 * @param ep_index index of the endpoint
362 * @param start_cycle cycle flag of the first TRB
363 * @param start_trb pionter to the first TRB
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100364 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530365 */
366static void giveback_first_trb(struct usb_device *udev, int ep_index,
367 int start_cycle,
368 struct xhci_generic_trb *start_trb)
369{
Simon Glassa49e27b2015-03-25 12:22:49 -0600370 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530371
372 /*
373 * Pass all the TRBs to the hardware at once and make sure this write
374 * isn't reordered.
375 */
376 if (start_cycle)
377 start_trb->field[3] |= cpu_to_le32(start_cycle);
378 else
379 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
380
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300381 xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530382
383 /* Ringing EP doorbell here */
384 xhci_writel(&ctrl->dba->doorbell[udev->slot_id],
385 DB_VALUE(ep_index, 0));
386
387 return;
388}
389
390/**** POLLING mechanism for XHCI ****/
391
392/**
393 * Finalizes a handled event TRB by advancing our dequeue pointer and giving
394 * the TRB back to the hardware for recycling. Must call this exactly once at
395 * the end of each event handler, and not touch the TRB again afterwards.
396 *
397 * @param ctrl Host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100398 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530399 */
400void xhci_acknowledge_event(struct xhci_ctrl *ctrl)
401{
402 /* Advance our dequeue pointer to the next event */
403 inc_deq(ctrl, ctrl->event_ring);
404
405 /* Inform the hardware */
406 xhci_writeq(&ctrl->ir_set->erst_dequeue,
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100407 xhci_virt_to_bus(ctrl, ctrl->event_ring->dequeue) | ERST_EHB);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530408}
409
410/**
411 * Checks if there is a new event to handle on the event ring.
412 *
413 * @param ctrl Host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100414 * Return: 0 if failure else 1 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530415 */
416static int event_ready(struct xhci_ctrl *ctrl)
417{
418 union xhci_trb *event;
419
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300420 xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue,
421 sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530422
423 event = ctrl->event_ring->dequeue;
424
425 /* Does the HC or OS own the TRB? */
426 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
427 ctrl->event_ring->cycle_state)
428 return 0;
429
430 return 1;
431}
432
433/**
434 * Waits for a specific type of event and returns it. Discards unexpected
435 * events. Caller *must* call xhci_acknowledge_event() after it is finished
436 * processing the event, and must not access the returned pointer afterwards.
437 *
438 * @param ctrl Host controller data structure
439 * @param expected TRB type expected from Event TRB
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100440 * Return: pointer to event trb
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530441 */
442union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
443{
444 trb_type type;
445 unsigned long ts = get_timer(0);
446
447 do {
448 union xhci_trb *event = ctrl->event_ring->dequeue;
449
450 if (!event_ready(ctrl))
451 continue;
452
453 type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
454 if (type == expected)
455 return event;
456
457 if (type == TRB_PORT_STATUS)
458 /* TODO: remove this once enumeration has been reworked */
459 /*
460 * Port status change events always have a
461 * successful completion code
462 */
463 BUG_ON(GET_COMP_CODE(
464 le32_to_cpu(event->generic.field[2])) !=
465 COMP_SUCCESS);
466 else
467 printf("Unexpected XHCI event TRB, skipping... "
468 "(%08x %08x %08x %08x)\n",
469 le32_to_cpu(event->generic.field[0]),
470 le32_to_cpu(event->generic.field[1]),
471 le32_to_cpu(event->generic.field[2]),
472 le32_to_cpu(event->generic.field[3]));
473
474 xhci_acknowledge_event(ctrl);
475 } while (get_timer(ts) < XHCI_TIMEOUT);
476
477 if (expected == TRB_TRANSFER)
478 return NULL;
479
480 printf("XHCI timeout on event type %d... cannot recover.\n", expected);
481 BUG();
482}
483
484/*
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200485 * Send reset endpoint command for given endpoint. This recovers from a
486 * halted endpoint (e.g. due to a stall error).
487 */
488static void reset_ep(struct usb_device *udev, int ep_index)
489{
490 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
491 struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring;
492 union xhci_trb *event;
493 u32 field;
494
495 printf("Resetting EP %d...\n", ep_index);
496 xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_RESET_EP);
497 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
498 field = le32_to_cpu(event->trans_event.flags);
499 BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
500 xhci_acknowledge_event(ctrl);
501
502 xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
503 ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
504 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
505 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
506 != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
507 event->event_cmd.status)) != COMP_SUCCESS);
508 xhci_acknowledge_event(ctrl);
509}
510
511/*
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530512 * Stops transfer processing for an endpoint and throws away all unprocessed
513 * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next
514 * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and
515 * ring the doorbell, causing this endpoint to start working again.
516 * (Careful: This will BUG() when there was no transfer in progress. Shouldn't
517 * happen in practice for current uses and is too complicated to fix right now.)
518 */
519static void abort_td(struct usb_device *udev, int ep_index)
520{
Simon Glassa49e27b2015-03-25 12:22:49 -0600521 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530522 struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring;
523 union xhci_trb *event;
524 u32 field;
525
526 xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING);
527
528 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
529 field = le32_to_cpu(event->trans_event.flags);
530 BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
531 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
532 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
533 != COMP_STOP)));
534 xhci_acknowledge_event(ctrl);
535
536 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
537 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
538 != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
539 event->event_cmd.status)) != COMP_SUCCESS);
540 xhci_acknowledge_event(ctrl);
541
542 xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
543 ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
544 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
545 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
546 != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
547 event->event_cmd.status)) != COMP_SUCCESS);
548 xhci_acknowledge_event(ctrl);
549}
550
551static void record_transfer_result(struct usb_device *udev,
552 union xhci_trb *event, int length)
553{
554 udev->act_len = min(length, length -
Masahiro Yamadadb204642014-11-07 03:03:31 +0900555 (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530556
557 switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) {
558 case COMP_SUCCESS:
559 BUG_ON(udev->act_len != length);
560 /* fallthrough */
561 case COMP_SHORT_TX:
562 udev->status = 0;
563 break;
564 case COMP_STALL:
565 udev->status = USB_ST_STALLED;
566 break;
567 case COMP_DB_ERR:
568 case COMP_TRB_ERR:
569 udev->status = USB_ST_BUF_ERR;
570 break;
571 case COMP_BABBLE:
572 udev->status = USB_ST_BABBLE_DET;
573 break;
574 default:
575 udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */
576 }
577}
578
579/**** Bulk and Control transfer methods ****/
580/**
581 * Queues up the BULK Request
582 *
583 * @param udev pointer to the USB device structure
584 * @param pipe contains the DIR_IN or OUT , devnum
585 * @param length length of the buffer
586 * @param buffer buffer to be read/written based on the request
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100587 * Return: returns 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530588 */
589int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
590 int length, void *buffer)
591{
592 int num_trbs = 0;
593 struct xhci_generic_trb *start_trb;
Gustavo A. R. Silva0a1ef7c2018-01-20 02:37:31 -0600594 bool first_trb = false;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530595 int start_cycle;
596 u32 field = 0;
597 u32 length_field = 0;
Simon Glassa49e27b2015-03-25 12:22:49 -0600598 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530599 int slot_id = udev->slot_id;
600 int ep_index;
601 struct xhci_virt_device *virt_dev;
602 struct xhci_ep_ctx *ep_ctx;
603 struct xhci_ring *ring; /* EP transfer ring */
604 union xhci_trb *event;
605
606 int running_total, trb_buff_len;
developer570c2a92020-09-08 18:59:56 +0200607 bool more_trbs_coming = true;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530608 int maxpacketsize;
609 u64 addr;
610 int ret;
611 u32 trb_fields[4];
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100612 u64 val_64 = xhci_virt_to_bus(ctrl, buffer);
Ran Wanga0505832020-11-18 15:49:02 +0800613 void *last_transfer_trb_addr;
614 int available_length;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530615
616 debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n",
617 udev, pipe, buffer, length);
618
Ran Wanga0505832020-11-18 15:49:02 +0800619 available_length = length;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530620 ep_index = usb_pipe_ep_index(pipe);
621 virt_dev = ctrl->devs[slot_id];
622
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300623 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
624 virt_dev->out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530625
626 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
627
628 ring = virt_dev->eps[ep_index].ring;
629 /*
630 * How much data is (potentially) left before the 64KB boundary?
631 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
632 * that the buffer should not span 64KB boundary. if so
633 * we send request in more than 1 TRB by chaining them.
634 */
635 running_total = TRB_MAX_BUFF_SIZE -
636 (lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1));
637 trb_buff_len = running_total;
638 running_total &= TRB_MAX_BUFF_SIZE - 1;
639
640 /*
641 * If there's some data on this 64KB chunk, or we have to send a
642 * zero-length transfer, we need at least one TRB
643 */
644 if (running_total != 0 || length == 0)
645 num_trbs++;
646
647 /* How many more 64KB chunks to transfer, how many more TRBs? */
648 while (running_total < length) {
649 num_trbs++;
650 running_total += TRB_MAX_BUFF_SIZE;
651 }
652
653 /*
654 * XXX: Calling routine prepare_ring() called in place of
655 * prepare_trasfer() as there in 'Linux' since we are not
656 * maintaining multiple TDs/transfer at the same time.
657 */
658 ret = prepare_ring(ctrl, ring,
659 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
660 if (ret < 0)
661 return ret;
662
663 /*
664 * Don't give the first TRB to the hardware (by toggling the cycle bit)
665 * until we've finished creating all the other TRBs. The ring's cycle
666 * state may change as we enqueue the other TRBs, so save it too.
667 */
668 start_trb = &ring->enqueue->generic;
669 start_cycle = ring->cycle_state;
670
671 running_total = 0;
672 maxpacketsize = usb_maxpacket(udev, pipe);
673
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530674 /* How much data is in the first TRB? */
675 /*
676 * How much data is (potentially) left before the 64KB boundary?
677 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
678 * that the buffer should not span 64KB boundary. if so
679 * we send request in more than 1 TRB by chaining them.
680 */
681 addr = val_64;
682
683 if (trb_buff_len > length)
684 trb_buff_len = length;
685
686 first_trb = true;
687
688 /* flush the buffer before use */
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300689 xhci_flush_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530690
691 /* Queue the first TRB, even if it's zero-length */
692 do {
693 u32 remainder = 0;
694 field = 0;
695 /* Don't change the cycle bit of the first TRB until later */
696 if (first_trb) {
697 first_trb = false;
698 if (start_cycle == 0)
699 field |= TRB_CYCLE;
700 } else {
701 field |= ring->cycle_state;
702 }
703
704 /*
705 * Chain all the TRBs together; clear the chain bit in the last
706 * TRB to indicate it's the last TRB in the chain.
707 */
developer570c2a92020-09-08 18:59:56 +0200708 if (num_trbs > 1) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530709 field |= TRB_CHAIN;
developer570c2a92020-09-08 18:59:56 +0200710 } else {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530711 field |= TRB_IOC;
developer570c2a92020-09-08 18:59:56 +0200712 more_trbs_coming = false;
713 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530714
715 /* Only set interrupt on short packet for IN endpoints */
716 if (usb_pipein(pipe))
717 field |= TRB_ISP;
718
719 /* Set the TRB length, TD size, and interrupter fields. */
developer570c2a92020-09-08 18:59:56 +0200720 remainder = xhci_td_remainder(ctrl, running_total, trb_buff_len,
721 length, maxpacketsize,
722 more_trbs_coming);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530723
developer6cabb142020-09-08 19:00:00 +0200724 length_field = (TRB_LEN(trb_buff_len) |
developer570c2a92020-09-08 18:59:56 +0200725 TRB_TD_SIZE(remainder) |
developer6cabb142020-09-08 19:00:00 +0200726 TRB_INTR_TARGET(0));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530727
728 trb_fields[0] = lower_32_bits(addr);
729 trb_fields[1] = upper_32_bits(addr);
730 trb_fields[2] = length_field;
developer497dcfa2020-09-08 18:59:59 +0200731 trb_fields[3] = field | TRB_TYPE(TRB_NORMAL);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530732
Ran Wanga0505832020-11-18 15:49:02 +0800733 last_transfer_trb_addr = queue_trb(ctrl, ring, (num_trbs > 1), trb_fields);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530734
735 --num_trbs;
736
737 running_total += trb_buff_len;
738
739 /* Calculate length for next transfer */
740 addr += trb_buff_len;
741 trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE);
742 } while (running_total < length);
743
744 giveback_first_trb(udev, ep_index, start_cycle, start_trb);
745
Ran Wanga0505832020-11-18 15:49:02 +0800746again:
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530747 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
748 if (!event) {
749 debug("XHCI bulk transfer timed out, aborting...\n");
750 abort_td(udev, ep_index);
751 udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */
752 udev->act_len = 0;
753 return -ETIMEDOUT;
754 }
Ran Wanga0505832020-11-18 15:49:02 +0800755
Stefan Roese5e3c1462021-01-15 08:52:56 +0100756 if ((uintptr_t)(le64_to_cpu(event->trans_event.buffer)) !=
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100757 (uintptr_t)xhci_virt_to_bus(ctrl, last_transfer_trb_addr)) {
Ran Wanga0505832020-11-18 15:49:02 +0800758 available_length -=
759 (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len));
760 xhci_acknowledge_event(ctrl);
761 goto again;
762 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530763
Ran Wanga0505832020-11-18 15:49:02 +0800764 field = le32_to_cpu(event->trans_event.flags);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530765 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
766 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530767
Ran Wanga0505832020-11-18 15:49:02 +0800768 record_transfer_result(udev, event, available_length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530769 xhci_acknowledge_event(ctrl);
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300770 xhci_inval_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530771
772 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
773}
774
775/**
776 * Queues up the Control Transfer Request
777 *
778 * @param udev pointer to the USB device structure
779 * @param pipe contains the DIR_IN or OUT , devnum
780 * @param req request type
781 * @param length length of the buffer
782 * @param buffer buffer to be read/written based on the request
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100783 * Return: returns 0 if successful else error code on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530784 */
785int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
786 struct devrequest *req, int length,
787 void *buffer)
788{
789 int ret;
790 int start_cycle;
791 int num_trbs;
792 u32 field;
793 u32 length_field;
794 u64 buf_64 = 0;
795 struct xhci_generic_trb *start_trb;
Simon Glassa49e27b2015-03-25 12:22:49 -0600796 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530797 int slot_id = udev->slot_id;
798 int ep_index;
799 u32 trb_fields[4];
800 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
801 struct xhci_ring *ep_ring;
802 union xhci_trb *event;
developer570c2a92020-09-08 18:59:56 +0200803 u32 remainder;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530804
805 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
806 req->request, req->request,
807 req->requesttype, req->requesttype,
808 le16_to_cpu(req->value), le16_to_cpu(req->value),
809 le16_to_cpu(req->index));
810
811 ep_index = usb_pipe_ep_index(pipe);
812
813 ep_ring = virt_dev->eps[ep_index].ring;
814
815 /*
816 * Check to see if the max packet size for the default control
817 * endpoint changed during FS device enumeration
818 */
819 if (udev->speed == USB_SPEED_FULL) {
820 ret = xhci_check_maxpacket(udev);
821 if (ret < 0)
822 return ret;
823 }
824
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300825 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
826 virt_dev->out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530827
828 struct xhci_ep_ctx *ep_ctx = NULL;
829 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
830
831 /* 1 TRB for setup, 1 for status */
832 num_trbs = 2;
833 /*
834 * Don't need to check if we need additional event data and normal TRBs,
835 * since data in control transfers will never get bigger than 16MB
836 * XXX: can we get a buffer that crosses 64KB boundaries?
837 */
838
839 if (length > 0)
840 num_trbs++;
841 /*
842 * XXX: Calling routine prepare_ring() called in place of
843 * prepare_trasfer() as there in 'Linux' since we are not
844 * maintaining multiple TDs/transfer at the same time.
845 */
846 ret = prepare_ring(ctrl, ep_ring,
847 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
848
849 if (ret < 0)
850 return ret;
851
852 /*
853 * Don't give the first TRB to the hardware (by toggling the cycle bit)
854 * until we've finished creating all the other TRBs. The ring's cycle
855 * state may change as we enqueue the other TRBs, so save it too.
856 */
857 start_trb = &ep_ring->enqueue->generic;
858 start_cycle = ep_ring->cycle_state;
859
860 debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle);
861
862 /* Queue setup TRB - see section 6.4.1.2.1 */
863 /* FIXME better way to translate setup_packet into two u32 fields? */
864 field = 0;
developer497dcfa2020-09-08 18:59:59 +0200865 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530866 if (start_cycle == 0)
867 field |= 0x1;
868
869 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
developer80390532020-09-08 18:59:57 +0200870 if (ctrl->hci_version >= 0x100 || ctrl->quirks & XHCI_MTK_HOST) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530871 if (length > 0) {
872 if (req->requesttype & USB_DIR_IN)
developer57c052b2020-09-08 19:00:01 +0200873 field |= TRB_TX_TYPE(TRB_DATA_IN);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530874 else
developer57c052b2020-09-08 19:00:01 +0200875 field |= TRB_TX_TYPE(TRB_DATA_OUT);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530876 }
877 }
878
Stefan Roeseede9de12021-04-06 12:10:18 +0200879 debug("req->requesttype = %d, req->request = %d, req->value = %d, req->index = %d, req->length = %d\n",
880 req->requesttype, req->request, le16_to_cpu(req->value),
881 le16_to_cpu(req->index), le16_to_cpu(req->length));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530882
883 trb_fields[0] = req->requesttype | req->request << 8 |
884 le16_to_cpu(req->value) << 16;
885 trb_fields[1] = le16_to_cpu(req->index) |
886 le16_to_cpu(req->length) << 16;
887 /* TRB_LEN | (TRB_INTR_TARGET) */
developer6cabb142020-09-08 19:00:00 +0200888 trb_fields[2] = (TRB_LEN(8) | TRB_INTR_TARGET(0));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530889 /* Immediate data in pointer */
890 trb_fields[3] = field;
891 queue_trb(ctrl, ep_ring, true, trb_fields);
892
893 /* Re-initializing field to zero */
894 field = 0;
895 /* If there's data, queue data TRBs */
896 /* Only set interrupt on short packet for IN endpoints */
897 if (usb_pipein(pipe))
developer497dcfa2020-09-08 18:59:59 +0200898 field = TRB_ISP | TRB_TYPE(TRB_DATA);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530899 else
developer497dcfa2020-09-08 18:59:59 +0200900 field = TRB_TYPE(TRB_DATA);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530901
developer570c2a92020-09-08 18:59:56 +0200902 remainder = xhci_td_remainder(ctrl, 0, length, length,
903 usb_maxpacket(udev, pipe), true);
developer6cabb142020-09-08 19:00:00 +0200904 length_field = TRB_LEN(length) | TRB_TD_SIZE(remainder) |
905 TRB_INTR_TARGET(0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530906 debug("length_field = %d, length = %d,"
907 "xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n",
developer6cabb142020-09-08 19:00:00 +0200908 length_field, TRB_LEN(length),
developer570c2a92020-09-08 18:59:56 +0200909 TRB_TD_SIZE(remainder), 0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530910
911 if (length > 0) {
912 if (req->requesttype & USB_DIR_IN)
913 field |= TRB_DIR_IN;
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100914 buf_64 = xhci_virt_to_bus(ctrl, buffer);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530915
916 trb_fields[0] = lower_32_bits(buf_64);
917 trb_fields[1] = upper_32_bits(buf_64);
918 trb_fields[2] = length_field;
919 trb_fields[3] = field | ep_ring->cycle_state;
920
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300921 xhci_flush_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530922 queue_trb(ctrl, ep_ring, true, trb_fields);
923 }
924
925 /*
926 * Queue status TRB -
927 * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3
928 */
929
930 /* If the device sent data, the status stage is an OUT transfer */
931 field = 0;
932 if (length > 0 && req->requesttype & USB_DIR_IN)
933 field = 0;
934 else
935 field = TRB_DIR_IN;
936
937 trb_fields[0] = 0;
938 trb_fields[1] = 0;
developer6cabb142020-09-08 19:00:00 +0200939 trb_fields[2] = TRB_INTR_TARGET(0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530940 /* Event on completion */
941 trb_fields[3] = field | TRB_IOC |
developer497dcfa2020-09-08 18:59:59 +0200942 TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530943
944 queue_trb(ctrl, ep_ring, false, trb_fields);
945
946 giveback_first_trb(udev, ep_index, start_cycle, start_trb);
947
948 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
949 if (!event)
950 goto abort;
951 field = le32_to_cpu(event->trans_event.flags);
952
953 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
954 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
955
956 record_transfer_result(udev, event, length);
957 xhci_acknowledge_event(ctrl);
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200958 if (udev->status == USB_ST_STALLED) {
959 reset_ep(udev, ep_index);
960 return -EPIPE;
961 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530962
963 /* Invalidate buffer to make it available to usb-core */
964 if (length > 0)
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300965 xhci_inval_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530966
967 if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))
968 == COMP_SHORT_TX) {
969 /* Short data stage, clear up additional status stage event */
970 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
971 if (!event)
972 goto abort;
973 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
974 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
975 xhci_acknowledge_event(ctrl);
976 }
977
978 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
979
980abort:
981 debug("XHCI control transfer timed out, aborting...\n");
982 abort_td(udev, ep_index);
983 udev->status = USB_ST_NAK_REC;
984 udev->act_len = 0;
985 return -ETIMEDOUT;
986}