1 | /*
|
---|
2 | * Copyright (c) 2017 Michal Staruch
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbxhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief The roothub structures abstraction.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 | #include "debug.h"
|
---|
40 | #include "hc.h"
|
---|
41 | #include "rh.h"
|
---|
42 |
|
---|
43 | #include "hw_struct/trb.h"
|
---|
44 |
|
---|
45 | static int handle_connected_device(xhci_hc_t* hc, xhci_port_regs_t* regs, uint8_t port_id) {
|
---|
46 | uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
|
---|
47 | if(link_state == 0) {
|
---|
48 | // USB3 is automatically advance to enabled
|
---|
49 | uint8_t port_speed = XHCI_REG_RD(regs, XHCI_PORT_PS);
|
---|
50 | usb_log_debug2("Detected new device on port %u, port speed id %u.", port_id, port_speed);
|
---|
51 | // TODO: Assign device slot (specification 4.3.2)
|
---|
52 | }
|
---|
53 | else if(link_state == 5) {
|
---|
54 | // USB 3 failed to enable
|
---|
55 | usb_log_debug("USB 3 port couldn't be enabled.");
|
---|
56 | }
|
---|
57 | else if(link_state == 7) {
|
---|
58 | usb_log_debug("USB 2 device attached, issuing reset.");
|
---|
59 | xhci_reset_hub_port(hc, port_id);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return EOK;
|
---|
63 | }
|
---|
64 |
|
---|
65 | int xhci_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb) {
|
---|
66 | uint8_t port_id = xhci_get_hub_port(trb);
|
---|
67 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
---|
68 | xhci_port_regs_t* regs = &hc->op_regs->portrs[port_id];
|
---|
69 |
|
---|
70 | // Port reset change
|
---|
71 | if(XHCI_REG_RD(regs, XHCI_PORT_PRC)) {
|
---|
72 | // Clear the flag
|
---|
73 | XHCI_REG_WR(regs, XHCI_PORT_PRC, 1);
|
---|
74 |
|
---|
75 | uint8_t port_speed = XHCI_REG_RD(regs, XHCI_PORT_PS);
|
---|
76 | usb_log_debug2("Detected port reset on port %u, port speed id %u.", port_id, port_speed);
|
---|
77 | // TODO: Assign device slot (specification 4.3.2)
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Connection status change
|
---|
81 | if(XHCI_REG_RD(regs, XHCI_PORT_CSC)) {
|
---|
82 | XHCI_REG_WR(regs, XHCI_PORT_CSC, 1);
|
---|
83 |
|
---|
84 | if(XHCI_REG_RD(regs, XHCI_PORT_CCS) == 1) {
|
---|
85 | handle_connected_device(hc, regs, port_id);
|
---|
86 | }
|
---|
87 | else {
|
---|
88 | // Device disconnected
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int xhci_get_hub_port(xhci_trb_t *trb) {
|
---|
95 | assert(trb);
|
---|
96 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 63, 56);
|
---|
97 | return port_id;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int xhci_reset_hub_port(xhci_hc_t* hc, uint8_t port) {
|
---|
101 | usb_log_debug2("Resetting port %u.", port);
|
---|
102 | xhci_port_regs_t regs = hc->op_regs->portrs[port];
|
---|
103 | XHCI_REG_WR(®s, XHCI_PORT_PR, 1);
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|