xref: /openbmc/linux/drivers/usb/musb/musb_virthub.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2550a7375SFelipe Balbi /*
3550a7375SFelipe Balbi  * MUSB OTG driver virtual root hub support
4550a7375SFelipe Balbi  *
5550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
6550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
7550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
8550a7375SFelipe Balbi  */
9550a7375SFelipe Balbi 
10550a7375SFelipe Balbi #include <linux/module.h>
11550a7375SFelipe Balbi #include <linux/kernel.h>
12550a7375SFelipe Balbi #include <linux/sched.h>
13550a7375SFelipe Balbi #include <linux/errno.h>
14550a7375SFelipe Balbi #include <linux/time.h>
15550a7375SFelipe Balbi #include <linux/timer.h>
16550a7375SFelipe Balbi 
17550a7375SFelipe Balbi #include <asm/unaligned.h>
18550a7375SFelipe Balbi 
19550a7375SFelipe Balbi #include "musb_core.h"
20550a7375SFelipe Balbi 
musb_host_finish_resume(struct work_struct * work)218ed1fb79SDaniel Mack void musb_host_finish_resume(struct work_struct *work)
228ed1fb79SDaniel Mack {
238ed1fb79SDaniel Mack 	struct musb *musb;
248ed1fb79SDaniel Mack 	unsigned long flags;
258ed1fb79SDaniel Mack 	u8 power;
268ed1fb79SDaniel Mack 
27fa6997d3SDaniel Mack 	musb = container_of(work, struct musb, finish_resume_work.work);
288ed1fb79SDaniel Mack 
298ed1fb79SDaniel Mack 	spin_lock_irqsave(&musb->lock, flags);
308ed1fb79SDaniel Mack 
318ed1fb79SDaniel Mack 	power = musb_readb(musb->mregs, MUSB_POWER);
328ed1fb79SDaniel Mack 	power &= ~MUSB_POWER_RESUME;
33b99d3659SBin Liu 	musb_dbg(musb, "root port resume stopped, power %02x", power);
348ed1fb79SDaniel Mack 	musb_writeb(musb->mregs, MUSB_POWER, power);
358ed1fb79SDaniel Mack 
368ed1fb79SDaniel Mack 	/*
378ed1fb79SDaniel Mack 	 * ISSUE:  DaVinci (RTL 1.300) disconnects after
388ed1fb79SDaniel Mack 	 * resume of high speed peripherals (but not full
398ed1fb79SDaniel Mack 	 * speed ones).
408ed1fb79SDaniel Mack 	 */
418ed1fb79SDaniel Mack 	musb->is_active = 1;
428ed1fb79SDaniel Mack 	musb->port1_status &= ~(USB_PORT_STAT_SUSPEND | MUSB_PORT_STAT_RESUME);
438ed1fb79SDaniel Mack 	musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
448ed1fb79SDaniel Mack 	usb_hcd_poll_rh_status(musb->hcd);
458ed1fb79SDaniel Mack 	/* NOTE: it might really be A_WAIT_BCON ... */
4621acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_A_HOST);
478ed1fb79SDaniel Mack 
488ed1fb79SDaniel Mack 	spin_unlock_irqrestore(&musb->lock, flags);
498ed1fb79SDaniel Mack }
508ed1fb79SDaniel Mack 
musb_port_suspend(struct musb * musb,bool do_suspend)51ebc3dd68SDaniel Glöckner int musb_port_suspend(struct musb *musb, bool do_suspend)
52550a7375SFelipe Balbi {
53550a7375SFelipe Balbi 	u8		power;
54550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
55550a7375SFelipe Balbi 
56550a7375SFelipe Balbi 	if (!is_host_active(musb))
57ebc3dd68SDaniel Glöckner 		return 0;
58550a7375SFelipe Balbi 
59550a7375SFelipe Balbi 	/* NOTE:  this doesn't necessarily put PHY into low power mode,
60550a7375SFelipe Balbi 	 * turning off its clock; that's a function of PHY integration and
61550a7375SFelipe Balbi 	 * MUSB_POWER_ENSUSPEND.  PHY may need a clock (sigh) to detect
62550a7375SFelipe Balbi 	 * SE0 changing to connect (J) or wakeup (K) states.
63550a7375SFelipe Balbi 	 */
64550a7375SFelipe Balbi 	power = musb_readb(mbase, MUSB_POWER);
65550a7375SFelipe Balbi 	if (do_suspend) {
66550a7375SFelipe Balbi 		int retries = 10000;
67550a7375SFelipe Balbi 
68ebc3dd68SDaniel Glöckner 		if (power & MUSB_POWER_RESUME)
69ebc3dd68SDaniel Glöckner 			return -EBUSY;
70ebc3dd68SDaniel Glöckner 
71ebc3dd68SDaniel Glöckner 		if (!(power & MUSB_POWER_SUSPENDM)) {
72550a7375SFelipe Balbi 			power |= MUSB_POWER_SUSPENDM;
73550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_POWER, power);
74550a7375SFelipe Balbi 
75550a7375SFelipe Balbi 			/* Needed for OPT A tests */
76550a7375SFelipe Balbi 			power = musb_readb(mbase, MUSB_POWER);
77550a7375SFelipe Balbi 			while (power & MUSB_POWER_SUSPENDM) {
78550a7375SFelipe Balbi 				power = musb_readb(mbase, MUSB_POWER);
79550a7375SFelipe Balbi 				if (retries-- < 1)
80550a7375SFelipe Balbi 					break;
81550a7375SFelipe Balbi 			}
82ebc3dd68SDaniel Glöckner 		}
83550a7375SFelipe Balbi 
84b99d3659SBin Liu 		musb_dbg(musb, "Root port suspended, power %02x", power);
85550a7375SFelipe Balbi 
86550a7375SFelipe Balbi 		musb->port1_status |= USB_PORT_STAT_SUSPEND;
8721acc656SPaul Cercueil 		switch (musb_get_state(musb)) {
88550a7375SFelipe Balbi 		case OTG_STATE_A_HOST:
8921acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_SUSPEND);
90*a6d45ea0SPaul Cercueil 			musb->is_active = musb->xceiv &&
91*a6d45ea0SPaul Cercueil 				musb->xceiv->otg->host->b_hnp_enable;
92ab983f2aSDavid Brownell 			if (musb->is_active)
93ab983f2aSDavid Brownell 				mod_timer(&musb->otg_timer, jiffies
94ab983f2aSDavid Brownell 					+ msecs_to_jiffies(
95ab983f2aSDavid Brownell 						OTG_TIME_A_AIDL_BDIS));
96550a7375SFelipe Balbi 			musb_platform_try_idle(musb, 0);
97550a7375SFelipe Balbi 			break;
98550a7375SFelipe Balbi 		case OTG_STATE_B_HOST:
9921acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_WAIT_ACON);
100*a6d45ea0SPaul Cercueil 			musb->is_active = musb->xceiv &&
101*a6d45ea0SPaul Cercueil 				musb->xceiv->otg->host->b_hnp_enable;
102550a7375SFelipe Balbi 			musb_platform_try_idle(musb, 0);
103550a7375SFelipe Balbi 			break;
104550a7375SFelipe Balbi 		default:
105b99d3659SBin Liu 			musb_dbg(musb, "bogus rh suspend? %s",
106285f28bfSPaul Cercueil 				 musb_otg_state_string(musb));
107550a7375SFelipe Balbi 		}
108550a7375SFelipe Balbi 	} else if (power & MUSB_POWER_SUSPENDM) {
109550a7375SFelipe Balbi 		power &= ~MUSB_POWER_SUSPENDM;
110550a7375SFelipe Balbi 		power |= MUSB_POWER_RESUME;
111550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_POWER, power);
112550a7375SFelipe Balbi 
113b99d3659SBin Liu 		musb_dbg(musb, "Root port resuming, power %02x", power);
114550a7375SFelipe Balbi 
115550a7375SFelipe Balbi 		musb->port1_status |= MUSB_PORT_STAT_RESUME;
116f2dece44SDaniel Mack 		schedule_delayed_work(&musb->finish_resume_work,
117309be239SFelipe Balbi 				      msecs_to_jiffies(USB_RESUME_TIMEOUT));
118550a7375SFelipe Balbi 	}
119ebc3dd68SDaniel Glöckner 	return 0;
120550a7375SFelipe Balbi }
121550a7375SFelipe Balbi 
musb_port_reset(struct musb * musb,bool do_reset)122869c5978SDaniel Mack void musb_port_reset(struct musb *musb, bool do_reset)
123550a7375SFelipe Balbi {
124550a7375SFelipe Balbi 	u8		power;
125550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
126550a7375SFelipe Balbi 
12721acc656SPaul Cercueil 	if (musb_get_state(musb) == OTG_STATE_B_IDLE) {
128b99d3659SBin Liu 		musb_dbg(musb, "HNP: Returning from HNP; no hub reset from b_idle");
129550a7375SFelipe Balbi 		musb->port1_status &= ~USB_PORT_STAT_RESET;
130550a7375SFelipe Balbi 		return;
131550a7375SFelipe Balbi 	}
132550a7375SFelipe Balbi 
133550a7375SFelipe Balbi 	if (!is_host_active(musb))
134550a7375SFelipe Balbi 		return;
135550a7375SFelipe Balbi 
136550a7375SFelipe Balbi 	/* NOTE:  caller guarantees it will turn off the reset when
137550a7375SFelipe Balbi 	 * the appropriate amount of time has passed
138550a7375SFelipe Balbi 	 */
139550a7375SFelipe Balbi 	power = musb_readb(mbase, MUSB_POWER);
140550a7375SFelipe Balbi 	if (do_reset) {
141550a7375SFelipe Balbi 		/*
142550a7375SFelipe Balbi 		 * If RESUME is set, we must make sure it stays minimum 20 ms.
143550a7375SFelipe Balbi 		 * Then we must clear RESUME and wait a bit to let musb start
144550a7375SFelipe Balbi 		 * generating SOFs. If we don't do this, OPT HS A 6.8 tests
145550a7375SFelipe Balbi 		 * fail with "Error! Did not receive an SOF before suspend
146550a7375SFelipe Balbi 		 * detected".
147550a7375SFelipe Balbi 		 */
148550a7375SFelipe Balbi 		if (power &  MUSB_POWER_RESUME) {
14973926db3SDaniel Mack 			long remain = (unsigned long) musb->rh_timer - jiffies;
15073926db3SDaniel Mack 
15173926db3SDaniel Mack 			if (musb->rh_timer > 0 && remain > 0) {
15273926db3SDaniel Mack 				/* take into account the minimum delay after resume */
15373926db3SDaniel Mack 				schedule_delayed_work(
154f2dece44SDaniel Mack 					&musb->deassert_reset_work, remain);
15573926db3SDaniel Mack 				return;
15673926db3SDaniel Mack 			}
15773926db3SDaniel Mack 
158550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_POWER,
159550a7375SFelipe Balbi 				    power & ~MUSB_POWER_RESUME);
16073926db3SDaniel Mack 
16173926db3SDaniel Mack 			/* Give the core 1 ms to clear MUSB_POWER_RESUME */
162f2dece44SDaniel Mack 			schedule_delayed_work(&musb->deassert_reset_work,
163f2dece44SDaniel Mack 					      msecs_to_jiffies(1));
16473926db3SDaniel Mack 			return;
165550a7375SFelipe Balbi 		}
166550a7375SFelipe Balbi 
167550a7375SFelipe Balbi 		power &= 0xf0;
168550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_POWER,
169550a7375SFelipe Balbi 				power | MUSB_POWER_RESET);
170550a7375SFelipe Balbi 
171550a7375SFelipe Balbi 		musb->port1_status |= USB_PORT_STAT_RESET;
172550a7375SFelipe Balbi 		musb->port1_status &= ~USB_PORT_STAT_ENABLE;
173f2dece44SDaniel Mack 		schedule_delayed_work(&musb->deassert_reset_work,
174f2dece44SDaniel Mack 				      msecs_to_jiffies(50));
175550a7375SFelipe Balbi 	} else {
176b99d3659SBin Liu 		musb_dbg(musb, "root port reset stopped");
177591fa9ddSHans de Goede 		musb_platform_pre_root_reset_end(musb);
178550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_POWER,
179550a7375SFelipe Balbi 				power & ~MUSB_POWER_RESET);
180591fa9ddSHans de Goede 		musb_platform_post_root_reset_end(musb);
181550a7375SFelipe Balbi 
182550a7375SFelipe Balbi 		power = musb_readb(mbase, MUSB_POWER);
183550a7375SFelipe Balbi 		if (power & MUSB_POWER_HSMODE) {
184b99d3659SBin Liu 			musb_dbg(musb, "high-speed device connected");
185550a7375SFelipe Balbi 			musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
186550a7375SFelipe Balbi 		}
187550a7375SFelipe Balbi 
188550a7375SFelipe Balbi 		musb->port1_status &= ~USB_PORT_STAT_RESET;
189550a7375SFelipe Balbi 		musb->port1_status |= USB_PORT_STAT_ENABLE
190550a7375SFelipe Balbi 					| (USB_PORT_STAT_C_RESET << 16)
191550a7375SFelipe Balbi 					| (USB_PORT_STAT_C_ENABLE << 16);
1928b125df5SDaniel Mack 		usb_hcd_poll_rh_status(musb->hcd);
193550a7375SFelipe Balbi 
194550a7375SFelipe Balbi 		musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
195550a7375SFelipe Balbi 	}
196550a7375SFelipe Balbi }
197550a7375SFelipe Balbi 
musb_root_disconnect(struct musb * musb)198550a7375SFelipe Balbi void musb_root_disconnect(struct musb *musb)
199550a7375SFelipe Balbi {
200749da5f8SAlan Stern 	musb->port1_status = USB_PORT_STAT_POWER
201749da5f8SAlan Stern 			| (USB_PORT_STAT_C_CONNECTION << 16);
202550a7375SFelipe Balbi 
2038b125df5SDaniel Mack 	usb_hcd_poll_rh_status(musb->hcd);
204550a7375SFelipe Balbi 	musb->is_active = 0;
205550a7375SFelipe Balbi 
20621acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
207550a7375SFelipe Balbi 	case OTG_STATE_A_SUSPEND:
208*a6d45ea0SPaul Cercueil 		if (musb->xceiv && musb->xceiv->otg->host->b_hnp_enable) {
20921acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_PERIPHERAL);
2101de00daeSDavid Brownell 			musb->g.is_a_peripheral = 1;
2111de00daeSDavid Brownell 			break;
2121de00daeSDavid Brownell 		}
213df561f66SGustavo A. R. Silva 		fallthrough;
2141de00daeSDavid Brownell 	case OTG_STATE_A_HOST:
21521acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_WAIT_BCON);
216550a7375SFelipe Balbi 		musb->is_active = 0;
217550a7375SFelipe Balbi 		break;
218550a7375SFelipe Balbi 	case OTG_STATE_A_WAIT_VFALL:
21921acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_IDLE);
220550a7375SFelipe Balbi 		break;
221550a7375SFelipe Balbi 	default:
222b99d3659SBin Liu 		musb_dbg(musb, "host disconnect (%s)",
223285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
224550a7375SFelipe Balbi 	}
225550a7375SFelipe Balbi }
226b36df090SHans de Goede EXPORT_SYMBOL_GPL(musb_root_disconnect);
227550a7375SFelipe Balbi 
228550a7375SFelipe Balbi 
229550a7375SFelipe Balbi /*---------------------------------------------------------------------*/
230550a7375SFelipe Balbi 
231550a7375SFelipe Balbi /* Caller may or may not hold musb->lock */
musb_hub_status_data(struct usb_hcd * hcd,char * buf)232550a7375SFelipe Balbi int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
233550a7375SFelipe Balbi {
234550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
235550a7375SFelipe Balbi 	int		retval = 0;
236550a7375SFelipe Balbi 
237550a7375SFelipe Balbi 	/* called in_irq() via usb_hcd_poll_rh_status() */
238550a7375SFelipe Balbi 	if (musb->port1_status & 0xffff0000) {
239550a7375SFelipe Balbi 		*buf = 0x02;
240550a7375SFelipe Balbi 		retval = 1;
241550a7375SFelipe Balbi 	}
242550a7375SFelipe Balbi 	return retval;
243550a7375SFelipe Balbi }
244550a7375SFelipe Balbi 
musb_has_gadget(struct musb * musb)245ae44df2eSSebastian Andrzej Siewior static int musb_has_gadget(struct musb *musb)
246ae44df2eSSebastian Andrzej Siewior {
247ae44df2eSSebastian Andrzej Siewior 	/*
248ae44df2eSSebastian Andrzej Siewior 	 * In host-only mode we start a connection right away. In OTG mode
249ae44df2eSSebastian Andrzej Siewior 	 * we have to wait until we loaded a gadget. We don't really need a
250ae44df2eSSebastian Andrzej Siewior 	 * gadget if we operate as a host but we should not start a session
251ae44df2eSSebastian Andrzej Siewior 	 * as a device without a gadget or else we explode.
252ae44df2eSSebastian Andrzej Siewior 	 */
253ae44df2eSSebastian Andrzej Siewior #ifdef CONFIG_USB_MUSB_HOST
254ae44df2eSSebastian Andrzej Siewior 	return 1;
255ae44df2eSSebastian Andrzej Siewior #else
2567ad76955SBin Liu 	return musb->port_mode == MUSB_HOST;
257ae44df2eSSebastian Andrzej Siewior #endif
258ae44df2eSSebastian Andrzej Siewior }
259ae44df2eSSebastian Andrzej Siewior 
musb_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)260550a7375SFelipe Balbi int musb_hub_control(
261550a7375SFelipe Balbi 	struct usb_hcd	*hcd,
262550a7375SFelipe Balbi 	u16		typeReq,
263550a7375SFelipe Balbi 	u16		wValue,
264550a7375SFelipe Balbi 	u16		wIndex,
265550a7375SFelipe Balbi 	char		*buf,
266550a7375SFelipe Balbi 	u16		wLength)
267550a7375SFelipe Balbi {
268550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
269550a7375SFelipe Balbi 	u32		temp;
270550a7375SFelipe Balbi 	int		retval = 0;
271550a7375SFelipe Balbi 	unsigned long	flags;
2722c557540STony Lindgren 	bool		start_musb = false;
273550a7375SFelipe Balbi 
274550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
275550a7375SFelipe Balbi 
276541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
277550a7375SFelipe Balbi 		spin_unlock_irqrestore(&musb->lock, flags);
278550a7375SFelipe Balbi 		return -ESHUTDOWN;
279550a7375SFelipe Balbi 	}
280550a7375SFelipe Balbi 
281550a7375SFelipe Balbi 	/* hub features:  always zero, setting is a NOP
282550a7375SFelipe Balbi 	 * port features: reported, sometimes updated when host is active
283550a7375SFelipe Balbi 	 * no indicators
284550a7375SFelipe Balbi 	 */
285550a7375SFelipe Balbi 	switch (typeReq) {
286550a7375SFelipe Balbi 	case ClearHubFeature:
287550a7375SFelipe Balbi 	case SetHubFeature:
288550a7375SFelipe Balbi 		switch (wValue) {
289550a7375SFelipe Balbi 		case C_HUB_OVER_CURRENT:
290550a7375SFelipe Balbi 		case C_HUB_LOCAL_POWER:
291550a7375SFelipe Balbi 			break;
292550a7375SFelipe Balbi 		default:
293550a7375SFelipe Balbi 			goto error;
294550a7375SFelipe Balbi 		}
295550a7375SFelipe Balbi 		break;
296550a7375SFelipe Balbi 	case ClearPortFeature:
297550a7375SFelipe Balbi 		if ((wIndex & 0xff) != 1)
298550a7375SFelipe Balbi 			goto error;
299550a7375SFelipe Balbi 
300550a7375SFelipe Balbi 		switch (wValue) {
301550a7375SFelipe Balbi 		case USB_PORT_FEAT_ENABLE:
302550a7375SFelipe Balbi 			break;
303550a7375SFelipe Balbi 		case USB_PORT_FEAT_SUSPEND:
304550a7375SFelipe Balbi 			musb_port_suspend(musb, false);
305550a7375SFelipe Balbi 			break;
306550a7375SFelipe Balbi 		case USB_PORT_FEAT_POWER:
307032ec49fSFelipe Balbi 			if (!hcd->self.is_b_host)
308743411b3SFelipe Balbi 				musb_platform_set_vbus(musb, 0);
309550a7375SFelipe Balbi 			break;
310550a7375SFelipe Balbi 		case USB_PORT_FEAT_C_CONNECTION:
311550a7375SFelipe Balbi 		case USB_PORT_FEAT_C_ENABLE:
312550a7375SFelipe Balbi 		case USB_PORT_FEAT_C_OVER_CURRENT:
313550a7375SFelipe Balbi 		case USB_PORT_FEAT_C_RESET:
314550a7375SFelipe Balbi 		case USB_PORT_FEAT_C_SUSPEND:
315550a7375SFelipe Balbi 			break;
316550a7375SFelipe Balbi 		default:
317550a7375SFelipe Balbi 			goto error;
318550a7375SFelipe Balbi 		}
319b99d3659SBin Liu 		musb_dbg(musb, "clear feature %d", wValue);
320550a7375SFelipe Balbi 		musb->port1_status &= ~(1 << wValue);
321550a7375SFelipe Balbi 		break;
322550a7375SFelipe Balbi 	case GetHubDescriptor:
323550a7375SFelipe Balbi 		{
324550a7375SFelipe Balbi 		struct usb_hub_descriptor *desc = (void *)buf;
325550a7375SFelipe Balbi 
326550a7375SFelipe Balbi 		desc->bDescLength = 9;
327e95af3aaSSergei Shtylyov 		desc->bDescriptorType = USB_DT_HUB;
328550a7375SFelipe Balbi 		desc->bNbrPorts = 1;
329551509d2SHarvey Harrison 		desc->wHubCharacteristics = cpu_to_le16(
330c509ba6eSSergei Shtylyov 			HUB_CHAR_INDV_PORT_LPSM /* per-port power switching */
331c509ba6eSSergei Shtylyov 			| HUB_CHAR_NO_OCPM	/* no overcurrent reporting */
332550a7375SFelipe Balbi 			);
333550a7375SFelipe Balbi 		desc->bPwrOn2PwrGood = 5;	/* msec/2 */
334550a7375SFelipe Balbi 		desc->bHubContrCurrent = 0;
335550a7375SFelipe Balbi 
336550a7375SFelipe Balbi 		/* workaround bogus struct definition */
337dbe79bbeSJohn Youn 		desc->u.hs.DeviceRemovable[0] = 0x02;	/* port 1 */
338dbe79bbeSJohn Youn 		desc->u.hs.DeviceRemovable[1] = 0xff;
339550a7375SFelipe Balbi 		}
340550a7375SFelipe Balbi 		break;
341550a7375SFelipe Balbi 	case GetHubStatus:
342550a7375SFelipe Balbi 		temp = 0;
343550a7375SFelipe Balbi 		*(__le32 *) buf = cpu_to_le32(temp);
344550a7375SFelipe Balbi 		break;
345550a7375SFelipe Balbi 	case GetPortStatus:
346550a7375SFelipe Balbi 		if (wIndex != 1)
347550a7375SFelipe Balbi 			goto error;
348550a7375SFelipe Balbi 
349550a7375SFelipe Balbi 		put_unaligned(cpu_to_le32(musb->port1_status
350550a7375SFelipe Balbi 					& ~MUSB_PORT_STAT_RESUME),
351550a7375SFelipe Balbi 				(__le32 *) buf);
352550a7375SFelipe Balbi 
353550a7375SFelipe Balbi 		/* port change status is more interesting */
354b99d3659SBin Liu 		musb_dbg(musb, "port status %08x", musb->port1_status);
355550a7375SFelipe Balbi 		break;
356550a7375SFelipe Balbi 	case SetPortFeature:
357550a7375SFelipe Balbi 		if ((wIndex & 0xff) != 1)
358550a7375SFelipe Balbi 			goto error;
359550a7375SFelipe Balbi 
360550a7375SFelipe Balbi 		switch (wValue) {
361550a7375SFelipe Balbi 		case USB_PORT_FEAT_POWER:
362550a7375SFelipe Balbi 			/* NOTE: this controller has a strange state machine
363550a7375SFelipe Balbi 			 * that involves "requesting sessions" according to
364550a7375SFelipe Balbi 			 * magic side effects from incompletely-described
365550a7375SFelipe Balbi 			 * rules about startup...
366550a7375SFelipe Balbi 			 *
367550a7375SFelipe Balbi 			 * This call is what really starts the host mode; be
368550a7375SFelipe Balbi 			 * very careful about side effects if you reorder any
369550a7375SFelipe Balbi 			 * initialization logic, e.g. for OTG, or change any
370550a7375SFelipe Balbi 			 * logic relating to VBUS power-up.
371550a7375SFelipe Balbi 			 */
372ae44df2eSSebastian Andrzej Siewior 			if (!hcd->self.is_b_host && musb_has_gadget(musb))
3732c557540STony Lindgren 				start_musb = true;
374550a7375SFelipe Balbi 			break;
375550a7375SFelipe Balbi 		case USB_PORT_FEAT_RESET:
376550a7375SFelipe Balbi 			musb_port_reset(musb, true);
377550a7375SFelipe Balbi 			break;
378550a7375SFelipe Balbi 		case USB_PORT_FEAT_SUSPEND:
379550a7375SFelipe Balbi 			musb_port_suspend(musb, true);
380550a7375SFelipe Balbi 			break;
381550a7375SFelipe Balbi 		case USB_PORT_FEAT_TEST:
382550a7375SFelipe Balbi 			if (unlikely(is_host_active(musb)))
383550a7375SFelipe Balbi 				goto error;
384550a7375SFelipe Balbi 
385550a7375SFelipe Balbi 			wIndex >>= 8;
386550a7375SFelipe Balbi 			switch (wIndex) {
38762fb45d3SGreg Kroah-Hartman 			case USB_TEST_J:
38862fb45d3SGreg Kroah-Hartman 				pr_debug("USB_TEST_J\n");
389550a7375SFelipe Balbi 				temp = MUSB_TEST_J;
390550a7375SFelipe Balbi 				break;
39162fb45d3SGreg Kroah-Hartman 			case USB_TEST_K:
39262fb45d3SGreg Kroah-Hartman 				pr_debug("USB_TEST_K\n");
393550a7375SFelipe Balbi 				temp = MUSB_TEST_K;
394550a7375SFelipe Balbi 				break;
39562fb45d3SGreg Kroah-Hartman 			case USB_TEST_SE0_NAK:
39662fb45d3SGreg Kroah-Hartman 				pr_debug("USB_TEST_SE0_NAK\n");
397550a7375SFelipe Balbi 				temp = MUSB_TEST_SE0_NAK;
398550a7375SFelipe Balbi 				break;
39962fb45d3SGreg Kroah-Hartman 			case USB_TEST_PACKET:
40062fb45d3SGreg Kroah-Hartman 				pr_debug("USB_TEST_PACKET\n");
401550a7375SFelipe Balbi 				temp = MUSB_TEST_PACKET;
402550a7375SFelipe Balbi 				musb_load_testpacket(musb);
403550a7375SFelipe Balbi 				break;
40462fb45d3SGreg Kroah-Hartman 			case USB_TEST_FORCE_ENABLE:
40562fb45d3SGreg Kroah-Hartman 				pr_debug("USB_TEST_FORCE_ENABLE\n");
406550a7375SFelipe Balbi 				temp = MUSB_TEST_FORCE_HOST
407550a7375SFelipe Balbi 					| MUSB_TEST_FORCE_HS;
408550a7375SFelipe Balbi 
409550a7375SFelipe Balbi 				musb_writeb(musb->mregs, MUSB_DEVCTL,
410550a7375SFelipe Balbi 						MUSB_DEVCTL_SESSION);
411550a7375SFelipe Balbi 				break;
412550a7375SFelipe Balbi 			case 6:
413550a7375SFelipe Balbi 				pr_debug("TEST_FIFO_ACCESS\n");
414550a7375SFelipe Balbi 				temp = MUSB_TEST_FIFO_ACCESS;
415550a7375SFelipe Balbi 				break;
416550a7375SFelipe Balbi 			default:
417550a7375SFelipe Balbi 				goto error;
418550a7375SFelipe Balbi 			}
419550a7375SFelipe Balbi 			musb_writeb(musb->mregs, MUSB_TESTMODE, temp);
420550a7375SFelipe Balbi 			break;
421550a7375SFelipe Balbi 		default:
422550a7375SFelipe Balbi 			goto error;
423550a7375SFelipe Balbi 		}
424b99d3659SBin Liu 		musb_dbg(musb, "set feature %d", wValue);
425550a7375SFelipe Balbi 		musb->port1_status |= 1 << wValue;
426550a7375SFelipe Balbi 		break;
427550a7375SFelipe Balbi 
428550a7375SFelipe Balbi 	default:
429550a7375SFelipe Balbi error:
430550a7375SFelipe Balbi 		/* "protocol stall" on error */
431550a7375SFelipe Balbi 		retval = -EPIPE;
432550a7375SFelipe Balbi 	}
433550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
4342c557540STony Lindgren 
4352c557540STony Lindgren 	if (start_musb)
4362c557540STony Lindgren 		musb_start(musb);
4372c557540STony Lindgren 
438550a7375SFelipe Balbi 	return retval;
439550a7375SFelipe Balbi }
440