1 // SPDX-License-Identifier: GPL-2.0+
3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
5 * hub.c - virtual hub handling
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/bcd.h>
33 #include <linux/version.h>
34 #include <linux/usb.h>
35 #include <linux/usb/hcd.h>
39 /* usb 2.0 hub device descriptor
41 * A few things we may want to improve here:
43 * - We may need to indicate TT support
44 * - We may need a device qualifier descriptor
45 * as devices can pretend to be usb1 or 2
46 * - Make vid/did overridable
47 * - make it look like usb1 if usb1 mode forced
49 #define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
50 #define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
53 AST_VHUB_STR_INDEX_MAX
= 4,
54 AST_VHUB_STR_MANUF
= 3,
55 AST_VHUB_STR_PRODUCT
= 2,
56 AST_VHUB_STR_SERIAL
= 1,
59 static const struct usb_device_descriptor ast_vhub_dev_desc
= {
60 .bLength
= USB_DT_DEVICE_SIZE
,
61 .bDescriptorType
= USB_DT_DEVICE
,
62 .bcdUSB
= cpu_to_le16(0x0200),
63 .bDeviceClass
= USB_CLASS_HUB
,
66 .bMaxPacketSize0
= 64,
67 .idVendor
= cpu_to_le16(0x1d6b),
68 .idProduct
= cpu_to_le16(0x0107),
69 .bcdDevice
= cpu_to_le16(0x0100),
70 .iManufacturer
= AST_VHUB_STR_MANUF
,
71 .iProduct
= AST_VHUB_STR_PRODUCT
,
72 .iSerialNumber
= AST_VHUB_STR_SERIAL
,
73 .bNumConfigurations
= 1,
77 * Configuration descriptor: same comments as above
78 * regarding handling USB1 mode.
82 * We don't use sizeof() as Linux definition of
83 * struct usb_endpoint_descriptor contains 2
86 #define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \
87 USB_DT_INTERFACE_SIZE + \
90 static const struct ast_vhub_full_cdesc ast_vhub_conf_desc
= {
92 .bLength
= USB_DT_CONFIG_SIZE
,
93 .bDescriptorType
= USB_DT_CONFIG
,
94 .wTotalLength
= cpu_to_le16(AST_VHUB_CONF_DESC_SIZE
),
96 .bConfigurationValue
= 1,
98 .bmAttributes
= USB_CONFIG_ATT_ONE
|
99 USB_CONFIG_ATT_SELFPOWER
|
100 USB_CONFIG_ATT_WAKEUP
,
104 .bLength
= USB_DT_INTERFACE_SIZE
,
105 .bDescriptorType
= USB_DT_INTERFACE
,
106 .bInterfaceNumber
= 0,
107 .bAlternateSetting
= 0,
109 .bInterfaceClass
= USB_CLASS_HUB
,
110 .bInterfaceSubClass
= 0,
111 .bInterfaceProtocol
= 0,
115 .bLength
= USB_DT_ENDPOINT_SIZE
,
116 .bDescriptorType
= USB_DT_ENDPOINT
,
117 .bEndpointAddress
= 0x81,
118 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
119 .wMaxPacketSize
= cpu_to_le16(1),
124 #define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2)
126 static const struct usb_hub_descriptor ast_vhub_hub_desc
= {
127 .bDescLength
= AST_VHUB_HUB_DESC_SIZE
,
128 .bDescriptorType
= USB_DT_HUB
,
129 .bNbrPorts
= AST_VHUB_NUM_PORTS
,
130 .wHubCharacteristics
= cpu_to_le16(HUB_CHAR_NO_LPSM
),
131 .bPwrOn2PwrGood
= 10,
132 .bHubContrCurrent
= 0,
133 .u
.hs
.DeviceRemovable
[0] = 0,
134 .u
.hs
.DeviceRemovable
[1] = 0xff,
138 * These strings converted to UTF-16 must be smaller than
141 static const struct usb_string ast_vhub_str_array
[] = {
143 .id
= AST_VHUB_STR_SERIAL
,
147 .id
= AST_VHUB_STR_PRODUCT
,
148 .s
= "USB Virtual Hub"
151 .id
= AST_VHUB_STR_MANUF
,
157 static const struct usb_gadget_strings ast_vhub_strings
= {
159 .strings
= (struct usb_string
*)ast_vhub_str_array
162 static int ast_vhub_hub_dev_status(struct ast_vhub_ep
*ep
,
163 u16 wIndex
, u16 wValue
)
167 EPDBG(ep
, "GET_STATUS(dev)\n");
170 * Mark it as self-powered, I doubt the BMC is powered off
173 st0
= 1 << USB_DEVICE_SELF_POWERED
;
176 * Need to double check how remote wakeup actually works
177 * on that chip and what triggers it.
179 if (ep
->vhub
->wakeup_en
)
180 st0
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
182 return ast_vhub_simple_reply(ep
, st0
, 0);
185 static int ast_vhub_hub_ep_status(struct ast_vhub_ep
*ep
,
186 u16 wIndex
, u16 wValue
)
191 ep_num
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
192 EPDBG(ep
, "GET_STATUS(ep%d)\n", ep_num
);
194 /* On the hub we have only EP 0 and 1 */
196 if (ep
->vhub
->ep1_stalled
)
197 st0
|= 1 << USB_ENDPOINT_HALT
;
198 } else if (ep_num
!= 0)
199 return std_req_stall
;
201 return ast_vhub_simple_reply(ep
, st0
, 0);
204 static int ast_vhub_hub_dev_feature(struct ast_vhub_ep
*ep
,
205 u16 wIndex
, u16 wValue
,
208 EPDBG(ep
, "%s_FEATURE(dev val=%02x)\n",
209 is_set
? "SET" : "CLEAR", wValue
);
211 if (wValue
!= USB_DEVICE_REMOTE_WAKEUP
)
212 return std_req_stall
;
214 ep
->vhub
->wakeup_en
= is_set
;
215 EPDBG(ep
, "Hub remote wakeup %s\n",
216 is_set
? "enabled" : "disabled");
218 return std_req_complete
;
221 static int ast_vhub_hub_ep_feature(struct ast_vhub_ep
*ep
,
222 u16 wIndex
, u16 wValue
,
228 ep_num
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
229 EPDBG(ep
, "%s_FEATURE(ep%d val=%02x)\n",
230 is_set
? "SET" : "CLEAR", ep_num
, wValue
);
233 return std_req_stall
;
234 if (wValue
!= USB_ENDPOINT_HALT
)
235 return std_req_stall
;
237 return std_req_complete
;
239 EPDBG(ep
, "%s stall on EP 1\n",
240 is_set
? "setting" : "clearing");
242 ep
->vhub
->ep1_stalled
= is_set
;
243 reg
= readl(ep
->vhub
->regs
+ AST_VHUB_EP1_CTRL
);
245 reg
|= VHUB_EP1_CTRL_STALL
;
247 reg
&= ~VHUB_EP1_CTRL_STALL
;
248 reg
|= VHUB_EP1_CTRL_RESET_TOGGLE
;
250 writel(reg
, ep
->vhub
->regs
+ AST_VHUB_EP1_CTRL
);
252 return std_req_complete
;
255 static int ast_vhub_rep_desc(struct ast_vhub_ep
*ep
,
256 u8 desc_type
, u16 len
)
259 struct ast_vhub
*vhub
= ep
->vhub
;
261 EPDBG(ep
, "GET_DESCRIPTOR(type:%d)\n", desc_type
);
264 * Copy first to EP buffer and send from there, so
265 * we can do some in-place patching if needed. We know
266 * the EP buffer is big enough but ensure that doesn't
267 * change. We do that now rather than later after we
268 * have checked sizes etc... to avoid a gcc bug where
269 * it thinks len is constant and barfs about read
270 * overflows in memcpy.
274 dsize
= USB_DT_DEVICE_SIZE
;
275 memcpy(ep
->buf
, &vhub
->vhub_dev_desc
, dsize
);
276 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_dev_desc
));
277 BUILD_BUG_ON(USB_DT_DEVICE_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
280 dsize
= AST_VHUB_CONF_DESC_SIZE
;
281 memcpy(ep
->buf
, &vhub
->vhub_conf_desc
, dsize
);
282 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_conf_desc
));
283 BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
286 dsize
= AST_VHUB_HUB_DESC_SIZE
;
287 memcpy(ep
->buf
, &vhub
->vhub_hub_desc
, dsize
);
288 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_hub_desc
));
289 BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
292 return std_req_stall
;
295 /* Crop requested length */
299 /* Shoot it from the EP buffer */
300 return ast_vhub_reply(ep
, NULL
, len
);
303 static struct usb_gadget_strings
*
304 ast_vhub_str_of_container(struct usb_gadget_string_container
*container
)
306 return (struct usb_gadget_strings
*)container
->stash
;
309 static int ast_vhub_collect_languages(struct ast_vhub
*vhub
, void *buf
,
312 int rc
, hdr_len
, nlangs
, max_langs
;
313 struct usb_gadget_strings
*lang_str
;
314 struct usb_gadget_string_container
*container
;
315 struct usb_string_descriptor
*sdesc
= buf
;
318 hdr_len
= sizeof(struct usb_descriptor_header
);
319 max_langs
= (size
- hdr_len
) / sizeof(sdesc
->wData
[0]);
320 list_for_each_entry(container
, &vhub
->vhub_str_desc
, list
) {
321 if (nlangs
>= max_langs
)
324 lang_str
= ast_vhub_str_of_container(container
);
325 sdesc
->wData
[nlangs
++] = cpu_to_le16(lang_str
->language
);
328 rc
= hdr_len
+ nlangs
* sizeof(sdesc
->wData
[0]);
330 sdesc
->bDescriptorType
= USB_DT_STRING
;
335 static struct usb_gadget_strings
*ast_vhub_lookup_string(struct ast_vhub
*vhub
,
338 struct usb_gadget_strings
*lang_str
;
339 struct usb_gadget_string_container
*container
;
341 list_for_each_entry(container
, &vhub
->vhub_str_desc
, list
) {
342 lang_str
= ast_vhub_str_of_container(container
);
343 if (lang_str
->language
== lang_id
)
350 static int ast_vhub_rep_string(struct ast_vhub_ep
*ep
,
351 u8 string_id
, u16 lang_id
,
356 struct ast_vhub
*vhub
= ep
->vhub
;
357 struct usb_gadget_strings
*lang_str
;
359 if (string_id
== 0) {
360 rc
= ast_vhub_collect_languages(vhub
, buf
, sizeof(buf
));
362 lang_str
= ast_vhub_lookup_string(vhub
, lang_id
);
364 return std_req_stall
;
366 rc
= usb_gadget_get_string(lang_str
, string_id
, buf
);
369 if (rc
< 0 || rc
>= AST_VHUB_EP0_MAX_PACKET
)
370 return std_req_stall
;
372 /* Shoot it from the EP buffer */
373 memcpy(ep
->buf
, buf
, rc
);
374 return ast_vhub_reply(ep
, NULL
, min_t(u16
, rc
, len
));
377 enum std_req_rc
ast_vhub_std_hub_request(struct ast_vhub_ep
*ep
,
378 struct usb_ctrlrequest
*crq
)
380 struct ast_vhub
*vhub
= ep
->vhub
;
381 u16 wValue
, wIndex
, wLength
;
383 wValue
= le16_to_cpu(crq
->wValue
);
384 wIndex
= le16_to_cpu(crq
->wIndex
);
385 wLength
= le16_to_cpu(crq
->wLength
);
387 /* First packet, grab speed */
388 if (vhub
->speed
== USB_SPEED_UNKNOWN
) {
389 u32 ustat
= readl(vhub
->regs
+ AST_VHUB_USBSTS
);
390 if (ustat
& VHUB_USBSTS_HISPEED
)
391 vhub
->speed
= USB_SPEED_HIGH
;
393 vhub
->speed
= USB_SPEED_FULL
;
394 UDCDBG(vhub
, "USB status=%08x speed=%s\n", ustat
,
395 vhub
->speed
== USB_SPEED_HIGH
? "high" : "full");
398 switch ((crq
->bRequestType
<< 8) | crq
->bRequest
) {
400 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
401 EPDBG(ep
, "SET_ADDRESS: Got address %x\n", wValue
);
402 writel(wValue
, vhub
->regs
+ AST_VHUB_CONF
);
403 return std_req_complete
;
406 case DeviceRequest
| USB_REQ_GET_STATUS
:
407 return ast_vhub_hub_dev_status(ep
, wIndex
, wValue
);
408 case InterfaceRequest
| USB_REQ_GET_STATUS
:
409 return ast_vhub_simple_reply(ep
, 0, 0);
410 case EndpointRequest
| USB_REQ_GET_STATUS
:
411 return ast_vhub_hub_ep_status(ep
, wIndex
, wValue
);
413 /* SET/CLEAR_FEATURE */
414 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
415 return ast_vhub_hub_dev_feature(ep
, wIndex
, wValue
, true);
416 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
417 return ast_vhub_hub_dev_feature(ep
, wIndex
, wValue
, false);
418 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
419 return ast_vhub_hub_ep_feature(ep
, wIndex
, wValue
, true);
420 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
421 return ast_vhub_hub_ep_feature(ep
, wIndex
, wValue
, false);
423 /* GET/SET_CONFIGURATION */
424 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
425 return ast_vhub_simple_reply(ep
, 1);
426 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
428 return std_req_stall
;
429 return std_req_complete
;
432 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
433 switch (wValue
>> 8) {
436 return ast_vhub_rep_desc(ep
, wValue
>> 8,
439 return ast_vhub_rep_string(ep
, wValue
& 0xff,
442 return std_req_stall
;
444 /* GET/SET_INTERFACE */
445 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
446 return ast_vhub_simple_reply(ep
, 0);
447 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
448 if (wValue
!= 0 || wIndex
!= 0)
449 return std_req_stall
;
450 return std_req_complete
;
452 return std_req_stall
;
455 static void ast_vhub_update_hub_ep1(struct ast_vhub
*vhub
,
458 /* Update HW EP1 response */
459 u32 reg
= readl(vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
460 u32 pmask
= (1 << (port
+ 1));
461 if (vhub
->ports
[port
].change
)
465 writel(reg
, vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
468 static void ast_vhub_change_port_stat(struct ast_vhub
*vhub
,
474 struct ast_vhub_port
*p
= &vhub
->ports
[port
];
477 /* Update port status */
479 p
->status
= (prev
& ~clr_flags
) | set_flags
;
480 DDBG(&p
->dev
, "port %d status %04x -> %04x (C=%d)\n",
481 port
+ 1, prev
, p
->status
, set_c
);
483 /* Update change bits if needed */
485 u16 chg
= p
->status
^ prev
;
487 /* Only these are relevant for change */
488 chg
&= USB_PORT_STAT_C_CONNECTION
|
489 USB_PORT_STAT_C_ENABLE
|
490 USB_PORT_STAT_C_SUSPEND
|
491 USB_PORT_STAT_C_OVERCURRENT
|
492 USB_PORT_STAT_C_RESET
|
496 * We only set USB_PORT_STAT_C_ENABLE if we are disabling
497 * the port as per USB spec, otherwise MacOS gets upset
499 if (p
->status
& USB_PORT_STAT_ENABLE
)
500 chg
&= ~USB_PORT_STAT_C_ENABLE
;
503 ast_vhub_update_hub_ep1(vhub
, port
);
507 static void ast_vhub_send_host_wakeup(struct ast_vhub
*vhub
)
509 u32 reg
= readl(vhub
->regs
+ AST_VHUB_CTRL
);
510 UDCDBG(vhub
, "Waking up host !\n");
511 reg
|= VHUB_CTRL_MANUAL_REMOTE_WAKEUP
;
512 writel(reg
, vhub
->regs
+ AST_VHUB_CTRL
);
515 void ast_vhub_device_connect(struct ast_vhub
*vhub
,
516 unsigned int port
, bool on
)
519 ast_vhub_change_port_stat(vhub
, port
, 0,
520 USB_PORT_STAT_CONNECTION
, true);
522 ast_vhub_change_port_stat(vhub
, port
,
523 USB_PORT_STAT_CONNECTION
|
524 USB_PORT_STAT_ENABLE
,
528 * If the hub is set to wakup the host on connection events
529 * then send a wakeup.
532 ast_vhub_send_host_wakeup(vhub
);
535 static void ast_vhub_wake_work(struct work_struct
*work
)
537 struct ast_vhub
*vhub
= container_of(work
,
544 * Wake all sleeping ports. If a port is suspended by
545 * the host suspend (without explicit state suspend),
546 * we let the normal host wake path deal with it later.
548 spin_lock_irqsave(&vhub
->lock
, flags
);
549 for (i
= 0; i
< vhub
->max_ports
; i
++) {
550 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
552 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
554 ast_vhub_change_port_stat(vhub
, i
,
555 USB_PORT_STAT_SUSPEND
,
557 ast_vhub_dev_resume(&p
->dev
);
559 ast_vhub_send_host_wakeup(vhub
);
560 spin_unlock_irqrestore(&vhub
->lock
, flags
);
563 void ast_vhub_hub_wake_all(struct ast_vhub
*vhub
)
566 * A device is trying to wake the world, because this
567 * can recurse into the device, we break the call chain
570 schedule_work(&vhub
->wake_work
);
573 static void ast_vhub_port_reset(struct ast_vhub
*vhub
, u8 port
)
575 struct ast_vhub_port
*p
= &vhub
->ports
[port
];
578 /* First mark disabled */
579 ast_vhub_change_port_stat(vhub
, port
,
580 USB_PORT_STAT_ENABLE
|
581 USB_PORT_STAT_SUSPEND
,
589 * This will either "start" the port or reset the
590 * device if already started...
592 ast_vhub_dev_reset(&p
->dev
);
594 /* Grab the right speed */
595 speed
= p
->dev
.driver
->max_speed
;
596 if (speed
== USB_SPEED_UNKNOWN
|| speed
> vhub
->speed
)
601 set
= USB_PORT_STAT_LOW_SPEED
;
602 clr
= USB_PORT_STAT_HIGH_SPEED
;
606 clr
= USB_PORT_STAT_LOW_SPEED
|
607 USB_PORT_STAT_HIGH_SPEED
;
610 set
= USB_PORT_STAT_HIGH_SPEED
;
611 clr
= USB_PORT_STAT_LOW_SPEED
;
614 UDCDBG(vhub
, "Unsupported speed %d when"
615 " connecting device\n",
619 clr
|= USB_PORT_STAT_RESET
;
620 set
|= USB_PORT_STAT_ENABLE
;
622 /* This should ideally be delayed ... */
623 ast_vhub_change_port_stat(vhub
, port
, clr
, set
, true);
626 static enum std_req_rc
ast_vhub_set_port_feature(struct ast_vhub_ep
*ep
,
629 struct ast_vhub
*vhub
= ep
->vhub
;
630 struct ast_vhub_port
*p
;
632 if (port
== 0 || port
> vhub
->max_ports
)
633 return std_req_stall
;
635 p
= &vhub
->ports
[port
];
638 case USB_PORT_FEAT_SUSPEND
:
639 if (!(p
->status
& USB_PORT_STAT_ENABLE
))
640 return std_req_complete
;
641 ast_vhub_change_port_stat(vhub
, port
,
642 0, USB_PORT_STAT_SUSPEND
,
644 ast_vhub_dev_suspend(&p
->dev
);
645 return std_req_complete
;
646 case USB_PORT_FEAT_RESET
:
647 EPDBG(ep
, "Port reset !\n");
648 ast_vhub_port_reset(vhub
, port
);
649 return std_req_complete
;
650 case USB_PORT_FEAT_POWER
:
652 * On Power-on, we mark the connected flag changed,
653 * if there's a connected device, some hosts will
654 * otherwise fail to detect it.
656 if (p
->status
& USB_PORT_STAT_CONNECTION
) {
657 p
->change
|= USB_PORT_STAT_C_CONNECTION
;
658 ast_vhub_update_hub_ep1(vhub
, port
);
660 return std_req_complete
;
661 case USB_PORT_FEAT_TEST
:
662 case USB_PORT_FEAT_INDICATOR
:
663 /* We don't do anything with these */
664 return std_req_complete
;
666 return std_req_stall
;
669 static enum std_req_rc
ast_vhub_clr_port_feature(struct ast_vhub_ep
*ep
,
672 struct ast_vhub
*vhub
= ep
->vhub
;
673 struct ast_vhub_port
*p
;
675 if (port
== 0 || port
> vhub
->max_ports
)
676 return std_req_stall
;
678 p
= &vhub
->ports
[port
];
681 case USB_PORT_FEAT_ENABLE
:
682 ast_vhub_change_port_stat(vhub
, port
,
683 USB_PORT_STAT_ENABLE
|
684 USB_PORT_STAT_SUSPEND
, 0,
686 ast_vhub_dev_suspend(&p
->dev
);
687 return std_req_complete
;
688 case USB_PORT_FEAT_SUSPEND
:
689 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
690 return std_req_complete
;
691 ast_vhub_change_port_stat(vhub
, port
,
692 USB_PORT_STAT_SUSPEND
, 0,
694 ast_vhub_dev_resume(&p
->dev
);
695 return std_req_complete
;
696 case USB_PORT_FEAT_POWER
:
697 /* We don't do power control */
698 return std_req_complete
;
699 case USB_PORT_FEAT_INDICATOR
:
700 /* We don't have indicators */
701 return std_req_complete
;
702 case USB_PORT_FEAT_C_CONNECTION
:
703 case USB_PORT_FEAT_C_ENABLE
:
704 case USB_PORT_FEAT_C_SUSPEND
:
705 case USB_PORT_FEAT_C_OVER_CURRENT
:
706 case USB_PORT_FEAT_C_RESET
:
707 /* Clear state-change feature */
708 p
->change
&= ~(1u << (feat
- 16));
709 ast_vhub_update_hub_ep1(vhub
, port
);
710 return std_req_complete
;
712 return std_req_stall
;
715 static enum std_req_rc
ast_vhub_get_port_stat(struct ast_vhub_ep
*ep
,
718 struct ast_vhub
*vhub
= ep
->vhub
;
721 if (port
== 0 || port
> vhub
->max_ports
)
722 return std_req_stall
;
725 stat
= vhub
->ports
[port
].status
;
726 chg
= vhub
->ports
[port
].change
;
728 /* We always have power */
729 stat
|= USB_PORT_STAT_POWER
;
731 EPDBG(ep
, " port status=%04x change=%04x\n", stat
, chg
);
733 return ast_vhub_simple_reply(ep
,
740 enum std_req_rc
ast_vhub_class_hub_request(struct ast_vhub_ep
*ep
,
741 struct usb_ctrlrequest
*crq
)
743 u16 wValue
, wIndex
, wLength
;
745 wValue
= le16_to_cpu(crq
->wValue
);
746 wIndex
= le16_to_cpu(crq
->wIndex
);
747 wLength
= le16_to_cpu(crq
->wLength
);
749 switch ((crq
->bRequestType
<< 8) | crq
->bRequest
) {
751 EPDBG(ep
, "GetHubStatus\n");
752 return ast_vhub_simple_reply(ep
, 0, 0, 0, 0);
754 EPDBG(ep
, "GetPortStatus(%d)\n", wIndex
& 0xff);
755 return ast_vhub_get_port_stat(ep
, wIndex
& 0xf);
756 case GetHubDescriptor
:
757 if (wValue
!= (USB_DT_HUB
<< 8))
758 return std_req_stall
;
759 EPDBG(ep
, "GetHubDescriptor(%d)\n", wIndex
& 0xff);
760 return ast_vhub_rep_desc(ep
, USB_DT_HUB
, wLength
);
762 case ClearHubFeature
:
763 EPDBG(ep
, "Get/SetHubFeature(%d)\n", wValue
);
764 /* No feature, just complete the requests */
765 if (wValue
== C_HUB_LOCAL_POWER
||
766 wValue
== C_HUB_OVER_CURRENT
)
767 return std_req_complete
;
768 return std_req_stall
;
770 EPDBG(ep
, "SetPortFeature(%d,%d)\n", wIndex
& 0xf, wValue
);
771 return ast_vhub_set_port_feature(ep
, wIndex
& 0xf, wValue
);
772 case ClearPortFeature
:
773 EPDBG(ep
, "ClearPortFeature(%d,%d)\n", wIndex
& 0xf, wValue
);
774 return ast_vhub_clr_port_feature(ep
, wIndex
& 0xf, wValue
);
778 return std_req_complete
;
780 return ast_vhub_simple_reply(ep
, 0, 0, 0, 0);
782 EPDBG(ep
, "Unknown class request\n");
784 return std_req_stall
;
787 void ast_vhub_hub_suspend(struct ast_vhub
*vhub
)
791 UDCDBG(vhub
, "USB bus suspend\n");
796 vhub
->suspended
= true;
799 * Forward to unsuspended ports without changing
800 * their connection status.
802 for (i
= 0; i
< vhub
->max_ports
; i
++) {
803 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
805 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
806 ast_vhub_dev_suspend(&p
->dev
);
810 void ast_vhub_hub_resume(struct ast_vhub
*vhub
)
814 UDCDBG(vhub
, "USB bus resume\n");
816 if (!vhub
->suspended
)
819 vhub
->suspended
= false;
822 * Forward to unsuspended ports without changing
823 * their connection status.
825 for (i
= 0; i
< vhub
->max_ports
; i
++) {
826 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
828 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
829 ast_vhub_dev_resume(&p
->dev
);
833 void ast_vhub_hub_reset(struct ast_vhub
*vhub
)
837 UDCDBG(vhub
, "USB bus reset\n");
840 * Is the speed known ? If not we don't care, we aren't
841 * initialized yet and ports haven't been enabled.
843 if (vhub
->speed
== USB_SPEED_UNKNOWN
)
846 /* We aren't suspended anymore obviously */
847 vhub
->suspended
= false;
850 vhub
->speed
= USB_SPEED_UNKNOWN
;
852 /* Wakeup not enabled anymore */
853 vhub
->wakeup_en
= false;
856 * Clear all port status, disable gadgets and "suspend"
857 * them. They will be woken up by a port reset.
859 for (i
= 0; i
< vhub
->max_ports
; i
++) {
860 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
862 /* Only keep the connected flag */
863 p
->status
&= USB_PORT_STAT_CONNECTION
;
866 /* Suspend the gadget if any */
867 ast_vhub_dev_suspend(&p
->dev
);
871 writel(0, vhub
->regs
+ AST_VHUB_CONF
);
872 writel(0, vhub
->regs
+ AST_VHUB_EP0_CTRL
);
873 writel(VHUB_EP1_CTRL_RESET_TOGGLE
|
874 VHUB_EP1_CTRL_ENABLE
,
875 vhub
->regs
+ AST_VHUB_EP1_CTRL
);
876 writel(0, vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
879 static void ast_vhub_of_parse_dev_desc(struct ast_vhub
*vhub
,
880 const struct device_node
*vhub_np
)
885 if (!of_property_read_u32(vhub_np
, "vhub-vendor-id", &data
)) {
887 vhub
->vhub_dev_desc
.idVendor
= cpu_to_le16(id
);
889 if (!of_property_read_u32(vhub_np
, "vhub-product-id", &data
)) {
891 vhub
->vhub_dev_desc
.idProduct
= cpu_to_le16(id
);
893 if (!of_property_read_u32(vhub_np
, "vhub-device-revision", &data
)) {
895 vhub
->vhub_dev_desc
.bcdDevice
= cpu_to_le16(id
);
899 static void ast_vhub_fixup_usb1_dev_desc(struct ast_vhub
*vhub
)
901 vhub
->vhub_dev_desc
.bcdUSB
= cpu_to_le16(0x0100);
902 vhub
->vhub_dev_desc
.bDeviceProtocol
= 0;
905 static struct usb_gadget_string_container
*
906 ast_vhub_str_container_alloc(struct ast_vhub
*vhub
)
909 struct usb_string
*str_array
;
910 struct usb_gadget_strings
*lang_str
;
911 struct usb_gadget_string_container
*container
;
913 size
= sizeof(*container
);
914 size
+= sizeof(struct usb_gadget_strings
);
915 size
+= sizeof(struct usb_string
) * AST_VHUB_STR_INDEX_MAX
;
916 container
= devm_kzalloc(&vhub
->pdev
->dev
, size
, GFP_KERNEL
);
918 return ERR_PTR(-ENOMEM
);
920 lang_str
= ast_vhub_str_of_container(container
);
921 str_array
= (struct usb_string
*)(lang_str
+ 1);
922 lang_str
->strings
= str_array
;
926 static void ast_vhub_str_deep_copy(struct usb_gadget_strings
*dest
,
927 const struct usb_gadget_strings
*src
)
929 struct usb_string
*src_array
= src
->strings
;
930 struct usb_string
*dest_array
= dest
->strings
;
932 dest
->language
= src
->language
;
933 if (src_array
&& dest_array
) {
935 *dest_array
= *src_array
;
938 } while (src_array
->s
);
942 static int ast_vhub_str_alloc_add(struct ast_vhub
*vhub
,
943 const struct usb_gadget_strings
*src_str
)
945 struct usb_gadget_strings
*dest_str
;
946 struct usb_gadget_string_container
*container
;
948 container
= ast_vhub_str_container_alloc(vhub
);
949 if (IS_ERR(container
))
950 return PTR_ERR(container
);
952 dest_str
= ast_vhub_str_of_container(container
);
953 ast_vhub_str_deep_copy(dest_str
, src_str
);
954 list_add_tail(&container
->list
, &vhub
->vhub_str_desc
);
959 static const struct {
963 {"manufacturer", AST_VHUB_STR_MANUF
},
964 {"product", AST_VHUB_STR_PRODUCT
},
965 {"serial-number", AST_VHUB_STR_SERIAL
},
969 static int ast_vhub_of_parse_str_desc(struct ast_vhub
*vhub
,
970 const struct device_node
*desc_np
)
976 struct device_node
*child
;
977 struct usb_string str_array
[AST_VHUB_STR_INDEX_MAX
];
978 struct usb_gadget_strings lang_str
= {
979 .strings
= (struct usb_string
*)str_array
,
982 for_each_child_of_node(desc_np
, child
) {
983 if (of_property_read_u32(child
, "reg", &langid
))
984 continue; /* no language identifier specified */
986 if (!usb_validate_langid(langid
))
987 continue; /* invalid language identifier */
989 lang_str
.language
= langid
;
990 for (i
= offset
= 0; str_id_map
[i
].name
; i
++) {
991 str
= of_get_property(child
, str_id_map
[i
].name
, NULL
);
993 str_array
[offset
].s
= str
;
994 str_array
[offset
].id
= str_id_map
[i
].id
;
998 str_array
[offset
].id
= 0;
999 str_array
[offset
].s
= NULL
;
1001 ret
= ast_vhub_str_alloc_add(vhub
, &lang_str
);
1009 static int ast_vhub_init_desc(struct ast_vhub
*vhub
)
1012 struct device_node
*desc_np
;
1013 const struct device_node
*vhub_np
= vhub
->pdev
->dev
.of_node
;
1015 /* Initialize vhub Device Descriptor. */
1016 memcpy(&vhub
->vhub_dev_desc
, &ast_vhub_dev_desc
,
1017 sizeof(vhub
->vhub_dev_desc
));
1018 ast_vhub_of_parse_dev_desc(vhub
, vhub_np
);
1019 if (vhub
->force_usb1
)
1020 ast_vhub_fixup_usb1_dev_desc(vhub
);
1022 /* Initialize vhub Configuration Descriptor. */
1023 memcpy(&vhub
->vhub_conf_desc
, &ast_vhub_conf_desc
,
1024 sizeof(vhub
->vhub_conf_desc
));
1026 /* Initialize vhub Hub Descriptor. */
1027 memcpy(&vhub
->vhub_hub_desc
, &ast_vhub_hub_desc
,
1028 sizeof(vhub
->vhub_hub_desc
));
1029 vhub
->vhub_hub_desc
.bNbrPorts
= vhub
->max_ports
;
1031 /* Initialize vhub String Descriptors. */
1032 INIT_LIST_HEAD(&vhub
->vhub_str_desc
);
1033 desc_np
= of_get_child_by_name(vhub_np
, "vhub-strings");
1035 ret
= ast_vhub_of_parse_str_desc(vhub
, desc_np
);
1037 ret
= ast_vhub_str_alloc_add(vhub
, &ast_vhub_strings
);
1042 int ast_vhub_init_hub(struct ast_vhub
*vhub
)
1044 vhub
->speed
= USB_SPEED_UNKNOWN
;
1045 INIT_WORK(&vhub
->wake_work
, ast_vhub_wake_work
);
1047 return ast_vhub_init_desc(vhub
);