162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * usb hub driver head file 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 1999 Linus Torvalds 662306a36Sopenharmony_ci * Copyright (C) 1999 Johannes Erdfelt 762306a36Sopenharmony_ci * Copyright (C) 1999 Gregory P. Smith 862306a36Sopenharmony_ci * Copyright (C) 2001 Brad Hards (bhards@bigpond.net.au) 962306a36Sopenharmony_ci * Copyright (C) 2012 Intel Corp (tianyu.lan@intel.com) 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * move struct usb_hub to this file. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/usb.h> 1562306a36Sopenharmony_ci#include <linux/usb/ch11.h> 1662306a36Sopenharmony_ci#include <linux/usb/hcd.h> 1762306a36Sopenharmony_ci#include "usb.h" 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cistruct usb_hub { 2062306a36Sopenharmony_ci struct device *intfdev; /* the "interface" device */ 2162306a36Sopenharmony_ci struct usb_device *hdev; 2262306a36Sopenharmony_ci struct kref kref; 2362306a36Sopenharmony_ci struct urb *urb; /* for interrupt polling pipe */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci /* buffer for urb ... with extra space in case of babble */ 2662306a36Sopenharmony_ci u8 (*buffer)[8]; 2762306a36Sopenharmony_ci union { 2862306a36Sopenharmony_ci struct usb_hub_status hub; 2962306a36Sopenharmony_ci struct usb_port_status port; 3062306a36Sopenharmony_ci } *status; /* buffer for status reports */ 3162306a36Sopenharmony_ci struct mutex status_mutex; /* for the status buffer */ 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci int error; /* last reported error */ 3462306a36Sopenharmony_ci int nerrors; /* track consecutive errors */ 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci unsigned long event_bits[1]; /* status change bitmask */ 3762306a36Sopenharmony_ci unsigned long change_bits[1]; /* ports with logical connect 3862306a36Sopenharmony_ci status change */ 3962306a36Sopenharmony_ci unsigned long removed_bits[1]; /* ports with a "removed" 4062306a36Sopenharmony_ci device present */ 4162306a36Sopenharmony_ci unsigned long wakeup_bits[1]; /* ports that have signaled 4262306a36Sopenharmony_ci remote wakeup */ 4362306a36Sopenharmony_ci unsigned long power_bits[1]; /* ports that are powered */ 4462306a36Sopenharmony_ci unsigned long child_usage_bits[1]; /* ports powered on for 4562306a36Sopenharmony_ci children */ 4662306a36Sopenharmony_ci unsigned long warm_reset_bits[1]; /* ports requesting warm 4762306a36Sopenharmony_ci reset recovery */ 4862306a36Sopenharmony_ci#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 4962306a36Sopenharmony_ci#error event_bits[] is too short! 5062306a36Sopenharmony_ci#endif 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci struct usb_hub_descriptor *descriptor; /* class descriptor */ 5362306a36Sopenharmony_ci struct usb_tt tt; /* Transaction Translator */ 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci unsigned mA_per_port; /* current for each child */ 5662306a36Sopenharmony_ci#ifdef CONFIG_PM 5762306a36Sopenharmony_ci unsigned wakeup_enabled_descendants; 5862306a36Sopenharmony_ci#endif 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci unsigned limited_power:1; 6162306a36Sopenharmony_ci unsigned quiescing:1; 6262306a36Sopenharmony_ci unsigned disconnected:1; 6362306a36Sopenharmony_ci unsigned in_reset:1; 6462306a36Sopenharmony_ci unsigned quirk_disable_autosuspend:1; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci unsigned quirk_check_port_auto_suspend:1; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci unsigned has_indicators:1; 6962306a36Sopenharmony_ci u8 indicator[USB_MAXCHILDREN]; 7062306a36Sopenharmony_ci struct delayed_work leds; 7162306a36Sopenharmony_ci struct delayed_work init_work; 7262306a36Sopenharmony_ci struct work_struct events; 7362306a36Sopenharmony_ci spinlock_t irq_urb_lock; 7462306a36Sopenharmony_ci struct timer_list irq_urb_retry; 7562306a36Sopenharmony_ci struct usb_port **ports; 7662306a36Sopenharmony_ci struct list_head onboard_hub_devs; 7762306a36Sopenharmony_ci}; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci/** 8062306a36Sopenharmony_ci * struct usb port - kernel's representation of a usb port 8162306a36Sopenharmony_ci * @child: usb device attached to the port 8262306a36Sopenharmony_ci * @dev: generic device interface 8362306a36Sopenharmony_ci * @port_owner: port's owner 8462306a36Sopenharmony_ci * @peer: related usb2 and usb3 ports (share the same connector) 8562306a36Sopenharmony_ci * @req: default pm qos request for hubs without port power control 8662306a36Sopenharmony_ci * @connect_type: port's connect type 8762306a36Sopenharmony_ci * @state: device state of the usb device attached to the port 8862306a36Sopenharmony_ci * @state_kn: kernfs_node of the sysfs attribute that accesses @state 8962306a36Sopenharmony_ci * @location: opaque representation of platform connector location 9062306a36Sopenharmony_ci * @status_lock: synchronize port_event() vs usb_port_{suspend|resume} 9162306a36Sopenharmony_ci * @portnum: port index num based one 9262306a36Sopenharmony_ci * @is_superspeed cache super-speed status 9362306a36Sopenharmony_ci * @usb3_lpm_u1_permit: whether USB3 U1 LPM is permitted. 9462306a36Sopenharmony_ci * @usb3_lpm_u2_permit: whether USB3 U2 LPM is permitted. 9562306a36Sopenharmony_ci * @early_stop: whether port initialization will be stopped earlier. 9662306a36Sopenharmony_ci * @ignore_event: whether events of the port are ignored. 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_cistruct usb_port { 9962306a36Sopenharmony_ci struct usb_device *child; 10062306a36Sopenharmony_ci struct device dev; 10162306a36Sopenharmony_ci struct usb_dev_state *port_owner; 10262306a36Sopenharmony_ci struct usb_port *peer; 10362306a36Sopenharmony_ci struct dev_pm_qos_request *req; 10462306a36Sopenharmony_ci enum usb_port_connect_type connect_type; 10562306a36Sopenharmony_ci enum usb_device_state state; 10662306a36Sopenharmony_ci struct kernfs_node *state_kn; 10762306a36Sopenharmony_ci usb_port_location_t location; 10862306a36Sopenharmony_ci struct mutex status_lock; 10962306a36Sopenharmony_ci u32 over_current_count; 11062306a36Sopenharmony_ci u8 portnum; 11162306a36Sopenharmony_ci u32 quirks; 11262306a36Sopenharmony_ci unsigned int early_stop:1; 11362306a36Sopenharmony_ci unsigned int ignore_event:1; 11462306a36Sopenharmony_ci unsigned int is_superspeed:1; 11562306a36Sopenharmony_ci unsigned int usb3_lpm_u1_permit:1; 11662306a36Sopenharmony_ci unsigned int usb3_lpm_u2_permit:1; 11762306a36Sopenharmony_ci}; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci#define to_usb_port(_dev) \ 12062306a36Sopenharmony_ci container_of(_dev, struct usb_port, dev) 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciextern int usb_hub_create_port_device(struct usb_hub *hub, 12362306a36Sopenharmony_ci int port1); 12462306a36Sopenharmony_ciextern void usb_hub_remove_port_device(struct usb_hub *hub, 12562306a36Sopenharmony_ci int port1); 12662306a36Sopenharmony_ciextern int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, 12762306a36Sopenharmony_ci int port1, bool set); 12862306a36Sopenharmony_ciextern struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev); 12962306a36Sopenharmony_ciextern int hub_port_debounce(struct usb_hub *hub, int port1, 13062306a36Sopenharmony_ci bool must_be_connected); 13162306a36Sopenharmony_ciextern int usb_clear_port_feature(struct usb_device *hdev, 13262306a36Sopenharmony_ci int port1, int feature); 13362306a36Sopenharmony_ciextern int usb_hub_port_status(struct usb_hub *hub, int port1, 13462306a36Sopenharmony_ci u16 *status, u16 *change); 13562306a36Sopenharmony_ciextern int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_cistatic inline bool hub_is_port_power_switchable(struct usb_hub *hub) 13862306a36Sopenharmony_ci{ 13962306a36Sopenharmony_ci __le16 hcs; 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci if (!hub) 14262306a36Sopenharmony_ci return false; 14362306a36Sopenharmony_ci hcs = hub->descriptor->wHubCharacteristics; 14462306a36Sopenharmony_ci return (le16_to_cpu(hcs) & HUB_CHAR_LPSM) < HUB_CHAR_NO_LPSM; 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_cistatic inline int hub_is_superspeed(struct usb_device *hdev) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci return hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS; 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_cistatic inline int hub_is_superspeedplus(struct usb_device *hdev) 15362306a36Sopenharmony_ci{ 15462306a36Sopenharmony_ci return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS && 15562306a36Sopenharmony_ci le16_to_cpu(hdev->descriptor.bcdUSB) >= 0x0310 && 15662306a36Sopenharmony_ci hdev->bos && hdev->bos->ssp_cap); 15762306a36Sopenharmony_ci} 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_cistatic inline unsigned hub_power_on_good_delay(struct usb_hub *hub) 16062306a36Sopenharmony_ci{ 16162306a36Sopenharmony_ci unsigned delay = hub->descriptor->bPwrOn2PwrGood * 2; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci if (!hub->hdev->parent) /* root hub */ 16462306a36Sopenharmony_ci return delay; 16562306a36Sopenharmony_ci else /* Wait at least 100 msec for power to become stable */ 16662306a36Sopenharmony_ci return max(delay, 100U); 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cistatic inline int hub_port_debounce_be_connected(struct usb_hub *hub, 17062306a36Sopenharmony_ci int port1) 17162306a36Sopenharmony_ci{ 17262306a36Sopenharmony_ci return hub_port_debounce(hub, port1, true); 17362306a36Sopenharmony_ci} 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_cistatic inline int hub_port_debounce_be_stable(struct usb_hub *hub, 17662306a36Sopenharmony_ci int port1) 17762306a36Sopenharmony_ci{ 17862306a36Sopenharmony_ci return hub_port_debounce(hub, port1, false); 17962306a36Sopenharmony_ci} 180