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