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