1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale QUICC Engine USB Host Controller Driver
5 * Copyright (c) Freescale Semicondutor, Inc. 2006.
6 * Shlomi Gridish <gridish@freescale.com>
7 * Jerry Huang <Chang-Ming.Huang@freescale.com>
8 * Copyright (c) Logic Product Development, Inc. 2007
9 * Peter Barada <peterb@logicpd.com>
10 * Copyright (c) MontaVista Software, Inc. 2008.
11 * Anton Vorontsov <avorontsov@ru.mvista.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/spinlock.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
25 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
29 /* maps the hardware error code to the USB error code */
30 static int status_to_error(u32 status)
32 if (status == USB_TD_OK)
34 else if (status & USB_TD_RX_ER_CRC)
36 else if (status & USB_TD_RX_ER_NONOCT)
38 else if (status & USB_TD_RX_ER_OVERUN)
40 else if (status & USB_TD_RX_ER_BITSTUFF)
42 else if (status & USB_TD_RX_ER_PID)
44 else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
46 else if (status & USB_TD_TX_ER_STALL)
48 else if (status & USB_TD_TX_ER_UNDERUN)
50 else if (status & USB_TD_RX_DATA_UNDERUN)
52 else if (status & USB_TD_RX_DATA_OVERUN)
58 void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
60 list_add_tail(&td->frame_lh, &frame->tds_list);
63 void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
67 for (i = 0; i < number; i++) {
68 struct td *td = td_list[i];
69 list_add_tail(&td->node, &ed->td_list);
71 if (ed->td_head == NULL)
72 ed->td_head = td_list[0];
75 static struct td *peek_td_from_ed(struct ed *ed)
79 if (!list_empty(&ed->td_list))
80 td = list_entry(ed->td_list.next, struct td, node);
87 struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
91 if (!list_empty(&frame->tds_list)) {
92 td = list_entry(frame->tds_list.next, struct td, frame_lh);
93 list_del_init(frame->tds_list.next);
100 struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
104 if (!list_empty(&frame->tds_list))
105 td = list_entry(frame->tds_list.next, struct td, frame_lh);
112 struct td *fhci_remove_td_from_ed(struct ed *ed)
116 if (!list_empty(&ed->td_list)) {
117 td = list_entry(ed->td_list.next, struct td, node);
118 list_del_init(ed->td_list.next);
120 /* if this TD was the ED's head, find next TD */
121 if (!list_empty(&ed->td_list))
122 ed->td_head = list_entry(ed->td_list.next, struct td,
132 struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
136 if (!list_empty(&p_list->done_list)) {
137 td = list_entry(p_list->done_list.next, struct td, node);
138 list_del_init(p_list->done_list.next);
145 void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
150 list_del_init(&td->node);
152 /* If this TD was the ED's head,find next TD */
153 if (!list_empty(&ed->td_list))
154 ed->td_head = list_entry(ed->td_list.next, struct td, node);
157 ed->state = FHCI_ED_SKIP;
159 ed->toggle_carry = td->toggle;
160 list_add_tail(&td->node, &usb->hc_list->done_list);
162 usb->transfer_confirm(usb->fhci);
165 /* free done FHCI URB resource such as ED and TD */
166 static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
169 struct urb_priv *urb_priv = urb->hcpriv;
170 struct ed *ed = urb_priv->ed;
172 for (i = 0; i < urb_priv->num_of_tds; i++) {
173 list_del_init(&urb_priv->tds[i]->node);
174 fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
177 /* if this TD was the ED's head,find the next TD */
178 if (!list_empty(&ed->td_list))
179 ed->td_head = list_entry(ed->td_list.next, struct td, node);
183 kfree(urb_priv->tds);
187 /* if this TD was the ED's head,find next TD */
188 if (ed->td_head == NULL)
189 list_del_init(&ed->node);
193 /* this routine called to complete and free done URB */
194 void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
196 free_urb_priv(fhci, urb);
198 if (urb->status == -EINPROGRESS) {
199 if (urb->actual_length != urb->transfer_buffer_length &&
200 urb->transfer_flags & URB_SHORT_NOT_OK)
201 urb->status = -EREMOTEIO;
206 usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
208 spin_unlock(&fhci->lock);
210 usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
212 spin_lock(&fhci->lock);
216 * caculate transfer length/stats and update the urb
217 * Precondition: irqsafe(only for urb-?status locking)
219 void fhci_done_td(struct urb *urb, struct td *td)
221 struct ed *ed = td->ed;
224 /* ISO...drivers see per-TD length/status */
225 if (ed->mode == FHCI_TF_ISO) {
227 if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
228 cc == USB_TD_RX_DATA_UNDERUN))
231 if (usb_pipeout(urb->pipe))
232 len = urb->iso_frame_desc[td->iso_index].length;
234 len = td->actual_len;
236 urb->actual_length += len;
237 urb->iso_frame_desc[td->iso_index].actual_length = len;
238 urb->iso_frame_desc[td->iso_index].status =
242 /* BULK,INT,CONTROL... drivers see aggregate length/status,
243 * except that "setup" bytes aren't counted and "short" transfers
244 * might not be reported as errors.
247 if (td->error_cnt >= 3)
248 urb->error_count = 3;
250 /* control endpoint only have soft stalls */
252 /* update packet status if needed(short may be ok) */
253 if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
254 cc == USB_TD_RX_DATA_UNDERUN) {
255 ed->state = FHCI_ED_OPER;
258 if (cc != USB_TD_OK) {
259 if (urb->status == -EINPROGRESS)
260 urb->status = status_to_error(cc);
263 /* count all non-empty packets except control SETUP packet */
264 if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
265 urb->actual_length += td->actual_len;
269 /* there are some pedning request to unlink */
270 void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
272 struct td *td = peek_td_from_ed(ed);
273 struct urb *urb = td->urb;
274 struct urb_priv *urb_priv = urb->hcpriv;
276 if (urb_priv->state == URB_DEL) {
277 td = fhci_remove_td_from_ed(ed);
278 /* HC may have partly processed this TD */
279 if (td->status != USB_TD_INPROGRESS)
280 fhci_done_td(urb, td);
282 /* URB is done;clean up */
283 if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
284 fhci_urb_complete_free(fhci, urb);