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_MANUF
= 3,
54 AST_VHUB_STR_PRODUCT
= 2,
55 AST_VHUB_STR_SERIAL
= 1,
58 static const struct usb_device_descriptor ast_vhub_dev_desc
= {
59 .bLength
= USB_DT_DEVICE_SIZE
,
60 .bDescriptorType
= USB_DT_DEVICE
,
61 .bcdUSB
= cpu_to_le16(0x0200),
62 .bDeviceClass
= USB_CLASS_HUB
,
65 .bMaxPacketSize0
= 64,
66 .idVendor
= cpu_to_le16(0x1d6b),
67 .idProduct
= cpu_to_le16(0x0107),
68 .bcdDevice
= cpu_to_le16(0x0100),
69 .iManufacturer
= AST_VHUB_STR_MANUF
,
70 .iProduct
= AST_VHUB_STR_PRODUCT
,
71 .iSerialNumber
= AST_VHUB_STR_SERIAL
,
72 .bNumConfigurations
= 1,
75 /* Patches to the above when forcing USB1 mode */
76 static void ast_vhub_patch_dev_desc_usb1(struct usb_device_descriptor
*desc
)
78 desc
->bcdUSB
= cpu_to_le16(0x0100);
79 desc
->bDeviceProtocol
= 0;
83 * Configuration descriptor: same comments as above
84 * regarding handling USB1 mode.
88 * We don't use sizeof() as Linux definition of
89 * struct usb_endpoint_descriptor contains 2
92 #define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \
93 USB_DT_INTERFACE_SIZE + \
96 static const struct ast_vhub_full_cdesc ast_vhub_conf_desc
= {
98 .bLength
= USB_DT_CONFIG_SIZE
,
99 .bDescriptorType
= USB_DT_CONFIG
,
100 .wTotalLength
= cpu_to_le16(AST_VHUB_CONF_DESC_SIZE
),
102 .bConfigurationValue
= 1,
104 .bmAttributes
= USB_CONFIG_ATT_ONE
|
105 USB_CONFIG_ATT_SELFPOWER
|
106 USB_CONFIG_ATT_WAKEUP
,
110 .bLength
= USB_DT_INTERFACE_SIZE
,
111 .bDescriptorType
= USB_DT_INTERFACE
,
112 .bInterfaceNumber
= 0,
113 .bAlternateSetting
= 0,
115 .bInterfaceClass
= USB_CLASS_HUB
,
116 .bInterfaceSubClass
= 0,
117 .bInterfaceProtocol
= 0,
121 .bLength
= USB_DT_ENDPOINT_SIZE
,
122 .bDescriptorType
= USB_DT_ENDPOINT
,
123 .bEndpointAddress
= 0x81,
124 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
125 .wMaxPacketSize
= cpu_to_le16(1),
130 #define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2)
132 static const struct usb_hub_descriptor ast_vhub_hub_desc
= {
133 .bDescLength
= AST_VHUB_HUB_DESC_SIZE
,
134 .bDescriptorType
= USB_DT_HUB
,
135 .bNbrPorts
= AST_VHUB_NUM_PORTS
,
136 .wHubCharacteristics
= cpu_to_le16(HUB_CHAR_NO_LPSM
),
137 .bPwrOn2PwrGood
= 10,
138 .bHubContrCurrent
= 0,
139 .u
.hs
.DeviceRemovable
[0] = 0,
140 .u
.hs
.DeviceRemovable
[1] = 0xff,
144 * These strings converted to UTF-16 must be smaller than
147 static const struct usb_string ast_vhub_str_array
[] = {
149 .id
= AST_VHUB_STR_SERIAL
,
153 .id
= AST_VHUB_STR_PRODUCT
,
154 .s
= "USB Virtual Hub"
157 .id
= AST_VHUB_STR_MANUF
,
163 static const struct usb_gadget_strings ast_vhub_strings
= {
165 .strings
= (struct usb_string
*)ast_vhub_str_array
168 static int ast_vhub_hub_dev_status(struct ast_vhub_ep
*ep
,
169 u16 wIndex
, u16 wValue
)
173 EPDBG(ep
, "GET_STATUS(dev)\n");
176 * Mark it as self-powered, I doubt the BMC is powered off
179 st0
= 1 << USB_DEVICE_SELF_POWERED
;
182 * Need to double check how remote wakeup actually works
183 * on that chip and what triggers it.
185 if (ep
->vhub
->wakeup_en
)
186 st0
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
188 return ast_vhub_simple_reply(ep
, st0
, 0);
191 static int ast_vhub_hub_ep_status(struct ast_vhub_ep
*ep
,
192 u16 wIndex
, u16 wValue
)
197 ep_num
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
198 EPDBG(ep
, "GET_STATUS(ep%d)\n", ep_num
);
200 /* On the hub we have only EP 0 and 1 */
202 if (ep
->vhub
->ep1_stalled
)
203 st0
|= 1 << USB_ENDPOINT_HALT
;
204 } else if (ep_num
!= 0)
205 return std_req_stall
;
207 return ast_vhub_simple_reply(ep
, st0
, 0);
210 static int ast_vhub_hub_dev_feature(struct ast_vhub_ep
*ep
,
211 u16 wIndex
, u16 wValue
,
214 EPDBG(ep
, "%s_FEATURE(dev val=%02x)\n",
215 is_set
? "SET" : "CLEAR", wValue
);
217 if (wValue
!= USB_DEVICE_REMOTE_WAKEUP
)
218 return std_req_stall
;
220 ep
->vhub
->wakeup_en
= is_set
;
221 EPDBG(ep
, "Hub remote wakeup %s\n",
222 is_set
? "enabled" : "disabled");
224 return std_req_complete
;
227 static int ast_vhub_hub_ep_feature(struct ast_vhub_ep
*ep
,
228 u16 wIndex
, u16 wValue
,
234 ep_num
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
235 EPDBG(ep
, "%s_FEATURE(ep%d val=%02x)\n",
236 is_set
? "SET" : "CLEAR", ep_num
, wValue
);
239 return std_req_stall
;
240 if (wValue
!= USB_ENDPOINT_HALT
)
241 return std_req_stall
;
243 return std_req_complete
;
245 EPDBG(ep
, "%s stall on EP 1\n",
246 is_set
? "setting" : "clearing");
248 ep
->vhub
->ep1_stalled
= is_set
;
249 reg
= readl(ep
->vhub
->regs
+ AST_VHUB_EP1_CTRL
);
251 reg
|= VHUB_EP1_CTRL_STALL
;
253 reg
&= ~VHUB_EP1_CTRL_STALL
;
254 reg
|= VHUB_EP1_CTRL_RESET_TOGGLE
;
256 writel(reg
, ep
->vhub
->regs
+ AST_VHUB_EP1_CTRL
);
258 return std_req_complete
;
261 static int ast_vhub_rep_desc(struct ast_vhub_ep
*ep
,
262 u8 desc_type
, u16 len
)
265 struct ast_vhub
*vhub
= ep
->vhub
;
267 EPDBG(ep
, "GET_DESCRIPTOR(type:%d)\n", desc_type
);
270 * Copy first to EP buffer and send from there, so
271 * we can do some in-place patching if needed. We know
272 * the EP buffer is big enough but ensure that doesn't
273 * change. We do that now rather than later after we
274 * have checked sizes etc... to avoid a gcc bug where
275 * it thinks len is constant and barfs about read
276 * overflows in memcpy.
280 dsize
= USB_DT_DEVICE_SIZE
;
281 memcpy(ep
->buf
, &vhub
->vhub_dev_desc
, dsize
);
282 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_dev_desc
));
283 BUILD_BUG_ON(USB_DT_DEVICE_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
286 dsize
= AST_VHUB_CONF_DESC_SIZE
;
287 memcpy(ep
->buf
, &vhub
->vhub_conf_desc
, dsize
);
288 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_conf_desc
));
289 BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
292 dsize
= AST_VHUB_HUB_DESC_SIZE
;
293 memcpy(ep
->buf
, &vhub
->vhub_hub_desc
, dsize
);
294 BUILD_BUG_ON(dsize
> sizeof(vhub
->vhub_hub_desc
));
295 BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE
>= AST_VHUB_EP0_MAX_PACKET
);
298 return std_req_stall
;
301 /* Crop requested length */
305 /* Patch it if forcing USB1 */
306 if (desc_type
== USB_DT_DEVICE
&& ep
->vhub
->force_usb1
)
307 ast_vhub_patch_dev_desc_usb1(ep
->buf
);
309 /* Shoot it from the EP buffer */
310 return ast_vhub_reply(ep
, NULL
, len
);
313 static int ast_vhub_rep_string(struct ast_vhub_ep
*ep
,
314 u8 string_id
, u16 lang_id
,
317 int rc
= usb_gadget_get_string(&ep
->vhub
->vhub_str_desc
,
321 * This should never happen unless we put too big strings in
324 BUG_ON(rc
>= AST_VHUB_EP0_MAX_PACKET
);
327 return std_req_stall
;
329 /* Shoot it from the EP buffer */
330 return ast_vhub_reply(ep
, NULL
, min_t(u16
, rc
, len
));
333 enum std_req_rc
ast_vhub_std_hub_request(struct ast_vhub_ep
*ep
,
334 struct usb_ctrlrequest
*crq
)
336 struct ast_vhub
*vhub
= ep
->vhub
;
337 u16 wValue
, wIndex
, wLength
;
339 wValue
= le16_to_cpu(crq
->wValue
);
340 wIndex
= le16_to_cpu(crq
->wIndex
);
341 wLength
= le16_to_cpu(crq
->wLength
);
343 /* First packet, grab speed */
344 if (vhub
->speed
== USB_SPEED_UNKNOWN
) {
345 u32 ustat
= readl(vhub
->regs
+ AST_VHUB_USBSTS
);
346 if (ustat
& VHUB_USBSTS_HISPEED
)
347 vhub
->speed
= USB_SPEED_HIGH
;
349 vhub
->speed
= USB_SPEED_FULL
;
350 UDCDBG(vhub
, "USB status=%08x speed=%s\n", ustat
,
351 vhub
->speed
== USB_SPEED_HIGH
? "high" : "full");
354 switch ((crq
->bRequestType
<< 8) | crq
->bRequest
) {
356 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
357 EPDBG(ep
, "SET_ADDRESS: Got address %x\n", wValue
);
358 writel(wValue
, vhub
->regs
+ AST_VHUB_CONF
);
359 return std_req_complete
;
362 case DeviceRequest
| USB_REQ_GET_STATUS
:
363 return ast_vhub_hub_dev_status(ep
, wIndex
, wValue
);
364 case InterfaceRequest
| USB_REQ_GET_STATUS
:
365 return ast_vhub_simple_reply(ep
, 0, 0);
366 case EndpointRequest
| USB_REQ_GET_STATUS
:
367 return ast_vhub_hub_ep_status(ep
, wIndex
, wValue
);
369 /* SET/CLEAR_FEATURE */
370 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
371 return ast_vhub_hub_dev_feature(ep
, wIndex
, wValue
, true);
372 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
373 return ast_vhub_hub_dev_feature(ep
, wIndex
, wValue
, false);
374 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
375 return ast_vhub_hub_ep_feature(ep
, wIndex
, wValue
, true);
376 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
377 return ast_vhub_hub_ep_feature(ep
, wIndex
, wValue
, false);
379 /* GET/SET_CONFIGURATION */
380 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
381 return ast_vhub_simple_reply(ep
, 1);
382 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
384 return std_req_stall
;
385 return std_req_complete
;
388 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
389 switch (wValue
>> 8) {
392 return ast_vhub_rep_desc(ep
, wValue
>> 8,
395 return ast_vhub_rep_string(ep
, wValue
& 0xff,
398 return std_req_stall
;
400 /* GET/SET_INTERFACE */
401 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
402 return ast_vhub_simple_reply(ep
, 0);
403 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
404 if (wValue
!= 0 || wIndex
!= 0)
405 return std_req_stall
;
406 return std_req_complete
;
408 return std_req_stall
;
411 static void ast_vhub_update_hub_ep1(struct ast_vhub
*vhub
,
414 /* Update HW EP1 response */
415 u32 reg
= readl(vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
416 u32 pmask
= (1 << (port
+ 1));
417 if (vhub
->ports
[port
].change
)
421 writel(reg
, vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
424 static void ast_vhub_change_port_stat(struct ast_vhub
*vhub
,
430 struct ast_vhub_port
*p
= &vhub
->ports
[port
];
433 /* Update port status */
435 p
->status
= (prev
& ~clr_flags
) | set_flags
;
436 DDBG(&p
->dev
, "port %d status %04x -> %04x (C=%d)\n",
437 port
+ 1, prev
, p
->status
, set_c
);
439 /* Update change bits if needed */
441 u16 chg
= p
->status
^ prev
;
443 /* Only these are relevant for change */
444 chg
&= USB_PORT_STAT_C_CONNECTION
|
445 USB_PORT_STAT_C_ENABLE
|
446 USB_PORT_STAT_C_SUSPEND
|
447 USB_PORT_STAT_C_OVERCURRENT
|
448 USB_PORT_STAT_C_RESET
|
452 * We only set USB_PORT_STAT_C_ENABLE if we are disabling
453 * the port as per USB spec, otherwise MacOS gets upset
455 if (p
->status
& USB_PORT_STAT_ENABLE
)
456 chg
&= ~USB_PORT_STAT_C_ENABLE
;
459 ast_vhub_update_hub_ep1(vhub
, port
);
463 static void ast_vhub_send_host_wakeup(struct ast_vhub
*vhub
)
465 u32 reg
= readl(vhub
->regs
+ AST_VHUB_CTRL
);
466 UDCDBG(vhub
, "Waking up host !\n");
467 reg
|= VHUB_CTRL_MANUAL_REMOTE_WAKEUP
;
468 writel(reg
, vhub
->regs
+ AST_VHUB_CTRL
);
471 void ast_vhub_device_connect(struct ast_vhub
*vhub
,
472 unsigned int port
, bool on
)
475 ast_vhub_change_port_stat(vhub
, port
, 0,
476 USB_PORT_STAT_CONNECTION
, true);
478 ast_vhub_change_port_stat(vhub
, port
,
479 USB_PORT_STAT_CONNECTION
|
480 USB_PORT_STAT_ENABLE
,
484 * If the hub is set to wakup the host on connection events
485 * then send a wakeup.
488 ast_vhub_send_host_wakeup(vhub
);
491 static void ast_vhub_wake_work(struct work_struct
*work
)
493 struct ast_vhub
*vhub
= container_of(work
,
500 * Wake all sleeping ports. If a port is suspended by
501 * the host suspend (without explicit state suspend),
502 * we let the normal host wake path deal with it later.
504 spin_lock_irqsave(&vhub
->lock
, flags
);
505 for (i
= 0; i
< vhub
->max_ports
; i
++) {
506 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
508 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
510 ast_vhub_change_port_stat(vhub
, i
,
511 USB_PORT_STAT_SUSPEND
,
513 ast_vhub_dev_resume(&p
->dev
);
515 ast_vhub_send_host_wakeup(vhub
);
516 spin_unlock_irqrestore(&vhub
->lock
, flags
);
519 void ast_vhub_hub_wake_all(struct ast_vhub
*vhub
)
522 * A device is trying to wake the world, because this
523 * can recurse into the device, we break the call chain
526 schedule_work(&vhub
->wake_work
);
529 static void ast_vhub_port_reset(struct ast_vhub
*vhub
, u8 port
)
531 struct ast_vhub_port
*p
= &vhub
->ports
[port
];
534 /* First mark disabled */
535 ast_vhub_change_port_stat(vhub
, port
,
536 USB_PORT_STAT_ENABLE
|
537 USB_PORT_STAT_SUSPEND
,
545 * This will either "start" the port or reset the
546 * device if already started...
548 ast_vhub_dev_reset(&p
->dev
);
550 /* Grab the right speed */
551 speed
= p
->dev
.driver
->max_speed
;
552 if (speed
== USB_SPEED_UNKNOWN
|| speed
> vhub
->speed
)
557 set
= USB_PORT_STAT_LOW_SPEED
;
558 clr
= USB_PORT_STAT_HIGH_SPEED
;
562 clr
= USB_PORT_STAT_LOW_SPEED
|
563 USB_PORT_STAT_HIGH_SPEED
;
566 set
= USB_PORT_STAT_HIGH_SPEED
;
567 clr
= USB_PORT_STAT_LOW_SPEED
;
570 UDCDBG(vhub
, "Unsupported speed %d when"
571 " connecting device\n",
575 clr
|= USB_PORT_STAT_RESET
;
576 set
|= USB_PORT_STAT_ENABLE
;
578 /* This should ideally be delayed ... */
579 ast_vhub_change_port_stat(vhub
, port
, clr
, set
, true);
582 static enum std_req_rc
ast_vhub_set_port_feature(struct ast_vhub_ep
*ep
,
585 struct ast_vhub
*vhub
= ep
->vhub
;
586 struct ast_vhub_port
*p
;
588 if (port
== 0 || port
> vhub
->max_ports
)
589 return std_req_stall
;
591 p
= &vhub
->ports
[port
];
594 case USB_PORT_FEAT_SUSPEND
:
595 if (!(p
->status
& USB_PORT_STAT_ENABLE
))
596 return std_req_complete
;
597 ast_vhub_change_port_stat(vhub
, port
,
598 0, USB_PORT_STAT_SUSPEND
,
600 ast_vhub_dev_suspend(&p
->dev
);
601 return std_req_complete
;
602 case USB_PORT_FEAT_RESET
:
603 EPDBG(ep
, "Port reset !\n");
604 ast_vhub_port_reset(vhub
, port
);
605 return std_req_complete
;
606 case USB_PORT_FEAT_POWER
:
608 * On Power-on, we mark the connected flag changed,
609 * if there's a connected device, some hosts will
610 * otherwise fail to detect it.
612 if (p
->status
& USB_PORT_STAT_CONNECTION
) {
613 p
->change
|= USB_PORT_STAT_C_CONNECTION
;
614 ast_vhub_update_hub_ep1(vhub
, port
);
616 return std_req_complete
;
617 case USB_PORT_FEAT_TEST
:
618 case USB_PORT_FEAT_INDICATOR
:
619 /* We don't do anything with these */
620 return std_req_complete
;
622 return std_req_stall
;
625 static enum std_req_rc
ast_vhub_clr_port_feature(struct ast_vhub_ep
*ep
,
628 struct ast_vhub
*vhub
= ep
->vhub
;
629 struct ast_vhub_port
*p
;
631 if (port
== 0 || port
> vhub
->max_ports
)
632 return std_req_stall
;
634 p
= &vhub
->ports
[port
];
637 case USB_PORT_FEAT_ENABLE
:
638 ast_vhub_change_port_stat(vhub
, port
,
639 USB_PORT_STAT_ENABLE
|
640 USB_PORT_STAT_SUSPEND
, 0,
642 ast_vhub_dev_suspend(&p
->dev
);
643 return std_req_complete
;
644 case USB_PORT_FEAT_SUSPEND
:
645 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
646 return std_req_complete
;
647 ast_vhub_change_port_stat(vhub
, port
,
648 USB_PORT_STAT_SUSPEND
, 0,
650 ast_vhub_dev_resume(&p
->dev
);
651 return std_req_complete
;
652 case USB_PORT_FEAT_POWER
:
653 /* We don't do power control */
654 return std_req_complete
;
655 case USB_PORT_FEAT_INDICATOR
:
656 /* We don't have indicators */
657 return std_req_complete
;
658 case USB_PORT_FEAT_C_CONNECTION
:
659 case USB_PORT_FEAT_C_ENABLE
:
660 case USB_PORT_FEAT_C_SUSPEND
:
661 case USB_PORT_FEAT_C_OVER_CURRENT
:
662 case USB_PORT_FEAT_C_RESET
:
663 /* Clear state-change feature */
664 p
->change
&= ~(1u << (feat
- 16));
665 ast_vhub_update_hub_ep1(vhub
, port
);
666 return std_req_complete
;
668 return std_req_stall
;
671 static enum std_req_rc
ast_vhub_get_port_stat(struct ast_vhub_ep
*ep
,
674 struct ast_vhub
*vhub
= ep
->vhub
;
677 if (port
== 0 || port
> vhub
->max_ports
)
678 return std_req_stall
;
681 stat
= vhub
->ports
[port
].status
;
682 chg
= vhub
->ports
[port
].change
;
684 /* We always have power */
685 stat
|= USB_PORT_STAT_POWER
;
687 EPDBG(ep
, " port status=%04x change=%04x\n", stat
, chg
);
689 return ast_vhub_simple_reply(ep
,
696 enum std_req_rc
ast_vhub_class_hub_request(struct ast_vhub_ep
*ep
,
697 struct usb_ctrlrequest
*crq
)
699 u16 wValue
, wIndex
, wLength
;
701 wValue
= le16_to_cpu(crq
->wValue
);
702 wIndex
= le16_to_cpu(crq
->wIndex
);
703 wLength
= le16_to_cpu(crq
->wLength
);
705 switch ((crq
->bRequestType
<< 8) | crq
->bRequest
) {
707 EPDBG(ep
, "GetHubStatus\n");
708 return ast_vhub_simple_reply(ep
, 0, 0, 0, 0);
710 EPDBG(ep
, "GetPortStatus(%d)\n", wIndex
& 0xff);
711 return ast_vhub_get_port_stat(ep
, wIndex
& 0xf);
712 case GetHubDescriptor
:
713 if (wValue
!= (USB_DT_HUB
<< 8))
714 return std_req_stall
;
715 EPDBG(ep
, "GetHubDescriptor(%d)\n", wIndex
& 0xff);
716 return ast_vhub_rep_desc(ep
, USB_DT_HUB
, wLength
);
718 case ClearHubFeature
:
719 EPDBG(ep
, "Get/SetHubFeature(%d)\n", wValue
);
720 /* No feature, just complete the requests */
721 if (wValue
== C_HUB_LOCAL_POWER
||
722 wValue
== C_HUB_OVER_CURRENT
)
723 return std_req_complete
;
724 return std_req_stall
;
726 EPDBG(ep
, "SetPortFeature(%d,%d)\n", wIndex
& 0xf, wValue
);
727 return ast_vhub_set_port_feature(ep
, wIndex
& 0xf, wValue
);
728 case ClearPortFeature
:
729 EPDBG(ep
, "ClearPortFeature(%d,%d)\n", wIndex
& 0xf, wValue
);
730 return ast_vhub_clr_port_feature(ep
, wIndex
& 0xf, wValue
);
734 return std_req_complete
;
736 return ast_vhub_simple_reply(ep
, 0, 0, 0, 0);
738 EPDBG(ep
, "Unknown class request\n");
740 return std_req_stall
;
743 void ast_vhub_hub_suspend(struct ast_vhub
*vhub
)
747 UDCDBG(vhub
, "USB bus suspend\n");
752 vhub
->suspended
= true;
755 * Forward to unsuspended ports without changing
756 * their connection status.
758 for (i
= 0; i
< vhub
->max_ports
; i
++) {
759 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
761 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
762 ast_vhub_dev_suspend(&p
->dev
);
766 void ast_vhub_hub_resume(struct ast_vhub
*vhub
)
770 UDCDBG(vhub
, "USB bus resume\n");
772 if (!vhub
->suspended
)
775 vhub
->suspended
= false;
778 * Forward to unsuspended ports without changing
779 * their connection status.
781 for (i
= 0; i
< vhub
->max_ports
; i
++) {
782 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
784 if (!(p
->status
& USB_PORT_STAT_SUSPEND
))
785 ast_vhub_dev_resume(&p
->dev
);
789 void ast_vhub_hub_reset(struct ast_vhub
*vhub
)
793 UDCDBG(vhub
, "USB bus reset\n");
796 * Is the speed known ? If not we don't care, we aren't
797 * initialized yet and ports haven't been enabled.
799 if (vhub
->speed
== USB_SPEED_UNKNOWN
)
802 /* We aren't suspended anymore obviously */
803 vhub
->suspended
= false;
806 vhub
->speed
= USB_SPEED_UNKNOWN
;
808 /* Wakeup not enabled anymore */
809 vhub
->wakeup_en
= false;
812 * Clear all port status, disable gadgets and "suspend"
813 * them. They will be woken up by a port reset.
815 for (i
= 0; i
< vhub
->max_ports
; i
++) {
816 struct ast_vhub_port
*p
= &vhub
->ports
[i
];
818 /* Only keep the connected flag */
819 p
->status
&= USB_PORT_STAT_CONNECTION
;
822 /* Suspend the gadget if any */
823 ast_vhub_dev_suspend(&p
->dev
);
827 writel(0, vhub
->regs
+ AST_VHUB_CONF
);
828 writel(0, vhub
->regs
+ AST_VHUB_EP0_CTRL
);
829 writel(VHUB_EP1_CTRL_RESET_TOGGLE
|
830 VHUB_EP1_CTRL_ENABLE
,
831 vhub
->regs
+ AST_VHUB_EP1_CTRL
);
832 writel(0, vhub
->regs
+ AST_VHUB_EP1_STS_CHG
);
835 static void ast_vhub_init_desc(struct ast_vhub
*vhub
)
837 /* Initialize vhub Device Descriptor. */
838 memcpy(&vhub
->vhub_dev_desc
, &ast_vhub_dev_desc
,
839 sizeof(vhub
->vhub_dev_desc
));
841 /* Initialize vhub Configuration Descriptor. */
842 memcpy(&vhub
->vhub_conf_desc
, &ast_vhub_conf_desc
,
843 sizeof(vhub
->vhub_conf_desc
));
845 /* Initialize vhub Hub Descriptor. */
846 memcpy(&vhub
->vhub_hub_desc
, &ast_vhub_hub_desc
,
847 sizeof(vhub
->vhub_hub_desc
));
848 vhub
->vhub_hub_desc
.bNbrPorts
= vhub
->max_ports
;
850 /* Initialize vhub String Descriptors. */
851 memcpy(&vhub
->vhub_str_desc
, &ast_vhub_strings
,
852 sizeof(vhub
->vhub_str_desc
));
855 void ast_vhub_init_hub(struct ast_vhub
*vhub
)
857 vhub
->speed
= USB_SPEED_UNKNOWN
;
858 INIT_WORK(&vhub
->wake_work
, ast_vhub_wake_work
);
860 ast_vhub_init_desc(vhub
);