xref: /linux-6.15/include/linux/usb/hcd.h (revision 8fea0c8f)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
227729aadSEric Lescouet /*
327729aadSEric Lescouet  * Copyright (c) 2001-2002 by David Brownell
427729aadSEric Lescouet  */
527729aadSEric Lescouet 
627729aadSEric Lescouet #ifndef __USB_CORE_HCD_H
727729aadSEric Lescouet #define __USB_CORE_HCD_H
827729aadSEric Lescouet 
927729aadSEric Lescouet #ifdef __KERNEL__
1027729aadSEric Lescouet 
1127729aadSEric Lescouet #include <linux/rwsem.h>
1294dfd7edSMing Lei #include <linux/interrupt.h>
135363de75SHeiner Kallweit #include <linux/idr.h>
1427729aadSEric Lescouet 
1527729aadSEric Lescouet #define MAX_TOPO_LEVEL		6
1627729aadSEric Lescouet 
1727729aadSEric Lescouet /* This file contains declarations of usbcore internals that are mostly
1827729aadSEric Lescouet  * used or exposed by Host Controller Drivers.
1927729aadSEric Lescouet  */
2027729aadSEric Lescouet 
2127729aadSEric Lescouet /*
2227729aadSEric Lescouet  * USB Packet IDs (PIDs)
2327729aadSEric Lescouet  */
2427729aadSEric Lescouet #define USB_PID_EXT			0xf0	/* USB 2.0 LPM ECN */
2527729aadSEric Lescouet #define USB_PID_OUT			0xe1
2627729aadSEric Lescouet #define USB_PID_ACK			0xd2
2727729aadSEric Lescouet #define USB_PID_DATA0			0xc3
2827729aadSEric Lescouet #define USB_PID_PING			0xb4	/* USB 2.0 */
2927729aadSEric Lescouet #define USB_PID_SOF			0xa5
3027729aadSEric Lescouet #define USB_PID_NYET			0x96	/* USB 2.0 */
3127729aadSEric Lescouet #define USB_PID_DATA2			0x87	/* USB 2.0 */
3227729aadSEric Lescouet #define USB_PID_SPLIT			0x78	/* USB 2.0 */
3327729aadSEric Lescouet #define USB_PID_IN			0x69
3427729aadSEric Lescouet #define USB_PID_NAK			0x5a
3527729aadSEric Lescouet #define USB_PID_DATA1			0x4b
3627729aadSEric Lescouet #define USB_PID_PREAMBLE		0x3c	/* Token mode */
3727729aadSEric Lescouet #define USB_PID_ERR			0x3c	/* USB 2.0: handshake mode */
3827729aadSEric Lescouet #define USB_PID_SETUP			0x2d
3927729aadSEric Lescouet #define USB_PID_STALL			0x1e
4027729aadSEric Lescouet #define USB_PID_MDATA			0x0f	/* USB 2.0 */
4127729aadSEric Lescouet 
4227729aadSEric Lescouet /*-------------------------------------------------------------------------*/
4327729aadSEric Lescouet 
4427729aadSEric Lescouet /*
4527729aadSEric Lescouet  * USB Host Controller Driver (usb_hcd) framework
4627729aadSEric Lescouet  *
4727729aadSEric Lescouet  * Since "struct usb_bus" is so thin, you can't share much code in it.
4824bb0076SZhen Lei  * This framework is a layer over that, and should be more shareable.
4927729aadSEric Lescouet  */
5027729aadSEric Lescouet 
5127729aadSEric Lescouet /*-------------------------------------------------------------------------*/
5227729aadSEric Lescouet 
5394dfd7edSMing Lei struct giveback_urb_bh {
5494dfd7edSMing Lei 	bool running;
5526c6c2f8SWeitao Wang 	bool high_prio;
5694dfd7edSMing Lei 	spinlock_t lock;
5794dfd7edSMing Lei 	struct list_head  head;
58*8fea0c8fSTejun Heo 	struct work_struct bh;
59c7ccde6eSAlan Stern 	struct usb_host_endpoint *completing_ep;
6094dfd7edSMing Lei };
6194dfd7edSMing Lei 
627bae0432SDmitry Torokhov enum usb_dev_authorize_policy {
637bae0432SDmitry Torokhov 	USB_DEVICE_AUTHORIZE_NONE	= 0,
647bae0432SDmitry Torokhov 	USB_DEVICE_AUTHORIZE_ALL	= 1,
657bae0432SDmitry Torokhov 	USB_DEVICE_AUTHORIZE_INTERNAL	= 2,
667bae0432SDmitry Torokhov };
677bae0432SDmitry Torokhov 
6827729aadSEric Lescouet struct usb_hcd {
6927729aadSEric Lescouet 
7027729aadSEric Lescouet 	/*
7127729aadSEric Lescouet 	 * housekeeping
7227729aadSEric Lescouet 	 */
7327729aadSEric Lescouet 	struct usb_bus		self;		/* hcd is-a bus */
7427729aadSEric Lescouet 	struct kref		kref;		/* reference counter */
7527729aadSEric Lescouet 
7627729aadSEric Lescouet 	const char		*product_desc;	/* product/vendor string */
7783de4b2bSSarah Sharp 	int			speed;		/* Speed for this roothub.
7883de4b2bSSarah Sharp 						 * May be different from
7983de4b2bSSarah Sharp 						 * hcd->driver->flags & HCD_MASK
8083de4b2bSSarah Sharp 						 */
8127729aadSEric Lescouet 	char			irq_descr[24];	/* driver + bus # */
8227729aadSEric Lescouet 
8327729aadSEric Lescouet 	struct timer_list	rh_timer;	/* drives root-hub polling */
8427729aadSEric Lescouet 	struct urb		*status_urb;	/* the current status urb */
85ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM
8627729aadSEric Lescouet 	struct work_struct	wakeup_work;	/* for remote wakeup */
8727729aadSEric Lescouet #endif
88a4d6a298SRaul E Rangel 	struct work_struct	died_work;	/* for when the device dies */
8927729aadSEric Lescouet 
9027729aadSEric Lescouet 	/*
9127729aadSEric Lescouet 	 * hardware info/state
9227729aadSEric Lescouet 	 */
9327729aadSEric Lescouet 	const struct hc_driver	*driver;	/* hw-specific hooks */
9427729aadSEric Lescouet 
95c2e935a7SRichard Zhao 	/*
96c2e935a7SRichard Zhao 	 * OTG and some Host controllers need software interaction with phys;
97c2e935a7SRichard Zhao 	 * other external phys should be software-transparent
98c2e935a7SRichard Zhao 	 */
993d46e73dSAntoine Tenart 	struct usb_phy		*usb_phy;
100178a0bceSMartin Blumenstingl 	struct usb_phy_roothub	*phy_roothub;
101c2e935a7SRichard Zhao 
102541c7d43SAlan Stern 	/* Flags that need to be manipulated atomically because they can
103541c7d43SAlan Stern 	 * change while the host controller is running.  Always use
104541c7d43SAlan Stern 	 * set_bit() or clear_bit() to change their values.
105541c7d43SAlan Stern 	 */
10627729aadSEric Lescouet 	unsigned long		flags;
107541c7d43SAlan Stern #define HCD_FLAG_HW_ACCESSIBLE		0	/* at full power */
108541c7d43SAlan Stern #define HCD_FLAG_POLL_RH		2	/* poll for rh status? */
109541c7d43SAlan Stern #define HCD_FLAG_POLL_PENDING		3	/* status has changed? */
110ff2f0787SAlan Stern #define HCD_FLAG_WAKEUP_PENDING		4	/* root hub is resuming? */
1119b37596aSAlan Stern #define HCD_FLAG_RH_RUNNING		5	/* root hub is running? */
1129b37596aSAlan Stern #define HCD_FLAG_DEAD			6	/* controller has died? */
1136b2bd3c8SStefan Koch #define HCD_FLAG_INTF_AUTHORIZED	7	/* authorize interfaces? */
114a44623d9SKishon Vijay Abraham I #define HCD_FLAG_DEFER_RH_REGISTER	8	/* Defer roothub registration */
11527729aadSEric Lescouet 
116541c7d43SAlan Stern 	/* The flags can be tested using these macros; they are likely to
117541c7d43SAlan Stern 	 * be slightly faster than test_bit().
118541c7d43SAlan Stern 	 */
119541c7d43SAlan Stern #define HCD_HW_ACCESSIBLE(hcd)	((hcd)->flags & (1U << HCD_FLAG_HW_ACCESSIBLE))
120541c7d43SAlan Stern #define HCD_POLL_RH(hcd)	((hcd)->flags & (1U << HCD_FLAG_POLL_RH))
121541c7d43SAlan Stern #define HCD_POLL_PENDING(hcd)	((hcd)->flags & (1U << HCD_FLAG_POLL_PENDING))
122ff2f0787SAlan Stern #define HCD_WAKEUP_PENDING(hcd)	((hcd)->flags & (1U << HCD_FLAG_WAKEUP_PENDING))
1239b37596aSAlan Stern #define HCD_RH_RUNNING(hcd)	((hcd)->flags & (1U << HCD_FLAG_RH_RUNNING))
1249b37596aSAlan Stern #define HCD_DEAD(hcd)		((hcd)->flags & (1U << HCD_FLAG_DEAD))
125a44623d9SKishon Vijay Abraham I #define HCD_DEFER_RH_REGISTER(hcd) ((hcd)->flags & (1U << HCD_FLAG_DEFER_RH_REGISTER))
126541c7d43SAlan Stern 
1276b2bd3c8SStefan Koch 	/*
1286b2bd3c8SStefan Koch 	 * Specifies if interfaces are authorized by default
1296b2bd3c8SStefan Koch 	 * or they require explicit user space authorization; this bit is
1306b2bd3c8SStefan Koch 	 * settable through /sys/class/usb_host/X/interface_authorized_default
1316b2bd3c8SStefan Koch 	 */
1326b2bd3c8SStefan Koch #define HCD_INTF_AUTHORIZED(hcd) \
1336b2bd3c8SStefan Koch 	((hcd)->flags & (1U << HCD_FLAG_INTF_AUTHORIZED))
1346b2bd3c8SStefan Koch 
135ff8e2c56SStefan Koch 	/*
136ff8e2c56SStefan Koch 	 * Specifies if devices are authorized by default
137ff8e2c56SStefan Koch 	 * or they require explicit user space authorization; this bit is
138ff8e2c56SStefan Koch 	 * settable through /sys/class/usb_host/X/authorized_default
139ff8e2c56SStefan Koch 	 */
1407bae0432SDmitry Torokhov 	enum usb_dev_authorize_policy dev_policy;
141ff8e2c56SStefan Koch 
142541c7d43SAlan Stern 	/* Flags that get set only during HCD registration or removal. */
14327729aadSEric Lescouet 	unsigned		rh_registered:1;/* is root hub registered? */
1446d88e679SAlan Stern 	unsigned		rh_pollable:1;	/* may we poll the root hub? */
1450029227fSAndiry Xu 	unsigned		msix_enabled:1;	/* driver has MSI-X enabled? */
1466a29beefSPeter Chen 	unsigned		msi_enabled:1;	/* driver has MSI enabled? */
1474e88d4c0SMartin Blumenstingl 	/*
1484e88d4c0SMartin Blumenstingl 	 * do not manage the PHY state in the HCD core, instead let the driver
1494e88d4c0SMartin Blumenstingl 	 * handle this (for example if the PHY can only be turned on after a
1504e88d4c0SMartin Blumenstingl 	 * specific event)
1514e88d4c0SMartin Blumenstingl 	 */
1524e88d4c0SMartin Blumenstingl 	unsigned		skip_phy_initialization:1;
15327729aadSEric Lescouet 
15427729aadSEric Lescouet 	/* The next flag is a stopgap, to be removed when all the HCDs
15527729aadSEric Lescouet 	 * support the new root-hub polling mechanism. */
15627729aadSEric Lescouet 	unsigned		uses_new_polling:1;
15727729aadSEric Lescouet 	unsigned		has_tt:1;	/* Integrated TT in root hub */
1587868943dSHuang Rui 	unsigned		amd_resume_bug:1; /* AMD remote wakeup quirk */
15914aec589SOliver Neukum 	unsigned		can_do_streams:1; /* HC supports streams */
160f2693b43SPeter Chen 	unsigned		tpl_support:1; /* OTG & EH TPL support */
161074f9dd5SAlan Stern 	unsigned		cant_recv_wakeups:1;
162074f9dd5SAlan Stern 			/* wakeup requests from downstream aren't received */
16327729aadSEric Lescouet 
164cd70469dSFelipe Balbi 	unsigned int		irq;		/* irq allocated */
16527729aadSEric Lescouet 	void __iomem		*regs;		/* device memory/io */
16626f944b2SKishon Vijay Abraham I 	resource_size_t		rsrc_start;	/* memory/io resource start */
16726f944b2SKishon Vijay Abraham I 	resource_size_t		rsrc_len;	/* memory/io resource length */
16827729aadSEric Lescouet 	unsigned		power_budget;	/* in mA, 0 = no limit */
16927729aadSEric Lescouet 
17094dfd7edSMing Lei 	struct giveback_urb_bh  high_prio_bh;
17194dfd7edSMing Lei 	struct giveback_urb_bh  low_prio_bh;
17294dfd7edSMing Lei 
17327729aadSEric Lescouet 	/* bandwidth_mutex should be taken before adding or removing
17427729aadSEric Lescouet 	 * any new bus bandwidth constraints:
17527729aadSEric Lescouet 	 *   1. Before adding a configuration for a new device.
17627729aadSEric Lescouet 	 *   2. Before removing the configuration to put the device into
17727729aadSEric Lescouet 	 *      the addressed state.
17827729aadSEric Lescouet 	 *   3. Before selecting a different configuration.
17927729aadSEric Lescouet 	 *   4. Before selecting an alternate interface setting.
18027729aadSEric Lescouet 	 *
18127729aadSEric Lescouet 	 * bandwidth_mutex should be dropped after a successful control message
18227729aadSEric Lescouet 	 * to the device, or resetting the bandwidth after a failed attempt.
18327729aadSEric Lescouet 	 */
184feb26ac3SChris Bainbridge 	struct mutex		*address0_mutex;
185d673bfcbSSarah Sharp 	struct mutex		*bandwidth_mutex;
186c5635437SSarah Sharp 	struct usb_hcd		*shared_hcd;
187c5635437SSarah Sharp 	struct usb_hcd		*primary_hcd;
18827729aadSEric Lescouet 
18927729aadSEric Lescouet 
19027729aadSEric Lescouet #define HCD_BUFFER_POOLS	4
19127729aadSEric Lescouet 	struct dma_pool		*pool[HCD_BUFFER_POOLS];
19227729aadSEric Lescouet 
19327729aadSEric Lescouet 	int			state;
19427729aadSEric Lescouet #	define	__ACTIVE		0x01
19527729aadSEric Lescouet #	define	__SUSPEND		0x04
19627729aadSEric Lescouet #	define	__TRANSIENT		0x80
19727729aadSEric Lescouet 
19827729aadSEric Lescouet #	define	HC_STATE_HALT		0
19927729aadSEric Lescouet #	define	HC_STATE_RUNNING	(__ACTIVE)
20027729aadSEric Lescouet #	define	HC_STATE_QUIESCING	(__SUSPEND|__TRANSIENT|__ACTIVE)
20127729aadSEric Lescouet #	define	HC_STATE_RESUMING	(__SUSPEND|__TRANSIENT)
20227729aadSEric Lescouet #	define	HC_STATE_SUSPENDED	(__SUSPEND)
20327729aadSEric Lescouet 
20427729aadSEric Lescouet #define	HC_IS_RUNNING(state) ((state) & __ACTIVE)
20527729aadSEric Lescouet #define	HC_IS_SUSPENDED(state) ((state) & __SUSPEND)
20627729aadSEric Lescouet 
207b0310c2fSLaurentiu Tudor 	/* memory pool for HCs having local memory, or %NULL */
208b0310c2fSLaurentiu Tudor 	struct gen_pool         *localmem_pool;
209b0310c2fSLaurentiu Tudor 
21027729aadSEric Lescouet 	/* more shared queuing code would be good; it should support
21127729aadSEric Lescouet 	 * smarter scheduling, handle transaction translators, etc;
21227729aadSEric Lescouet 	 * input size of periodic table to an interrupt scheduler.
21327729aadSEric Lescouet 	 * (ohci 32, uhci 1024, ehci 256/512/1024).
21427729aadSEric Lescouet 	 */
21527729aadSEric Lescouet 
21627729aadSEric Lescouet 	/* The HC driver's private data is stored at the end of
21727729aadSEric Lescouet 	 * this structure.
21827729aadSEric Lescouet 	 */
2196bc3f397SGustavo A. R. Silva 	unsigned long hcd_priv[]
220276532baSHarro Haan 			__attribute__ ((aligned(sizeof(s64))));
22127729aadSEric Lescouet };
22227729aadSEric Lescouet 
22327729aadSEric Lescouet /* 2.4 does this a bit differently ... */
hcd_to_bus(struct usb_hcd * hcd)22427729aadSEric Lescouet static inline struct usb_bus *hcd_to_bus(struct usb_hcd *hcd)
22527729aadSEric Lescouet {
22627729aadSEric Lescouet 	return &hcd->self;
22727729aadSEric Lescouet }
22827729aadSEric Lescouet 
bus_to_hcd(struct usb_bus * bus)22927729aadSEric Lescouet static inline struct usb_hcd *bus_to_hcd(struct usb_bus *bus)
23027729aadSEric Lescouet {
23127729aadSEric Lescouet 	return container_of(bus, struct usb_hcd, self);
23227729aadSEric Lescouet }
23327729aadSEric Lescouet 
23427729aadSEric Lescouet /*-------------------------------------------------------------------------*/
23527729aadSEric Lescouet 
23627729aadSEric Lescouet 
23727729aadSEric Lescouet struct hc_driver {
23827729aadSEric Lescouet 	const char	*description;	/* "ehci-hcd" etc */
23927729aadSEric Lescouet 	const char	*product_desc;	/* product/vendor string */
24027729aadSEric Lescouet 	size_t		hcd_priv_size;	/* size of private data */
24127729aadSEric Lescouet 
24227729aadSEric Lescouet 	/* irq handler */
24327729aadSEric Lescouet 	irqreturn_t	(*irq) (struct usb_hcd *hcd);
24427729aadSEric Lescouet 
24527729aadSEric Lescouet 	int	flags;
24627729aadSEric Lescouet #define	HCD_MEMORY	0x0001		/* HC regs use memory (else I/O) */
2477b81cb6bSChristoph Hellwig #define	HCD_DMA		0x0002		/* HC uses DMA */
248c5635437SSarah Sharp #define	HCD_SHARED	0x0004		/* Two (or more) usb_hcds share HW */
24927729aadSEric Lescouet #define	HCD_USB11	0x0010		/* USB 1.1 */
25027729aadSEric Lescouet #define	HCD_USB2	0x0020		/* USB 2.0 */
25127729aadSEric Lescouet #define	HCD_USB3	0x0040		/* USB 3.0 */
25271175225SMathias Nyman #define	HCD_USB31	0x0050		/* USB 3.1 */
253ffe95371SMathias Nyman #define	HCD_USB32	0x0060		/* USB 3.2 */
25427729aadSEric Lescouet #define	HCD_MASK	0x0070
25594dfd7edSMing Lei #define	HCD_BH		0x0100		/* URB complete in BH context */
25627729aadSEric Lescouet 
25727729aadSEric Lescouet 	/* called to init HCD and root hub */
25827729aadSEric Lescouet 	int	(*reset) (struct usb_hcd *hcd);
25927729aadSEric Lescouet 	int	(*start) (struct usb_hcd *hcd);
26027729aadSEric Lescouet 
26127729aadSEric Lescouet 	/* NOTE:  these suspend/resume calls relate to the HC as
26227729aadSEric Lescouet 	 * a whole, not just the root hub; they're for PCI bus glue.
26327729aadSEric Lescouet 	 */
26427729aadSEric Lescouet 	/* called after suspending the hub, before entering D3 etc */
2654147200dSAlan Stern 	int	(*pci_suspend)(struct usb_hcd *hcd, bool do_wakeup);
26627729aadSEric Lescouet 
26727729aadSEric Lescouet 	/* called after entering D0 (etc), before resuming the hub */
2681f7d5520SBasavaraj Natikar 	int	(*pci_resume)(struct usb_hcd *hcd, pm_message_t state);
26927729aadSEric Lescouet 
270c3bbacd6SMathias Nyman 	/* called just before hibernate final D3 state, allows host to poweroff parts */
271c3bbacd6SMathias Nyman 	int	(*pci_poweroff_late)(struct usb_hcd *hcd, bool do_wakeup);
272c3bbacd6SMathias Nyman 
27327729aadSEric Lescouet 	/* cleanly make HCD stop writing memory and doing I/O */
27427729aadSEric Lescouet 	void	(*stop) (struct usb_hcd *hcd);
27527729aadSEric Lescouet 
27627729aadSEric Lescouet 	/* shutdown HCD */
27727729aadSEric Lescouet 	void	(*shutdown) (struct usb_hcd *hcd);
27827729aadSEric Lescouet 
27927729aadSEric Lescouet 	/* return current frame number */
28027729aadSEric Lescouet 	int	(*get_frame_number) (struct usb_hcd *hcd);
28127729aadSEric Lescouet 
28227729aadSEric Lescouet 	/* manage i/o requests, device state */
28327729aadSEric Lescouet 	int	(*urb_enqueue)(struct usb_hcd *hcd,
28427729aadSEric Lescouet 				struct urb *urb, gfp_t mem_flags);
28527729aadSEric Lescouet 	int	(*urb_dequeue)(struct usb_hcd *hcd,
28627729aadSEric Lescouet 				struct urb *urb, int status);
28727729aadSEric Lescouet 
2882694a48dSRobert Morell 	/*
2892694a48dSRobert Morell 	 * (optional) these hooks allow an HCD to override the default DMA
2902694a48dSRobert Morell 	 * mapping and unmapping routines.  In general, they shouldn't be
2912694a48dSRobert Morell 	 * necessary unless the host controller has special DMA requirements,
29224bb0076SZhen Lei 	 * such as alignment constraints.  If these are not specified, the
2932694a48dSRobert Morell 	 * general usb_hcd_(un)?map_urb_for_dma functions will be used instead
2942694a48dSRobert Morell 	 * (and it may be a good idea to call these functions in your HCD
2952694a48dSRobert Morell 	 * implementation)
2962694a48dSRobert Morell 	 */
2972694a48dSRobert Morell 	int	(*map_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb,
2982694a48dSRobert Morell 				   gfp_t mem_flags);
2992694a48dSRobert Morell 	void    (*unmap_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb);
3002694a48dSRobert Morell 
30127729aadSEric Lescouet 	/* hw synch, freeing endpoint resources that urb_dequeue can't */
30227729aadSEric Lescouet 	void	(*endpoint_disable)(struct usb_hcd *hcd,
30327729aadSEric Lescouet 			struct usb_host_endpoint *ep);
30427729aadSEric Lescouet 
30527729aadSEric Lescouet 	/* (optional) reset any endpoint state such as sequence number
30627729aadSEric Lescouet 	   and current window */
30727729aadSEric Lescouet 	void	(*endpoint_reset)(struct usb_hcd *hcd,
30827729aadSEric Lescouet 			struct usb_host_endpoint *ep);
30927729aadSEric Lescouet 
31027729aadSEric Lescouet 	/* root hub support */
31127729aadSEric Lescouet 	int	(*hub_status_data) (struct usb_hcd *hcd, char *buf);
31227729aadSEric Lescouet 	int	(*hub_control) (struct usb_hcd *hcd,
31327729aadSEric Lescouet 				u16 typeReq, u16 wValue, u16 wIndex,
31427729aadSEric Lescouet 				char *buf, u16 wLength);
31527729aadSEric Lescouet 	int	(*bus_suspend)(struct usb_hcd *);
31627729aadSEric Lescouet 	int	(*bus_resume)(struct usb_hcd *);
31727729aadSEric Lescouet 	int	(*start_port_reset)(struct usb_hcd *, unsigned port_num);
318379cacc5SAlan Stern 	unsigned long	(*get_resuming_ports)(struct usb_hcd *);
31927729aadSEric Lescouet 
32027729aadSEric Lescouet 		/* force handover of high-speed port to full-speed companion */
32127729aadSEric Lescouet 	void	(*relinquish_port)(struct usb_hcd *, int);
32227729aadSEric Lescouet 		/* has a port been handed over to a companion? */
32327729aadSEric Lescouet 	int	(*port_handed_over)(struct usb_hcd *, int);
32427729aadSEric Lescouet 
32527729aadSEric Lescouet 		/* CLEAR_TT_BUFFER completion callback */
32627729aadSEric Lescouet 	void	(*clear_tt_buffer_complete)(struct usb_hcd *,
32727729aadSEric Lescouet 				struct usb_host_endpoint *);
32827729aadSEric Lescouet 
32927729aadSEric Lescouet 	/* xHCI specific functions */
33027729aadSEric Lescouet 		/* Called by usb_alloc_dev to alloc HC device structures */
33127729aadSEric Lescouet 	int	(*alloc_dev)(struct usb_hcd *, struct usb_device *);
33227729aadSEric Lescouet 		/* Called by usb_disconnect to free HC device structures */
33327729aadSEric Lescouet 	void	(*free_dev)(struct usb_hcd *, struct usb_device *);
334eab1cafcSSarah Sharp 	/* Change a group of bulk endpoints to support multiple stream IDs */
335eab1cafcSSarah Sharp 	int	(*alloc_streams)(struct usb_hcd *hcd, struct usb_device *udev,
336eab1cafcSSarah Sharp 		struct usb_host_endpoint **eps, unsigned int num_eps,
337eab1cafcSSarah Sharp 		unsigned int num_streams, gfp_t mem_flags);
338eab1cafcSSarah Sharp 	/* Reverts a group of bulk endpoints back to not using stream IDs.
339eab1cafcSSarah Sharp 	 * Can fail if we run out of memory.
340eab1cafcSSarah Sharp 	 */
341eab1cafcSSarah Sharp 	int	(*free_streams)(struct usb_hcd *hcd, struct usb_device *udev,
342eab1cafcSSarah Sharp 		struct usb_host_endpoint **eps, unsigned int num_eps,
343eab1cafcSSarah Sharp 		gfp_t mem_flags);
34427729aadSEric Lescouet 
34527729aadSEric Lescouet 	/* Bandwidth computation functions */
34627729aadSEric Lescouet 	/* Note that add_endpoint() can only be called once per endpoint before
34727729aadSEric Lescouet 	 * check_bandwidth() or reset_bandwidth() must be called.
34827729aadSEric Lescouet 	 * drop_endpoint() can only be called once per endpoint also.
3490858a3a5SGreg Kroah-Hartman 	 * A call to xhci_drop_endpoint() followed by a call to
3500858a3a5SGreg Kroah-Hartman 	 * xhci_add_endpoint() will add the endpoint to the schedule with
3510858a3a5SGreg Kroah-Hartman 	 * possibly new parameters denoted by a different endpoint descriptor
3520858a3a5SGreg Kroah-Hartman 	 * in usb_host_endpoint.  A call to xhci_add_endpoint() followed by a
3530858a3a5SGreg Kroah-Hartman 	 * call to xhci_drop_endpoint() is not allowed.
35427729aadSEric Lescouet 	 */
35527729aadSEric Lescouet 		/* Allocate endpoint resources and add them to a new schedule */
3560858a3a5SGreg Kroah-Hartman 	int	(*add_endpoint)(struct usb_hcd *, struct usb_device *,
3570858a3a5SGreg Kroah-Hartman 				struct usb_host_endpoint *);
35827729aadSEric Lescouet 		/* Drop an endpoint from a new schedule */
3590858a3a5SGreg Kroah-Hartman 	int	(*drop_endpoint)(struct usb_hcd *, struct usb_device *,
3600858a3a5SGreg Kroah-Hartman 				 struct usb_host_endpoint *);
36127729aadSEric Lescouet 		/* Check that a new hardware configuration, set using
36227729aadSEric Lescouet 		 * endpoint_enable and endpoint_disable, does not exceed bus
36327729aadSEric Lescouet 		 * bandwidth.  This must be called before any set configuration
36427729aadSEric Lescouet 		 * or set interface requests are sent to the device.
36527729aadSEric Lescouet 		 */
36627729aadSEric Lescouet 	int	(*check_bandwidth)(struct usb_hcd *, struct usb_device *);
36727729aadSEric Lescouet 		/* Reset the device schedule to the last known good schedule,
36827729aadSEric Lescouet 		 * which was set from a previous successful call to
36927729aadSEric Lescouet 		 * check_bandwidth().  This reverts any add_endpoint() and
37027729aadSEric Lescouet 		 * drop_endpoint() calls since that last successful call.
37127729aadSEric Lescouet 		 * Used for when a check_bandwidth() call fails due to resource
37227729aadSEric Lescouet 		 * or bandwidth constraints.
37327729aadSEric Lescouet 		 */
37427729aadSEric Lescouet 	void	(*reset_bandwidth)(struct usb_hcd *, struct usb_device *);
37527729aadSEric Lescouet 		/* Set the hardware-chosen device address */
37627729aadSEric Lescouet 	int	(*address_device)(struct usb_hcd *, struct usb_device *udev,
37748fc7dbdSDan Williams 				  unsigned int timeout_ms);
37848fc7dbdSDan Williams 		/* prepares the hardware to send commands to the device */
37927729aadSEric Lescouet 	int	(*enable_device)(struct usb_hcd *, struct usb_device *udev);
38027729aadSEric Lescouet 		/* Notifies the HCD after a hub descriptor is fetched.
38127729aadSEric Lescouet 		 * Will block.
38227729aadSEric Lescouet 		 */
38327729aadSEric Lescouet 	int	(*update_hub_device)(struct usb_hcd *, struct usb_device *hdev,
38427729aadSEric Lescouet 			struct usb_tt *tt, gfp_t mem_flags);
38548f24970SAlek Du 	int	(*reset_device)(struct usb_hcd *, struct usb_device *);
38648f24970SAlek Du 		/* Notifies the HCD after a device is connected and its
38748f24970SAlek Du 		 * address is set
38848f24970SAlek Du 		 */
38965580b43SAndiry Xu 	int	(*update_device)(struct usb_hcd *, struct usb_device *);
3901ea7e0e8SSarah Sharp 	int	(*set_usb2_hw_lpm)(struct usb_hcd *, struct usb_device *, int);
3911ea7e0e8SSarah Sharp 	/* USB 3.0 Link Power Management */
3921ea7e0e8SSarah Sharp 		/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
3931ea7e0e8SSarah Sharp 	int	(*enable_usb3_lpm_timeout)(struct usb_hcd *,
3941ea7e0e8SSarah Sharp 			struct usb_device *, enum usb3_link_state state);
3951ea7e0e8SSarah Sharp 		/* The xHCI host controller can still fail the command to
3961ea7e0e8SSarah Sharp 		 * disable the LPM timeouts, so this can return an error code.
3971ea7e0e8SSarah Sharp 		 */
3981ea7e0e8SSarah Sharp 	int	(*disable_usb3_lpm_timeout)(struct usb_hcd *,
3993f5eb141SLan Tianyu 			struct usb_device *, enum usb3_link_state state);
40011a7e594SMichael Grzeschik 	int	(*find_raw_port_number)(struct usb_hcd *, int);
40111a7e594SMichael Grzeschik 	/* Call for power on/off the port if necessary */
402cbbc07e1SPeter Chen 	int	(*port_power)(struct usb_hcd *hcd, int portnum, bool enable);
403cbbc07e1SPeter Chen 	/* Call for SINGLE_STEP_SET_FEATURE Test for USB2 EH certification */
404cbbc07e1SPeter Chen #define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06
405cbbc07e1SPeter Chen 	int	(*submit_single_step_set_feature)(struct usb_hcd *,
40627729aadSEric Lescouet 			struct urb *, int);
40727729aadSEric Lescouet };
40894dfd7edSMing Lei 
hcd_giveback_urb_in_bh(struct usb_hcd * hcd)40994dfd7edSMing Lei static inline int hcd_giveback_urb_in_bh(struct usb_hcd *hcd)
41094dfd7edSMing Lei {
41194dfd7edSMing Lei 	return hcd->driver->flags & HCD_BH;
41294dfd7edSMing Lei }
413c7ccde6eSAlan Stern 
hcd_periodic_completion_in_progress(struct usb_hcd * hcd,struct usb_host_endpoint * ep)414c7ccde6eSAlan Stern static inline bool hcd_periodic_completion_in_progress(struct usb_hcd *hcd,
415c7ccde6eSAlan Stern 		struct usb_host_endpoint *ep)
416c7ccde6eSAlan Stern {
417c7ccde6eSAlan Stern 	return hcd->high_prio_bh.completing_ep == ep;
418c7ccde6eSAlan Stern }
4197b81cb6bSChristoph Hellwig 
hcd_uses_dma(struct usb_hcd * hcd)4207b81cb6bSChristoph Hellwig static inline bool hcd_uses_dma(struct usb_hcd *hcd)
4217b81cb6bSChristoph Hellwig {
4227b81cb6bSChristoph Hellwig 	return IS_ENABLED(CONFIG_HAS_DMA) && (hcd->driver->flags & HCD_DMA);
423edfbcb32SChristoph Hellwig }
42427729aadSEric Lescouet 
42527729aadSEric Lescouet extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb);
42627729aadSEric Lescouet extern int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
42727729aadSEric Lescouet 		int status);
42827729aadSEric Lescouet extern void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb);
42927729aadSEric Lescouet 
43027729aadSEric Lescouet extern int usb_hcd_submit_urb(struct urb *urb, gfp_t mem_flags);
43127729aadSEric Lescouet extern int usb_hcd_unlink_urb(struct urb *urb, int status);
43227729aadSEric Lescouet extern void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb,
4332694a48dSRobert Morell 		int status);
4342694a48dSRobert Morell extern int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
435c8cf203aSRobert Morell 		gfp_t mem_flags);
436c8cf203aSRobert Morell extern void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *, struct urb *);
43727729aadSEric Lescouet extern void usb_hcd_unmap_urb_for_dma(struct usb_hcd *, struct urb *);
43827729aadSEric Lescouet extern void usb_hcd_flush_endpoint(struct usb_device *udev,
43927729aadSEric Lescouet 		struct usb_host_endpoint *ep);
44027729aadSEric Lescouet extern void usb_hcd_disable_endpoint(struct usb_device *udev,
44127729aadSEric Lescouet 		struct usb_host_endpoint *ep);
44227729aadSEric Lescouet extern void usb_hcd_reset_endpoint(struct usb_device *udev,
44327729aadSEric Lescouet 		struct usb_host_endpoint *ep);
44427729aadSEric Lescouet extern void usb_hcd_synchronize_unlinks(struct usb_device *udev);
44527729aadSEric Lescouet extern int usb_hcd_alloc_bandwidth(struct usb_device *udev,
44627729aadSEric Lescouet 		struct usb_host_config *new_config,
44727729aadSEric Lescouet 		struct usb_host_interface *old_alt,
44827729aadSEric Lescouet 		struct usb_host_interface *new_alt);
44927729aadSEric Lescouet extern int usb_hcd_get_frame_number(struct usb_device *udev);
450a8c06e40SArnd Bergmann 
451a8c06e40SArnd Bergmann struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver,
452a8c06e40SArnd Bergmann 		struct device *sysdev, struct device *dev, const char *bus_name,
45327729aadSEric Lescouet 		struct usb_hcd *primary_hcd);
45427729aadSEric Lescouet extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
455c5635437SSarah Sharp 		struct device *dev, const char *bus_name);
456c5635437SSarah Sharp extern struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
457c5635437SSarah Sharp 		struct device *dev, const char *bus_name,
45827729aadSEric Lescouet 		struct usb_hcd *shared_hcd);
45927729aadSEric Lescouet extern struct usb_hcd *usb_get_hcd(struct usb_hcd *hcd);
460c5635437SSarah Sharp extern void usb_put_hcd(struct usb_hcd *hcd);
46127729aadSEric Lescouet extern int usb_hcd_is_primary_hcd(struct usb_hcd *hcd);
46227729aadSEric Lescouet extern int usb_add_hcd(struct usb_hcd *hcd,
46327729aadSEric Lescouet 		unsigned int irqnum, unsigned long irqflags);
4643f5eb141SLan Tianyu extern void usb_remove_hcd(struct usb_hcd *hcd);
465b0310c2fSLaurentiu Tudor extern int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1);
466b0310c2fSLaurentiu Tudor int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr,
46727729aadSEric Lescouet 			    dma_addr_t dma, size_t size);
46827729aadSEric Lescouet 
46927729aadSEric Lescouet struct platform_device;
470cbbc07e1SPeter Chen extern void usb_hcd_platform_shutdown(struct platform_device *dev);
471cbbc07e1SPeter Chen #ifdef CONFIG_USB_HCD_TEST_MODE
472cbbc07e1SPeter Chen extern int ehset_single_step_set_feature(struct usb_hcd *hcd, int port);
473cbbc07e1SPeter Chen #else
ehset_single_step_set_feature(struct usb_hcd * hcd,int port)474cbbc07e1SPeter Chen static inline int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
475cbbc07e1SPeter Chen {
476cbbc07e1SPeter Chen 	return 0;
477cbbc07e1SPeter Chen }
47827729aadSEric Lescouet #endif /* CONFIG_USB_HCD_TEST_MODE */
4792c93e790Syuan linyu 
48027729aadSEric Lescouet #ifdef CONFIG_USB_PCI
48127729aadSEric Lescouet struct pci_dev;
48227729aadSEric Lescouet struct pci_device_id;
483ff4c65caSVinod Koul extern int usb_hcd_pci_probe(struct pci_dev *dev,
48427729aadSEric Lescouet 			     const struct hc_driver *driver);
48527729aadSEric Lescouet extern void usb_hcd_pci_remove(struct pci_dev *dev);
48627729aadSEric Lescouet extern void usb_hcd_pci_shutdown(struct pci_dev *dev);
48752e24f8cSNiklas Schnelle 
4887868943dSHuang Rui #ifdef CONFIG_USB_PCI_AMD
4897868943dSHuang Rui extern int usb_hcd_amd_remote_wakeup_quirk(struct pci_dev *dev);
49052e24f8cSNiklas Schnelle 
usb_hcd_amd_resume_bug(struct pci_dev * dev,const struct hc_driver * driver)49152e24f8cSNiklas Schnelle static inline bool usb_hcd_amd_resume_bug(struct pci_dev *dev,
49252e24f8cSNiklas Schnelle 					  const struct hc_driver *driver)
49352e24f8cSNiklas Schnelle {
49452e24f8cSNiklas Schnelle 	if (!usb_hcd_amd_remote_wakeup_quirk(dev))
49552e24f8cSNiklas Schnelle 		return false;
49652e24f8cSNiklas Schnelle 	if (driver->flags & (HCD_USB11 | HCD_USB3))
49752e24f8cSNiklas Schnelle 		return true;
49852e24f8cSNiklas Schnelle 	return false;
49952e24f8cSNiklas Schnelle }
50052e24f8cSNiklas Schnelle #else /* CONFIG_USB_PCI_AMD */
usb_hcd_amd_resume_bug(struct pci_dev * dev,const struct hc_driver * driver)50152e24f8cSNiklas Schnelle static inline bool usb_hcd_amd_resume_bug(struct pci_dev *dev,
50252e24f8cSNiklas Schnelle 					  const struct hc_driver *driver)
50352e24f8cSNiklas Schnelle {
50452e24f8cSNiklas Schnelle 	return false;
50552e24f8cSNiklas Schnelle }
50627729aadSEric Lescouet #endif
5072c93e790Syuan linyu extern const struct dev_pm_ops usb_hcd_pci_pm_ops;
50827729aadSEric Lescouet #endif /* CONFIG_USB_PCI */
50927729aadSEric Lescouet 
5105efd2ea8SSebastian Andrzej Siewior /* pci-ish (pdev null is ok) buffer alloc/mapping support */
51127729aadSEric Lescouet void usb_init_pool_max(void);
51227729aadSEric Lescouet int hcd_buffer_create(struct usb_hcd *hcd);
51327729aadSEric Lescouet void hcd_buffer_destroy(struct usb_hcd *hcd);
51427729aadSEric Lescouet 
51527729aadSEric Lescouet void *hcd_buffer_alloc(struct usb_bus *bus, size_t size,
51627729aadSEric Lescouet 	gfp_t mem_flags, dma_addr_t *dma);
51727729aadSEric Lescouet void hcd_buffer_free(struct usb_bus *bus, size_t size,
51827729aadSEric Lescouet 	void *addr, dma_addr_t dma);
5190143d148SRuihan Li 
5200143d148SRuihan Li void *hcd_buffer_alloc_pages(struct usb_hcd *hcd,
5210143d148SRuihan Li 		size_t size, gfp_t mem_flags, dma_addr_t *dma);
5220143d148SRuihan Li void hcd_buffer_free_pages(struct usb_hcd *hcd,
5230143d148SRuihan Li 		size_t size, void *addr, dma_addr_t dma);
52427729aadSEric Lescouet 
52527729aadSEric Lescouet /* generic bus glue, needed for host controllers that don't use PCI */
52627729aadSEric Lescouet extern irqreturn_t usb_hcd_irq(int irq, void *__hcd);
52727729aadSEric Lescouet 
52827729aadSEric Lescouet extern void usb_hc_died(struct usb_hcd *hcd);
5294ee823b8SSarah Sharp extern void usb_hcd_poll_rh_status(struct usb_hcd *hcd);
5304ee823b8SSarah Sharp extern void usb_wakeup_notification(struct usb_device *hdev,
53127729aadSEric Lescouet 		unsigned int portnum);
532da0aa716SAlan Stern 
533da0aa716SAlan Stern extern void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum);
534da0aa716SAlan Stern extern void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum);
53527729aadSEric Lescouet 
53627729aadSEric Lescouet /* The D0/D1 toggle bits ... USE WITH CAUTION (they're almost hcd-internal) */
53727729aadSEric Lescouet #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> (ep)) & 1)
53827729aadSEric Lescouet #define	usb_dotoggle(dev, ep, out)  ((dev)->toggle[out] ^= (1 << (ep)))
53927729aadSEric Lescouet #define usb_settoggle(dev, ep, out, bit) \
54027729aadSEric Lescouet 		((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ep))) | \
54127729aadSEric Lescouet 		 ((bit) << (ep)))
54227729aadSEric Lescouet 
54327729aadSEric Lescouet /* -------------------------------------------------------------------------- */
54427729aadSEric Lescouet 
54527729aadSEric Lescouet /* Enumeration is only for the hub driver, or HCD virtual root hubs */
54627729aadSEric Lescouet extern struct usb_device *usb_alloc_dev(struct usb_device *parent,
54727729aadSEric Lescouet 					struct usb_bus *, unsigned port);
54827729aadSEric Lescouet extern int usb_new_device(struct usb_device *dev);
54927729aadSEric Lescouet extern void usb_disconnect(struct usb_device **);
55027729aadSEric Lescouet 
55127729aadSEric Lescouet extern int usb_get_configuration(struct usb_device *dev);
55227729aadSEric Lescouet extern void usb_destroy_configuration(struct usb_device *dev);
55327729aadSEric Lescouet 
55427729aadSEric Lescouet /*-------------------------------------------------------------------------*/
55527729aadSEric Lescouet 
55627729aadSEric Lescouet /*
55727729aadSEric Lescouet  * HCD Root Hub support
55827729aadSEric Lescouet  */
559d20db4b4SEric Lescouet 
560d20db4b4SEric Lescouet #include <linux/usb/ch11.h>
561d20db4b4SEric Lescouet 
562d20db4b4SEric Lescouet /*
563d20db4b4SEric Lescouet  * As of USB 2.0, full/low speed devices are segregated into trees.
564d20db4b4SEric Lescouet  * One type grows from USB 1.1 host controllers (OHCI, UHCI etc).
565d20db4b4SEric Lescouet  * The other type grows from high speed hubs when they connect to
566d20db4b4SEric Lescouet  * full/low speed devices using "Transaction Translators" (TTs).
567d20db4b4SEric Lescouet  *
568d20db4b4SEric Lescouet  * TTs should only be known to the hub driver, and high speed bus
569d20db4b4SEric Lescouet  * drivers (only EHCI for now).  They affect periodic scheduling and
570d20db4b4SEric Lescouet  * sometimes control/bulk error recovery.
571d20db4b4SEric Lescouet  */
572d20db4b4SEric Lescouet 
573d20db4b4SEric Lescouet struct usb_device;
574d20db4b4SEric Lescouet 
575d20db4b4SEric Lescouet struct usb_tt {
576d20db4b4SEric Lescouet 	struct usb_device	*hub;	/* upstream highspeed hub */
577d20db4b4SEric Lescouet 	int			multi;	/* true means one TT per port */
5787c4bb942SAlan Stern 	unsigned		think_time;	/* think time in ns */
579d20db4b4SEric Lescouet 	void			*hcpriv;	/* HCD private data */
580d20db4b4SEric Lescouet 
581d20db4b4SEric Lescouet 	/* for control/bulk error recovery (CLEAR_TT_BUFFER) */
582d20db4b4SEric Lescouet 	spinlock_t		lock;
583d20db4b4SEric Lescouet 	struct list_head	clear_list;	/* of usb_tt_clear */
584d20db4b4SEric Lescouet 	struct work_struct	clear_work;
585d20db4b4SEric Lescouet };
586d20db4b4SEric Lescouet 
587d20db4b4SEric Lescouet struct usb_tt_clear {
588d20db4b4SEric Lescouet 	struct list_head	clear_list;
589d20db4b4SEric Lescouet 	unsigned		tt;
590d20db4b4SEric Lescouet 	u16			devinfo;
591d20db4b4SEric Lescouet 	struct usb_hcd		*hcd;
592d20db4b4SEric Lescouet 	struct usb_host_endpoint	*ep;
593d20db4b4SEric Lescouet };
594d20db4b4SEric Lescouet 
595d20db4b4SEric Lescouet extern int usb_hub_clear_tt_buffer(struct urb *urb);
59627729aadSEric Lescouet extern void usb_ep0_reinit(struct usb_device *);
59727729aadSEric Lescouet 
59827729aadSEric Lescouet /* (shifted) direction/type/recipient from the USB 2.0 spec, table 9.2 */
59927729aadSEric Lescouet #define DeviceRequest \
60027729aadSEric Lescouet 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
60127729aadSEric Lescouet #define DeviceOutRequest \
60227729aadSEric Lescouet 	((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
60327729aadSEric Lescouet 
60427729aadSEric Lescouet #define InterfaceRequest \
60527729aadSEric Lescouet 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
60627729aadSEric Lescouet 
6077cf916bdSBenjamin Herrenschmidt #define EndpointRequest \
60827729aadSEric Lescouet 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
6097cf916bdSBenjamin Herrenschmidt #define EndpointOutRequest \
61027729aadSEric Lescouet 	((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
61127729aadSEric Lescouet 
612b9c2a2a3STal Shorer /* class requests from the USB 2.0 hub spec, table 11-15 */
61327729aadSEric Lescouet #define HUB_CLASS_REQ(dir, type, request) ((((dir) | (type)) << 8) | (request))
614b9c2a2a3STal Shorer /* GetBusState and SetHubDescriptor are optional, omitted */
615b9c2a2a3STal Shorer #define ClearHubFeature		HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_HUB, USB_REQ_CLEAR_FEATURE)
616b9c2a2a3STal Shorer #define ClearPortFeature	HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, USB_REQ_CLEAR_FEATURE)
617b9c2a2a3STal Shorer #define GetHubDescriptor	HUB_CLASS_REQ(USB_DIR_IN, USB_RT_HUB, USB_REQ_GET_DESCRIPTOR)
618b9c2a2a3STal Shorer #define GetHubStatus		HUB_CLASS_REQ(USB_DIR_IN, USB_RT_HUB, USB_REQ_GET_STATUS)
619b9c2a2a3STal Shorer #define GetPortStatus		HUB_CLASS_REQ(USB_DIR_IN, USB_RT_PORT, USB_REQ_GET_STATUS)
620b9c2a2a3STal Shorer #define SetHubFeature		HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_HUB, USB_REQ_SET_FEATURE)
62122a5d3ceSBenjamin Herrenschmidt #define SetPortFeature		HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, USB_REQ_SET_FEATURE)
62222a5d3ceSBenjamin Herrenschmidt #define ClearTTBuffer		HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, HUB_CLEAR_TT_BUFFER)
62322a5d3ceSBenjamin Herrenschmidt #define ResetTT			HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, HUB_RESET_TT)
62422a5d3ceSBenjamin Herrenschmidt #define GetTTState		HUB_CLASS_REQ(USB_DIR_IN, USB_RT_PORT, HUB_GET_TT_STATE)
62527729aadSEric Lescouet #define StopTT			HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_PORT, HUB_STOP_TT)
62627729aadSEric Lescouet 
62727729aadSEric Lescouet 
62827729aadSEric Lescouet /*-------------------------------------------------------------------------*/
6297e34d70aSTal Shorer 
630b9c2a2a3STal Shorer /* class requests from USB 3.1 hub spec, table 10-7 */
631b9c2a2a3STal Shorer #define SetHubDepth		HUB_CLASS_REQ(USB_DIR_OUT, USB_RT_HUB, HUB_SET_DEPTH)
6320eadcc09STatyana Brokhman #define GetPortErrorCount	HUB_CLASS_REQ(USB_DIR_IN, USB_RT_PORT, HUB_GET_PORT_ERR_COUNT)
63327729aadSEric Lescouet 
63427729aadSEric Lescouet /*
63527729aadSEric Lescouet  * Generic bandwidth allocation constants/support
63627729aadSEric Lescouet  */
63727729aadSEric Lescouet #define FRAME_TIME_USECS	1000L
63827729aadSEric Lescouet #define BitTime(bytecount) (7 * 8 * bytecount / 6) /* with integer truncation */
63927729aadSEric Lescouet 		/* Trying not to use worst-case bit-stuffing
64027729aadSEric Lescouet 		 * of (7/6 * 8 * bytecount) = 9.33 * bytecount */
64127729aadSEric Lescouet 		/* bytecount = data payload byte count */
64263fb3a28SAlan Stern 
64363fb3a28SAlan Stern #define NS_TO_US(ns)	DIV_ROUND_UP(ns, 1000L)
64427729aadSEric Lescouet 			/* convert nanoseconds to microseconds, rounding up */
64527729aadSEric Lescouet 
64627729aadSEric Lescouet /*
64727729aadSEric Lescouet  * Full/low speed bandwidth allocation constants/support.
64827729aadSEric Lescouet  */
64927729aadSEric Lescouet #define BW_HOST_DELAY	1000L		/* nanoseconds */
65027729aadSEric Lescouet #define BW_HUB_LS_SETUP	333L		/* nanoseconds */
65127729aadSEric Lescouet 			/* 4 full-speed bit times (est.) */
65227729aadSEric Lescouet 
65327729aadSEric Lescouet #define FRAME_TIME_BITS			12000L	/* frame = 1 millisecond */
65427729aadSEric Lescouet #define FRAME_TIME_MAX_BITS_ALLOC	(90L * FRAME_TIME_BITS / 100L)
65527729aadSEric Lescouet #define FRAME_TIME_MAX_USECS_ALLOC	(90L * FRAME_TIME_USECS / 100L)
65627729aadSEric Lescouet 
65727729aadSEric Lescouet /*
65827729aadSEric Lescouet  * Ceiling [nano/micro]seconds (typical) for that many bytes at high speed
65927729aadSEric Lescouet  * ISO is a bit less, no ACK ... from USB 2.0 spec, 5.11.3 (and needed
66027729aadSEric Lescouet  * to preallocate bandwidth)
66127729aadSEric Lescouet  */
66227729aadSEric Lescouet #define USB2_HOST_DELAY	5	/* nsec, guess */
66327729aadSEric Lescouet #define HS_NSECS(bytes) (((55 * 8 * 2083) \
66427729aadSEric Lescouet 	+ (2083UL * (3 + BitTime(bytes))))/1000 \
66527729aadSEric Lescouet 	+ USB2_HOST_DELAY)
66627729aadSEric Lescouet #define HS_NSECS_ISO(bytes) (((38 * 8 * 2083) \
66727729aadSEric Lescouet 	+ (2083UL * (3 + BitTime(bytes))))/1000 \
66827729aadSEric Lescouet 	+ USB2_HOST_DELAY)
66927729aadSEric Lescouet #define HS_USECS(bytes)		NS_TO_US(HS_NSECS(bytes))
67027729aadSEric Lescouet #define HS_USECS_ISO(bytes)	NS_TO_US(HS_NSECS_ISO(bytes))
67127729aadSEric Lescouet 
67227729aadSEric Lescouet extern long usb_calc_bus_time(int speed, int is_input,
67327729aadSEric Lescouet 			int isoc, int bytecount);
67427729aadSEric Lescouet 
67527729aadSEric Lescouet /*-------------------------------------------------------------------------*/
67627729aadSEric Lescouet 
67727729aadSEric Lescouet extern void usb_set_device_state(struct usb_device *udev,
67827729aadSEric Lescouet 		enum usb_device_state new_state);
67927729aadSEric Lescouet 
68027729aadSEric Lescouet /*-------------------------------------------------------------------------*/
68127729aadSEric Lescouet 
68227729aadSEric Lescouet /* exported only within usbcore */
6835363de75SHeiner Kallweit 
684a4b5d606SHeiner Kallweit extern struct idr usb_bus_idr;
68527729aadSEric Lescouet extern struct mutex usb_bus_idr_lock;
68627729aadSEric Lescouet extern wait_queue_head_t usb_kill_urb_queue;
68727729aadSEric Lescouet 
68827729aadSEric Lescouet 
68927729aadSEric Lescouet #define usb_endpoint_out(ep_dir)	(!((ep_dir) & USB_DIR_IN))
69027729aadSEric Lescouet 
6917a6127e3SDouglas Anderson #ifdef CONFIG_PM
69227729aadSEric Lescouet extern unsigned usb_wakeup_enabled_descendants(struct usb_device *udev);
69327729aadSEric Lescouet extern void usb_root_hub_lost_power(struct usb_device *rhdev);
69427729aadSEric Lescouet extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg);
69527729aadSEric Lescouet extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg);
69627729aadSEric Lescouet extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd);
6977a6127e3SDouglas Anderson #else
usb_wakeup_enabled_descendants(struct usb_device * udev)6987a6127e3SDouglas Anderson static inline unsigned usb_wakeup_enabled_descendants(struct usb_device *udev)
6997a6127e3SDouglas Anderson {
7007a6127e3SDouglas Anderson 	return 0;
70127729aadSEric Lescouet }
usb_hcd_resume_root_hub(struct usb_hcd * hcd)70227729aadSEric Lescouet static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd)
70327729aadSEric Lescouet {
70427729aadSEric Lescouet 	return;
705ceb6c9c8SRafael J. Wysocki }
70627729aadSEric Lescouet #endif /* CONFIG_PM */
70727729aadSEric Lescouet 
70827729aadSEric Lescouet /*-------------------------------------------------------------------------*/
70927729aadSEric Lescouet 
71027729aadSEric Lescouet #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
71127729aadSEric Lescouet 
71227729aadSEric Lescouet struct usb_mon_operations {
71327729aadSEric Lescouet 	void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
71427729aadSEric Lescouet 	void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
71527729aadSEric Lescouet 	void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
71627729aadSEric Lescouet 	/* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
71727729aadSEric Lescouet };
7186fb8ac81SJulia Lawall 
71927729aadSEric Lescouet extern const struct usb_mon_operations *mon_ops;
72027729aadSEric Lescouet 
usbmon_urb_submit(struct usb_bus * bus,struct urb * urb)72127729aadSEric Lescouet static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
72227729aadSEric Lescouet {
72327729aadSEric Lescouet 	if (bus->monitored)
72427729aadSEric Lescouet 		(*mon_ops->urb_submit)(bus, urb);
72527729aadSEric Lescouet }
72627729aadSEric Lescouet 
usbmon_urb_submit_error(struct usb_bus * bus,struct urb * urb,int error)72727729aadSEric Lescouet static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
72827729aadSEric Lescouet     int error)
72927729aadSEric Lescouet {
73027729aadSEric Lescouet 	if (bus->monitored)
73127729aadSEric Lescouet 		(*mon_ops->urb_submit_error)(bus, urb, error);
73227729aadSEric Lescouet }
73327729aadSEric Lescouet 
usbmon_urb_complete(struct usb_bus * bus,struct urb * urb,int status)73427729aadSEric Lescouet static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
73527729aadSEric Lescouet 		int status)
73627729aadSEric Lescouet {
73727729aadSEric Lescouet 	if (bus->monitored)
73827729aadSEric Lescouet 		(*mon_ops->urb_complete)(bus, urb, status);
73927729aadSEric Lescouet }
7406fb8ac81SJulia Lawall 
74127729aadSEric Lescouet int usb_mon_register(const struct usb_mon_operations *ops);
74227729aadSEric Lescouet void usb_mon_deregister(void);
74327729aadSEric Lescouet 
74427729aadSEric Lescouet #else
74527729aadSEric Lescouet 
usbmon_urb_submit(struct usb_bus * bus,struct urb * urb)74627729aadSEric Lescouet static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb) {}
usbmon_urb_submit_error(struct usb_bus * bus,struct urb * urb,int error)74727729aadSEric Lescouet static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
74827729aadSEric Lescouet     int error) {}
usbmon_urb_complete(struct usb_bus * bus,struct urb * urb,int status)74927729aadSEric Lescouet static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
75027729aadSEric Lescouet 		int status) {}
75127729aadSEric Lescouet 
75227729aadSEric Lescouet #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */
75327729aadSEric Lescouet 
75427729aadSEric Lescouet /*-------------------------------------------------------------------------*/
75527729aadSEric Lescouet 
75627729aadSEric Lescouet /* random stuff */
75727729aadSEric Lescouet 
75827729aadSEric Lescouet /* This rwsem is for use only by the hub driver and ehci-hcd.
75927729aadSEric Lescouet  * Nobody else should touch it.
76027729aadSEric Lescouet  */
76127729aadSEric Lescouet extern struct rw_semaphore ehci_cf_port_reset_rwsem;
76227729aadSEric Lescouet 
76327729aadSEric Lescouet /* Keep track of which host controller drivers are loaded */
76427729aadSEric Lescouet #define USB_UHCI_LOADED		0
76527729aadSEric Lescouet #define USB_OHCI_LOADED		1
76627729aadSEric Lescouet #define USB_EHCI_LOADED		2
76727729aadSEric Lescouet extern unsigned long usb_hcds_loaded;
76827729aadSEric Lescouet 
76927729aadSEric Lescouet #endif /* __KERNEL__ */
77027729aadSEric Lescouet 
771 #endif /* __USB_CORE_HCD_H */
772