Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  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
 14#include <linux/kernel.h>
 15#include <linux/types.h>
 16#include <linux/spinlock.h>
 17#include <linux/errno.h>
 18#include <linux/slab.h>
 19#include <linux/list.h>
 20#include <linux/usb.h>
 21#include <linux/usb/hcd.h>
 22#include "fhci.h"
 23
 24/* maps the hardware error code to the USB error code */
 25static int status_to_error(u32 status)
 26{
 27	if (status == USB_TD_OK)
 28		return 0;
 29	else if (status & USB_TD_RX_ER_CRC)
 30		return -EILSEQ;
 31	else if (status & USB_TD_RX_ER_NONOCT)
 32		return -EPROTO;
 33	else if (status & USB_TD_RX_ER_OVERUN)
 34		return -ECOMM;
 35	else if (status & USB_TD_RX_ER_BITSTUFF)
 36		return -EPROTO;
 37	else if (status & USB_TD_RX_ER_PID)
 38		return -EILSEQ;
 39	else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
 40		return -ETIMEDOUT;
 41	else if (status & USB_TD_TX_ER_STALL)
 42		return -EPIPE;
 43	else if (status & USB_TD_TX_ER_UNDERUN)
 44		return -ENOSR;
 45	else if (status & USB_TD_RX_DATA_UNDERUN)
 46		return -EREMOTEIO;
 47	else if (status & USB_TD_RX_DATA_OVERUN)
 48		return -EOVERFLOW;
 49	else
 50		return -EINVAL;
 51}
 52
 53void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
 54{
 55	list_add_tail(&td->frame_lh, &frame->tds_list);
 56}
 57
 58void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
 59{
 60	int i;
 61
 62	for (i = 0; i < number; i++) {
 63		struct td *td = td_list[i];
 64		list_add_tail(&td->node, &ed->td_list);
 65	}
 66	if (ed->td_head == NULL)
 67		ed->td_head = td_list[0];
 68}
 69
 70static struct td *peek_td_from_ed(struct ed *ed)
 71{
 72	struct td *td;
 73
 74	if (!list_empty(&ed->td_list))
 75		td = list_entry(ed->td_list.next, struct td, node);
 76	else
 77		td = NULL;
 78
 79	return td;
 80}
 81
 82struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
 83{
 84	struct td *td;
 85
 86	if (!list_empty(&frame->tds_list)) {
 87		td = list_entry(frame->tds_list.next, struct td, frame_lh);
 88		list_del_init(frame->tds_list.next);
 89	} else
 90		td = NULL;
 91
 92	return td;
 93}
 94
 95struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
 96{
 97	struct td *td;
 98
 99	if (!list_empty(&frame->tds_list))
100		td = list_entry(frame->tds_list.next, struct td, frame_lh);
101	else
102		td = NULL;
103
104	return td;
105}
106
107struct td *fhci_remove_td_from_ed(struct ed *ed)
108{
109	struct td *td;
110
111	if (!list_empty(&ed->td_list)) {
112		td = list_entry(ed->td_list.next, struct td, node);
113		list_del_init(ed->td_list.next);
114
115		/* if this TD was the ED's head, find next TD */
116		if (!list_empty(&ed->td_list))
117			ed->td_head = list_entry(ed->td_list.next, struct td,
118						 node);
119		else
120			ed->td_head = NULL;
121	} else
122		td = NULL;
123
124	return td;
125}
126
127struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
128{
129	struct td *td;
130
131	if (!list_empty(&p_list->done_list)) {
132		td = list_entry(p_list->done_list.next, struct td, node);
133		list_del_init(p_list->done_list.next);
134	} else
135		td = NULL;
136
137	return td;
138}
139
140void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
141{
142	struct td *td;
143
144	td = ed->td_head;
145	list_del_init(&td->node);
146
147	/* If this TD was the ED's head,find next TD */
148	if (!list_empty(&ed->td_list))
149		ed->td_head = list_entry(ed->td_list.next, struct td, node);
150	else {
151		ed->td_head = NULL;
152		ed->state = FHCI_ED_SKIP;
153	}
154	ed->toggle_carry = td->toggle;
155	list_add_tail(&td->node, &usb->hc_list->done_list);
156	if (td->ioc)
157		usb->transfer_confirm(usb->fhci);
158}
159
160/* free done FHCI URB resource such as ED and TD */
161static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
162{
163	int i;
164	struct urb_priv *urb_priv = urb->hcpriv;
165	struct ed *ed = urb_priv->ed;
166
167	for (i = 0; i < urb_priv->num_of_tds; i++) {
168		list_del_init(&urb_priv->tds[i]->node);
169		fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
170	}
171
172	/* if this TD was the ED's head,find the next TD */
173	if (!list_empty(&ed->td_list))
174		ed->td_head = list_entry(ed->td_list.next, struct td, node);
175	else
176		ed->td_head = NULL;
177
178	kfree(urb_priv->tds);
179	kfree(urb_priv);
180	urb->hcpriv = NULL;
181
182	/* if this TD was the ED's head,find next TD */
183	if (ed->td_head == NULL)
184		list_del_init(&ed->node);
185	fhci->active_urbs--;
186}
187
188/* this routine called to complete and free done URB */
189void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
190{
191	free_urb_priv(fhci, urb);
192
193	if (urb->status == -EINPROGRESS) {
194		if (urb->actual_length != urb->transfer_buffer_length &&
195				urb->transfer_flags & URB_SHORT_NOT_OK)
196			urb->status = -EREMOTEIO;
197		else
198			urb->status = 0;
199	}
200
201	usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
202
203	spin_unlock(&fhci->lock);
204
205	usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
206
207	spin_lock(&fhci->lock);
208}
209
210/*
211 * caculate transfer length/stats and update the urb
212 * Precondition: irqsafe(only for urb-?status locking)
213 */
214void fhci_done_td(struct urb *urb, struct td *td)
215{
216	struct ed *ed = td->ed;
217	u32 cc = td->status;
218
219	/* ISO...drivers see per-TD length/status */
220	if (ed->mode == FHCI_TF_ISO) {
221		u32 len;
222		if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
223				cc == USB_TD_RX_DATA_UNDERUN))
224			cc = USB_TD_OK;
225
226		if (usb_pipeout(urb->pipe))
227			len = urb->iso_frame_desc[td->iso_index].length;
228		else
229			len = td->actual_len;
230
231		urb->actual_length += len;
232		urb->iso_frame_desc[td->iso_index].actual_length = len;
233		urb->iso_frame_desc[td->iso_index].status =
234			status_to_error(cc);
235	}
236
237	/* BULK,INT,CONTROL... drivers see aggregate length/status,
238	 * except that "setup" bytes aren't counted and "short" transfers
239	 * might not be reported as errors.
240	 */
241	else {
242		if (td->error_cnt >= 3)
243			urb->error_count = 3;
244
245		/* control endpoint only have soft stalls */
246
247		/* update packet status if needed(short may be ok) */
248		if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
249				cc == USB_TD_RX_DATA_UNDERUN) {
250			ed->state = FHCI_ED_OPER;
251			cc = USB_TD_OK;
252		}
253		if (cc != USB_TD_OK) {
254			if (urb->status == -EINPROGRESS)
255				urb->status = status_to_error(cc);
256		}
257
258		/* count all non-empty packets except control SETUP packet */
259		if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
260			urb->actual_length += td->actual_len;
261	}
262}
263
264/* there are some pedning request to unlink */
265void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
266{
267	struct td *td = peek_td_from_ed(ed);
268	struct urb *urb = td->urb;
269	struct urb_priv *urb_priv = urb->hcpriv;
270
271	if (urb_priv->state == URB_DEL) {
272		td = fhci_remove_td_from_ed(ed);
273		/* HC may have partly processed this TD */
274		if (td->status != USB_TD_INPROGRESS)
275			fhci_done_td(urb, td);
276
277		/* URB is done;clean up */
278		if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
279			fhci_urb_complete_free(fhci, urb);
280	}
281}
v3.1
 
  1/*
  2 * Freescale QUICC Engine USB Host Controller Driver
  3 *
  4 * Copyright (c) Freescale Semicondutor, Inc. 2006.
  5 *               Shlomi Gridish <gridish@freescale.com>
  6 *               Jerry Huang <Chang-Ming.Huang@freescale.com>
  7 * Copyright (c) Logic Product Development, Inc. 2007
  8 *               Peter Barada <peterb@logicpd.com>
  9 * Copyright (c) MontaVista Software, Inc. 2008.
 10 *               Anton Vorontsov <avorontsov@ru.mvista.com>
 11 *
 12 * This program is free software; you can redistribute  it and/or modify it
 13 * under  the terms of  the GNU General  Public License as published by the
 14 * Free Software Foundation;  either version 2 of the  License, or (at your
 15 * option) any later version.
 16 */
 17
 18#include <linux/kernel.h>
 19#include <linux/types.h>
 20#include <linux/spinlock.h>
 21#include <linux/errno.h>
 22#include <linux/slab.h>
 23#include <linux/list.h>
 24#include <linux/usb.h>
 25#include <linux/usb/hcd.h>
 26#include "fhci.h"
 27
 28/* maps the hardware error code to the USB error code */
 29static int status_to_error(u32 status)
 30{
 31	if (status == USB_TD_OK)
 32		return 0;
 33	else if (status & USB_TD_RX_ER_CRC)
 34		return -EILSEQ;
 35	else if (status & USB_TD_RX_ER_NONOCT)
 36		return -EPROTO;
 37	else if (status & USB_TD_RX_ER_OVERUN)
 38		return -ECOMM;
 39	else if (status & USB_TD_RX_ER_BITSTUFF)
 40		return -EPROTO;
 41	else if (status & USB_TD_RX_ER_PID)
 42		return -EILSEQ;
 43	else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
 44		return -ETIMEDOUT;
 45	else if (status & USB_TD_TX_ER_STALL)
 46		return -EPIPE;
 47	else if (status & USB_TD_TX_ER_UNDERUN)
 48		return -ENOSR;
 49	else if (status & USB_TD_RX_DATA_UNDERUN)
 50		return -EREMOTEIO;
 51	else if (status & USB_TD_RX_DATA_OVERUN)
 52		return -EOVERFLOW;
 53	else
 54		return -EINVAL;
 55}
 56
 57void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
 58{
 59	list_add_tail(&td->frame_lh, &frame->tds_list);
 60}
 61
 62void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
 63{
 64	int i;
 65
 66	for (i = 0; i < number; i++) {
 67		struct td *td = td_list[i];
 68		list_add_tail(&td->node, &ed->td_list);
 69	}
 70	if (ed->td_head == NULL)
 71		ed->td_head = td_list[0];
 72}
 73
 74static struct td *peek_td_from_ed(struct ed *ed)
 75{
 76	struct td *td;
 77
 78	if (!list_empty(&ed->td_list))
 79		td = list_entry(ed->td_list.next, struct td, node);
 80	else
 81		td = NULL;
 82
 83	return td;
 84}
 85
 86struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
 87{
 88	struct td *td;
 89
 90	if (!list_empty(&frame->tds_list)) {
 91		td = list_entry(frame->tds_list.next, struct td, frame_lh);
 92		list_del_init(frame->tds_list.next);
 93	} else
 94		td = NULL;
 95
 96	return td;
 97}
 98
 99struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
100{
101	struct td *td;
102
103	if (!list_empty(&frame->tds_list))
104		td = list_entry(frame->tds_list.next, struct td, frame_lh);
105	else
106		td = NULL;
107
108	return td;
109}
110
111struct td *fhci_remove_td_from_ed(struct ed *ed)
112{
113	struct td *td;
114
115	if (!list_empty(&ed->td_list)) {
116		td = list_entry(ed->td_list.next, struct td, node);
117		list_del_init(ed->td_list.next);
118
119		/* if this TD was the ED's head, find next TD */
120		if (!list_empty(&ed->td_list))
121			ed->td_head = list_entry(ed->td_list.next, struct td,
122						 node);
123		else
124			ed->td_head = NULL;
125	} else
126		td = NULL;
127
128	return td;
129}
130
131struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
132{
133	struct td *td;
134
135	if (!list_empty(&p_list->done_list)) {
136		td = list_entry(p_list->done_list.next, struct td, node);
137		list_del_init(p_list->done_list.next);
138	} else
139		td = NULL;
140
141	return td;
142}
143
144void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
145{
146	struct td *td;
147
148	td = ed->td_head;
149	list_del_init(&td->node);
150
151	/* If this TD was the ED's head,find next TD */
152	if (!list_empty(&ed->td_list))
153		ed->td_head = list_entry(ed->td_list.next, struct td, node);
154	else {
155		ed->td_head = NULL;
156		ed->state = FHCI_ED_SKIP;
157	}
158	ed->toggle_carry = td->toggle;
159	list_add_tail(&td->node, &usb->hc_list->done_list);
160	if (td->ioc)
161		usb->transfer_confirm(usb->fhci);
162}
163
164/* free done FHCI URB resource such as ED and TD */
165static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
166{
167	int i;
168	struct urb_priv *urb_priv = urb->hcpriv;
169	struct ed *ed = urb_priv->ed;
170
171	for (i = 0; i < urb_priv->num_of_tds; i++) {
172		list_del_init(&urb_priv->tds[i]->node);
173		fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
174	}
175
176	/* if this TD was the ED's head,find the next TD */
177	if (!list_empty(&ed->td_list))
178		ed->td_head = list_entry(ed->td_list.next, struct td, node);
179	else
180		ed->td_head = NULL;
181
182	kfree(urb_priv->tds);
183	kfree(urb_priv);
184	urb->hcpriv = NULL;
185
186	/* if this TD was the ED's head,find next TD */
187	if (ed->td_head == NULL)
188		list_del_init(&ed->node);
189	fhci->active_urbs--;
190}
191
192/* this routine called to complete and free done URB */
193void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
194{
195	free_urb_priv(fhci, urb);
196
197	if (urb->status == -EINPROGRESS) {
198		if (urb->actual_length != urb->transfer_buffer_length &&
199				urb->transfer_flags & URB_SHORT_NOT_OK)
200			urb->status = -EREMOTEIO;
201		else
202			urb->status = 0;
203	}
204
205	usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
206
207	spin_unlock(&fhci->lock);
208
209	usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
210
211	spin_lock(&fhci->lock);
212}
213
214/*
215 * caculate transfer length/stats and update the urb
216 * Precondition: irqsafe(only for urb-?status locking)
217 */
218void fhci_done_td(struct urb *urb, struct td *td)
219{
220	struct ed *ed = td->ed;
221	u32 cc = td->status;
222
223	/* ISO...drivers see per-TD length/status */
224	if (ed->mode == FHCI_TF_ISO) {
225		u32 len;
226		if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
227				cc == USB_TD_RX_DATA_UNDERUN))
228			cc = USB_TD_OK;
229
230		if (usb_pipeout(urb->pipe))
231			len = urb->iso_frame_desc[td->iso_index].length;
232		else
233			len = td->actual_len;
234
235		urb->actual_length += len;
236		urb->iso_frame_desc[td->iso_index].actual_length = len;
237		urb->iso_frame_desc[td->iso_index].status =
238			status_to_error(cc);
239	}
240
241	/* BULK,INT,CONTROL... drivers see aggregate length/status,
242	 * except that "setup" bytes aren't counted and "short" transfers
243	 * might not be reported as errors.
244	 */
245	else {
246		if (td->error_cnt >= 3)
247			urb->error_count = 3;
248
249		/* control endpoint only have soft stalls */
250
251		/* update packet status if needed(short may be ok) */
252		if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
253				cc == USB_TD_RX_DATA_UNDERUN) {
254			ed->state = FHCI_ED_OPER;
255			cc = USB_TD_OK;
256		}
257		if (cc != USB_TD_OK) {
258			if (urb->status == -EINPROGRESS)
259				urb->status = status_to_error(cc);
260		}
261
262		/* count all non-empty packets except control SETUP packet */
263		if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
264			urb->actual_length += td->actual_len;
265	}
266}
267
268/* there are some pedning request to unlink */
269void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
270{
271	struct td *td = peek_td_from_ed(ed);
272	struct urb *urb = td->urb;
273	struct urb_priv *urb_priv = urb->hcpriv;
274
275	if (urb_priv->state == URB_DEL) {
276		td = fhci_remove_td_from_ed(ed);
277		/* HC may have partly processed this TD */
278		if (td->status != USB_TD_INPROGRESS)
279			fhci_done_td(urb, td);
280
281		/* URB is done;clean up */
282		if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
283			fhci_urb_complete_free(fhci, urb);
284	}
285}