musb_virthub.c revision 8ed1fb790ea24bb223e3b30e2b22bccf5b0a76c9
1/*
2 * MUSB OTG driver virtual root hub support
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#include <linux/module.h>
36#include <linux/kernel.h>
37#include <linux/sched.h>
38#include <linux/errno.h>
39#include <linux/init.h>
40#include <linux/time.h>
41#include <linux/timer.h>
42
43#include <asm/unaligned.h>
44
45#include "musb_core.h"
46
47void musb_host_finish_resume(struct work_struct *work)
48{
49	struct musb *musb;
50	unsigned long flags;
51	u8 power;
52
53	musb = container_of(work, struct musb, deassert_reset_work.work);
54
55	spin_lock_irqsave(&musb->lock, flags);
56
57	power = musb_readb(musb->mregs, MUSB_POWER);
58	power &= ~MUSB_POWER_RESUME;
59	dev_dbg(musb->controller, "root port resume stopped, power %02x\n",
60		power);
61	musb_writeb(musb->mregs, MUSB_POWER, power);
62
63	/*
64	 * ISSUE:  DaVinci (RTL 1.300) disconnects after
65	 * resume of high speed peripherals (but not full
66	 * speed ones).
67	 */
68	musb->is_active = 1;
69	musb->port1_status &= ~(USB_PORT_STAT_SUSPEND | MUSB_PORT_STAT_RESUME);
70	musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
71	usb_hcd_poll_rh_status(musb->hcd);
72	/* NOTE: it might really be A_WAIT_BCON ... */
73	musb->xceiv->state = OTG_STATE_A_HOST;
74
75	spin_unlock_irqrestore(&musb->lock, flags);
76}
77
78void musb_port_suspend(struct musb *musb, bool do_suspend)
79{
80	struct usb_otg	*otg = musb->xceiv->otg;
81	u8		power;
82	void __iomem	*mbase = musb->mregs;
83
84	if (!is_host_active(musb))
85		return;
86
87	/* NOTE:  this doesn't necessarily put PHY into low power mode,
88	 * turning off its clock; that's a function of PHY integration and
89	 * MUSB_POWER_ENSUSPEND.  PHY may need a clock (sigh) to detect
90	 * SE0 changing to connect (J) or wakeup (K) states.
91	 */
92	power = musb_readb(mbase, MUSB_POWER);
93	if (do_suspend) {
94		int retries = 10000;
95
96		power &= ~MUSB_POWER_RESUME;
97		power |= MUSB_POWER_SUSPENDM;
98		musb_writeb(mbase, MUSB_POWER, power);
99
100		/* Needed for OPT A tests */
101		power = musb_readb(mbase, MUSB_POWER);
102		while (power & MUSB_POWER_SUSPENDM) {
103			power = musb_readb(mbase, MUSB_POWER);
104			if (retries-- < 1)
105				break;
106		}
107
108		dev_dbg(musb->controller, "Root port suspended, power %02x\n", power);
109
110		musb->port1_status |= USB_PORT_STAT_SUSPEND;
111		switch (musb->xceiv->state) {
112		case OTG_STATE_A_HOST:
113			musb->xceiv->state = OTG_STATE_A_SUSPEND;
114			musb->is_active = otg->host->b_hnp_enable;
115			if (musb->is_active)
116				mod_timer(&musb->otg_timer, jiffies
117					+ msecs_to_jiffies(
118						OTG_TIME_A_AIDL_BDIS));
119			musb_platform_try_idle(musb, 0);
120			break;
121		case OTG_STATE_B_HOST:
122			musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
123			musb->is_active = otg->host->b_hnp_enable;
124			musb_platform_try_idle(musb, 0);
125			break;
126		default:
127			dev_dbg(musb->controller, "bogus rh suspend? %s\n",
128				usb_otg_state_string(musb->xceiv->state));
129		}
130	} else if (power & MUSB_POWER_SUSPENDM) {
131		power &= ~MUSB_POWER_SUSPENDM;
132		power |= MUSB_POWER_RESUME;
133		musb_writeb(mbase, MUSB_POWER, power);
134
135		dev_dbg(musb->controller, "Root port resuming, power %02x\n", power);
136
137		/* later, GetPortStatus will stop RESUME signaling */
138		musb->port1_status |= MUSB_PORT_STAT_RESUME;
139		schedule_delayed_work(&musb->finish_resume_work, 20);
140	}
141}
142
143void musb_port_reset(struct musb *musb, bool do_reset)
144{
145	u8		power;
146	void __iomem	*mbase = musb->mregs;
147
148	if (musb->xceiv->state == OTG_STATE_B_IDLE) {
149		dev_dbg(musb->controller, "HNP: Returning from HNP; no hub reset from b_idle\n");
150		musb->port1_status &= ~USB_PORT_STAT_RESET;
151		return;
152	}
153
154	if (!is_host_active(musb))
155		return;
156
157	/* NOTE:  caller guarantees it will turn off the reset when
158	 * the appropriate amount of time has passed
159	 */
160	power = musb_readb(mbase, MUSB_POWER);
161	if (do_reset) {
162
163		/*
164		 * If RESUME is set, we must make sure it stays minimum 20 ms.
165		 * Then we must clear RESUME and wait a bit to let musb start
166		 * generating SOFs. If we don't do this, OPT HS A 6.8 tests
167		 * fail with "Error! Did not receive an SOF before suspend
168		 * detected".
169		 */
170		if (power &  MUSB_POWER_RESUME) {
171			while (time_before(jiffies, musb->rh_timer))
172				msleep(1);
173			musb_writeb(mbase, MUSB_POWER,
174				power & ~MUSB_POWER_RESUME);
175			msleep(1);
176		}
177
178		power &= 0xf0;
179		musb_writeb(mbase, MUSB_POWER,
180				power | MUSB_POWER_RESET);
181
182		musb->port1_status |= USB_PORT_STAT_RESET;
183		musb->port1_status &= ~USB_PORT_STAT_ENABLE;
184		schedule_delayed_work(&musb->deassert_reset_work, 50);
185	} else {
186		dev_dbg(musb->controller, "root port reset stopped\n");
187		musb_writeb(mbase, MUSB_POWER,
188				power & ~MUSB_POWER_RESET);
189
190		power = musb_readb(mbase, MUSB_POWER);
191		if (power & MUSB_POWER_HSMODE) {
192			dev_dbg(musb->controller, "high-speed device connected\n");
193			musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
194		}
195
196		musb->port1_status &= ~USB_PORT_STAT_RESET;
197		musb->port1_status |= USB_PORT_STAT_ENABLE
198					| (USB_PORT_STAT_C_RESET << 16)
199					| (USB_PORT_STAT_C_ENABLE << 16);
200		usb_hcd_poll_rh_status(musb->hcd);
201
202		musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
203	}
204}
205
206void musb_root_disconnect(struct musb *musb)
207{
208	struct usb_otg	*otg = musb->xceiv->otg;
209
210	musb->port1_status = USB_PORT_STAT_POWER
211			| (USB_PORT_STAT_C_CONNECTION << 16);
212
213	usb_hcd_poll_rh_status(musb->hcd);
214	musb->is_active = 0;
215
216	switch (musb->xceiv->state) {
217	case OTG_STATE_A_SUSPEND:
218		if (otg->host->b_hnp_enable) {
219			musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
220			musb->g.is_a_peripheral = 1;
221			break;
222		}
223		/* FALLTHROUGH */
224	case OTG_STATE_A_HOST:
225		musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
226		musb->is_active = 0;
227		break;
228	case OTG_STATE_A_WAIT_VFALL:
229		musb->xceiv->state = OTG_STATE_B_IDLE;
230		break;
231	default:
232		dev_dbg(musb->controller, "host disconnect (%s)\n",
233			usb_otg_state_string(musb->xceiv->state));
234	}
235}
236
237
238/*---------------------------------------------------------------------*/
239
240/* Caller may or may not hold musb->lock */
241int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
242{
243	struct musb	*musb = hcd_to_musb(hcd);
244	int		retval = 0;
245
246	/* called in_irq() via usb_hcd_poll_rh_status() */
247	if (musb->port1_status & 0xffff0000) {
248		*buf = 0x02;
249		retval = 1;
250	}
251	return retval;
252}
253
254static int musb_has_gadget(struct musb *musb)
255{
256	/*
257	 * In host-only mode we start a connection right away. In OTG mode
258	 * we have to wait until we loaded a gadget. We don't really need a
259	 * gadget if we operate as a host but we should not start a session
260	 * as a device without a gadget or else we explode.
261	 */
262#ifdef CONFIG_USB_MUSB_HOST
263	return 1;
264#else
265	if (musb->port_mode == MUSB_PORT_MODE_HOST)
266		return 1;
267	return musb->g.dev.driver != NULL;
268#endif
269}
270
271int musb_hub_control(
272	struct usb_hcd	*hcd,
273	u16		typeReq,
274	u16		wValue,
275	u16		wIndex,
276	char		*buf,
277	u16		wLength)
278{
279	struct musb	*musb = hcd_to_musb(hcd);
280	u32		temp;
281	int		retval = 0;
282	unsigned long	flags;
283
284	spin_lock_irqsave(&musb->lock, flags);
285
286	if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
287		spin_unlock_irqrestore(&musb->lock, flags);
288		return -ESHUTDOWN;
289	}
290
291	/* hub features:  always zero, setting is a NOP
292	 * port features: reported, sometimes updated when host is active
293	 * no indicators
294	 */
295	switch (typeReq) {
296	case ClearHubFeature:
297	case SetHubFeature:
298		switch (wValue) {
299		case C_HUB_OVER_CURRENT:
300		case C_HUB_LOCAL_POWER:
301			break;
302		default:
303			goto error;
304		}
305		break;
306	case ClearPortFeature:
307		if ((wIndex & 0xff) != 1)
308			goto error;
309
310		switch (wValue) {
311		case USB_PORT_FEAT_ENABLE:
312			break;
313		case USB_PORT_FEAT_SUSPEND:
314			musb_port_suspend(musb, false);
315			break;
316		case USB_PORT_FEAT_POWER:
317			if (!hcd->self.is_b_host)
318				musb_platform_set_vbus(musb, 0);
319			break;
320		case USB_PORT_FEAT_C_CONNECTION:
321		case USB_PORT_FEAT_C_ENABLE:
322		case USB_PORT_FEAT_C_OVER_CURRENT:
323		case USB_PORT_FEAT_C_RESET:
324		case USB_PORT_FEAT_C_SUSPEND:
325			break;
326		default:
327			goto error;
328		}
329		dev_dbg(musb->controller, "clear feature %d\n", wValue);
330		musb->port1_status &= ~(1 << wValue);
331		break;
332	case GetHubDescriptor:
333		{
334		struct usb_hub_descriptor *desc = (void *)buf;
335
336		desc->bDescLength = 9;
337		desc->bDescriptorType = 0x29;
338		desc->bNbrPorts = 1;
339		desc->wHubCharacteristics = cpu_to_le16(
340				  0x0001	/* per-port power switching */
341				| 0x0010	/* no overcurrent reporting */
342				);
343		desc->bPwrOn2PwrGood = 5;	/* msec/2 */
344		desc->bHubContrCurrent = 0;
345
346		/* workaround bogus struct definition */
347		desc->u.hs.DeviceRemovable[0] = 0x02;	/* port 1 */
348		desc->u.hs.DeviceRemovable[1] = 0xff;
349		}
350		break;
351	case GetHubStatus:
352		temp = 0;
353		*(__le32 *) buf = cpu_to_le32(temp);
354		break;
355	case GetPortStatus:
356		if (wIndex != 1)
357			goto error;
358
359		put_unaligned(cpu_to_le32(musb->port1_status
360					& ~MUSB_PORT_STAT_RESUME),
361				(__le32 *) buf);
362
363		/* port change status is more interesting */
364		dev_dbg(musb->controller, "port status %08x\n",
365				musb->port1_status);
366		break;
367	case SetPortFeature:
368		if ((wIndex & 0xff) != 1)
369			goto error;
370
371		switch (wValue) {
372		case USB_PORT_FEAT_POWER:
373			/* NOTE: this controller has a strange state machine
374			 * that involves "requesting sessions" according to
375			 * magic side effects from incompletely-described
376			 * rules about startup...
377			 *
378			 * This call is what really starts the host mode; be
379			 * very careful about side effects if you reorder any
380			 * initialization logic, e.g. for OTG, or change any
381			 * logic relating to VBUS power-up.
382			 */
383			if (!hcd->self.is_b_host && musb_has_gadget(musb))
384				musb_start(musb);
385			break;
386		case USB_PORT_FEAT_RESET:
387			musb_port_reset(musb, true);
388			break;
389		case USB_PORT_FEAT_SUSPEND:
390			musb_port_suspend(musb, true);
391			break;
392		case USB_PORT_FEAT_TEST:
393			if (unlikely(is_host_active(musb)))
394				goto error;
395
396			wIndex >>= 8;
397			switch (wIndex) {
398			case 1:
399				pr_debug("TEST_J\n");
400				temp = MUSB_TEST_J;
401				break;
402			case 2:
403				pr_debug("TEST_K\n");
404				temp = MUSB_TEST_K;
405				break;
406			case 3:
407				pr_debug("TEST_SE0_NAK\n");
408				temp = MUSB_TEST_SE0_NAK;
409				break;
410			case 4:
411				pr_debug("TEST_PACKET\n");
412				temp = MUSB_TEST_PACKET;
413				musb_load_testpacket(musb);
414				break;
415			case 5:
416				pr_debug("TEST_FORCE_ENABLE\n");
417				temp = MUSB_TEST_FORCE_HOST
418					| MUSB_TEST_FORCE_HS;
419
420				musb_writeb(musb->mregs, MUSB_DEVCTL,
421						MUSB_DEVCTL_SESSION);
422				break;
423			case 6:
424				pr_debug("TEST_FIFO_ACCESS\n");
425				temp = MUSB_TEST_FIFO_ACCESS;
426				break;
427			default:
428				goto error;
429			}
430			musb_writeb(musb->mregs, MUSB_TESTMODE, temp);
431			break;
432		default:
433			goto error;
434		}
435		dev_dbg(musb->controller, "set feature %d\n", wValue);
436		musb->port1_status |= 1 << wValue;
437		break;
438
439	default:
440error:
441		/* "protocol stall" on error */
442		retval = -EPIPE;
443	}
444	spin_unlock_irqrestore(&musb->lock, flags);
445	return retval;
446}
447