1 // SPDX-License-Identifier: GPL-2.0+
3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
5 * dev.c - Individual device/gadget management (ie, a port = a gadget)
7 * Copyright 2017 IBM Corporation
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.
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>
29 #include <linux/of_gpio.h>
30 #include <linux/regmap.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/usb.h>
33 #include <linux/usb/hcd.h>
37 void ast_vhub_dev_irq(struct ast_vhub_dev *d)
39 u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
41 writel(istat, d->regs + AST_VHUB_DEV_ISR);
43 if (istat & VHUV_DEV_IRQ_EP0_IN_ACK_STALL)
44 ast_vhub_ep0_handle_ack(&d->ep0, true);
45 if (istat & VHUV_DEV_IRQ_EP0_OUT_ACK_STALL)
46 ast_vhub_ep0_handle_ack(&d->ep0, false);
47 if (istat & VHUV_DEV_IRQ_EP0_SETUP)
48 ast_vhub_ep0_handle_setup(&d->ep0);
51 static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
58 /* Cleanup EP0 state */
59 ast_vhub_reset_ep0(d);
61 /* Enable device and its EP0 interrupts */
62 reg = VHUB_DEV_EN_ENABLE_PORT |
63 VHUB_DEV_EN_EP0_IN_ACK_IRQEN |
64 VHUB_DEV_EN_EP0_OUT_ACK_IRQEN |
65 VHUB_DEV_EN_EP0_SETUP_IRQEN;
66 if (d->gadget.speed == USB_SPEED_HIGH)
67 reg |= VHUB_DEV_EN_SPEED_SEL_HIGH;
68 writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
70 /* Enable device interrupt in the hub as well */
71 hmsk = VHUB_IRQ_DEVICE1 << d->index;
72 reg = readl(d->vhub->regs + AST_VHUB_IER);
74 writel(reg, d->vhub->regs + AST_VHUB_IER);
76 /* Set EP0 DMA buffer address */
77 writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
79 /* Clear stall on all EPs */
80 for (i = 0; i < AST_VHUB_NUM_GEN_EPs; i++) {
81 struct ast_vhub_ep *ep = d->epns[i];
83 if (ep && (ep->epn.stalled || ep->epn.wedged)) {
84 ep->epn.stalled = false;
85 ep->epn.wedged = false;
86 ast_vhub_update_epn_stall(ep);
90 /* Additional cleanups */
95 static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
102 /* Disable device interrupt in the hub */
103 hmsk = VHUB_IRQ_DEVICE1 << d->index;
104 reg = readl(d->vhub->regs + AST_VHUB_IER);
106 writel(reg, d->vhub->regs + AST_VHUB_IER);
108 /* Then disable device */
109 writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
110 d->gadget.speed = USB_SPEED_UNKNOWN;
114 static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
115 u16 wIndex, u16 wValue,
118 DDBG(d, "%s_FEATURE(dev val=%02x)\n",
119 is_set ? "SET" : "CLEAR", wValue);
121 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
122 return std_req_driver;
124 d->wakeup_en = is_set;
126 return std_req_complete;
129 static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
130 u16 wIndex, u16 wValue, bool is_set)
132 struct ast_vhub_ep *ep;
135 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
136 DDBG(d, "%s_FEATURE(ep%d val=%02x)\n",
137 is_set ? "SET" : "CLEAR", ep_num, wValue);
139 return std_req_complete;
140 if (ep_num >= AST_VHUB_NUM_GEN_EPs || !d->epns[ep_num - 1])
141 return std_req_stall;
142 if (wValue != USB_ENDPOINT_HALT)
143 return std_req_driver;
145 ep = d->epns[ep_num - 1];
147 return std_req_stall;
149 if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
150 ep->epn.is_in != !!(wIndex & USB_DIR_IN))
151 return std_req_stall;
153 DDBG(d, "%s stall on EP %d\n",
154 is_set ? "setting" : "clearing", ep_num);
155 ep->epn.stalled = is_set;
156 ast_vhub_update_epn_stall(ep);
158 return std_req_complete;
161 static int ast_vhub_dev_status(struct ast_vhub_dev *d,
162 u16 wIndex, u16 wValue)
166 DDBG(d, "GET_STATUS(dev)\n");
168 st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
170 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
172 return ast_vhub_simple_reply(&d->ep0, st0, 0);
175 static int ast_vhub_ep_status(struct ast_vhub_dev *d,
176 u16 wIndex, u16 wValue)
178 int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
179 struct ast_vhub_ep *ep;
182 DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
184 if (ep_num >= AST_VHUB_NUM_GEN_EPs)
185 return std_req_stall;
187 ep = d->epns[ep_num - 1];
189 return std_req_stall;
190 if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
191 ep->epn.is_in != !!(wIndex & USB_DIR_IN))
192 return std_req_stall;
194 st0 |= 1 << USB_ENDPOINT_HALT;
197 return ast_vhub_simple_reply(&d->ep0, st0, 0);
200 static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
204 DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
206 reg = readl(d->regs + AST_VHUB_DEV_EN_CTRL);
207 reg &= ~VHUB_DEV_EN_ADDR_MASK;
208 reg |= VHUB_DEV_EN_SET_ADDR(addr);
209 writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
212 int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
213 struct usb_ctrlrequest *crq)
215 struct ast_vhub_dev *d = ep->dev;
218 /* No driver, we shouldn't be enabled ... */
219 if (!d->driver || !d->enabled) {
221 "Device is wrong state driver=%p enabled=%d\n",
222 d->driver, d->enabled);
223 return std_req_stall;
227 * Note: we used to reject/stall requests while suspended,
228 * we don't do that anymore as we seem to have cases of
229 * mass storage getting very upset.
232 /* First packet, grab speed */
233 if (d->gadget.speed == USB_SPEED_UNKNOWN) {
234 d->gadget.speed = ep->vhub->speed;
235 if (d->gadget.speed > d->driver->max_speed)
236 d->gadget.speed = d->driver->max_speed;
237 DDBG(d, "fist packet, captured speed %d\n",
241 wValue = le16_to_cpu(crq->wValue);
242 wIndex = le16_to_cpu(crq->wIndex);
244 switch ((crq->bRequestType << 8) | crq->bRequest) {
246 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
247 ast_vhub_dev_set_address(d, wValue);
248 return std_req_complete;
251 case DeviceRequest | USB_REQ_GET_STATUS:
252 return ast_vhub_dev_status(d, wIndex, wValue);
253 case InterfaceRequest | USB_REQ_GET_STATUS:
254 return ast_vhub_simple_reply(ep, 0, 0);
255 case EndpointRequest | USB_REQ_GET_STATUS:
256 return ast_vhub_ep_status(d, wIndex, wValue);
258 /* SET/CLEAR_FEATURE */
259 case DeviceOutRequest | USB_REQ_SET_FEATURE:
260 return ast_vhub_dev_feature(d, wIndex, wValue, true);
261 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
262 return ast_vhub_dev_feature(d, wIndex, wValue, false);
263 case EndpointOutRequest | USB_REQ_SET_FEATURE:
264 return ast_vhub_ep_feature(d, wIndex, wValue, true);
265 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
266 return ast_vhub_ep_feature(d, wIndex, wValue, false);
268 return std_req_driver;
271 static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
273 struct ast_vhub_dev *d = to_ast_dev(gadget);
277 spin_lock_irqsave(&d->vhub->lock, flags);
281 DDBG(d, "Device initiated wakeup\n");
283 /* Wakeup the host */
284 ast_vhub_hub_wake_all(d->vhub);
287 spin_unlock_irqrestore(&d->vhub->lock, flags);
291 static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
293 struct ast_vhub_dev *d = to_ast_dev(gadget);
295 return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
298 static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
302 for (i = 0; i < AST_VHUB_NUM_GEN_EPs; i++) {
305 ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
309 static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
311 struct ast_vhub_dev *d = to_ast_dev(gadget);
314 spin_lock_irqsave(&d->vhub->lock, flags);
316 DDBG(d, "pullup(%d)\n", on);
318 /* Mark disconnected in the hub */
319 ast_vhub_device_connect(d->vhub, d->index, on);
322 * If enabled, nuke all requests if any (there shouldn't be)
323 * and disable the port. This will clear the address too.
326 ast_vhub_dev_nuke(d);
327 ast_vhub_dev_disable(d);
330 spin_unlock_irqrestore(&d->vhub->lock, flags);
335 static int ast_vhub_udc_start(struct usb_gadget *gadget,
336 struct usb_gadget_driver *driver)
338 struct ast_vhub_dev *d = to_ast_dev(gadget);
341 spin_lock_irqsave(&d->vhub->lock, flags);
345 /* We don't do much more until the hub enables us */
347 d->gadget.is_selfpowered = 1;
349 spin_unlock_irqrestore(&d->vhub->lock, flags);
354 static struct usb_ep *ast_vhub_udc_match_ep(struct usb_gadget *gadget,
355 struct usb_endpoint_descriptor *desc,
356 struct usb_ss_ep_comp_descriptor *ss)
358 struct ast_vhub_dev *d = to_ast_dev(gadget);
359 struct ast_vhub_ep *ep;
361 unsigned int max, addr, i;
363 DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
366 * First we need to look for an existing unclaimed EP as another
367 * configuration may have already associated a bunch of EPs with
368 * this gadget. This duplicates the code in usb_ep_autoconfig_ss()
371 list_for_each_entry(u_ep, &gadget->ep_list, ep_list) {
372 if (usb_gadget_ep_match_desc(gadget, u_ep, desc, ss)) {
373 DDBG(d, " -> using existing EP%d\n",
374 to_ast_ep(u_ep)->d_idx);
380 * We didn't find one, we need to grab one from the pool.
382 * First let's do some sanity checking
384 switch(usb_endpoint_type(desc)) {
385 case USB_ENDPOINT_XFER_CONTROL:
386 /* Only EP0 can be a control endpoint */
388 case USB_ENDPOINT_XFER_ISOC:
389 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
390 if (gadget_is_dualspeed(gadget))
395 case USB_ENDPOINT_XFER_BULK:
396 if (gadget_is_dualspeed(gadget))
401 case USB_ENDPOINT_XFER_INT:
402 if (gadget_is_dualspeed(gadget))
408 if (usb_endpoint_maxp(desc) > max)
412 * Find a free EP address for that device. We can't
413 * let the generic code assign these as it would
414 * create overlapping numbers for IN and OUT which
415 * we don't support, so also create a suitable name
416 * that will allow the generic code to use our
419 for (i = 0; i < AST_VHUB_NUM_GEN_EPs; i++)
420 if (d->epns[i] == NULL)
422 if (i >= AST_VHUB_NUM_GEN_EPs)
427 * Now grab an EP from the shared pool and associate
430 ep = ast_vhub_alloc_epn(d, addr);
433 DDBG(d, "Allocated epn#%d for port EP%d\n",
434 ep->epn.g_idx, addr);
439 static int ast_vhub_udc_stop(struct usb_gadget *gadget)
441 struct ast_vhub_dev *d = to_ast_dev(gadget);
444 spin_lock_irqsave(&d->vhub->lock, flags);
449 d->gadget.speed = USB_SPEED_UNKNOWN;
451 ast_vhub_dev_nuke(d);
454 ast_vhub_dev_disable(d);
456 spin_unlock_irqrestore(&d->vhub->lock, flags);
461 static const struct usb_gadget_ops ast_vhub_udc_ops = {
462 .get_frame = ast_vhub_udc_get_frame,
463 .wakeup = ast_vhub_udc_wakeup,
464 .pullup = ast_vhub_udc_pullup,
465 .udc_start = ast_vhub_udc_start,
466 .udc_stop = ast_vhub_udc_stop,
467 .match_ep = ast_vhub_udc_match_ep,
470 void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
472 if (d->driver && d->driver->suspend) {
473 spin_unlock(&d->vhub->lock);
474 d->driver->suspend(&d->gadget);
475 spin_lock(&d->vhub->lock);
479 void ast_vhub_dev_resume(struct ast_vhub_dev *d)
481 if (d->driver && d->driver->resume) {
482 spin_unlock(&d->vhub->lock);
483 d->driver->resume(&d->gadget);
484 spin_lock(&d->vhub->lock);
488 void ast_vhub_dev_reset(struct ast_vhub_dev *d)
490 /* No driver, just disable the device and return */
492 ast_vhub_dev_disable(d);
496 /* If the port isn't enabled, just enable it */
498 DDBG(d, "Reset of disabled device, enabling...\n");
499 ast_vhub_dev_enable(d);
501 DDBG(d, "Reset of enabled device, resetting...\n");
502 spin_unlock(&d->vhub->lock);
503 usb_gadget_udc_reset(&d->gadget, d->driver);
504 spin_lock(&d->vhub->lock);
507 * Disable and maybe re-enable HW, this will clear the address
510 ast_vhub_dev_disable(d);
511 ast_vhub_dev_enable(d);
515 void ast_vhub_del_dev(struct ast_vhub_dev *d)
519 spin_lock_irqsave(&d->vhub->lock, flags);
520 if (!d->registered) {
521 spin_unlock_irqrestore(&d->vhub->lock, flags);
524 d->registered = false;
525 spin_unlock_irqrestore(&d->vhub->lock, flags);
527 usb_del_gadget_udc(&d->gadget);
528 device_unregister(d->port_dev);
531 static void ast_vhub_dev_release(struct device *dev)
536 int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
538 struct ast_vhub_dev *d = &vhub->ports[idx].dev;
539 struct device *parent = &vhub->pdev->dev;
544 d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
545 d->regs = vhub->regs + 0x100 + 0x10 * idx;
547 ast_vhub_init_ep0(vhub, &d->ep0, d);
550 * The UDC core really needs us to have separate and uniquely
551 * named "parent" devices for each port so we create a sub device
552 * here for that purpose
554 d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
557 device_initialize(d->port_dev);
558 d->port_dev->release = ast_vhub_dev_release;
559 d->port_dev->parent = parent;
560 dev_set_name(d->port_dev, "%s:p%d", dev_name(parent), idx + 1);
561 rc = device_add(d->port_dev);
565 /* Populate gadget */
566 INIT_LIST_HEAD(&d->gadget.ep_list);
567 d->gadget.ops = &ast_vhub_udc_ops;
568 d->gadget.ep0 = &d->ep0.ep;
569 d->gadget.name = KBUILD_MODNAME;
570 if (vhub->force_usb1)
571 d->gadget.max_speed = USB_SPEED_FULL;
573 d->gadget.max_speed = USB_SPEED_HIGH;
574 d->gadget.speed = USB_SPEED_UNKNOWN;
575 d->gadget.dev.of_node = vhub->pdev->dev.of_node;
577 rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
580 d->registered = true;
584 device_del(d->port_dev);
586 put_device(d->port_dev);