xref: /openbmc/linux/drivers/usb/host/fhci-q.c (revision 664b0bae0b87f69bc9deb098f5e0158b9cf18e04)
1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2236dd4d1SAnton Vorontsov /*
3236dd4d1SAnton Vorontsov  * Freescale QUICC Engine USB Host Controller Driver
4236dd4d1SAnton Vorontsov  *
5236dd4d1SAnton Vorontsov  * Copyright (c) Freescale Semicondutor, Inc. 2006.
6236dd4d1SAnton Vorontsov  *               Shlomi Gridish <gridish@freescale.com>
7236dd4d1SAnton Vorontsov  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
8236dd4d1SAnton Vorontsov  * Copyright (c) Logic Product Development, Inc. 2007
9236dd4d1SAnton Vorontsov  *               Peter Barada <peterb@logicpd.com>
10236dd4d1SAnton Vorontsov  * Copyright (c) MontaVista Software, Inc. 2008.
11236dd4d1SAnton Vorontsov  *               Anton Vorontsov <avorontsov@ru.mvista.com>
12236dd4d1SAnton Vorontsov  */
13236dd4d1SAnton Vorontsov 
14236dd4d1SAnton Vorontsov #include <linux/kernel.h>
15236dd4d1SAnton Vorontsov #include <linux/types.h>
16236dd4d1SAnton Vorontsov #include <linux/spinlock.h>
17236dd4d1SAnton Vorontsov #include <linux/errno.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
19236dd4d1SAnton Vorontsov #include <linux/list.h>
20236dd4d1SAnton Vorontsov #include <linux/usb.h>
2127729aadSEric Lescouet #include <linux/usb/hcd.h>
22236dd4d1SAnton Vorontsov #include "fhci.h"
23236dd4d1SAnton Vorontsov 
24236dd4d1SAnton Vorontsov /* maps the hardware error code to the USB error code */
status_to_error(u32 status)25236dd4d1SAnton Vorontsov static int status_to_error(u32 status)
26236dd4d1SAnton Vorontsov {
27236dd4d1SAnton Vorontsov 	if (status == USB_TD_OK)
28236dd4d1SAnton Vorontsov 		return 0;
29236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_ER_CRC)
30236dd4d1SAnton Vorontsov 		return -EILSEQ;
31236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_ER_NONOCT)
32236dd4d1SAnton Vorontsov 		return -EPROTO;
33236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_ER_OVERUN)
34236dd4d1SAnton Vorontsov 		return -ECOMM;
35236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_ER_BITSTUFF)
36236dd4d1SAnton Vorontsov 		return -EPROTO;
37236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_ER_PID)
38236dd4d1SAnton Vorontsov 		return -EILSEQ;
39236dd4d1SAnton Vorontsov 	else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
40236dd4d1SAnton Vorontsov 		return -ETIMEDOUT;
41236dd4d1SAnton Vorontsov 	else if (status & USB_TD_TX_ER_STALL)
42236dd4d1SAnton Vorontsov 		return -EPIPE;
43236dd4d1SAnton Vorontsov 	else if (status & USB_TD_TX_ER_UNDERUN)
44236dd4d1SAnton Vorontsov 		return -ENOSR;
45236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_DATA_UNDERUN)
46236dd4d1SAnton Vorontsov 		return -EREMOTEIO;
47236dd4d1SAnton Vorontsov 	else if (status & USB_TD_RX_DATA_OVERUN)
48236dd4d1SAnton Vorontsov 		return -EOVERFLOW;
49236dd4d1SAnton Vorontsov 	else
50236dd4d1SAnton Vorontsov 		return -EINVAL;
51236dd4d1SAnton Vorontsov }
52236dd4d1SAnton Vorontsov 
fhci_add_td_to_frame(struct fhci_time_frame * frame,struct td * td)53236dd4d1SAnton Vorontsov void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
54236dd4d1SAnton Vorontsov {
55236dd4d1SAnton Vorontsov 	list_add_tail(&td->frame_lh, &frame->tds_list);
56236dd4d1SAnton Vorontsov }
57236dd4d1SAnton Vorontsov 
fhci_add_tds_to_ed(struct ed * ed,struct td ** td_list,int number)58236dd4d1SAnton Vorontsov void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
59236dd4d1SAnton Vorontsov {
60236dd4d1SAnton Vorontsov 	int i;
61236dd4d1SAnton Vorontsov 
62236dd4d1SAnton Vorontsov 	for (i = 0; i < number; i++) {
63236dd4d1SAnton Vorontsov 		struct td *td = td_list[i];
64236dd4d1SAnton Vorontsov 		list_add_tail(&td->node, &ed->td_list);
65236dd4d1SAnton Vorontsov 	}
66236dd4d1SAnton Vorontsov 	if (ed->td_head == NULL)
67236dd4d1SAnton Vorontsov 		ed->td_head = td_list[0];
68236dd4d1SAnton Vorontsov }
69236dd4d1SAnton Vorontsov 
peek_td_from_ed(struct ed * ed)70236dd4d1SAnton Vorontsov static struct td *peek_td_from_ed(struct ed *ed)
71236dd4d1SAnton Vorontsov {
72236dd4d1SAnton Vorontsov 	struct td *td;
73236dd4d1SAnton Vorontsov 
74236dd4d1SAnton Vorontsov 	if (!list_empty(&ed->td_list))
75236dd4d1SAnton Vorontsov 		td = list_entry(ed->td_list.next, struct td, node);
76236dd4d1SAnton Vorontsov 	else
77236dd4d1SAnton Vorontsov 		td = NULL;
78236dd4d1SAnton Vorontsov 
79236dd4d1SAnton Vorontsov 	return td;
80236dd4d1SAnton Vorontsov }
81236dd4d1SAnton Vorontsov 
fhci_remove_td_from_frame(struct fhci_time_frame * frame)82236dd4d1SAnton Vorontsov struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
83236dd4d1SAnton Vorontsov {
84236dd4d1SAnton Vorontsov 	struct td *td;
85236dd4d1SAnton Vorontsov 
86236dd4d1SAnton Vorontsov 	if (!list_empty(&frame->tds_list)) {
87236dd4d1SAnton Vorontsov 		td = list_entry(frame->tds_list.next, struct td, frame_lh);
88236dd4d1SAnton Vorontsov 		list_del_init(frame->tds_list.next);
89236dd4d1SAnton Vorontsov 	} else
90236dd4d1SAnton Vorontsov 		td = NULL;
91236dd4d1SAnton Vorontsov 
92236dd4d1SAnton Vorontsov 	return td;
93236dd4d1SAnton Vorontsov }
94236dd4d1SAnton Vorontsov 
fhci_peek_td_from_frame(struct fhci_time_frame * frame)95236dd4d1SAnton Vorontsov struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
96236dd4d1SAnton Vorontsov {
97236dd4d1SAnton Vorontsov 	struct td *td;
98236dd4d1SAnton Vorontsov 
99236dd4d1SAnton Vorontsov 	if (!list_empty(&frame->tds_list))
100236dd4d1SAnton Vorontsov 		td = list_entry(frame->tds_list.next, struct td, frame_lh);
101236dd4d1SAnton Vorontsov 	else
102236dd4d1SAnton Vorontsov 		td = NULL;
103236dd4d1SAnton Vorontsov 
104236dd4d1SAnton Vorontsov 	return td;
105236dd4d1SAnton Vorontsov }
106236dd4d1SAnton Vorontsov 
fhci_remove_td_from_ed(struct ed * ed)107236dd4d1SAnton Vorontsov struct td *fhci_remove_td_from_ed(struct ed *ed)
108236dd4d1SAnton Vorontsov {
109236dd4d1SAnton Vorontsov 	struct td *td;
110236dd4d1SAnton Vorontsov 
111236dd4d1SAnton Vorontsov 	if (!list_empty(&ed->td_list)) {
112236dd4d1SAnton Vorontsov 		td = list_entry(ed->td_list.next, struct td, node);
113236dd4d1SAnton Vorontsov 		list_del_init(ed->td_list.next);
114236dd4d1SAnton Vorontsov 
115236dd4d1SAnton Vorontsov 		/* if this TD was the ED's head, find next TD */
116236dd4d1SAnton Vorontsov 		if (!list_empty(&ed->td_list))
117236dd4d1SAnton Vorontsov 			ed->td_head = list_entry(ed->td_list.next, struct td,
118236dd4d1SAnton Vorontsov 						 node);
119236dd4d1SAnton Vorontsov 		else
120236dd4d1SAnton Vorontsov 			ed->td_head = NULL;
121236dd4d1SAnton Vorontsov 	} else
122236dd4d1SAnton Vorontsov 		td = NULL;
123236dd4d1SAnton Vorontsov 
124236dd4d1SAnton Vorontsov 	return td;
125236dd4d1SAnton Vorontsov }
126236dd4d1SAnton Vorontsov 
fhci_remove_td_from_done_list(struct fhci_controller_list * p_list)127236dd4d1SAnton Vorontsov struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
128236dd4d1SAnton Vorontsov {
129236dd4d1SAnton Vorontsov 	struct td *td;
130236dd4d1SAnton Vorontsov 
131236dd4d1SAnton Vorontsov 	if (!list_empty(&p_list->done_list)) {
132236dd4d1SAnton Vorontsov 		td = list_entry(p_list->done_list.next, struct td, node);
133236dd4d1SAnton Vorontsov 		list_del_init(p_list->done_list.next);
134236dd4d1SAnton Vorontsov 	} else
135236dd4d1SAnton Vorontsov 		td = NULL;
136236dd4d1SAnton Vorontsov 
137236dd4d1SAnton Vorontsov 	return td;
138236dd4d1SAnton Vorontsov }
139236dd4d1SAnton Vorontsov 
fhci_move_td_from_ed_to_done_list(struct fhci_usb * usb,struct ed * ed)140236dd4d1SAnton Vorontsov void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
141236dd4d1SAnton Vorontsov {
142236dd4d1SAnton Vorontsov 	struct td *td;
143236dd4d1SAnton Vorontsov 
144236dd4d1SAnton Vorontsov 	td = ed->td_head;
145236dd4d1SAnton Vorontsov 	list_del_init(&td->node);
146236dd4d1SAnton Vorontsov 
147236dd4d1SAnton Vorontsov 	/* If this TD was the ED's head,find next TD */
148236dd4d1SAnton Vorontsov 	if (!list_empty(&ed->td_list))
149236dd4d1SAnton Vorontsov 		ed->td_head = list_entry(ed->td_list.next, struct td, node);
150236dd4d1SAnton Vorontsov 	else {
151236dd4d1SAnton Vorontsov 		ed->td_head = NULL;
152236dd4d1SAnton Vorontsov 		ed->state = FHCI_ED_SKIP;
153236dd4d1SAnton Vorontsov 	}
154236dd4d1SAnton Vorontsov 	ed->toggle_carry = td->toggle;
155236dd4d1SAnton Vorontsov 	list_add_tail(&td->node, &usb->hc_list->done_list);
156236dd4d1SAnton Vorontsov 	if (td->ioc)
157236dd4d1SAnton Vorontsov 		usb->transfer_confirm(usb->fhci);
158236dd4d1SAnton Vorontsov }
159236dd4d1SAnton Vorontsov 
160236dd4d1SAnton Vorontsov /* free done FHCI URB resource such as ED and TD */
free_urb_priv(struct fhci_hcd * fhci,struct urb * urb)161236dd4d1SAnton Vorontsov static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
162236dd4d1SAnton Vorontsov {
163236dd4d1SAnton Vorontsov 	int i;
164236dd4d1SAnton Vorontsov 	struct urb_priv *urb_priv = urb->hcpriv;
165236dd4d1SAnton Vorontsov 	struct ed *ed = urb_priv->ed;
166236dd4d1SAnton Vorontsov 
167236dd4d1SAnton Vorontsov 	for (i = 0; i < urb_priv->num_of_tds; i++) {
168236dd4d1SAnton Vorontsov 		list_del_init(&urb_priv->tds[i]->node);
169236dd4d1SAnton Vorontsov 		fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
170236dd4d1SAnton Vorontsov 	}
171236dd4d1SAnton Vorontsov 
172236dd4d1SAnton Vorontsov 	/* if this TD was the ED's head,find the next TD */
173236dd4d1SAnton Vorontsov 	if (!list_empty(&ed->td_list))
174236dd4d1SAnton Vorontsov 		ed->td_head = list_entry(ed->td_list.next, struct td, node);
175236dd4d1SAnton Vorontsov 	else
176236dd4d1SAnton Vorontsov 		ed->td_head = NULL;
177236dd4d1SAnton Vorontsov 
178236dd4d1SAnton Vorontsov 	kfree(urb_priv->tds);
179236dd4d1SAnton Vorontsov 	kfree(urb_priv);
180236dd4d1SAnton Vorontsov 	urb->hcpriv = NULL;
181236dd4d1SAnton Vorontsov 
182236dd4d1SAnton Vorontsov 	/* if this TD was the ED's head,find next TD */
183236dd4d1SAnton Vorontsov 	if (ed->td_head == NULL)
184236dd4d1SAnton Vorontsov 		list_del_init(&ed->node);
185236dd4d1SAnton Vorontsov 	fhci->active_urbs--;
186236dd4d1SAnton Vorontsov }
187236dd4d1SAnton Vorontsov 
188236dd4d1SAnton Vorontsov /* this routine called to complete and free done URB */
fhci_urb_complete_free(struct fhci_hcd * fhci,struct urb * urb)189236dd4d1SAnton Vorontsov void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
190236dd4d1SAnton Vorontsov {
191236dd4d1SAnton Vorontsov 	free_urb_priv(fhci, urb);
192236dd4d1SAnton Vorontsov 
193236dd4d1SAnton Vorontsov 	if (urb->status == -EINPROGRESS) {
194236dd4d1SAnton Vorontsov 		if (urb->actual_length != urb->transfer_buffer_length &&
195236dd4d1SAnton Vorontsov 				urb->transfer_flags & URB_SHORT_NOT_OK)
196236dd4d1SAnton Vorontsov 			urb->status = -EREMOTEIO;
197236dd4d1SAnton Vorontsov 		else
198236dd4d1SAnton Vorontsov 			urb->status = 0;
199236dd4d1SAnton Vorontsov 	}
200236dd4d1SAnton Vorontsov 
201236dd4d1SAnton Vorontsov 	usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
202236dd4d1SAnton Vorontsov 
203236dd4d1SAnton Vorontsov 	spin_unlock(&fhci->lock);
204236dd4d1SAnton Vorontsov 
205236dd4d1SAnton Vorontsov 	usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
206236dd4d1SAnton Vorontsov 
207236dd4d1SAnton Vorontsov 	spin_lock(&fhci->lock);
208236dd4d1SAnton Vorontsov }
209236dd4d1SAnton Vorontsov 
210236dd4d1SAnton Vorontsov /*
211236dd4d1SAnton Vorontsov  * caculate transfer length/stats and update the urb
212236dd4d1SAnton Vorontsov  * Precondition: irqsafe(only for urb-?status locking)
213236dd4d1SAnton Vorontsov  */
fhci_done_td(struct urb * urb,struct td * td)214236dd4d1SAnton Vorontsov void fhci_done_td(struct urb *urb, struct td *td)
215236dd4d1SAnton Vorontsov {
216236dd4d1SAnton Vorontsov 	struct ed *ed = td->ed;
217236dd4d1SAnton Vorontsov 	u32 cc = td->status;
218236dd4d1SAnton Vorontsov 
219236dd4d1SAnton Vorontsov 	/* ISO...drivers see per-TD length/status */
220236dd4d1SAnton Vorontsov 	if (ed->mode == FHCI_TF_ISO) {
221236dd4d1SAnton Vorontsov 		u32 len;
222236dd4d1SAnton Vorontsov 		if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
223236dd4d1SAnton Vorontsov 				cc == USB_TD_RX_DATA_UNDERUN))
224236dd4d1SAnton Vorontsov 			cc = USB_TD_OK;
225236dd4d1SAnton Vorontsov 
226236dd4d1SAnton Vorontsov 		if (usb_pipeout(urb->pipe))
227236dd4d1SAnton Vorontsov 			len = urb->iso_frame_desc[td->iso_index].length;
228236dd4d1SAnton Vorontsov 		else
229236dd4d1SAnton Vorontsov 			len = td->actual_len;
230236dd4d1SAnton Vorontsov 
231236dd4d1SAnton Vorontsov 		urb->actual_length += len;
232236dd4d1SAnton Vorontsov 		urb->iso_frame_desc[td->iso_index].actual_length = len;
233236dd4d1SAnton Vorontsov 		urb->iso_frame_desc[td->iso_index].status =
234236dd4d1SAnton Vorontsov 			status_to_error(cc);
235236dd4d1SAnton Vorontsov 	}
236236dd4d1SAnton Vorontsov 
237236dd4d1SAnton Vorontsov 	/* BULK,INT,CONTROL... drivers see aggregate length/status,
238236dd4d1SAnton Vorontsov 	 * except that "setup" bytes aren't counted and "short" transfers
239236dd4d1SAnton Vorontsov 	 * might not be reported as errors.
240236dd4d1SAnton Vorontsov 	 */
241236dd4d1SAnton Vorontsov 	else {
242236dd4d1SAnton Vorontsov 		if (td->error_cnt >= 3)
243236dd4d1SAnton Vorontsov 			urb->error_count = 3;
244236dd4d1SAnton Vorontsov 
245236dd4d1SAnton Vorontsov 		/* control endpoint only have soft stalls */
246236dd4d1SAnton Vorontsov 
247236dd4d1SAnton Vorontsov 		/* update packet status if needed(short may be ok) */
248236dd4d1SAnton Vorontsov 		if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
249236dd4d1SAnton Vorontsov 				cc == USB_TD_RX_DATA_UNDERUN) {
250236dd4d1SAnton Vorontsov 			ed->state = FHCI_ED_OPER;
251236dd4d1SAnton Vorontsov 			cc = USB_TD_OK;
252236dd4d1SAnton Vorontsov 		}
253236dd4d1SAnton Vorontsov 		if (cc != USB_TD_OK) {
254236dd4d1SAnton Vorontsov 			if (urb->status == -EINPROGRESS)
255236dd4d1SAnton Vorontsov 				urb->status = status_to_error(cc);
256236dd4d1SAnton Vorontsov 		}
257236dd4d1SAnton Vorontsov 
258236dd4d1SAnton Vorontsov 		/* count all non-empty packets except control SETUP packet */
259236dd4d1SAnton Vorontsov 		if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
260236dd4d1SAnton Vorontsov 			urb->actual_length += td->actual_len;
261236dd4d1SAnton Vorontsov 	}
262236dd4d1SAnton Vorontsov }
263236dd4d1SAnton Vorontsov 
264236dd4d1SAnton Vorontsov /* there are some pedning request to unlink */
fhci_del_ed_list(struct fhci_hcd * fhci,struct ed * ed)265236dd4d1SAnton Vorontsov void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
266236dd4d1SAnton Vorontsov {
267236dd4d1SAnton Vorontsov 	struct td *td = peek_td_from_ed(ed);
268236dd4d1SAnton Vorontsov 	struct urb *urb = td->urb;
269236dd4d1SAnton Vorontsov 	struct urb_priv *urb_priv = urb->hcpriv;
270236dd4d1SAnton Vorontsov 
271236dd4d1SAnton Vorontsov 	if (urb_priv->state == URB_DEL) {
272236dd4d1SAnton Vorontsov 		td = fhci_remove_td_from_ed(ed);
273236dd4d1SAnton Vorontsov 		/* HC may have partly processed this TD */
274236dd4d1SAnton Vorontsov 		if (td->status != USB_TD_INPROGRESS)
275236dd4d1SAnton Vorontsov 			fhci_done_td(urb, td);
276236dd4d1SAnton Vorontsov 
277236dd4d1SAnton Vorontsov 		/* URB is done;clean up */
278236dd4d1SAnton Vorontsov 		if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
279236dd4d1SAnton Vorontsov 			fhci_urb_complete_free(fhci, urb);
280236dd4d1SAnton Vorontsov 	}
281236dd4d1SAnton Vorontsov }
282