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
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>
24 #include <linux/regmap.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
31 void ast_vhub_dev_irq(struct ast_vhub_dev *d)
33 u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
35 writel(istat, d->regs + AST_VHUB_DEV_ISR);
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);
45 static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
52 /* Cleanup EP0 state */
53 ast_vhub_reset_ep0(d);
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);
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);
68 writel(reg, d->vhub->regs + AST_VHUB_IER);
70 /* Set EP0 DMA buffer address */
71 writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
73 /* Clear stall on all EPs */
74 for (i = 0; i < d->max_epns; i++) {
75 struct ast_vhub_ep *ep = d->epns[i];
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);
84 /* Additional cleanups */
89 static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
96 /* Disable device interrupt in the hub */
97 hmsk = VHUB_IRQ_DEVICE1 << d->index;
98 reg = readl(d->vhub->regs + AST_VHUB_IER);
100 writel(reg, d->vhub->regs + AST_VHUB_IER);
102 /* Then disable device */
103 writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
104 d->gadget.speed = USB_SPEED_UNKNOWN;
108 static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
109 u16 wIndex, u16 wValue,
114 DDBG(d, "%s_FEATURE(dev val=%02x)\n",
115 is_set ? "SET" : "CLEAR", wValue);
117 if (wValue == USB_DEVICE_REMOTE_WAKEUP) {
118 d->wakeup_en = is_set;
119 return std_req_complete;
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);
128 return std_req_complete;
131 return std_req_driver;
134 static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
135 u16 wIndex, u16 wValue, bool is_set)
137 struct ast_vhub_ep *ep;
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);
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;
150 ep = d->epns[ep_num - 1];
152 return std_req_stall;
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;
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);
163 return std_req_complete;
166 static int ast_vhub_dev_status(struct ast_vhub_dev *d,
167 u16 wIndex, u16 wValue)
171 DDBG(d, "GET_STATUS(dev)\n");
173 st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
175 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
177 return ast_vhub_simple_reply(&d->ep0, st0, 0);
180 static int ast_vhub_ep_status(struct ast_vhub_dev *d,
181 u16 wIndex, u16 wValue)
183 int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
184 struct ast_vhub_ep *ep;
187 DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
189 if (ep_num >= d->max_epns)
190 return std_req_stall;
192 ep = d->epns[ep_num - 1];
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;
199 st0 |= 1 << USB_ENDPOINT_HALT;
202 return ast_vhub_simple_reply(&d->ep0, st0, 0);
205 static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
209 DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
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);
217 int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
218 struct usb_ctrlrequest *crq)
220 struct ast_vhub_dev *d = ep->dev;
223 /* No driver, we shouldn't be enabled ... */
224 if (!d->driver || !d->enabled) {
226 "Device is wrong state driver=%p enabled=%d\n",
227 d->driver, d->enabled);
228 return std_req_stall;
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.
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",
246 wValue = le16_to_cpu(crq->wValue);
247 wIndex = le16_to_cpu(crq->wIndex);
249 switch ((crq->bRequestType << 8) | crq->bRequest) {
251 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
252 ast_vhub_dev_set_address(d, wValue);
253 return std_req_complete;
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);
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);
273 return std_req_driver;
276 static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
278 struct ast_vhub_dev *d = to_ast_dev(gadget);
282 spin_lock_irqsave(&d->vhub->lock, flags);
286 DDBG(d, "Device initiated wakeup\n");
288 /* Wakeup the host */
289 ast_vhub_hub_wake_all(d->vhub);
292 spin_unlock_irqrestore(&d->vhub->lock, flags);
296 static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
298 struct ast_vhub_dev *d = to_ast_dev(gadget);
300 return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
303 static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
307 for (i = 0; i < d->max_epns; i++) {
310 ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
314 static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
316 struct ast_vhub_dev *d = to_ast_dev(gadget);
319 spin_lock_irqsave(&d->vhub->lock, flags);
321 DDBG(d, "pullup(%d)\n", on);
323 /* Mark disconnected in the hub */
324 ast_vhub_device_connect(d->vhub, d->index, on);
327 * If enabled, nuke all requests if any (there shouldn't be)
328 * and disable the port. This will clear the address too.
331 ast_vhub_dev_nuke(d);
332 ast_vhub_dev_disable(d);
335 spin_unlock_irqrestore(&d->vhub->lock, flags);
340 static int ast_vhub_udc_start(struct usb_gadget *gadget,
341 struct usb_gadget_driver *driver)
343 struct ast_vhub_dev *d = to_ast_dev(gadget);
346 spin_lock_irqsave(&d->vhub->lock, flags);
350 /* We don't do much more until the hub enables us */
352 d->gadget.is_selfpowered = 1;
354 spin_unlock_irqrestore(&d->vhub->lock, flags);
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)
363 struct ast_vhub_dev *d = to_ast_dev(gadget);
364 struct ast_vhub_ep *ep;
366 unsigned int max, addr, i;
368 DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
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()
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);
385 * We didn't find one, we need to grab one from the pool.
387 * First let's do some sanity checking
389 switch(usb_endpoint_type(desc)) {
390 case USB_ENDPOINT_XFER_CONTROL:
391 /* Only EP0 can be a control endpoint */
393 case USB_ENDPOINT_XFER_ISOC:
394 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
395 if (gadget_is_dualspeed(gadget))
400 case USB_ENDPOINT_XFER_BULK:
401 if (gadget_is_dualspeed(gadget))
406 case USB_ENDPOINT_XFER_INT:
407 if (gadget_is_dualspeed(gadget))
413 if (usb_endpoint_maxp(desc) > max)
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
424 for (i = 0; i < d->max_epns; i++)
425 if (d->epns[i] == NULL)
427 if (i >= d->max_epns)
432 * Now grab an EP from the shared pool and associate
435 ep = ast_vhub_alloc_epn(d, addr);
438 DDBG(d, "Allocated epn#%d for port EP%d\n",
439 ep->epn.g_idx, addr);
444 static int ast_vhub_udc_stop(struct usb_gadget *gadget)
446 struct ast_vhub_dev *d = to_ast_dev(gadget);
449 spin_lock_irqsave(&d->vhub->lock, flags);
454 d->gadget.speed = USB_SPEED_UNKNOWN;
456 ast_vhub_dev_nuke(d);
459 ast_vhub_dev_disable(d);
461 spin_unlock_irqrestore(&d->vhub->lock, flags);
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,
475 void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
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);
484 void ast_vhub_dev_resume(struct ast_vhub_dev *d)
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);
493 void ast_vhub_dev_reset(struct ast_vhub_dev *d)
495 /* No driver, just disable the device and return */
497 ast_vhub_dev_disable(d);
501 /* If the port isn't enabled, just enable it */
503 DDBG(d, "Reset of disabled device, enabling...\n");
504 ast_vhub_dev_enable(d);
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);
512 * Disable and maybe re-enable HW, this will clear the address
515 ast_vhub_dev_disable(d);
516 ast_vhub_dev_enable(d);
520 void ast_vhub_del_dev(struct ast_vhub_dev *d)
524 spin_lock_irqsave(&d->vhub->lock, flags);
525 if (!d->registered) {
526 spin_unlock_irqrestore(&d->vhub->lock, flags);
529 d->registered = false;
530 spin_unlock_irqrestore(&d->vhub->lock, flags);
532 usb_del_gadget_udc(&d->gadget);
533 device_unregister(d->port_dev);
537 static void ast_vhub_dev_release(struct device *dev)
542 int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
544 struct ast_vhub_dev *d = &vhub->ports[idx].dev;
545 struct device *parent = &vhub->pdev->dev;
550 d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
551 d->regs = vhub->regs + 0x100 + 0x10 * idx;
553 ast_vhub_init_ep0(vhub, &d->ep0, d);
556 * A USB device can have up to 30 endpoints besides control
559 d->max_epns = min_t(u32, vhub->max_epns, 30);
560 d->epns = kcalloc(d->max_epns, sizeof(*d->epns), GFP_KERNEL);
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
569 d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
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);
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;
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;
595 rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
598 d->registered = true;
602 device_del(d->port_dev);
604 put_device(d->port_dev);