GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / usb / core / hub.h
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * usb hub driver head file
4  *
5  * Copyright (C) 1999 Linus Torvalds
6  * Copyright (C) 1999 Johannes Erdfelt
7  * Copyright (C) 1999 Gregory P. Smith
8  * Copyright (C) 2001 Brad Hards (bhards@bigpond.net.au)
9  * Copyright (C) 2012 Intel Corp (tianyu.lan@intel.com)
10  *
11  *  move struct usb_hub to this file.
12  */
13
14 #include <linux/usb.h>
15 #include <linux/usb/ch11.h>
16 #include <linux/usb/hcd.h>
17 #include "usb.h"
18
19 struct usb_hub {
20         struct device           *intfdev;       /* the "interface" device */
21         struct usb_device       *hdev;
22         struct kref             kref;
23         struct urb              *urb;           /* for interrupt polling pipe */
24
25         /* buffer for urb ... with extra space in case of babble */
26         u8                      (*buffer)[8];
27         union {
28                 struct usb_hub_status   hub;
29                 struct usb_port_status  port;
30         }                       *status;        /* buffer for status reports */
31         struct mutex            status_mutex;   /* for the status buffer */
32
33         int                     error;          /* last reported error */
34         int                     nerrors;        /* track consecutive errors */
35
36         unsigned long           event_bits[1];  /* status change bitmask */
37         unsigned long           change_bits[1]; /* ports with logical connect
38                                                         status change */
39         unsigned long           removed_bits[1]; /* ports with a "removed"
40                                                         device present */
41         unsigned long           wakeup_bits[1]; /* ports that have signaled
42                                                         remote wakeup */
43         unsigned long           power_bits[1]; /* ports that are powered */
44         unsigned long           child_usage_bits[1]; /* ports powered on for
45                                                         children */
46         unsigned long           warm_reset_bits[1]; /* ports requesting warm
47                                                         reset recovery */
48 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
49 #error event_bits[] is too short!
50 #endif
51
52         struct usb_hub_descriptor *descriptor;  /* class descriptor */
53         struct usb_tt           tt;             /* Transaction Translator */
54
55         unsigned                mA_per_port;    /* current for each child */
56 #ifdef  CONFIG_PM
57         unsigned                wakeup_enabled_descendants;
58 #endif
59
60         unsigned                limited_power:1;
61         unsigned                quiescing:1;
62         unsigned                disconnected:1;
63         unsigned                in_reset:1;
64         unsigned                quirk_disable_autosuspend:1;
65
66         unsigned                quirk_check_port_auto_suspend:1;
67
68         unsigned                has_indicators:1;
69         u8                      indicator[USB_MAXCHILDREN];
70         struct delayed_work     leds;
71         struct delayed_work     init_work;
72         struct work_struct      events;
73         struct usb_port         **ports;
74 };
75
76 /**
77  * struct usb port - kernel's representation of a usb port
78  * @child: usb device attached to the port
79  * @dev: generic device interface
80  * @port_owner: port's owner
81  * @peer: related usb2 and usb3 ports (share the same connector)
82  * @req: default pm qos request for hubs without port power control
83  * @connect_type: port's connect type
84  * @location: opaque representation of platform connector location
85  * @status_lock: synchronize port_event() vs usb_port_{suspend|resume}
86  * @portnum: port index num based one
87  * @is_superspeed cache super-speed status
88  * @usb3_lpm_u1_permit: whether USB3 U1 LPM is permitted.
89  * @usb3_lpm_u2_permit: whether USB3 U2 LPM is permitted.
90  */
91 struct usb_port {
92         struct usb_device *child;
93         struct device dev;
94         struct usb_dev_state *port_owner;
95         struct usb_port *peer;
96         struct dev_pm_qos_request *req;
97         enum usb_port_connect_type connect_type;
98         usb_port_location_t location;
99         struct mutex status_lock;
100         u32 over_current_count;
101         u8 portnum;
102         u32 quirks;
103         unsigned int is_superspeed:1;
104         unsigned int usb3_lpm_u1_permit:1;
105         unsigned int usb3_lpm_u2_permit:1;
106 };
107
108 #define to_usb_port(_dev) \
109         container_of(_dev, struct usb_port, dev)
110
111 extern int usb_hub_create_port_device(struct usb_hub *hub,
112                 int port1);
113 extern void usb_hub_remove_port_device(struct usb_hub *hub,
114                 int port1);
115 extern int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
116                 int port1, bool set);
117 extern struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev);
118 extern int hub_port_debounce(struct usb_hub *hub, int port1,
119                 bool must_be_connected);
120 extern int usb_clear_port_feature(struct usb_device *hdev,
121                 int port1, int feature);
122
123 static inline bool hub_is_port_power_switchable(struct usb_hub *hub)
124 {
125         __le16 hcs;
126
127         if (!hub)
128                 return false;
129         hcs = hub->descriptor->wHubCharacteristics;
130         return (le16_to_cpu(hcs) & HUB_CHAR_LPSM) < HUB_CHAR_NO_LPSM;
131 }
132
133 static inline int hub_is_superspeed(struct usb_device *hdev)
134 {
135         return hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS;
136 }
137
138 static inline int hub_is_superspeedplus(struct usb_device *hdev)
139 {
140         return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS &&
141                 le16_to_cpu(hdev->descriptor.bcdUSB) >= 0x0310 &&
142                 hdev->bos->ssp_cap);
143 }
144
145 static inline unsigned hub_power_on_good_delay(struct usb_hub *hub)
146 {
147         unsigned delay = hub->descriptor->bPwrOn2PwrGood * 2;
148
149         if (!hub->hdev->parent) /* root hub */
150                 return delay;
151         else /* Wait at least 100 msec for power to become stable */
152                 return max(delay, 100U);
153 }
154
155 static inline int hub_port_debounce_be_connected(struct usb_hub *hub,
156                 int port1)
157 {
158         return hub_port_debounce(hub, port1, true);
159 }
160
161 static inline int hub_port_debounce_be_stable(struct usb_hub *hub,
162                 int port1)
163 {
164         return hub_port_debounce(hub, port1, false);
165 }