1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
4  *
5  * hub.c - virtual hub handling
6  *
7  * Copyright 2017 IBM Corporation
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/proc_fs.h>
25 #include <linux/prefetch.h>
26 #include <linux/clk.h>
27 #include <linux/usb/gadget.h>
28 #include <linux/of.h>
29 #include <linux/of_gpio.h>
30 #include <linux/regmap.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/bcd.h>
33 #include <linux/version.h>
34 #include <linux/usb.h>
35 #include <linux/usb/hcd.h>
36 
37 #include "vhub.h"
38 
39 /* usb 2.0 hub device descriptor
40  *
41  * A few things we may want to improve here:
42  *
43  *    - We may need to indicate TT support
44  *    - We may need a device qualifier descriptor
45  *	as devices can pretend to be usb1 or 2
46  *    - Make vid/did overridable
47  *    - make it look like usb1 if usb1 mode forced
48  */
49 #define KERNEL_REL	bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
50 #define KERNEL_VER	bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
51 
52 enum {
53 	AST_VHUB_STR_MANUF = 3,
54 	AST_VHUB_STR_PRODUCT = 2,
55 	AST_VHUB_STR_SERIAL = 1,
56 };
57 
58 static const struct usb_device_descriptor ast_vhub_dev_desc = {
59 	.bLength		= USB_DT_DEVICE_SIZE,
60 	.bDescriptorType	= USB_DT_DEVICE,
61 	.bcdUSB			= cpu_to_le16(0x0200),
62 	.bDeviceClass		= USB_CLASS_HUB,
63 	.bDeviceSubClass	= 0,
64 	.bDeviceProtocol	= 1,
65 	.bMaxPacketSize0	= 64,
66 	.idVendor		= cpu_to_le16(0x1d6b),
67 	.idProduct		= cpu_to_le16(0x0107),
68 	.bcdDevice		= cpu_to_le16(0x0100),
69 	.iManufacturer		= AST_VHUB_STR_MANUF,
70 	.iProduct		= AST_VHUB_STR_PRODUCT,
71 	.iSerialNumber		= AST_VHUB_STR_SERIAL,
72 	.bNumConfigurations	= 1,
73 };
74 
75 /* Patches to the above when forcing USB1 mode */
76 static void ast_vhub_patch_dev_desc_usb1(struct usb_device_descriptor *desc)
77 {
78 	desc->bcdUSB = cpu_to_le16(0x0100);
79 	desc->bDeviceProtocol = 0;
80 }
81 
82 /*
83  * Configuration descriptor: same comments as above
84  * regarding handling USB1 mode.
85  */
86 
87 /*
88  * We don't use sizeof() as Linux definition of
89  * struct usb_endpoint_descriptor contains 2
90  * extra bytes
91  */
92 #define AST_VHUB_CONF_DESC_SIZE	(USB_DT_CONFIG_SIZE + \
93 				 USB_DT_INTERFACE_SIZE + \
94 				 USB_DT_ENDPOINT_SIZE)
95 
96 static const struct ast_vhub_full_cdesc ast_vhub_conf_desc = {
97 	.cfg = {
98 		.bLength		= USB_DT_CONFIG_SIZE,
99 		.bDescriptorType	= USB_DT_CONFIG,
100 		.wTotalLength		= cpu_to_le16(AST_VHUB_CONF_DESC_SIZE),
101 		.bNumInterfaces		= 1,
102 		.bConfigurationValue	= 1,
103 		.iConfiguration		= 0,
104 		.bmAttributes		= USB_CONFIG_ATT_ONE |
105 					  USB_CONFIG_ATT_SELFPOWER |
106 					  USB_CONFIG_ATT_WAKEUP,
107 		.bMaxPower		= 0,
108 	},
109 	.intf = {
110 		.bLength		= USB_DT_INTERFACE_SIZE,
111 		.bDescriptorType	= USB_DT_INTERFACE,
112 		.bInterfaceNumber	= 0,
113 		.bAlternateSetting	= 0,
114 		.bNumEndpoints		= 1,
115 		.bInterfaceClass	= USB_CLASS_HUB,
116 		.bInterfaceSubClass	= 0,
117 		.bInterfaceProtocol	= 0,
118 		.iInterface		= 0,
119 	},
120 	.ep = {
121 		.bLength		= USB_DT_ENDPOINT_SIZE,
122 		.bDescriptorType	= USB_DT_ENDPOINT,
123 		.bEndpointAddress	= 0x81,
124 		.bmAttributes		= USB_ENDPOINT_XFER_INT,
125 		.wMaxPacketSize		= cpu_to_le16(1),
126 		.bInterval		= 0x0c,
127 	},
128 };
129 
130 #define AST_VHUB_HUB_DESC_SIZE	(USB_DT_HUB_NONVAR_SIZE + 2)
131 
132 static const struct usb_hub_descriptor ast_vhub_hub_desc = {
133 	.bDescLength			= AST_VHUB_HUB_DESC_SIZE,
134 	.bDescriptorType		= USB_DT_HUB,
135 	.bNbrPorts			= AST_VHUB_NUM_PORTS,
136 	.wHubCharacteristics		= cpu_to_le16(HUB_CHAR_NO_LPSM),
137 	.bPwrOn2PwrGood			= 10,
138 	.bHubContrCurrent		= 0,
139 	.u.hs.DeviceRemovable[0]	= 0,
140 	.u.hs.DeviceRemovable[1]	= 0xff,
141 };
142 
143 /*
144  * These strings converted to UTF-16 must be smaller than
145  * our EP0 buffer.
146  */
147 static const struct usb_string ast_vhub_str_array[] = {
148 	{
149 		.id = AST_VHUB_STR_SERIAL,
150 		.s = "00000000"
151 	},
152 	{
153 		.id = AST_VHUB_STR_PRODUCT,
154 		.s = "USB Virtual Hub"
155 	},
156 	{
157 		.id = AST_VHUB_STR_MANUF,
158 		.s = "Aspeed"
159 	},
160 	{ }
161 };
162 
163 static const struct usb_gadget_strings ast_vhub_strings = {
164 	.language = 0x0409,
165 	.strings = (struct usb_string *)ast_vhub_str_array
166 };
167 
168 static int ast_vhub_hub_dev_status(struct ast_vhub_ep *ep,
169 				   u16 wIndex, u16 wValue)
170 {
171 	u8 st0;
172 
173 	EPDBG(ep, "GET_STATUS(dev)\n");
174 
175 	/*
176 	 * Mark it as self-powered, I doubt the BMC is powered off
177 	 * the USB bus ...
178 	 */
179 	st0 = 1 << USB_DEVICE_SELF_POWERED;
180 
181 	/*
182 	 * Need to double check how remote wakeup actually works
183 	 * on that chip and what triggers it.
184 	 */
185 	if (ep->vhub->wakeup_en)
186 		st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
187 
188 	return ast_vhub_simple_reply(ep, st0, 0);
189 }
190 
191 static int ast_vhub_hub_ep_status(struct ast_vhub_ep *ep,
192 				  u16 wIndex, u16 wValue)
193 {
194 	int ep_num;
195 	u8 st0 = 0;
196 
197 	ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
198 	EPDBG(ep, "GET_STATUS(ep%d)\n", ep_num);
199 
200 	/* On the hub we have only EP 0 and 1 */
201 	if (ep_num == 1) {
202 		if (ep->vhub->ep1_stalled)
203 			st0 |= 1 << USB_ENDPOINT_HALT;
204 	} else if (ep_num != 0)
205 		return std_req_stall;
206 
207 	return ast_vhub_simple_reply(ep, st0, 0);
208 }
209 
210 static int ast_vhub_hub_dev_feature(struct ast_vhub_ep *ep,
211 				    u16 wIndex, u16 wValue,
212 				    bool is_set)
213 {
214 	EPDBG(ep, "%s_FEATURE(dev val=%02x)\n",
215 	      is_set ? "SET" : "CLEAR", wValue);
216 
217 	if (wValue != USB_DEVICE_REMOTE_WAKEUP)
218 		return std_req_stall;
219 
220 	ep->vhub->wakeup_en = is_set;
221 	EPDBG(ep, "Hub remote wakeup %s\n",
222 	      is_set ? "enabled" : "disabled");
223 
224 	return std_req_complete;
225 }
226 
227 static int ast_vhub_hub_ep_feature(struct ast_vhub_ep *ep,
228 				   u16 wIndex, u16 wValue,
229 				   bool is_set)
230 {
231 	int ep_num;
232 	u32 reg;
233 
234 	ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
235 	EPDBG(ep, "%s_FEATURE(ep%d val=%02x)\n",
236 	      is_set ? "SET" : "CLEAR", ep_num, wValue);
237 
238 	if (ep_num > 1)
239 		return std_req_stall;
240 	if (wValue != USB_ENDPOINT_HALT)
241 		return std_req_stall;
242 	if (ep_num == 0)
243 		return std_req_complete;
244 
245 	EPDBG(ep, "%s stall on EP 1\n",
246 	      is_set ? "setting" : "clearing");
247 
248 	ep->vhub->ep1_stalled = is_set;
249 	reg = readl(ep->vhub->regs + AST_VHUB_EP1_CTRL);
250 	if (is_set) {
251 		reg |= VHUB_EP1_CTRL_STALL;
252 	} else {
253 		reg &= ~VHUB_EP1_CTRL_STALL;
254 		reg |= VHUB_EP1_CTRL_RESET_TOGGLE;
255 	}
256 	writel(reg, ep->vhub->regs + AST_VHUB_EP1_CTRL);
257 
258 	return std_req_complete;
259 }
260 
261 static int ast_vhub_rep_desc(struct ast_vhub_ep *ep,
262 			     u8 desc_type, u16 len)
263 {
264 	size_t dsize;
265 	struct ast_vhub *vhub = ep->vhub;
266 
267 	EPDBG(ep, "GET_DESCRIPTOR(type:%d)\n", desc_type);
268 
269 	/*
270 	 * Copy first to EP buffer and send from there, so
271 	 * we can do some in-place patching if needed. We know
272 	 * the EP buffer is big enough but ensure that doesn't
273 	 * change. We do that now rather than later after we
274 	 * have checked sizes etc... to avoid a gcc bug where
275 	 * it thinks len is constant and barfs about read
276 	 * overflows in memcpy.
277 	 */
278 	switch(desc_type) {
279 	case USB_DT_DEVICE:
280 		dsize = USB_DT_DEVICE_SIZE;
281 		memcpy(ep->buf, &vhub->vhub_dev_desc, dsize);
282 		BUILD_BUG_ON(dsize > sizeof(vhub->vhub_dev_desc));
283 		BUILD_BUG_ON(USB_DT_DEVICE_SIZE >= AST_VHUB_EP0_MAX_PACKET);
284 		break;
285 	case USB_DT_CONFIG:
286 		dsize = AST_VHUB_CONF_DESC_SIZE;
287 		memcpy(ep->buf, &vhub->vhub_conf_desc, dsize);
288 		BUILD_BUG_ON(dsize > sizeof(vhub->vhub_conf_desc));
289 		BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
290 		break;
291 	case USB_DT_HUB:
292 		dsize = AST_VHUB_HUB_DESC_SIZE;
293 		memcpy(ep->buf, &vhub->vhub_hub_desc, dsize);
294 		BUILD_BUG_ON(dsize > sizeof(vhub->vhub_hub_desc));
295 		BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
296 		break;
297 	default:
298 		return std_req_stall;
299 	}
300 
301 	/* Crop requested length */
302 	if (len > dsize)
303 		len = dsize;
304 
305 	/* Patch it if forcing USB1 */
306 	if (desc_type == USB_DT_DEVICE && ep->vhub->force_usb1)
307 		ast_vhub_patch_dev_desc_usb1(ep->buf);
308 
309 	/* Shoot it from the EP buffer */
310 	return ast_vhub_reply(ep, NULL, len);
311 }
312 
313 static int ast_vhub_rep_string(struct ast_vhub_ep *ep,
314 			       u8 string_id, u16 lang_id,
315 			       u16 len)
316 {
317 	int rc = usb_gadget_get_string(&ep->vhub->vhub_str_desc,
318 					string_id, ep->buf);
319 
320 	/*
321 	 * This should never happen unless we put too big strings in
322 	 * the array above
323 	 */
324 	BUG_ON(rc >= AST_VHUB_EP0_MAX_PACKET);
325 
326 	if (rc < 0)
327 		return std_req_stall;
328 
329 	/* Shoot it from the EP buffer */
330 	return ast_vhub_reply(ep, NULL, min_t(u16, rc, len));
331 }
332 
333 enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
334 					 struct usb_ctrlrequest *crq)
335 {
336 	struct ast_vhub *vhub = ep->vhub;
337 	u16 wValue, wIndex, wLength;
338 
339 	wValue = le16_to_cpu(crq->wValue);
340 	wIndex = le16_to_cpu(crq->wIndex);
341 	wLength = le16_to_cpu(crq->wLength);
342 
343 	/* First packet, grab speed */
344 	if (vhub->speed == USB_SPEED_UNKNOWN) {
345 		u32 ustat = readl(vhub->regs + AST_VHUB_USBSTS);
346 		if (ustat & VHUB_USBSTS_HISPEED)
347 			vhub->speed = USB_SPEED_HIGH;
348 		else
349 			vhub->speed = USB_SPEED_FULL;
350 		UDCDBG(vhub, "USB status=%08x speed=%s\n", ustat,
351 		       vhub->speed == USB_SPEED_HIGH ? "high" : "full");
352 	}
353 
354 	switch ((crq->bRequestType << 8) | crq->bRequest) {
355 		/* SET_ADDRESS */
356 	case DeviceOutRequest | USB_REQ_SET_ADDRESS:
357 		EPDBG(ep, "SET_ADDRESS: Got address %x\n", wValue);
358 		writel(wValue, vhub->regs + AST_VHUB_CONF);
359 		return std_req_complete;
360 
361 		/* GET_STATUS */
362 	case DeviceRequest | USB_REQ_GET_STATUS:
363 		return ast_vhub_hub_dev_status(ep, wIndex, wValue);
364 	case InterfaceRequest | USB_REQ_GET_STATUS:
365 		return ast_vhub_simple_reply(ep, 0, 0);
366 	case EndpointRequest | USB_REQ_GET_STATUS:
367 		return ast_vhub_hub_ep_status(ep, wIndex, wValue);
368 
369 		/* SET/CLEAR_FEATURE */
370 	case DeviceOutRequest | USB_REQ_SET_FEATURE:
371 		return ast_vhub_hub_dev_feature(ep, wIndex, wValue, true);
372 	case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
373 		return ast_vhub_hub_dev_feature(ep, wIndex, wValue, false);
374 	case EndpointOutRequest | USB_REQ_SET_FEATURE:
375 		return ast_vhub_hub_ep_feature(ep, wIndex, wValue, true);
376 	case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
377 		return ast_vhub_hub_ep_feature(ep, wIndex, wValue, false);
378 
379 		/* GET/SET_CONFIGURATION */
380 	case DeviceRequest | USB_REQ_GET_CONFIGURATION:
381 		return ast_vhub_simple_reply(ep, 1);
382 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
383 		if (wValue != 1)
384 			return std_req_stall;
385 		return std_req_complete;
386 
387 		/* GET_DESCRIPTOR */
388 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
389 		switch (wValue >> 8) {
390 		case USB_DT_DEVICE:
391 		case USB_DT_CONFIG:
392 			return ast_vhub_rep_desc(ep, wValue >> 8,
393 						 wLength);
394 		case USB_DT_STRING:
395 			return ast_vhub_rep_string(ep, wValue & 0xff,
396 						   wIndex, wLength);
397 		}
398 		return std_req_stall;
399 
400 		/* GET/SET_INTERFACE */
401 	case DeviceRequest | USB_REQ_GET_INTERFACE:
402 		return ast_vhub_simple_reply(ep, 0);
403 	case DeviceOutRequest | USB_REQ_SET_INTERFACE:
404 		if (wValue != 0 || wIndex != 0)
405 			return std_req_stall;
406 		return std_req_complete;
407 	}
408 	return std_req_stall;
409 }
410 
411 static void ast_vhub_update_hub_ep1(struct ast_vhub *vhub,
412 				    unsigned int port)
413 {
414 	/* Update HW EP1 response */
415 	u32 reg = readl(vhub->regs + AST_VHUB_EP1_STS_CHG);
416 	u32 pmask = (1 << (port + 1));
417 	if (vhub->ports[port].change)
418 		reg |= pmask;
419 	else
420 		reg &= ~pmask;
421 	writel(reg, vhub->regs + AST_VHUB_EP1_STS_CHG);
422 }
423 
424 static void ast_vhub_change_port_stat(struct ast_vhub *vhub,
425 				      unsigned int port,
426 				      u16 clr_flags,
427 				      u16 set_flags,
428 				      bool set_c)
429 {
430 	struct ast_vhub_port *p = &vhub->ports[port];
431 	u16 prev;
432 
433 	/* Update port status */
434 	prev = p->status;
435 	p->status = (prev & ~clr_flags) | set_flags;
436 	DDBG(&p->dev, "port %d status %04x -> %04x (C=%d)\n",
437 	     port + 1, prev, p->status, set_c);
438 
439 	/* Update change bits if needed */
440 	if (set_c) {
441 		u16 chg = p->status ^ prev;
442 
443 		/* Only these are relevant for change */
444 		chg &= USB_PORT_STAT_C_CONNECTION |
445 		       USB_PORT_STAT_C_ENABLE |
446 		       USB_PORT_STAT_C_SUSPEND |
447 		       USB_PORT_STAT_C_OVERCURRENT |
448 		       USB_PORT_STAT_C_RESET |
449 		       USB_PORT_STAT_C_L1;
450 
451 		/*
452 		 * We only set USB_PORT_STAT_C_ENABLE if we are disabling
453 		 * the port as per USB spec, otherwise MacOS gets upset
454 		 */
455 		if (p->status & USB_PORT_STAT_ENABLE)
456 			chg &= ~USB_PORT_STAT_C_ENABLE;
457 
458 		p->change = chg;
459 		ast_vhub_update_hub_ep1(vhub, port);
460 	}
461 }
462 
463 static void ast_vhub_send_host_wakeup(struct ast_vhub *vhub)
464 {
465 	u32 reg = readl(vhub->regs + AST_VHUB_CTRL);
466 	UDCDBG(vhub, "Waking up host !\n");
467 	reg |= VHUB_CTRL_MANUAL_REMOTE_WAKEUP;
468 	writel(reg, vhub->regs + AST_VHUB_CTRL);
469 }
470 
471 void ast_vhub_device_connect(struct ast_vhub *vhub,
472 			     unsigned int port, bool on)
473 {
474 	if (on)
475 		ast_vhub_change_port_stat(vhub, port, 0,
476 					  USB_PORT_STAT_CONNECTION, true);
477 	else
478 		ast_vhub_change_port_stat(vhub, port,
479 					  USB_PORT_STAT_CONNECTION |
480 					  USB_PORT_STAT_ENABLE,
481 					  0, true);
482 
483 	/*
484 	 * If the hub is set to wakup the host on connection events
485 	 * then send a wakeup.
486 	 */
487 	if (vhub->wakeup_en)
488 		ast_vhub_send_host_wakeup(vhub);
489 }
490 
491 static void ast_vhub_wake_work(struct work_struct *work)
492 {
493 	struct ast_vhub *vhub = container_of(work,
494 					     struct ast_vhub,
495 					     wake_work);
496 	unsigned long flags;
497 	unsigned int i;
498 
499 	/*
500 	 * Wake all sleeping ports. If a port is suspended by
501 	 * the host suspend (without explicit state suspend),
502 	 * we let the normal host wake path deal with it later.
503 	 */
504 	spin_lock_irqsave(&vhub->lock, flags);
505 	for (i = 0; i < vhub->max_ports; i++) {
506 		struct ast_vhub_port *p = &vhub->ports[i];
507 
508 		if (!(p->status & USB_PORT_STAT_SUSPEND))
509 			continue;
510 		ast_vhub_change_port_stat(vhub, i,
511 					  USB_PORT_STAT_SUSPEND,
512 					  0, true);
513 		ast_vhub_dev_resume(&p->dev);
514 	}
515 	ast_vhub_send_host_wakeup(vhub);
516 	spin_unlock_irqrestore(&vhub->lock, flags);
517 }
518 
519 void ast_vhub_hub_wake_all(struct ast_vhub *vhub)
520 {
521 	/*
522 	 * A device is trying to wake the world, because this
523 	 * can recurse into the device, we break the call chain
524 	 * using a work queue
525 	 */
526 	schedule_work(&vhub->wake_work);
527 }
528 
529 static void ast_vhub_port_reset(struct ast_vhub *vhub, u8 port)
530 {
531 	struct ast_vhub_port *p = &vhub->ports[port];
532 	u16 set, clr, speed;
533 
534 	/* First mark disabled */
535 	ast_vhub_change_port_stat(vhub, port,
536 				  USB_PORT_STAT_ENABLE |
537 				  USB_PORT_STAT_SUSPEND,
538 				  USB_PORT_STAT_RESET,
539 				  false);
540 
541 	if (!p->dev.driver)
542 		return;
543 
544 	/*
545 	 * This will either "start" the port or reset the
546 	 * device if already started...
547 	 */
548 	ast_vhub_dev_reset(&p->dev);
549 
550 	/* Grab the right speed */
551 	speed = p->dev.driver->max_speed;
552 	if (speed == USB_SPEED_UNKNOWN || speed > vhub->speed)
553 		speed = vhub->speed;
554 
555 	switch (speed) {
556 	case USB_SPEED_LOW:
557 		set = USB_PORT_STAT_LOW_SPEED;
558 		clr = USB_PORT_STAT_HIGH_SPEED;
559 		break;
560 	case USB_SPEED_FULL:
561 		set = 0;
562 		clr = USB_PORT_STAT_LOW_SPEED |
563 			USB_PORT_STAT_HIGH_SPEED;
564 		break;
565 	case USB_SPEED_HIGH:
566 		set = USB_PORT_STAT_HIGH_SPEED;
567 		clr = USB_PORT_STAT_LOW_SPEED;
568 		break;
569 	default:
570 		UDCDBG(vhub, "Unsupported speed %d when"
571 		       " connecting device\n",
572 		       speed);
573 		return;
574 	}
575 	clr |= USB_PORT_STAT_RESET;
576 	set |= USB_PORT_STAT_ENABLE;
577 
578 	/* This should ideally be delayed ... */
579 	ast_vhub_change_port_stat(vhub, port, clr, set, true);
580 }
581 
582 static enum std_req_rc ast_vhub_set_port_feature(struct ast_vhub_ep *ep,
583 						 u8 port, u16 feat)
584 {
585 	struct ast_vhub *vhub = ep->vhub;
586 	struct ast_vhub_port *p;
587 
588 	if (port == 0 || port > vhub->max_ports)
589 		return std_req_stall;
590 	port--;
591 	p = &vhub->ports[port];
592 
593 	switch(feat) {
594 	case USB_PORT_FEAT_SUSPEND:
595 		if (!(p->status & USB_PORT_STAT_ENABLE))
596 			return std_req_complete;
597 		ast_vhub_change_port_stat(vhub, port,
598 					  0, USB_PORT_STAT_SUSPEND,
599 					  false);
600 		ast_vhub_dev_suspend(&p->dev);
601 		return std_req_complete;
602 	case USB_PORT_FEAT_RESET:
603 		EPDBG(ep, "Port reset !\n");
604 		ast_vhub_port_reset(vhub, port);
605 		return std_req_complete;
606 	case USB_PORT_FEAT_POWER:
607 		/*
608 		 * On Power-on, we mark the connected flag changed,
609 		 * if there's a connected device, some hosts will
610 		 * otherwise fail to detect it.
611 		 */
612 		if (p->status & USB_PORT_STAT_CONNECTION) {
613 			p->change |= USB_PORT_STAT_C_CONNECTION;
614 			ast_vhub_update_hub_ep1(vhub, port);
615 		}
616 		return std_req_complete;
617 	case USB_PORT_FEAT_TEST:
618 	case USB_PORT_FEAT_INDICATOR:
619 		/* We don't do anything with these */
620 		return std_req_complete;
621 	}
622 	return std_req_stall;
623 }
624 
625 static enum std_req_rc ast_vhub_clr_port_feature(struct ast_vhub_ep *ep,
626 						 u8 port, u16 feat)
627 {
628 	struct ast_vhub *vhub = ep->vhub;
629 	struct ast_vhub_port *p;
630 
631 	if (port == 0 || port > vhub->max_ports)
632 		return std_req_stall;
633 	port--;
634 	p = &vhub->ports[port];
635 
636 	switch(feat) {
637 	case USB_PORT_FEAT_ENABLE:
638 		ast_vhub_change_port_stat(vhub, port,
639 					  USB_PORT_STAT_ENABLE |
640 					  USB_PORT_STAT_SUSPEND, 0,
641 					  false);
642 		ast_vhub_dev_suspend(&p->dev);
643 		return std_req_complete;
644 	case USB_PORT_FEAT_SUSPEND:
645 		if (!(p->status & USB_PORT_STAT_SUSPEND))
646 			return std_req_complete;
647 		ast_vhub_change_port_stat(vhub, port,
648 					  USB_PORT_STAT_SUSPEND, 0,
649 					  false);
650 		ast_vhub_dev_resume(&p->dev);
651 		return std_req_complete;
652 	case USB_PORT_FEAT_POWER:
653 		/* We don't do power control */
654 		return std_req_complete;
655 	case USB_PORT_FEAT_INDICATOR:
656 		/* We don't have indicators */
657 		return std_req_complete;
658 	case USB_PORT_FEAT_C_CONNECTION:
659 	case USB_PORT_FEAT_C_ENABLE:
660 	case USB_PORT_FEAT_C_SUSPEND:
661 	case USB_PORT_FEAT_C_OVER_CURRENT:
662 	case USB_PORT_FEAT_C_RESET:
663 		/* Clear state-change feature */
664 		p->change &= ~(1u << (feat - 16));
665 		ast_vhub_update_hub_ep1(vhub, port);
666 		return std_req_complete;
667 	}
668 	return std_req_stall;
669 }
670 
671 static enum std_req_rc ast_vhub_get_port_stat(struct ast_vhub_ep *ep,
672 					      u8 port)
673 {
674 	struct ast_vhub *vhub = ep->vhub;
675 	u16 stat, chg;
676 
677 	if (port == 0 || port > vhub->max_ports)
678 		return std_req_stall;
679 	port--;
680 
681 	stat = vhub->ports[port].status;
682 	chg = vhub->ports[port].change;
683 
684 	/* We always have power */
685 	stat |= USB_PORT_STAT_POWER;
686 
687 	EPDBG(ep, " port status=%04x change=%04x\n", stat, chg);
688 
689 	return ast_vhub_simple_reply(ep,
690 				     stat & 0xff,
691 				     stat >> 8,
692 				     chg & 0xff,
693 				     chg >> 8);
694 }
695 
696 enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep,
697 					   struct usb_ctrlrequest *crq)
698 {
699 	u16 wValue, wIndex, wLength;
700 
701 	wValue = le16_to_cpu(crq->wValue);
702 	wIndex = le16_to_cpu(crq->wIndex);
703 	wLength = le16_to_cpu(crq->wLength);
704 
705 	switch ((crq->bRequestType << 8) | crq->bRequest) {
706 	case GetHubStatus:
707 		EPDBG(ep, "GetHubStatus\n");
708 		return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
709 	case GetPortStatus:
710 		EPDBG(ep, "GetPortStatus(%d)\n", wIndex & 0xff);
711 		return ast_vhub_get_port_stat(ep, wIndex & 0xf);
712 	case GetHubDescriptor:
713 		if (wValue != (USB_DT_HUB << 8))
714 			return std_req_stall;
715 		EPDBG(ep, "GetHubDescriptor(%d)\n", wIndex & 0xff);
716 		return ast_vhub_rep_desc(ep, USB_DT_HUB, wLength);
717 	case SetHubFeature:
718 	case ClearHubFeature:
719 		EPDBG(ep, "Get/SetHubFeature(%d)\n", wValue);
720 		/* No feature, just complete the requests */
721 		if (wValue == C_HUB_LOCAL_POWER ||
722 		    wValue == C_HUB_OVER_CURRENT)
723 			return std_req_complete;
724 		return std_req_stall;
725 	case SetPortFeature:
726 		EPDBG(ep, "SetPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
727 		return ast_vhub_set_port_feature(ep, wIndex & 0xf, wValue);
728 	case ClearPortFeature:
729 		EPDBG(ep, "ClearPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
730 		return ast_vhub_clr_port_feature(ep, wIndex & 0xf, wValue);
731 	case ClearTTBuffer:
732 	case ResetTT:
733 	case StopTT:
734 		return std_req_complete;
735 	case GetTTState:
736 		return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
737 	default:
738 		EPDBG(ep, "Unknown class request\n");
739 	}
740 	return std_req_stall;
741 }
742 
743 void ast_vhub_hub_suspend(struct ast_vhub *vhub)
744 {
745 	unsigned int i;
746 
747 	UDCDBG(vhub, "USB bus suspend\n");
748 
749 	if (vhub->suspended)
750 		return;
751 
752 	vhub->suspended = true;
753 
754 	/*
755 	 * Forward to unsuspended ports without changing
756 	 * their connection status.
757 	 */
758 	for (i = 0; i < vhub->max_ports; i++) {
759 		struct ast_vhub_port *p = &vhub->ports[i];
760 
761 		if (!(p->status & USB_PORT_STAT_SUSPEND))
762 			ast_vhub_dev_suspend(&p->dev);
763 	}
764 }
765 
766 void ast_vhub_hub_resume(struct ast_vhub *vhub)
767 {
768 	unsigned int i;
769 
770 	UDCDBG(vhub, "USB bus resume\n");
771 
772 	if (!vhub->suspended)
773 		return;
774 
775 	vhub->suspended = false;
776 
777 	/*
778 	 * Forward to unsuspended ports without changing
779 	 * their connection status.
780 	 */
781 	for (i = 0; i < vhub->max_ports; i++) {
782 		struct ast_vhub_port *p = &vhub->ports[i];
783 
784 		if (!(p->status & USB_PORT_STAT_SUSPEND))
785 			ast_vhub_dev_resume(&p->dev);
786 	}
787 }
788 
789 void ast_vhub_hub_reset(struct ast_vhub *vhub)
790 {
791 	unsigned int i;
792 
793 	UDCDBG(vhub, "USB bus reset\n");
794 
795 	/*
796 	 * Is the speed known ? If not we don't care, we aren't
797 	 * initialized yet and ports haven't been enabled.
798 	 */
799 	if (vhub->speed == USB_SPEED_UNKNOWN)
800 		return;
801 
802 	/* We aren't suspended anymore obviously */
803 	vhub->suspended = false;
804 
805 	/* No speed set */
806 	vhub->speed = USB_SPEED_UNKNOWN;
807 
808 	/* Wakeup not enabled anymore */
809 	vhub->wakeup_en = false;
810 
811 	/*
812 	 * Clear all port status, disable gadgets and "suspend"
813 	 * them. They will be woken up by a port reset.
814 	 */
815 	for (i = 0; i < vhub->max_ports; i++) {
816 		struct ast_vhub_port *p = &vhub->ports[i];
817 
818 		/* Only keep the connected flag */
819 		p->status &= USB_PORT_STAT_CONNECTION;
820 		p->change = 0;
821 
822 		/* Suspend the gadget if any */
823 		ast_vhub_dev_suspend(&p->dev);
824 	}
825 
826 	/* Cleanup HW */
827 	writel(0, vhub->regs + AST_VHUB_CONF);
828 	writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
829 	writel(VHUB_EP1_CTRL_RESET_TOGGLE |
830 	       VHUB_EP1_CTRL_ENABLE,
831 	       vhub->regs + AST_VHUB_EP1_CTRL);
832 	writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
833 }
834 
835 static void ast_vhub_init_desc(struct ast_vhub *vhub)
836 {
837 	/* Initialize vhub Device Descriptor. */
838 	memcpy(&vhub->vhub_dev_desc, &ast_vhub_dev_desc,
839 		sizeof(vhub->vhub_dev_desc));
840 
841 	/* Initialize vhub Configuration Descriptor. */
842 	memcpy(&vhub->vhub_conf_desc, &ast_vhub_conf_desc,
843 		sizeof(vhub->vhub_conf_desc));
844 
845 	/* Initialize vhub Hub Descriptor. */
846 	memcpy(&vhub->vhub_hub_desc, &ast_vhub_hub_desc,
847 		sizeof(vhub->vhub_hub_desc));
848 	vhub->vhub_hub_desc.bNbrPorts = vhub->max_ports;
849 
850 	/* Initialize vhub String Descriptors. */
851 	memcpy(&vhub->vhub_str_desc, &ast_vhub_strings,
852 		sizeof(vhub->vhub_str_desc));
853 }
854 
855 void ast_vhub_init_hub(struct ast_vhub *vhub)
856 {
857 	vhub->speed = USB_SPEED_UNKNOWN;
858 	INIT_WORK(&vhub->wake_work, ast_vhub_wake_work);
859 
860 	ast_vhub_init_desc(vhub);
861 }
862 
863