blob: 664e1f98d68f2e5c93d9b028da7345b2c761d5d4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov236dd4d2009-01-10 05:03:21 +03002/*
3 * Freescale QUICC Engine USB Host Controller Driver
4 *
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>
12 *
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.
17 */
18
19#include <linux/kernel.h>
20#include <linux/types.h>
21#include <linux/spinlock.h>
22#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030024#include <linux/list.h>
25#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020026#include <linux/usb/hcd.h>
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030027#include "fhci.h"
28
29/* maps the hardware error code to the USB error code */
30static int status_to_error(u32 status)
31{
32 if (status == USB_TD_OK)
33 return 0;
34 else if (status & USB_TD_RX_ER_CRC)
35 return -EILSEQ;
36 else if (status & USB_TD_RX_ER_NONOCT)
37 return -EPROTO;
38 else if (status & USB_TD_RX_ER_OVERUN)
39 return -ECOMM;
40 else if (status & USB_TD_RX_ER_BITSTUFF)
41 return -EPROTO;
42 else if (status & USB_TD_RX_ER_PID)
43 return -EILSEQ;
44 else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
45 return -ETIMEDOUT;
46 else if (status & USB_TD_TX_ER_STALL)
47 return -EPIPE;
48 else if (status & USB_TD_TX_ER_UNDERUN)
49 return -ENOSR;
50 else if (status & USB_TD_RX_DATA_UNDERUN)
51 return -EREMOTEIO;
52 else if (status & USB_TD_RX_DATA_OVERUN)
53 return -EOVERFLOW;
54 else
55 return -EINVAL;
56}
57
58void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
59{
60 list_add_tail(&td->frame_lh, &frame->tds_list);
61}
62
63void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
64{
65 int i;
66
67 for (i = 0; i < number; i++) {
68 struct td *td = td_list[i];
69 list_add_tail(&td->node, &ed->td_list);
70 }
71 if (ed->td_head == NULL)
72 ed->td_head = td_list[0];
73}
74
75static struct td *peek_td_from_ed(struct ed *ed)
76{
77 struct td *td;
78
79 if (!list_empty(&ed->td_list))
80 td = list_entry(ed->td_list.next, struct td, node);
81 else
82 td = NULL;
83
84 return td;
85}
86
87struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
88{
89 struct td *td;
90
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);
94 } else
95 td = NULL;
96
97 return td;
98}
99
100struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
101{
102 struct td *td;
103
104 if (!list_empty(&frame->tds_list))
105 td = list_entry(frame->tds_list.next, struct td, frame_lh);
106 else
107 td = NULL;
108
109 return td;
110}
111
112struct td *fhci_remove_td_from_ed(struct ed *ed)
113{
114 struct td *td;
115
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);
119
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,
123 node);
124 else
125 ed->td_head = NULL;
126 } else
127 td = NULL;
128
129 return td;
130}
131
132struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
133{
134 struct td *td;
135
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);
139 } else
140 td = NULL;
141
142 return td;
143}
144
145void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
146{
147 struct td *td;
148
149 td = ed->td_head;
150 list_del_init(&td->node);
151
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);
155 else {
156 ed->td_head = NULL;
157 ed->state = FHCI_ED_SKIP;
158 }
159 ed->toggle_carry = td->toggle;
160 list_add_tail(&td->node, &usb->hc_list->done_list);
161 if (td->ioc)
162 usb->transfer_confirm(usb->fhci);
163}
164
165/* free done FHCI URB resource such as ED and TD */
166static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
167{
168 int i;
169 struct urb_priv *urb_priv = urb->hcpriv;
170 struct ed *ed = urb_priv->ed;
171
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]);
175 }
176
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);
180 else
181 ed->td_head = NULL;
182
183 kfree(urb_priv->tds);
184 kfree(urb_priv);
185 urb->hcpriv = NULL;
186
187 /* if this TD was the ED's head,find next TD */
188 if (ed->td_head == NULL)
189 list_del_init(&ed->node);
190 fhci->active_urbs--;
191}
192
193/* this routine called to complete and free done URB */
194void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
195{
196 free_urb_priv(fhci, urb);
197
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;
202 else
203 urb->status = 0;
204 }
205
206 usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
207
208 spin_unlock(&fhci->lock);
209
210 usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
211
212 spin_lock(&fhci->lock);
213}
214
215/*
216 * caculate transfer length/stats and update the urb
217 * Precondition: irqsafe(only for urb-?status locking)
218 */
219void fhci_done_td(struct urb *urb, struct td *td)
220{
221 struct ed *ed = td->ed;
222 u32 cc = td->status;
223
224 /* ISO...drivers see per-TD length/status */
225 if (ed->mode == FHCI_TF_ISO) {
226 u32 len;
227 if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
228 cc == USB_TD_RX_DATA_UNDERUN))
229 cc = USB_TD_OK;
230
231 if (usb_pipeout(urb->pipe))
232 len = urb->iso_frame_desc[td->iso_index].length;
233 else
234 len = td->actual_len;
235
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 =
239 status_to_error(cc);
240 }
241
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.
245 */
246 else {
247 if (td->error_cnt >= 3)
248 urb->error_count = 3;
249
250 /* control endpoint only have soft stalls */
251
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;
256 cc = USB_TD_OK;
257 }
258 if (cc != USB_TD_OK) {
259 if (urb->status == -EINPROGRESS)
260 urb->status = status_to_error(cc);
261 }
262
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;
266 }
267}
268
269/* there are some pedning request to unlink */
270void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
271{
272 struct td *td = peek_td_from_ed(ed);
273 struct urb *urb = td->urb;
274 struct urb_priv *urb_priv = urb->hcpriv;
275
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);
281
282 /* URB is done;clean up */
283 if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
284 fhci_urb_complete_free(fhci, urb);
285 }
286}