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/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>
32 void ast_vhub_dev_irq(struct ast_vhub_dev *d)
34 u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
36 writel(istat, d->regs + AST_VHUB_DEV_ISR);
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);
46 static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
53 /* Cleanup EP0 state */
54 ast_vhub_reset_ep0(d);
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);
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);
69 writel(reg, d->vhub->regs + AST_VHUB_IER);
71 /* Set EP0 DMA buffer address */
72 writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
74 /* Clear stall on all EPs */
75 for (i = 0; i < d->max_epns; i++) {
76 struct ast_vhub_ep *ep = d->epns[i];
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);
85 /* Additional cleanups */
90 static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
97 /* Disable device interrupt in the hub */
98 hmsk = VHUB_IRQ_DEVICE1 << d->index;
99 reg = readl(d->vhub->regs + AST_VHUB_IER);
101 writel(reg, d->vhub->regs + AST_VHUB_IER);
103 /* Then disable device */
104 writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
105 d->gadget.speed = USB_SPEED_UNKNOWN;
109 static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
110 u16 wIndex, u16 wValue,
113 DDBG(d, "%s_FEATURE(dev val=%02x)\n",
114 is_set ? "SET" : "CLEAR", wValue);
116 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
117 return std_req_driver;
119 d->wakeup_en = is_set;
121 return std_req_complete;
124 static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
125 u16 wIndex, u16 wValue, bool is_set)
127 struct ast_vhub_ep *ep;
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);
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;
140 ep = d->epns[ep_num - 1];
142 return std_req_stall;
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;
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);
153 return std_req_complete;
156 static int ast_vhub_dev_status(struct ast_vhub_dev *d,
157 u16 wIndex, u16 wValue)
161 DDBG(d, "GET_STATUS(dev)\n");
163 st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
165 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
167 return ast_vhub_simple_reply(&d->ep0, st0, 0);
170 static int ast_vhub_ep_status(struct ast_vhub_dev *d,
171 u16 wIndex, u16 wValue)
173 int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
174 struct ast_vhub_ep *ep;
177 DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
179 if (ep_num >= d->max_epns)
180 return std_req_stall;
182 ep = d->epns[ep_num - 1];
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;
189 st0 |= 1 << USB_ENDPOINT_HALT;
192 return ast_vhub_simple_reply(&d->ep0, st0, 0);
195 static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
199 DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
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);
207 int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
208 struct usb_ctrlrequest *crq)
210 struct ast_vhub_dev *d = ep->dev;
213 /* No driver, we shouldn't be enabled ... */
214 if (!d->driver || !d->enabled) {
216 "Device is wrong state driver=%p enabled=%d\n",
217 d->driver, d->enabled);
218 return std_req_stall;
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.
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",
236 wValue = le16_to_cpu(crq->wValue);
237 wIndex = le16_to_cpu(crq->wIndex);
239 switch ((crq->bRequestType << 8) | crq->bRequest) {
241 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
242 ast_vhub_dev_set_address(d, wValue);
243 return std_req_complete;
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);
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);
263 return std_req_driver;
266 static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
268 struct ast_vhub_dev *d = to_ast_dev(gadget);
272 spin_lock_irqsave(&d->vhub->lock, flags);
276 DDBG(d, "Device initiated wakeup\n");
278 /* Wakeup the host */
279 ast_vhub_hub_wake_all(d->vhub);
282 spin_unlock_irqrestore(&d->vhub->lock, flags);
286 static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
288 struct ast_vhub_dev *d = to_ast_dev(gadget);
290 return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
293 static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
297 for (i = 0; i < d->max_epns; i++) {
300 ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
304 static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
306 struct ast_vhub_dev *d = to_ast_dev(gadget);
309 spin_lock_irqsave(&d->vhub->lock, flags);
311 DDBG(d, "pullup(%d)\n", on);
313 /* Mark disconnected in the hub */
314 ast_vhub_device_connect(d->vhub, d->index, on);
317 * If enabled, nuke all requests if any (there shouldn't be)
318 * and disable the port. This will clear the address too.
321 ast_vhub_dev_nuke(d);
322 ast_vhub_dev_disable(d);
325 spin_unlock_irqrestore(&d->vhub->lock, flags);
330 static int ast_vhub_udc_start(struct usb_gadget *gadget,
331 struct usb_gadget_driver *driver)
333 struct ast_vhub_dev *d = to_ast_dev(gadget);
336 spin_lock_irqsave(&d->vhub->lock, flags);
340 /* We don't do much more until the hub enables us */
342 d->gadget.is_selfpowered = 1;
344 spin_unlock_irqrestore(&d->vhub->lock, flags);
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)
353 struct ast_vhub_dev *d = to_ast_dev(gadget);
354 struct ast_vhub_ep *ep;
356 unsigned int max, addr, i;
358 DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
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()
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);
375 * We didn't find one, we need to grab one from the pool.
377 * First let's do some sanity checking
379 switch(usb_endpoint_type(desc)) {
380 case USB_ENDPOINT_XFER_CONTROL:
381 /* Only EP0 can be a control endpoint */
383 case USB_ENDPOINT_XFER_ISOC:
384 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
385 if (gadget_is_dualspeed(gadget))
390 case USB_ENDPOINT_XFER_BULK:
391 if (gadget_is_dualspeed(gadget))
396 case USB_ENDPOINT_XFER_INT:
397 if (gadget_is_dualspeed(gadget))
403 if (usb_endpoint_maxp(desc) > max)
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
414 for (i = 0; i < d->max_epns; i++)
415 if (d->epns[i] == NULL)
417 if (i >= d->max_epns)
422 * Now grab an EP from the shared pool and associate
425 ep = ast_vhub_alloc_epn(d, addr);
428 DDBG(d, "Allocated epn#%d for port EP%d\n",
429 ep->epn.g_idx, addr);
434 static int ast_vhub_udc_stop(struct usb_gadget *gadget)
436 struct ast_vhub_dev *d = to_ast_dev(gadget);
439 spin_lock_irqsave(&d->vhub->lock, flags);
444 d->gadget.speed = USB_SPEED_UNKNOWN;
446 ast_vhub_dev_nuke(d);
449 ast_vhub_dev_disable(d);
451 spin_unlock_irqrestore(&d->vhub->lock, flags);
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,
465 void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
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);
474 void ast_vhub_dev_resume(struct ast_vhub_dev *d)
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);
483 void ast_vhub_dev_reset(struct ast_vhub_dev *d)
485 /* No driver, just disable the device and return */
487 ast_vhub_dev_disable(d);
491 /* If the port isn't enabled, just enable it */
493 DDBG(d, "Reset of disabled device, enabling...\n");
494 ast_vhub_dev_enable(d);
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);
502 * Disable and maybe re-enable HW, this will clear the address
505 ast_vhub_dev_disable(d);
506 ast_vhub_dev_enable(d);
510 void ast_vhub_del_dev(struct ast_vhub_dev *d)
514 spin_lock_irqsave(&d->vhub->lock, flags);
515 if (!d->registered) {
516 spin_unlock_irqrestore(&d->vhub->lock, flags);
519 d->registered = false;
520 spin_unlock_irqrestore(&d->vhub->lock, flags);
522 usb_del_gadget_udc(&d->gadget);
523 device_unregister(d->port_dev);
527 static void ast_vhub_dev_release(struct device *dev)
532 int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
534 struct ast_vhub_dev *d = &vhub->ports[idx].dev;
535 struct device *parent = &vhub->pdev->dev;
540 d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
541 d->regs = vhub->regs + 0x100 + 0x10 * idx;
543 ast_vhub_init_ep0(vhub, &d->ep0, d);
546 * A USB device can have up to 30 endpoints besides control
549 d->max_epns = min_t(u32, vhub->max_epns, 30);
550 d->epns = kcalloc(d->max_epns, sizeof(*d->epns), GFP_KERNEL);
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
559 d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
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);
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;
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;
584 rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
587 d->registered = true;
591 device_del(d->port_dev);
593 put_device(d->port_dev);