1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
4  *
5  * dev.c - Individual device/gadget management (ie, a port = a gadget)
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/regmap.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
28 
29 #include "vhub.h"
30 
31 void ast_vhub_dev_irq(struct ast_vhub_dev *d)
32 {
33 	u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
34 
35 	writel(istat, d->regs + AST_VHUB_DEV_ISR);
36 
37 	if (istat & VHUV_DEV_IRQ_EP0_IN_ACK_STALL)
38 		ast_vhub_ep0_handle_ack(&d->ep0, true);
39 	if (istat & VHUV_DEV_IRQ_EP0_OUT_ACK_STALL)
40 		ast_vhub_ep0_handle_ack(&d->ep0, false);
41 	if (istat & VHUV_DEV_IRQ_EP0_SETUP)
42 		ast_vhub_ep0_handle_setup(&d->ep0);
43 }
44 
45 static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
46 {
47 	u32 reg, hmsk, i;
48 
49 	if (d->enabled)
50 		return;
51 
52 	/* Cleanup EP0 state */
53 	ast_vhub_reset_ep0(d);
54 
55 	/* Enable device and its EP0 interrupts */
56 	reg = VHUB_DEV_EN_ENABLE_PORT |
57 		VHUB_DEV_EN_EP0_IN_ACK_IRQEN |
58 		VHUB_DEV_EN_EP0_OUT_ACK_IRQEN |
59 		VHUB_DEV_EN_EP0_SETUP_IRQEN;
60 	if (d->gadget.speed == USB_SPEED_HIGH)
61 		reg |= VHUB_DEV_EN_SPEED_SEL_HIGH;
62 	writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
63 
64 	/* Enable device interrupt in the hub as well */
65 	hmsk = VHUB_IRQ_DEVICE1 << d->index;
66 	reg = readl(d->vhub->regs + AST_VHUB_IER);
67 	reg |= hmsk;
68 	writel(reg, d->vhub->regs + AST_VHUB_IER);
69 
70 	/* Set EP0 DMA buffer address */
71 	writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
72 
73 	/* Clear stall on all EPs */
74 	for (i = 0; i < d->max_epns; i++) {
75 		struct ast_vhub_ep *ep = d->epns[i];
76 
77 		if (ep && (ep->epn.stalled || ep->epn.wedged)) {
78 			ep->epn.stalled = false;
79 			ep->epn.wedged = false;
80 			ast_vhub_update_epn_stall(ep);
81 		}
82 	}
83 
84 	/* Additional cleanups */
85 	d->wakeup_en = false;
86 	d->enabled = true;
87 }
88 
89 static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
90 {
91 	u32 reg, hmsk;
92 
93 	if (!d->enabled)
94 		return;
95 
96 	/* Disable device interrupt in the hub */
97 	hmsk = VHUB_IRQ_DEVICE1 << d->index;
98 	reg = readl(d->vhub->regs + AST_VHUB_IER);
99 	reg &= ~hmsk;
100 	writel(reg, d->vhub->regs + AST_VHUB_IER);
101 
102 	/* Then disable device */
103 	writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
104 	d->gadget.speed = USB_SPEED_UNKNOWN;
105 	d->enabled = false;
106 }
107 
108 static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
109 				u16 wIndex, u16 wValue,
110 				bool is_set)
111 {
112 	u32 val;
113 
114 	DDBG(d, "%s_FEATURE(dev val=%02x)\n",
115 	     is_set ? "SET" : "CLEAR", wValue);
116 
117 	if (wValue == USB_DEVICE_REMOTE_WAKEUP) {
118 		d->wakeup_en = is_set;
119 		return std_req_complete;
120 	}
121 
122 	if (wValue == USB_DEVICE_TEST_MODE) {
123 		val = readl(d->vhub->regs + AST_VHUB_CTRL);
124 		val &= ~GENMASK(10, 8);
125 		val |= VHUB_CTRL_SET_TEST_MODE((wIndex >> 8) & 0x7);
126 		writel(val, d->vhub->regs + AST_VHUB_CTRL);
127 
128 		return std_req_complete;
129 	}
130 
131 	return std_req_driver;
132 }
133 
134 static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
135 			       u16 wIndex, u16 wValue, bool is_set)
136 {
137 	struct ast_vhub_ep *ep;
138 	int ep_num;
139 
140 	ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
141 	DDBG(d, "%s_FEATURE(ep%d val=%02x)\n",
142 	     is_set ? "SET" : "CLEAR", ep_num, wValue);
143 	if (ep_num == 0)
144 		return std_req_complete;
145 	if (ep_num >= d->max_epns || !d->epns[ep_num - 1])
146 		return std_req_stall;
147 	if (wValue != USB_ENDPOINT_HALT)
148 		return std_req_driver;
149 
150 	ep = d->epns[ep_num - 1];
151 	if (WARN_ON(!ep))
152 		return std_req_stall;
153 
154 	if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
155 	    ep->epn.is_in != !!(wIndex & USB_DIR_IN))
156 		return std_req_stall;
157 
158 	DDBG(d, "%s stall on EP %d\n",
159 	     is_set ? "setting" : "clearing", ep_num);
160 	ep->epn.stalled = is_set;
161 	ast_vhub_update_epn_stall(ep);
162 
163 	return std_req_complete;
164 }
165 
166 static int ast_vhub_dev_status(struct ast_vhub_dev *d,
167 			       u16 wIndex, u16 wValue)
168 {
169 	u8 st0;
170 
171 	DDBG(d, "GET_STATUS(dev)\n");
172 
173 	st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
174 	if (d->wakeup_en)
175 		st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
176 
177 	return ast_vhub_simple_reply(&d->ep0, st0, 0);
178 }
179 
180 static int ast_vhub_ep_status(struct ast_vhub_dev *d,
181 			      u16 wIndex, u16 wValue)
182 {
183 	int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
184 	struct ast_vhub_ep *ep;
185 	u8 st0 = 0;
186 
187 	DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
188 
189 	if (ep_num >= d->max_epns)
190 		return std_req_stall;
191 	if (ep_num != 0) {
192 		ep = d->epns[ep_num - 1];
193 		if (!ep)
194 			return std_req_stall;
195 		if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
196 		    ep->epn.is_in != !!(wIndex & USB_DIR_IN))
197 			return std_req_stall;
198 		if (ep->epn.stalled)
199 			st0 |= 1 << USB_ENDPOINT_HALT;
200 	}
201 
202 	return ast_vhub_simple_reply(&d->ep0, st0, 0);
203 }
204 
205 static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
206 {
207 	u32 reg;
208 
209 	DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
210 
211 	reg = readl(d->regs + AST_VHUB_DEV_EN_CTRL);
212 	reg &= ~VHUB_DEV_EN_ADDR_MASK;
213 	reg |= VHUB_DEV_EN_SET_ADDR(addr);
214 	writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
215 }
216 
217 int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
218 			     struct usb_ctrlrequest *crq)
219 {
220 	struct ast_vhub_dev *d = ep->dev;
221 	u16 wValue, wIndex;
222 
223 	/* No driver, we shouldn't be enabled ... */
224 	if (!d->driver || !d->enabled) {
225 		EPDBG(ep,
226 		      "Device is wrong state driver=%p enabled=%d\n",
227 		      d->driver, d->enabled);
228 		return std_req_stall;
229 	}
230 
231 	/*
232 	 * Note: we used to reject/stall requests while suspended,
233 	 * we don't do that anymore as we seem to have cases of
234 	 * mass storage getting very upset.
235 	 */
236 
237 	/* First packet, grab speed */
238 	if (d->gadget.speed == USB_SPEED_UNKNOWN) {
239 		d->gadget.speed = ep->vhub->speed;
240 		if (d->gadget.speed > d->driver->max_speed)
241 			d->gadget.speed = d->driver->max_speed;
242 		DDBG(d, "fist packet, captured speed %d\n",
243 		     d->gadget.speed);
244 	}
245 
246 	wValue = le16_to_cpu(crq->wValue);
247 	wIndex = le16_to_cpu(crq->wIndex);
248 
249 	switch ((crq->bRequestType << 8) | crq->bRequest) {
250 		/* SET_ADDRESS */
251 	case DeviceOutRequest | USB_REQ_SET_ADDRESS:
252 		ast_vhub_dev_set_address(d, wValue);
253 		return std_req_complete;
254 
255 		/* GET_STATUS */
256 	case DeviceRequest | USB_REQ_GET_STATUS:
257 		return ast_vhub_dev_status(d, wIndex, wValue);
258 	case InterfaceRequest | USB_REQ_GET_STATUS:
259 		return ast_vhub_simple_reply(ep, 0, 0);
260 	case EndpointRequest | USB_REQ_GET_STATUS:
261 		return ast_vhub_ep_status(d, wIndex, wValue);
262 
263 		/* SET/CLEAR_FEATURE */
264 	case DeviceOutRequest | USB_REQ_SET_FEATURE:
265 		return ast_vhub_dev_feature(d, wIndex, wValue, true);
266 	case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
267 		return ast_vhub_dev_feature(d, wIndex, wValue, false);
268 	case EndpointOutRequest | USB_REQ_SET_FEATURE:
269 		return ast_vhub_ep_feature(d, wIndex, wValue, true);
270 	case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
271 		return ast_vhub_ep_feature(d, wIndex, wValue, false);
272 	}
273 	return std_req_driver;
274 }
275 
276 static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
277 {
278 	struct ast_vhub_dev *d = to_ast_dev(gadget);
279 	unsigned long flags;
280 	int rc = -EINVAL;
281 
282 	spin_lock_irqsave(&d->vhub->lock, flags);
283 	if (!d->wakeup_en)
284 		goto err;
285 
286 	DDBG(d, "Device initiated wakeup\n");
287 
288 	/* Wakeup the host */
289 	ast_vhub_hub_wake_all(d->vhub);
290 	rc = 0;
291  err:
292 	spin_unlock_irqrestore(&d->vhub->lock, flags);
293 	return rc;
294 }
295 
296 static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
297 {
298 	struct ast_vhub_dev *d = to_ast_dev(gadget);
299 
300 	return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
301 }
302 
303 static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
304 {
305 	unsigned int i;
306 
307 	for (i = 0; i < d->max_epns; i++) {
308 		if (!d->epns[i])
309 			continue;
310 		ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
311 	}
312 }
313 
314 static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
315 {
316 	struct ast_vhub_dev *d = to_ast_dev(gadget);
317 	unsigned long flags;
318 
319 	spin_lock_irqsave(&d->vhub->lock, flags);
320 
321 	DDBG(d, "pullup(%d)\n", on);
322 
323 	/* Mark disconnected in the hub */
324 	ast_vhub_device_connect(d->vhub, d->index, on);
325 
326 	/*
327 	 * If enabled, nuke all requests if any (there shouldn't be)
328 	 * and disable the port. This will clear the address too.
329 	 */
330 	if (d->enabled) {
331 		ast_vhub_dev_nuke(d);
332 		ast_vhub_dev_disable(d);
333 	}
334 
335 	spin_unlock_irqrestore(&d->vhub->lock, flags);
336 
337 	return 0;
338 }
339 
340 static int ast_vhub_udc_start(struct usb_gadget *gadget,
341 			      struct usb_gadget_driver *driver)
342 {
343 	struct ast_vhub_dev *d = to_ast_dev(gadget);
344 	unsigned long flags;
345 
346 	spin_lock_irqsave(&d->vhub->lock, flags);
347 
348 	DDBG(d, "start\n");
349 
350 	/* We don't do much more until the hub enables us */
351 	d->driver = driver;
352 	d->gadget.is_selfpowered = 1;
353 
354 	spin_unlock_irqrestore(&d->vhub->lock, flags);
355 
356 	return 0;
357 }
358 
359 static struct usb_ep *ast_vhub_udc_match_ep(struct usb_gadget *gadget,
360 					    struct usb_endpoint_descriptor *desc,
361 					    struct usb_ss_ep_comp_descriptor *ss)
362 {
363 	struct ast_vhub_dev *d = to_ast_dev(gadget);
364 	struct ast_vhub_ep *ep;
365 	struct usb_ep *u_ep;
366 	unsigned int max, addr, i;
367 
368 	DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
369 
370 	/*
371 	 * First we need to look for an existing unclaimed EP as another
372 	 * configuration may have already associated a bunch of EPs with
373 	 * this gadget. This duplicates the code in usb_ep_autoconfig_ss()
374 	 * unfortunately.
375 	 */
376 	list_for_each_entry(u_ep, &gadget->ep_list, ep_list) {
377 		if (usb_gadget_ep_match_desc(gadget, u_ep, desc, ss)) {
378 			DDBG(d, " -> using existing EP%d\n",
379 			     to_ast_ep(u_ep)->d_idx);
380 			return u_ep;
381 		}
382 	}
383 
384 	/*
385 	 * We didn't find one, we need to grab one from the pool.
386 	 *
387 	 * First let's do some sanity checking
388 	 */
389 	switch(usb_endpoint_type(desc)) {
390 	case USB_ENDPOINT_XFER_CONTROL:
391 		/* Only EP0 can be a control endpoint */
392 		return NULL;
393 	case USB_ENDPOINT_XFER_ISOC:
394 		/* ISO:	 limit 1023 bytes full speed, 1024 high/super speed */
395 		if (gadget_is_dualspeed(gadget))
396 			max = 1024;
397 		else
398 			max = 1023;
399 		break;
400 	case USB_ENDPOINT_XFER_BULK:
401 		if (gadget_is_dualspeed(gadget))
402 			max = 512;
403 		else
404 			max = 64;
405 		break;
406 	case USB_ENDPOINT_XFER_INT:
407 		if (gadget_is_dualspeed(gadget))
408 			max = 1024;
409 		else
410 			max = 64;
411 		break;
412 	}
413 	if (usb_endpoint_maxp(desc) > max)
414 		return NULL;
415 
416 	/*
417 	 * Find a free EP address for that device. We can't
418 	 * let the generic code assign these as it would
419 	 * create overlapping numbers for IN and OUT which
420 	 * we don't support, so also create a suitable name
421 	 * that will allow the generic code to use our
422 	 * assigned address.
423 	 */
424 	for (i = 0; i < d->max_epns; i++)
425 		if (d->epns[i] == NULL)
426 			break;
427 	if (i >= d->max_epns)
428 		return NULL;
429 	addr = i + 1;
430 
431 	/*
432 	 * Now grab an EP from the shared pool and associate
433 	 * it with our device
434 	 */
435 	ep = ast_vhub_alloc_epn(d, addr);
436 	if (!ep)
437 		return NULL;
438 	DDBG(d, "Allocated epn#%d for port EP%d\n",
439 	     ep->epn.g_idx, addr);
440 
441 	return &ep->ep;
442 }
443 
444 static int ast_vhub_udc_stop(struct usb_gadget *gadget)
445 {
446 	struct ast_vhub_dev *d = to_ast_dev(gadget);
447 	unsigned long flags;
448 
449 	spin_lock_irqsave(&d->vhub->lock, flags);
450 
451 	DDBG(d, "stop\n");
452 
453 	d->driver = NULL;
454 	d->gadget.speed = USB_SPEED_UNKNOWN;
455 
456 	ast_vhub_dev_nuke(d);
457 
458 	if (d->enabled)
459 		ast_vhub_dev_disable(d);
460 
461 	spin_unlock_irqrestore(&d->vhub->lock, flags);
462 
463 	return 0;
464 }
465 
466 static const struct usb_gadget_ops ast_vhub_udc_ops = {
467 	.get_frame	= ast_vhub_udc_get_frame,
468 	.wakeup		= ast_vhub_udc_wakeup,
469 	.pullup		= ast_vhub_udc_pullup,
470 	.udc_start	= ast_vhub_udc_start,
471 	.udc_stop	= ast_vhub_udc_stop,
472 	.match_ep	= ast_vhub_udc_match_ep,
473 };
474 
475 void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
476 {
477 	if (d->driver && d->driver->suspend) {
478 		spin_unlock(&d->vhub->lock);
479 		d->driver->suspend(&d->gadget);
480 		spin_lock(&d->vhub->lock);
481 	}
482 }
483 
484 void ast_vhub_dev_resume(struct ast_vhub_dev *d)
485 {
486 	if (d->driver && d->driver->resume) {
487 		spin_unlock(&d->vhub->lock);
488 		d->driver->resume(&d->gadget);
489 		spin_lock(&d->vhub->lock);
490 	}
491 }
492 
493 void ast_vhub_dev_reset(struct ast_vhub_dev *d)
494 {
495 	/* No driver, just disable the device and return */
496 	if (!d->driver) {
497 		ast_vhub_dev_disable(d);
498 		return;
499 	}
500 
501 	/* If the port isn't enabled, just enable it */
502 	if (!d->enabled) {
503 		DDBG(d, "Reset of disabled device, enabling...\n");
504 		ast_vhub_dev_enable(d);
505 	} else {
506 		DDBG(d, "Reset of enabled device, resetting...\n");
507 		spin_unlock(&d->vhub->lock);
508 		usb_gadget_udc_reset(&d->gadget, d->driver);
509 		spin_lock(&d->vhub->lock);
510 
511 		/*
512 		 * Disable and maybe re-enable HW, this will clear the address
513 		 * and speed setting.
514 		 */
515 		ast_vhub_dev_disable(d);
516 		ast_vhub_dev_enable(d);
517 	}
518 }
519 
520 void ast_vhub_del_dev(struct ast_vhub_dev *d)
521 {
522 	unsigned long flags;
523 
524 	spin_lock_irqsave(&d->vhub->lock, flags);
525 	if (!d->registered) {
526 		spin_unlock_irqrestore(&d->vhub->lock, flags);
527 		return;
528 	}
529 	d->registered = false;
530 	spin_unlock_irqrestore(&d->vhub->lock, flags);
531 
532 	usb_del_gadget_udc(&d->gadget);
533 	device_unregister(d->port_dev);
534 	kfree(d->epns);
535 }
536 
537 static void ast_vhub_dev_release(struct device *dev)
538 {
539 	kfree(dev);
540 }
541 
542 int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
543 {
544 	struct ast_vhub_dev *d = &vhub->ports[idx].dev;
545 	struct device *parent = &vhub->pdev->dev;
546 	int rc;
547 
548 	d->vhub = vhub;
549 	d->index = idx;
550 	d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
551 	d->regs = vhub->regs + 0x100 + 0x10 * idx;
552 
553 	ast_vhub_init_ep0(vhub, &d->ep0, d);
554 
555 	/*
556 	 * A USB device can have up to 30 endpoints besides control
557 	 * endpoint 0.
558 	 */
559 	d->max_epns = min_t(u32, vhub->max_epns, 30);
560 	d->epns = kcalloc(d->max_epns, sizeof(*d->epns), GFP_KERNEL);
561 	if (!d->epns)
562 		return -ENOMEM;
563 
564 	/*
565 	 * The UDC core really needs us to have separate and uniquely
566 	 * named "parent" devices for each port so we create a sub device
567 	 * here for that purpose
568 	 */
569 	d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
570 	if (!d->port_dev) {
571 		rc = -ENOMEM;
572 		goto fail_alloc;
573 	}
574 	device_initialize(d->port_dev);
575 	d->port_dev->release = ast_vhub_dev_release;
576 	d->port_dev->parent = parent;
577 	dev_set_name(d->port_dev, "%s:p%d", dev_name(parent), idx + 1);
578 	rc = device_add(d->port_dev);
579 	if (rc)
580 		goto fail_add;
581 
582 	/* Populate gadget */
583 	INIT_LIST_HEAD(&d->gadget.ep_list);
584 	d->gadget.ops = &ast_vhub_udc_ops;
585 	d->gadget.ep0 = &d->ep0.ep;
586 	d->gadget.name = KBUILD_MODNAME;
587 	if (vhub->force_usb1)
588 		d->gadget.max_speed = USB_SPEED_FULL;
589 	else
590 		d->gadget.max_speed = USB_SPEED_HIGH;
591 	d->gadget.speed = USB_SPEED_UNKNOWN;
592 	d->gadget.dev.of_node = vhub->pdev->dev.of_node;
593 	d->gadget.dev.of_node_reused = true;
594 
595 	rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
596 	if (rc != 0)
597 		goto fail_udc;
598 	d->registered = true;
599 
600 	return 0;
601  fail_udc:
602 	device_del(d->port_dev);
603  fail_add:
604 	put_device(d->port_dev);
605  fail_alloc:
606 	kfree(d->epns);
607 
608 	return rc;
609 }
610