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