Blame view

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