1aa1f3bb5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3f30c2269SUwe Zeisberger * drivers/usb/core/usb.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * (C) Copyright Linus Torvalds 1999
61da177e4SLinus Torvalds * (C) Copyright Johannes Erdfelt 1999-2001
71da177e4SLinus Torvalds * (C) Copyright Andreas Gal 1999
81da177e4SLinus Torvalds * (C) Copyright Gregory P. Smith 1999
91da177e4SLinus Torvalds * (C) Copyright Deti Fliegl 1999 (new USB architecture)
101da177e4SLinus Torvalds * (C) Copyright Randy Dunlap 2000
111da177e4SLinus Torvalds * (C) Copyright David Brownell 2000-2004
121da177e4SLinus Torvalds * (C) Copyright Yggdrasil Computing, Inc. 2000
131da177e4SLinus Torvalds * (usb_device_id matching changes by Adam J. Richter)
141da177e4SLinus Torvalds * (C) Copyright Greg Kroah-Hartman 2002-2003
151da177e4SLinus Torvalds *
16b65fba3dSGreg Kroah-Hartman * Released under the GPLv2 only.
17b65fba3dSGreg Kroah-Hartman *
181da177e4SLinus Torvalds * NOTE! This is not actually a driver at all, rather this is
191da177e4SLinus Torvalds * just a collection of helper routines that implement the
201da177e4SLinus Torvalds * generic USB things that the real drivers can use..
211da177e4SLinus Torvalds *
22b9b70170SGreg Kroah-Hartman * Think of this as a "USB library" rather than anything else,
23b9b70170SGreg Kroah-Hartman * with no callbacks. Callbacks are evil.
241da177e4SLinus Torvalds */
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds #include <linux/module.h>
27b5e795f8SAlan Stern #include <linux/moduleparam.h>
28*484468fbSRob Herring #include <linux/of.h>
291da177e4SLinus Torvalds #include <linux/string.h>
301da177e4SLinus Torvalds #include <linux/bitops.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/kmod.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/spinlock.h>
351da177e4SLinus Torvalds #include <linux/errno.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
3727729aadSEric Lescouet #include <linux/usb/hcd.h>
384186ecf8SArjan van de Ven #include <linux/mutex.h>
39bd859281SAlan Stern #include <linux/workqueue.h>
4000048b8bSGreg Kroah-Hartman #include <linux/debugfs.h>
4169bec725SPeter Chen #include <linux/usb/of.h>
421da177e4SLinus Torvalds
431da177e4SLinus Torvalds #include <asm/io.h>
4487ae9afdSAdrian Bunk #include <linux/scatterlist.h>
451da177e4SLinus Torvalds #include <linux/mm.h>
461da177e4SLinus Torvalds #include <linux/dma-mapping.h>
471da177e4SLinus Torvalds
487bae0432SDmitry Torokhov #include "hub.h"
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds const char *usbcore_name = "usbcore";
511da177e4SLinus Torvalds
5290ab5ee9SRusty Russell static bool nousb; /* Disable USB when built into kernel image */
531da177e4SLinus Torvalds
54bb3247a3SViresh Kumar module_param(nousb, bool, 0444);
55bb3247a3SViresh Kumar
56bb3247a3SViresh Kumar /*
57bb3247a3SViresh Kumar * for external read access to <nousb>
58bb3247a3SViresh Kumar */
usb_disabled(void)59bb3247a3SViresh Kumar int usb_disabled(void)
60bb3247a3SViresh Kumar {
61bb3247a3SViresh Kumar return nousb;
62bb3247a3SViresh Kumar }
63bb3247a3SViresh Kumar EXPORT_SYMBOL_GPL(usb_disabled);
64bb3247a3SViresh Kumar
65ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM
665d5d44deSMans Rullgard /* Default delay value, in seconds */
675d5d44deSMans Rullgard static int usb_autosuspend_delay = CONFIG_USB_AUTOSUSPEND_DELAY;
68eaafbc3aSAlan Stern module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
69b5e795f8SAlan Stern MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
70b5e795f8SAlan Stern
71b5e795f8SAlan Stern #else
72b5e795f8SAlan Stern #define usb_autosuspend_delay 0
73b5e795f8SAlan Stern #endif
74b5e795f8SAlan Stern
match_endpoint(struct usb_endpoint_descriptor * epd,struct usb_endpoint_descriptor ** bulk_in,struct usb_endpoint_descriptor ** bulk_out,struct usb_endpoint_descriptor ** int_in,struct usb_endpoint_descriptor ** int_out)75279daf4eSJohan Hovold static bool match_endpoint(struct usb_endpoint_descriptor *epd,
76279daf4eSJohan Hovold struct usb_endpoint_descriptor **bulk_in,
77279daf4eSJohan Hovold struct usb_endpoint_descriptor **bulk_out,
78279daf4eSJohan Hovold struct usb_endpoint_descriptor **int_in,
79279daf4eSJohan Hovold struct usb_endpoint_descriptor **int_out)
80279daf4eSJohan Hovold {
81279daf4eSJohan Hovold switch (usb_endpoint_type(epd)) {
82279daf4eSJohan Hovold case USB_ENDPOINT_XFER_BULK:
83279daf4eSJohan Hovold if (usb_endpoint_dir_in(epd)) {
84279daf4eSJohan Hovold if (bulk_in && !*bulk_in) {
85279daf4eSJohan Hovold *bulk_in = epd;
86279daf4eSJohan Hovold break;
87279daf4eSJohan Hovold }
88279daf4eSJohan Hovold } else {
89279daf4eSJohan Hovold if (bulk_out && !*bulk_out) {
90279daf4eSJohan Hovold *bulk_out = epd;
91279daf4eSJohan Hovold break;
92279daf4eSJohan Hovold }
93279daf4eSJohan Hovold }
94279daf4eSJohan Hovold
95279daf4eSJohan Hovold return false;
96279daf4eSJohan Hovold case USB_ENDPOINT_XFER_INT:
97279daf4eSJohan Hovold if (usb_endpoint_dir_in(epd)) {
98279daf4eSJohan Hovold if (int_in && !*int_in) {
99279daf4eSJohan Hovold *int_in = epd;
100279daf4eSJohan Hovold break;
101279daf4eSJohan Hovold }
102279daf4eSJohan Hovold } else {
103279daf4eSJohan Hovold if (int_out && !*int_out) {
104279daf4eSJohan Hovold *int_out = epd;
105279daf4eSJohan Hovold break;
106279daf4eSJohan Hovold }
107279daf4eSJohan Hovold }
108279daf4eSJohan Hovold
109279daf4eSJohan Hovold return false;
110279daf4eSJohan Hovold default:
111279daf4eSJohan Hovold return false;
112279daf4eSJohan Hovold }
113279daf4eSJohan Hovold
114279daf4eSJohan Hovold return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&
115279daf4eSJohan Hovold (!int_in || *int_in) && (!int_out || *int_out);
116279daf4eSJohan Hovold }
1171da177e4SLinus Torvalds
1181da177e4SLinus Torvalds /**
11966a35939SJohan Hovold * usb_find_common_endpoints() -- look up common endpoint descriptors
12066a35939SJohan Hovold * @alt: alternate setting to search
12166a35939SJohan Hovold * @bulk_in: pointer to descriptor pointer, or NULL
12266a35939SJohan Hovold * @bulk_out: pointer to descriptor pointer, or NULL
12366a35939SJohan Hovold * @int_in: pointer to descriptor pointer, or NULL
12466a35939SJohan Hovold * @int_out: pointer to descriptor pointer, or NULL
12566a35939SJohan Hovold *
12666a35939SJohan Hovold * Search the alternate setting's endpoint descriptors for the first bulk-in,
12766a35939SJohan Hovold * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
12866a35939SJohan Hovold * provided pointers (unless they are NULL).
12966a35939SJohan Hovold *
13066a35939SJohan Hovold * If a requested endpoint is not found, the corresponding pointer is set to
13166a35939SJohan Hovold * NULL.
13266a35939SJohan Hovold *
13366a35939SJohan Hovold * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
13466a35939SJohan Hovold */
usb_find_common_endpoints(struct usb_host_interface * alt,struct usb_endpoint_descriptor ** bulk_in,struct usb_endpoint_descriptor ** bulk_out,struct usb_endpoint_descriptor ** int_in,struct usb_endpoint_descriptor ** int_out)13566a35939SJohan Hovold int usb_find_common_endpoints(struct usb_host_interface *alt,
13666a35939SJohan Hovold struct usb_endpoint_descriptor **bulk_in,
13766a35939SJohan Hovold struct usb_endpoint_descriptor **bulk_out,
13866a35939SJohan Hovold struct usb_endpoint_descriptor **int_in,
13966a35939SJohan Hovold struct usb_endpoint_descriptor **int_out)
14066a35939SJohan Hovold {
14166a35939SJohan Hovold struct usb_endpoint_descriptor *epd;
14266a35939SJohan Hovold int i;
14366a35939SJohan Hovold
14466a35939SJohan Hovold if (bulk_in)
14566a35939SJohan Hovold *bulk_in = NULL;
14666a35939SJohan Hovold if (bulk_out)
14766a35939SJohan Hovold *bulk_out = NULL;
14866a35939SJohan Hovold if (int_in)
14966a35939SJohan Hovold *int_in = NULL;
15066a35939SJohan Hovold if (int_out)
15166a35939SJohan Hovold *int_out = NULL;
15266a35939SJohan Hovold
15366a35939SJohan Hovold for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
15466a35939SJohan Hovold epd = &alt->endpoint[i].desc;
15566a35939SJohan Hovold
156279daf4eSJohan Hovold if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
15766a35939SJohan Hovold return 0;
15866a35939SJohan Hovold }
15966a35939SJohan Hovold
16066a35939SJohan Hovold return -ENXIO;
16166a35939SJohan Hovold }
16266a35939SJohan Hovold EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
16366a35939SJohan Hovold
16466a35939SJohan Hovold /**
165279daf4eSJohan Hovold * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
1662e58cafaSJohan Hovold * @alt: alternate setting to search
1672e58cafaSJohan Hovold * @bulk_in: pointer to descriptor pointer, or NULL
1682e58cafaSJohan Hovold * @bulk_out: pointer to descriptor pointer, or NULL
1692e58cafaSJohan Hovold * @int_in: pointer to descriptor pointer, or NULL
1702e58cafaSJohan Hovold * @int_out: pointer to descriptor pointer, or NULL
171279daf4eSJohan Hovold *
1722e58cafaSJohan Hovold * Search the alternate setting's endpoint descriptors for the last bulk-in,
1732e58cafaSJohan Hovold * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
1742e58cafaSJohan Hovold * provided pointers (unless they are NULL).
1752e58cafaSJohan Hovold *
1762e58cafaSJohan Hovold * If a requested endpoint is not found, the corresponding pointer is set to
1772e58cafaSJohan Hovold * NULL.
1782e58cafaSJohan Hovold *
1792e58cafaSJohan Hovold * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
180279daf4eSJohan Hovold */
usb_find_common_endpoints_reverse(struct usb_host_interface * alt,struct usb_endpoint_descriptor ** bulk_in,struct usb_endpoint_descriptor ** bulk_out,struct usb_endpoint_descriptor ** int_in,struct usb_endpoint_descriptor ** int_out)181279daf4eSJohan Hovold int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
182279daf4eSJohan Hovold struct usb_endpoint_descriptor **bulk_in,
183279daf4eSJohan Hovold struct usb_endpoint_descriptor **bulk_out,
184279daf4eSJohan Hovold struct usb_endpoint_descriptor **int_in,
185279daf4eSJohan Hovold struct usb_endpoint_descriptor **int_out)
186279daf4eSJohan Hovold {
187279daf4eSJohan Hovold struct usb_endpoint_descriptor *epd;
188279daf4eSJohan Hovold int i;
189279daf4eSJohan Hovold
190279daf4eSJohan Hovold if (bulk_in)
191279daf4eSJohan Hovold *bulk_in = NULL;
192279daf4eSJohan Hovold if (bulk_out)
193279daf4eSJohan Hovold *bulk_out = NULL;
194279daf4eSJohan Hovold if (int_in)
195279daf4eSJohan Hovold *int_in = NULL;
196279daf4eSJohan Hovold if (int_out)
197279daf4eSJohan Hovold *int_out = NULL;
198279daf4eSJohan Hovold
199279daf4eSJohan Hovold for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {
200279daf4eSJohan Hovold epd = &alt->endpoint[i].desc;
201279daf4eSJohan Hovold
202279daf4eSJohan Hovold if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
203279daf4eSJohan Hovold return 0;
204279daf4eSJohan Hovold }
205279daf4eSJohan Hovold
206279daf4eSJohan Hovold return -ENXIO;
207279daf4eSJohan Hovold }
208279daf4eSJohan Hovold EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
209279daf4eSJohan Hovold
210279daf4eSJohan Hovold /**
21113890626SAlan Stern * usb_find_endpoint() - Given an endpoint address, search for the endpoint's
21213890626SAlan Stern * usb_host_endpoint structure in an interface's current altsetting.
21313890626SAlan Stern * @intf: the interface whose current altsetting should be searched
21413890626SAlan Stern * @ep_addr: the endpoint address (number and direction) to find
21513890626SAlan Stern *
21613890626SAlan Stern * Search the altsetting's list of endpoints for one with the specified address.
21713890626SAlan Stern *
21813890626SAlan Stern * Return: Pointer to the usb_host_endpoint if found, %NULL otherwise.
21913890626SAlan Stern */
usb_find_endpoint(const struct usb_interface * intf,unsigned int ep_addr)22013890626SAlan Stern static const struct usb_host_endpoint *usb_find_endpoint(
22113890626SAlan Stern const struct usb_interface *intf, unsigned int ep_addr)
22213890626SAlan Stern {
22313890626SAlan Stern int n;
22413890626SAlan Stern const struct usb_host_endpoint *ep;
22513890626SAlan Stern
22613890626SAlan Stern n = intf->cur_altsetting->desc.bNumEndpoints;
22713890626SAlan Stern ep = intf->cur_altsetting->endpoint;
22813890626SAlan Stern for (; n > 0; (--n, ++ep)) {
22913890626SAlan Stern if (ep->desc.bEndpointAddress == ep_addr)
23013890626SAlan Stern return ep;
23113890626SAlan Stern }
23213890626SAlan Stern return NULL;
23313890626SAlan Stern }
23413890626SAlan Stern
23513890626SAlan Stern /**
23613890626SAlan Stern * usb_check_bulk_endpoints - Check whether an interface's current altsetting
23713890626SAlan Stern * contains a set of bulk endpoints with the given addresses.
23813890626SAlan Stern * @intf: the interface whose current altsetting should be searched
23913890626SAlan Stern * @ep_addrs: 0-terminated array of the endpoint addresses (number and
24013890626SAlan Stern * direction) to look for
24113890626SAlan Stern *
24213890626SAlan Stern * Search for endpoints with the specified addresses and check their types.
24313890626SAlan Stern *
24413890626SAlan Stern * Return: %true if all the endpoints are found and are bulk, %false otherwise.
24513890626SAlan Stern */
usb_check_bulk_endpoints(const struct usb_interface * intf,const u8 * ep_addrs)24613890626SAlan Stern bool usb_check_bulk_endpoints(
24713890626SAlan Stern const struct usb_interface *intf, const u8 *ep_addrs)
24813890626SAlan Stern {
24913890626SAlan Stern const struct usb_host_endpoint *ep;
25013890626SAlan Stern
25113890626SAlan Stern for (; *ep_addrs; ++ep_addrs) {
25213890626SAlan Stern ep = usb_find_endpoint(intf, *ep_addrs);
25313890626SAlan Stern if (!ep || !usb_endpoint_xfer_bulk(&ep->desc))
25413890626SAlan Stern return false;
25513890626SAlan Stern }
25613890626SAlan Stern return true;
25713890626SAlan Stern }
25813890626SAlan Stern EXPORT_SYMBOL_GPL(usb_check_bulk_endpoints);
25913890626SAlan Stern
26013890626SAlan Stern /**
26113890626SAlan Stern * usb_check_int_endpoints - Check whether an interface's current altsetting
26213890626SAlan Stern * contains a set of interrupt endpoints with the given addresses.
26313890626SAlan Stern * @intf: the interface whose current altsetting should be searched
26413890626SAlan Stern * @ep_addrs: 0-terminated array of the endpoint addresses (number and
26513890626SAlan Stern * direction) to look for
26613890626SAlan Stern *
26713890626SAlan Stern * Search for endpoints with the specified addresses and check their types.
26813890626SAlan Stern *
26913890626SAlan Stern * Return: %true if all the endpoints are found and are interrupt,
27013890626SAlan Stern * %false otherwise.
27113890626SAlan Stern */
usb_check_int_endpoints(const struct usb_interface * intf,const u8 * ep_addrs)27213890626SAlan Stern bool usb_check_int_endpoints(
27313890626SAlan Stern const struct usb_interface *intf, const u8 *ep_addrs)
27413890626SAlan Stern {
27513890626SAlan Stern const struct usb_host_endpoint *ep;
27613890626SAlan Stern
27713890626SAlan Stern for (; *ep_addrs; ++ep_addrs) {
27813890626SAlan Stern ep = usb_find_endpoint(intf, *ep_addrs);
27913890626SAlan Stern if (!ep || !usb_endpoint_xfer_int(&ep->desc))
28013890626SAlan Stern return false;
28113890626SAlan Stern }
28213890626SAlan Stern return true;
28313890626SAlan Stern }
28413890626SAlan Stern EXPORT_SYMBOL_GPL(usb_check_int_endpoints);
28513890626SAlan Stern
28613890626SAlan Stern /**
28791017f9cSSarah Sharp * usb_find_alt_setting() - Given a configuration, find the alternate setting
28891017f9cSSarah Sharp * for the given interface.
28970445ae6SRandy Dunlap * @config: the configuration to search (not necessarily the current config).
29070445ae6SRandy Dunlap * @iface_num: interface number to search in
29170445ae6SRandy Dunlap * @alt_num: alternate interface setting number to search for.
29291017f9cSSarah Sharp *
29391017f9cSSarah Sharp * Search the configuration's interface cache for the given alt setting.
294626f090cSYacine Belkadi *
295626f090cSYacine Belkadi * Return: The alternate setting, if found. %NULL otherwise.
29691017f9cSSarah Sharp */
usb_find_alt_setting(struct usb_host_config * config,unsigned int iface_num,unsigned int alt_num)29791017f9cSSarah Sharp struct usb_host_interface *usb_find_alt_setting(
29891017f9cSSarah Sharp struct usb_host_config *config,
29991017f9cSSarah Sharp unsigned int iface_num,
30091017f9cSSarah Sharp unsigned int alt_num)
30191017f9cSSarah Sharp {
30291017f9cSSarah Sharp struct usb_interface_cache *intf_cache = NULL;
30391017f9cSSarah Sharp int i;
30491017f9cSSarah Sharp
305c9a4cb20SAlan Stern if (!config)
306c9a4cb20SAlan Stern return NULL;
30791017f9cSSarah Sharp for (i = 0; i < config->desc.bNumInterfaces; i++) {
30891017f9cSSarah Sharp if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
30991017f9cSSarah Sharp == iface_num) {
31091017f9cSSarah Sharp intf_cache = config->intf_cache[i];
31191017f9cSSarah Sharp break;
31291017f9cSSarah Sharp }
31391017f9cSSarah Sharp }
31491017f9cSSarah Sharp if (!intf_cache)
31591017f9cSSarah Sharp return NULL;
31691017f9cSSarah Sharp for (i = 0; i < intf_cache->num_altsetting; i++)
31791017f9cSSarah Sharp if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
31891017f9cSSarah Sharp return &intf_cache->altsetting[i];
31991017f9cSSarah Sharp
32091017f9cSSarah Sharp printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
32191017f9cSSarah Sharp "config %u\n", alt_num, iface_num,
32291017f9cSSarah Sharp config->desc.bConfigurationValue);
32391017f9cSSarah Sharp return NULL;
32491017f9cSSarah Sharp }
32591017f9cSSarah Sharp EXPORT_SYMBOL_GPL(usb_find_alt_setting);
32691017f9cSSarah Sharp
32791017f9cSSarah Sharp /**
3281da177e4SLinus Torvalds * usb_ifnum_to_if - get the interface object with a given interface number
3291da177e4SLinus Torvalds * @dev: the device whose current configuration is considered
3301da177e4SLinus Torvalds * @ifnum: the desired interface
3311da177e4SLinus Torvalds *
3321da177e4SLinus Torvalds * This walks the device descriptor for the currently active configuration
333626f090cSYacine Belkadi * to find the interface object with the particular interface number.
3341da177e4SLinus Torvalds *
3351da177e4SLinus Torvalds * Note that configuration descriptors are not required to assign interface
3361da177e4SLinus Torvalds * numbers sequentially, so that it would be incorrect to assume that
3371da177e4SLinus Torvalds * the first interface in that descriptor corresponds to interface zero.
3381da177e4SLinus Torvalds * This routine helps device drivers avoid such mistakes.
3391da177e4SLinus Torvalds * However, you should make sure that you do the right thing with any
3401da177e4SLinus Torvalds * alternate settings available for this interfaces.
3411da177e4SLinus Torvalds *
3421da177e4SLinus Torvalds * Don't call this function unless you are bound to one of the interfaces
3431da177e4SLinus Torvalds * on this device or you have locked the device!
344626f090cSYacine Belkadi *
345626f090cSYacine Belkadi * Return: A pointer to the interface that has @ifnum as interface number,
346626f090cSYacine Belkadi * if found. %NULL otherwise.
3471da177e4SLinus Torvalds */
usb_ifnum_to_if(const struct usb_device * dev,unsigned ifnum)348095bc335SLuiz Fernando N. Capitulino struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
349095bc335SLuiz Fernando N. Capitulino unsigned ifnum)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds struct usb_host_config *config = dev->actconfig;
3521da177e4SLinus Torvalds int i;
3531da177e4SLinus Torvalds
3541da177e4SLinus Torvalds if (!config)
3551da177e4SLinus Torvalds return NULL;
3561da177e4SLinus Torvalds for (i = 0; i < config->desc.bNumInterfaces; i++)
3571da177e4SLinus Torvalds if (config->interface[i]->altsetting[0]
3581da177e4SLinus Torvalds .desc.bInterfaceNumber == ifnum)
3591da177e4SLinus Torvalds return config->interface[i];
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds return NULL;
3621da177e4SLinus Torvalds }
363782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
3641da177e4SLinus Torvalds
3651da177e4SLinus Torvalds /**
366d0bcabcdSRandy Dunlap * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
3671da177e4SLinus Torvalds * @intf: the interface containing the altsetting in question
3681da177e4SLinus Torvalds * @altnum: the desired alternate setting number
3691da177e4SLinus Torvalds *
3701da177e4SLinus Torvalds * This searches the altsetting array of the specified interface for
371626f090cSYacine Belkadi * an entry with the correct bAlternateSetting value.
3721da177e4SLinus Torvalds *
3731da177e4SLinus Torvalds * Note that altsettings need not be stored sequentially by number, so
3741da177e4SLinus Torvalds * it would be incorrect to assume that the first altsetting entry in
3751da177e4SLinus Torvalds * the array corresponds to altsetting zero. This routine helps device
3761da177e4SLinus Torvalds * drivers avoid such mistakes.
3771da177e4SLinus Torvalds *
3781da177e4SLinus Torvalds * Don't call this function unless you are bound to the intf interface
3791da177e4SLinus Torvalds * or you have locked the device!
380626f090cSYacine Belkadi *
381626f090cSYacine Belkadi * Return: A pointer to the entry of the altsetting array of @intf that
382626f090cSYacine Belkadi * has @altnum as the alternate setting number. %NULL if not found.
3831da177e4SLinus Torvalds */
usb_altnum_to_altsetting(const struct usb_interface * intf,unsigned int altnum)3842c044a48SGreg Kroah-Hartman struct usb_host_interface *usb_altnum_to_altsetting(
3852c044a48SGreg Kroah-Hartman const struct usb_interface *intf,
3861da177e4SLinus Torvalds unsigned int altnum)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds int i;
3891da177e4SLinus Torvalds
3901da177e4SLinus Torvalds for (i = 0; i < intf->num_altsetting; i++) {
3911da177e4SLinus Torvalds if (intf->altsetting[i].desc.bAlternateSetting == altnum)
3921da177e4SLinus Torvalds return &intf->altsetting[i];
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds return NULL;
3951da177e4SLinus Torvalds }
396782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
3971da177e4SLinus Torvalds
398ab7cd8c7SGreg Kroah-Hartman struct find_interface_arg {
399ab7cd8c7SGreg Kroah-Hartman int minor;
400c2d284eeSRuss Dill struct device_driver *drv;
401ab7cd8c7SGreg Kroah-Hartman };
402ab7cd8c7SGreg Kroah-Hartman
__find_interface(struct device * dev,const void * data)403418e3ea1SSuzuki K Poulose static int __find_interface(struct device *dev, const void *data)
4046034a080Smochel@digitalimplant.org {
405418e3ea1SSuzuki K Poulose const struct find_interface_arg *arg = data;
406f5691d70SPete Zaitcev struct usb_interface *intf;
4076034a080Smochel@digitalimplant.org
40855129666SKay Sievers if (!is_usb_interface(dev))
4096034a080Smochel@digitalimplant.org return 0;
4106034a080Smochel@digitalimplant.org
411c2d284eeSRuss Dill if (dev->driver != arg->drv)
4126034a080Smochel@digitalimplant.org return 0;
413c2d284eeSRuss Dill intf = to_usb_interface(dev);
414c2d284eeSRuss Dill return intf->minor == arg->minor;
4156034a080Smochel@digitalimplant.org }
4166034a080Smochel@digitalimplant.org
4171da177e4SLinus Torvalds /**
4181da177e4SLinus Torvalds * usb_find_interface - find usb_interface pointer for driver and device
4191da177e4SLinus Torvalds * @drv: the driver whose current configuration is considered
4201da177e4SLinus Torvalds * @minor: the minor number of the desired device
4211da177e4SLinus Torvalds *
422a2582bd4SRuss Dill * This walks the bus device list and returns a pointer to the interface
423c2d284eeSRuss Dill * with the matching minor and driver. Note, this only works for devices
424c2d284eeSRuss Dill * that share the USB major number.
425626f090cSYacine Belkadi *
426626f090cSYacine Belkadi * Return: A pointer to the interface with the matching major and @minor.
4271da177e4SLinus Torvalds */
usb_find_interface(struct usb_driver * drv,int minor)4281da177e4SLinus Torvalds struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
4291da177e4SLinus Torvalds {
430ab7cd8c7SGreg Kroah-Hartman struct find_interface_arg argb;
431a2582bd4SRuss Dill struct device *dev;
4321da177e4SLinus Torvalds
433ab7cd8c7SGreg Kroah-Hartman argb.minor = minor;
434c2d284eeSRuss Dill argb.drv = &drv->drvwrap.driver;
435c2d284eeSRuss Dill
436c2d284eeSRuss Dill dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
437a2582bd4SRuss Dill
438a2582bd4SRuss Dill /* Drop reference count from bus_find_device */
439a2582bd4SRuss Dill put_device(dev);
440a2582bd4SRuss Dill
441a2582bd4SRuss Dill return dev ? to_usb_interface(dev) : NULL;
4421da177e4SLinus Torvalds }
443782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_find_interface);
4441da177e4SLinus Torvalds
4459b790915SJulius Werner struct each_dev_arg {
4469b790915SJulius Werner void *data;
4479b790915SJulius Werner int (*fn)(struct usb_device *, void *);
4489b790915SJulius Werner };
4499b790915SJulius Werner
__each_dev(struct device * dev,void * data)4509b790915SJulius Werner static int __each_dev(struct device *dev, void *data)
4519b790915SJulius Werner {
4529b790915SJulius Werner struct each_dev_arg *arg = (struct each_dev_arg *)data;
4539b790915SJulius Werner
4549b790915SJulius Werner /* There are struct usb_interface on the same bus, filter them out */
4559b790915SJulius Werner if (!is_usb_device(dev))
4569b790915SJulius Werner return 0;
4579b790915SJulius Werner
45869ab55d7SGeliang Tang return arg->fn(to_usb_device(dev), arg->data);
4599b790915SJulius Werner }
4609b790915SJulius Werner
4619b790915SJulius Werner /**
4629b790915SJulius Werner * usb_for_each_dev - iterate over all USB devices in the system
4639b790915SJulius Werner * @data: data pointer that will be handed to the callback function
4649b790915SJulius Werner * @fn: callback function to be called for each USB device
4659b790915SJulius Werner *
4669b790915SJulius Werner * Iterate over all USB devices and call @fn for each, passing it @data. If it
4679b790915SJulius Werner * returns anything other than 0, we break the iteration prematurely and return
4689b790915SJulius Werner * that value.
4699b790915SJulius Werner */
usb_for_each_dev(void * data,int (* fn)(struct usb_device *,void *))4709b790915SJulius Werner int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *))
4719b790915SJulius Werner {
4729b790915SJulius Werner struct each_dev_arg arg = {data, fn};
4739b790915SJulius Werner
4749b790915SJulius Werner return bus_for_each_dev(&usb_bus_type, NULL, &arg, __each_dev);
4759b790915SJulius Werner }
4769b790915SJulius Werner EXPORT_SYMBOL_GPL(usb_for_each_dev);
4779b790915SJulius Werner
4781da177e4SLinus Torvalds /**
4791da177e4SLinus Torvalds * usb_release_dev - free a usb device structure when all users of it are finished.
4801da177e4SLinus Torvalds * @dev: device that's been disconnected
4811da177e4SLinus Torvalds *
4821da177e4SLinus Torvalds * Will be called only by the device core when all users of this usb device are
4831da177e4SLinus Torvalds * done.
4841da177e4SLinus Torvalds */
usb_release_dev(struct device * dev)4851da177e4SLinus Torvalds static void usb_release_dev(struct device *dev)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds struct usb_device *udev;
488c6515272SSarah Sharp struct usb_hcd *hcd;
4891da177e4SLinus Torvalds
4901da177e4SLinus Torvalds udev = to_usb_device(dev);
491c6515272SSarah Sharp hcd = bus_to_hcd(udev->bus);
4921da177e4SLinus Torvalds
4931da177e4SLinus Torvalds usb_destroy_configuration(udev);
4943148bf04SAndiry Xu usb_release_bos_descriptor(udev);
495e271b2c9SJohan Hovold of_node_put(dev->of_node);
496c6515272SSarah Sharp usb_put_hcd(hcd);
4971da177e4SLinus Torvalds kfree(udev->product);
4981da177e4SLinus Torvalds kfree(udev->manufacturer);
4991da177e4SLinus Torvalds kfree(udev->serial);
5001da177e4SLinus Torvalds kfree(udev);
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds
usb_dev_uevent(const struct device * dev,struct kobj_uevent_env * env)503162736b0SGreg Kroah-Hartman static int usb_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
5044a9bee82SAlan Stern {
505162736b0SGreg Kroah-Hartman const struct usb_device *usb_dev;
5064a9bee82SAlan Stern
5074a9bee82SAlan Stern usb_dev = to_usb_device(dev);
5084a9bee82SAlan Stern
5094a9bee82SAlan Stern if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
5104a9bee82SAlan Stern return -ENOMEM;
5114a9bee82SAlan Stern
5124a9bee82SAlan Stern if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
5134a9bee82SAlan Stern return -ENOMEM;
5144a9bee82SAlan Stern
5154a9bee82SAlan Stern return 0;
5164a9bee82SAlan Stern }
5174a9bee82SAlan Stern
518645daaabSAlan Stern #ifdef CONFIG_PM
519645daaabSAlan Stern
520f2189c47SAlan Stern /* USB device Power-Management thunks.
521f2189c47SAlan Stern * There's no need to distinguish here between quiescing a USB device
522f2189c47SAlan Stern * and powering it down; the generic_suspend() routine takes care of
523f2189c47SAlan Stern * it by skipping the usb_port_suspend() call for a quiesce. And for
524f2189c47SAlan Stern * USB interfaces there's no difference at all.
525f2189c47SAlan Stern */
526f2189c47SAlan Stern
usb_dev_prepare(struct device * dev)527f2189c47SAlan Stern static int usb_dev_prepare(struct device *dev)
528f2189c47SAlan Stern {
5299be427efSJohan Hovold return 0; /* Implement eventually? */
530f2189c47SAlan Stern }
531f2189c47SAlan Stern
usb_dev_complete(struct device * dev)532f2189c47SAlan Stern static void usb_dev_complete(struct device *dev)
533f2189c47SAlan Stern {
534f2189c47SAlan Stern /* Currently used only for rebinding interfaces */
53598d9a82eSOliver Neukum usb_resume_complete(dev);
536f2189c47SAlan Stern }
537f2189c47SAlan Stern
usb_dev_suspend(struct device * dev)538f2189c47SAlan Stern static int usb_dev_suspend(struct device *dev)
539f2189c47SAlan Stern {
540f2189c47SAlan Stern return usb_suspend(dev, PMSG_SUSPEND);
541f2189c47SAlan Stern }
542f2189c47SAlan Stern
usb_dev_resume(struct device * dev)543f2189c47SAlan Stern static int usb_dev_resume(struct device *dev)
544f2189c47SAlan Stern {
54565bfd296SAlan Stern return usb_resume(dev, PMSG_RESUME);
546f2189c47SAlan Stern }
547f2189c47SAlan Stern
usb_dev_freeze(struct device * dev)548f2189c47SAlan Stern static int usb_dev_freeze(struct device *dev)
549f2189c47SAlan Stern {
550f2189c47SAlan Stern return usb_suspend(dev, PMSG_FREEZE);
551f2189c47SAlan Stern }
552f2189c47SAlan Stern
usb_dev_thaw(struct device * dev)553f2189c47SAlan Stern static int usb_dev_thaw(struct device *dev)
554f2189c47SAlan Stern {
55565bfd296SAlan Stern return usb_resume(dev, PMSG_THAW);
556f2189c47SAlan Stern }
557f2189c47SAlan Stern
usb_dev_poweroff(struct device * dev)558f2189c47SAlan Stern static int usb_dev_poweroff(struct device *dev)
559f2189c47SAlan Stern {
560f2189c47SAlan Stern return usb_suspend(dev, PMSG_HIBERNATE);
561f2189c47SAlan Stern }
562f2189c47SAlan Stern
usb_dev_restore(struct device * dev)563f2189c47SAlan Stern static int usb_dev_restore(struct device *dev)
564f2189c47SAlan Stern {
56565bfd296SAlan Stern return usb_resume(dev, PMSG_RESTORE);
566f2189c47SAlan Stern }
567f2189c47SAlan Stern
56847145210SAlexey Dobriyan static const struct dev_pm_ops usb_device_pm_ops = {
569f2189c47SAlan Stern .prepare = usb_dev_prepare,
570f2189c47SAlan Stern .complete = usb_dev_complete,
571f2189c47SAlan Stern .suspend = usb_dev_suspend,
572f2189c47SAlan Stern .resume = usb_dev_resume,
573f2189c47SAlan Stern .freeze = usb_dev_freeze,
574f2189c47SAlan Stern .thaw = usb_dev_thaw,
575f2189c47SAlan Stern .poweroff = usb_dev_poweroff,
576f2189c47SAlan Stern .restore = usb_dev_restore,
577e1620d59SRafael J. Wysocki .runtime_suspend = usb_runtime_suspend,
578e1620d59SRafael J. Wysocki .runtime_resume = usb_runtime_resume,
579e1620d59SRafael J. Wysocki .runtime_idle = usb_runtime_idle,
580f2189c47SAlan Stern };
581f2189c47SAlan Stern
582db063507SAlan Stern #endif /* CONFIG_PM */
583645daaabSAlan Stern
584f7a386c5SKay Sievers
usb_devnode(const struct device * dev,umode_t * mode,kuid_t * uid,kgid_t * gid)585a9b12f8bSGreg Kroah-Hartman static char *usb_devnode(const struct device *dev,
5864e4098a3SGreg Kroah-Hartman umode_t *mode, kuid_t *uid, kgid_t *gid)
587f7a386c5SKay Sievers {
588a9b12f8bSGreg Kroah-Hartman const struct usb_device *usb_dev;
589f7a386c5SKay Sievers
590f7a386c5SKay Sievers usb_dev = to_usb_device(dev);
591f7a386c5SKay Sievers return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
592f7a386c5SKay Sievers usb_dev->bus->busnum, usb_dev->devnum);
593f7a386c5SKay Sievers }
594f7a386c5SKay Sievers
595f2189c47SAlan Stern struct device_type usb_device_type = {
596f2189c47SAlan Stern .name = "usb_device",
597f2189c47SAlan Stern .release = usb_release_dev,
598f2189c47SAlan Stern .uevent = usb_dev_uevent,
599e454cea2SKay Sievers .devnode = usb_devnode,
600b409214cSAlan Stern #ifdef CONFIG_PM
601f2189c47SAlan Stern .pm = &usb_device_pm_ops,
602b409214cSAlan Stern #endif
603f2189c47SAlan Stern };
604f2189c47SAlan Stern
usb_dev_authorized(struct usb_device * dev,struct usb_hcd * hcd)6057bae0432SDmitry Torokhov static bool usb_dev_authorized(struct usb_device *dev, struct usb_hcd *hcd)
6067bae0432SDmitry Torokhov {
6077bae0432SDmitry Torokhov struct usb_hub *hub;
6087bae0432SDmitry Torokhov
6097bae0432SDmitry Torokhov if (!dev->parent)
6107bae0432SDmitry Torokhov return true; /* Root hub always ok [and always wired] */
6117bae0432SDmitry Torokhov
6127bae0432SDmitry Torokhov switch (hcd->dev_policy) {
6137bae0432SDmitry Torokhov case USB_DEVICE_AUTHORIZE_NONE:
6147bae0432SDmitry Torokhov default:
6157bae0432SDmitry Torokhov return false;
6167bae0432SDmitry Torokhov
6177bae0432SDmitry Torokhov case USB_DEVICE_AUTHORIZE_ALL:
6187bae0432SDmitry Torokhov return true;
6197bae0432SDmitry Torokhov
6207bae0432SDmitry Torokhov case USB_DEVICE_AUTHORIZE_INTERNAL:
6217bae0432SDmitry Torokhov hub = usb_hub_to_struct_hub(dev->parent);
6227bae0432SDmitry Torokhov return hub->ports[dev->portnum - 1]->connect_type ==
6237bae0432SDmitry Torokhov USB_PORT_CONNECT_TYPE_HARD_WIRED;
6247bae0432SDmitry Torokhov }
6257bae0432SDmitry Torokhov }
626d7d07255SInaky Perez-Gonzalez
6271da177e4SLinus Torvalds /**
6281da177e4SLinus Torvalds * usb_alloc_dev - usb device constructor (usbcore-internal)
6291da177e4SLinus Torvalds * @parent: hub to which device is connected; null to allocate a root hub
6301da177e4SLinus Torvalds * @bus: bus used to access the device
6311da177e4SLinus Torvalds * @port1: one-based index of port; ignored for root hubs
63241631d36SAhmed S. Darwish *
63341631d36SAhmed S. Darwish * Context: task context, might sleep.
6341da177e4SLinus Torvalds *
6351da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host
6361da177e4SLinus Torvalds * controllers) should ever call this.
6371da177e4SLinus Torvalds *
6381da177e4SLinus Torvalds * This call may not be used in a non-sleeping context.
639626f090cSYacine Belkadi *
640626f090cSYacine Belkadi * Return: On success, a pointer to the allocated usb device. %NULL on
641626f090cSYacine Belkadi * failure.
6421da177e4SLinus Torvalds */
usb_alloc_dev(struct usb_device * parent,struct usb_bus * bus,unsigned port1)6432c044a48SGreg Kroah-Hartman struct usb_device *usb_alloc_dev(struct usb_device *parent,
6442c044a48SGreg Kroah-Hartman struct usb_bus *bus, unsigned port1)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds struct usb_device *dev;
64730b1e495SYuanhan Liu struct usb_hcd *usb_hcd = bus_to_hcd(bus);
6487222c832SNicolai Stange unsigned raw_port = port1;
6491da177e4SLinus Torvalds
6500a1ef3b5SAlan Stern dev = kzalloc(sizeof(*dev), GFP_KERNEL);
6511da177e4SLinus Torvalds if (!dev)
6521da177e4SLinus Torvalds return NULL;
6531da177e4SLinus Torvalds
65430b1e495SYuanhan Liu if (!usb_get_hcd(usb_hcd)) {
6551da177e4SLinus Torvalds kfree(dev);
6561da177e4SLinus Torvalds return NULL;
6571da177e4SLinus Torvalds }
658c6515272SSarah Sharp /* Root hubs aren't true devices, so don't allocate HCD resources */
659c6515272SSarah Sharp if (usb_hcd->driver->alloc_dev && parent &&
660c6515272SSarah Sharp !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
661c6515272SSarah Sharp usb_put_hcd(bus_to_hcd(bus));
662c6515272SSarah Sharp kfree(dev);
663c6515272SSarah Sharp return NULL;
664c6515272SSarah Sharp }
6651da177e4SLinus Torvalds
6661da177e4SLinus Torvalds device_initialize(&dev->dev);
6671da177e4SLinus Torvalds dev->dev.bus = &usb_bus_type;
6689f8b17e6SKay Sievers dev->dev.type = &usb_device_type;
6692e5f10e4SAlan Stern dev->dev.groups = usb_device_groups;
670a8c06e40SArnd Bergmann set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
6711da177e4SLinus Torvalds dev->state = USB_STATE_ATTACHED;
6729cf65991SSarah Sharp dev->lpm_disable_count = 1;
6734d59d8a1SSarah Sharp atomic_set(&dev->urbnum, 0);
6741da177e4SLinus Torvalds
6751da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->ep0.urb_list);
6761da177e4SLinus Torvalds dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
6771da177e4SLinus Torvalds dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
6781da177e4SLinus Torvalds /* ep0 maxpacket comes later, from device descriptor */
6793444b26aSDavid Vrabel usb_enable_endpoint(dev, &dev->ep0, false);
6806840d255SAlan Stern dev->can_submit = 1;
6811da177e4SLinus Torvalds
6821da177e4SLinus Torvalds /* Save readable and stable topology id, distinguishing devices
6831da177e4SLinus Torvalds * by location for diagnostics, tools, driver model, etc. The
6841da177e4SLinus Torvalds * string is a path along hub ports, from the root. Each device's
6851da177e4SLinus Torvalds * dev->devpath will be stable until USB is re-cabled, and hubs
6867071a3ceSKay Sievers * are often labeled with these port numbers. The name isn't
6871da177e4SLinus Torvalds * as stable: bus->busnum changes easily from modprobe order,
6881da177e4SLinus Torvalds * cardbus or pci hotplugging, and so on.
6891da177e4SLinus Torvalds */
6901da177e4SLinus Torvalds if (unlikely(!parent)) {
6911da177e4SLinus Torvalds dev->devpath[0] = '0';
6927206b001SSarah Sharp dev->route = 0;
6931da177e4SLinus Torvalds
6941da177e4SLinus Torvalds dev->dev.parent = bus->controller;
6952bf69867SJohan Hovold device_set_of_node_from_dev(&dev->dev, bus->sysdev);
6960031a06eSKay Sievers dev_set_name(&dev->dev, "usb%d", bus->busnum);
6971da177e4SLinus Torvalds } else {
6981da177e4SLinus Torvalds /* match any labeling on the hubs; it's one-based */
6997206b001SSarah Sharp if (parent->devpath[0] == '0') {
7001da177e4SLinus Torvalds snprintf(dev->devpath, sizeof dev->devpath,
7011da177e4SLinus Torvalds "%d", port1);
7027206b001SSarah Sharp /* Root ports are not counted in route string */
7037206b001SSarah Sharp dev->route = 0;
7047206b001SSarah Sharp } else {
7051da177e4SLinus Torvalds snprintf(dev->devpath, sizeof dev->devpath,
7061da177e4SLinus Torvalds "%s.%d", parent->devpath, port1);
7074a0cd967SSarah Sharp /* Route string assumes hubs have less than 16 ports */
7084a0cd967SSarah Sharp if (port1 < 15)
7097206b001SSarah Sharp dev->route = parent->route +
7107206b001SSarah Sharp (port1 << ((parent->level - 1)*4));
7114a0cd967SSarah Sharp else
7124a0cd967SSarah Sharp dev->route = parent->route +
7134a0cd967SSarah Sharp (15 << ((parent->level - 1)*4));
7147206b001SSarah Sharp }
7151da177e4SLinus Torvalds
7161da177e4SLinus Torvalds dev->dev.parent = &parent->dev;
7170031a06eSKay Sievers dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
7181da177e4SLinus Torvalds
71969bec725SPeter Chen if (!parent->parent) {
72069bec725SPeter Chen /* device under root hub's port */
7217222c832SNicolai Stange raw_port = usb_hcd_find_raw_port_number(usb_hcd,
72269bec725SPeter Chen port1);
72369bec725SPeter Chen }
7247739376eSJohan Hovold dev->dev.of_node = usb_of_get_device_node(parent, raw_port);
72569bec725SPeter Chen
7261da177e4SLinus Torvalds /* hub driver sets up TT records */
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds
72912c3da34SAlan Stern dev->portnum = port1;
7301da177e4SLinus Torvalds dev->bus = bus;
7311da177e4SLinus Torvalds dev->parent = parent;
7321da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->filelist);
7331da177e4SLinus Torvalds
734645daaabSAlan Stern #ifdef CONFIG_PM
735fcc4a01eSAlan Stern pm_runtime_set_autosuspend_delay(&dev->dev,
736fcc4a01eSAlan Stern usb_autosuspend_delay * 1000);
73715123006SSarah Sharp dev->connect_time = jiffies;
73815123006SSarah Sharp dev->active_duration = -jiffies;
739645daaabSAlan Stern #endif
7407bae0432SDmitry Torokhov
7417bae0432SDmitry Torokhov dev->authorized = usb_dev_authorized(dev, usb_hcd);
7421da177e4SLinus Torvalds return dev;
7431da177e4SLinus Torvalds }
744caa67a5eSPratyush Anand EXPORT_SYMBOL_GPL(usb_alloc_dev);
7451da177e4SLinus Torvalds
7461da177e4SLinus Torvalds /**
7471da177e4SLinus Torvalds * usb_get_dev - increments the reference count of the usb device structure
7481da177e4SLinus Torvalds * @dev: the device being referenced
7491da177e4SLinus Torvalds *
7501da177e4SLinus Torvalds * Each live reference to a device should be refcounted.
7511da177e4SLinus Torvalds *
7521da177e4SLinus Torvalds * Drivers for USB interfaces should normally record such references in
7531da177e4SLinus Torvalds * their probe() methods, when they bind to an interface, and release
7541da177e4SLinus Torvalds * them by calling usb_put_dev(), in their disconnect() methods.
755f6a9a2d6SAlan Stern * However, if a driver does not access the usb_device structure after
756f6a9a2d6SAlan Stern * its disconnect() method returns then refcounting is not necessary,
757f6a9a2d6SAlan Stern * because the USB core guarantees that a usb_device will not be
758f6a9a2d6SAlan Stern * deallocated until after all of its interface drivers have been unbound.
7591da177e4SLinus Torvalds *
760626f090cSYacine Belkadi * Return: A pointer to the device with the incremented reference counter.
7611da177e4SLinus Torvalds */
usb_get_dev(struct usb_device * dev)7621da177e4SLinus Torvalds struct usb_device *usb_get_dev(struct usb_device *dev)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds if (dev)
7651da177e4SLinus Torvalds get_device(&dev->dev);
7661da177e4SLinus Torvalds return dev;
7671da177e4SLinus Torvalds }
768782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_get_dev);
7691da177e4SLinus Torvalds
7701da177e4SLinus Torvalds /**
7711da177e4SLinus Torvalds * usb_put_dev - release a use of the usb device structure
7721da177e4SLinus Torvalds * @dev: device that's been disconnected
7731da177e4SLinus Torvalds *
7741da177e4SLinus Torvalds * Must be called when a user of a device is finished with it. When the last
7751da177e4SLinus Torvalds * user of the device calls this function, the memory of the device is freed.
7761da177e4SLinus Torvalds */
usb_put_dev(struct usb_device * dev)7771da177e4SLinus Torvalds void usb_put_dev(struct usb_device *dev)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds if (dev)
7801da177e4SLinus Torvalds put_device(&dev->dev);
7811da177e4SLinus Torvalds }
782782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_put_dev);
7831da177e4SLinus Torvalds
7841da177e4SLinus Torvalds /**
7851da177e4SLinus Torvalds * usb_get_intf - increments the reference count of the usb interface structure
7861da177e4SLinus Torvalds * @intf: the interface being referenced
7871da177e4SLinus Torvalds *
7881da177e4SLinus Torvalds * Each live reference to a interface must be refcounted.
7891da177e4SLinus Torvalds *
7901da177e4SLinus Torvalds * Drivers for USB interfaces should normally record such references in
7911da177e4SLinus Torvalds * their probe() methods, when they bind to an interface, and release
7921da177e4SLinus Torvalds * them by calling usb_put_intf(), in their disconnect() methods.
793f6a9a2d6SAlan Stern * However, if a driver does not access the usb_interface structure after
794f6a9a2d6SAlan Stern * its disconnect() method returns then refcounting is not necessary,
795f6a9a2d6SAlan Stern * because the USB core guarantees that a usb_interface will not be
796f6a9a2d6SAlan Stern * deallocated until after its driver has been unbound.
7971da177e4SLinus Torvalds *
798626f090cSYacine Belkadi * Return: A pointer to the interface with the incremented reference counter.
7991da177e4SLinus Torvalds */
usb_get_intf(struct usb_interface * intf)8001da177e4SLinus Torvalds struct usb_interface *usb_get_intf(struct usb_interface *intf)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds if (intf)
8031da177e4SLinus Torvalds get_device(&intf->dev);
8041da177e4SLinus Torvalds return intf;
8051da177e4SLinus Torvalds }
806782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_get_intf);
8071da177e4SLinus Torvalds
8081da177e4SLinus Torvalds /**
8091da177e4SLinus Torvalds * usb_put_intf - release a use of the usb interface structure
8101da177e4SLinus Torvalds * @intf: interface that's been decremented
8111da177e4SLinus Torvalds *
8121da177e4SLinus Torvalds * Must be called when a user of an interface is finished with it. When the
8131da177e4SLinus Torvalds * last user of the interface calls this function, the memory of the interface
8141da177e4SLinus Torvalds * is freed.
8151da177e4SLinus Torvalds */
usb_put_intf(struct usb_interface * intf)8161da177e4SLinus Torvalds void usb_put_intf(struct usb_interface *intf)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds if (intf)
8191da177e4SLinus Torvalds put_device(&intf->dev);
8201da177e4SLinus Torvalds }
821782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_put_intf);
8221da177e4SLinus Torvalds
823659ab7a4SThomas Zimmermann /**
824659ab7a4SThomas Zimmermann * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
825659ab7a4SThomas Zimmermann * @intf: the usb interface
826659ab7a4SThomas Zimmermann *
827659ab7a4SThomas Zimmermann * While a USB device cannot perform DMA operations by itself, many USB
828659ab7a4SThomas Zimmermann * controllers can. A call to usb_intf_get_dma_device() returns the DMA endpoint
829659ab7a4SThomas Zimmermann * for the given USB interface, if any. The returned device structure must be
830659ab7a4SThomas Zimmermann * released with put_device().
831659ab7a4SThomas Zimmermann *
832659ab7a4SThomas Zimmermann * See also usb_get_dma_device().
833659ab7a4SThomas Zimmermann *
834659ab7a4SThomas Zimmermann * Returns: A reference to the usb interface's DMA endpoint; or NULL if none
835659ab7a4SThomas Zimmermann * exists.
836659ab7a4SThomas Zimmermann */
usb_intf_get_dma_device(struct usb_interface * intf)837659ab7a4SThomas Zimmermann struct device *usb_intf_get_dma_device(struct usb_interface *intf)
838659ab7a4SThomas Zimmermann {
839659ab7a4SThomas Zimmermann struct usb_device *udev = interface_to_usbdev(intf);
840659ab7a4SThomas Zimmermann struct device *dmadev;
841659ab7a4SThomas Zimmermann
842659ab7a4SThomas Zimmermann if (!udev->bus)
843659ab7a4SThomas Zimmermann return NULL;
844659ab7a4SThomas Zimmermann
845659ab7a4SThomas Zimmermann dmadev = get_device(udev->bus->sysdev);
846659ab7a4SThomas Zimmermann if (!dmadev || !dmadev->dma_mask) {
847659ab7a4SThomas Zimmermann put_device(dmadev);
848659ab7a4SThomas Zimmermann return NULL;
849659ab7a4SThomas Zimmermann }
850659ab7a4SThomas Zimmermann
851659ab7a4SThomas Zimmermann return dmadev;
852659ab7a4SThomas Zimmermann }
853659ab7a4SThomas Zimmermann EXPORT_SYMBOL_GPL(usb_intf_get_dma_device);
854659ab7a4SThomas Zimmermann
8551da177e4SLinus Torvalds /* USB device locking
8561da177e4SLinus Torvalds *
8579ad3d6ccSAlan Stern * USB devices and interfaces are locked using the semaphore in their
8589ad3d6ccSAlan Stern * embedded struct device. The hub driver guarantees that whenever a
8599ad3d6ccSAlan Stern * device is connected or disconnected, drivers are called with the
8609ad3d6ccSAlan Stern * USB device locked as well as their particular interface.
8611da177e4SLinus Torvalds *
8621da177e4SLinus Torvalds * Complications arise when several devices are to be locked at the same
8631da177e4SLinus Torvalds * time. Only hub-aware drivers that are part of usbcore ever have to
8649ad3d6ccSAlan Stern * do this; nobody else needs to worry about it. The rule for locking
8659ad3d6ccSAlan Stern * is simple:
8661da177e4SLinus Torvalds *
8671da177e4SLinus Torvalds * When locking both a device and its parent, always lock the
868a7a9f4c0SJilin Yuan * parent first.
8691da177e4SLinus Torvalds */
8701da177e4SLinus Torvalds
8711da177e4SLinus Torvalds /**
872d0bcabcdSRandy Dunlap * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
8731da177e4SLinus Torvalds * @udev: device that's being locked
8741da177e4SLinus Torvalds * @iface: interface bound to the driver making the request (optional)
8751da177e4SLinus Torvalds *
8761da177e4SLinus Torvalds * Attempts to acquire the device lock, but fails if the device is
8771da177e4SLinus Torvalds * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
8781da177e4SLinus Torvalds * is neither BINDING nor BOUND. Rather than sleeping to wait for the
8791da177e4SLinus Torvalds * lock, the routine polls repeatedly. This is to prevent deadlock with
8801da177e4SLinus Torvalds * disconnect; in some drivers (such as usb-storage) the disconnect()
8813ea15966SAlan Stern * or suspend() method will block waiting for a device reset to complete.
8821da177e4SLinus Torvalds *
883626f090cSYacine Belkadi * Return: A negative error code for failure, otherwise 0.
8841da177e4SLinus Torvalds */
usb_lock_device_for_reset(struct usb_device * udev,const struct usb_interface * iface)8851da177e4SLinus Torvalds int usb_lock_device_for_reset(struct usb_device *udev,
886095bc335SLuiz Fernando N. Capitulino const struct usb_interface *iface)
8871da177e4SLinus Torvalds {
8883ea15966SAlan Stern unsigned long jiffies_expire = jiffies + HZ;
8893ea15966SAlan Stern
8901da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED)
8911da177e4SLinus Torvalds return -ENODEV;
8921da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED)
8931da177e4SLinus Torvalds return -EHOSTUNREACH;
894011b15dfSAlan Stern if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
895011b15dfSAlan Stern iface->condition == USB_INTERFACE_UNBOUND))
8961da177e4SLinus Torvalds return -EINTR;
8971da177e4SLinus Torvalds
8983142788bSThomas Gleixner while (!usb_trylock_device(udev)) {
8993ea15966SAlan Stern
9003ea15966SAlan Stern /* If we can't acquire the lock after waiting one second,
9013ea15966SAlan Stern * we're probably deadlocked */
9023ea15966SAlan Stern if (time_after(jiffies, jiffies_expire))
9033ea15966SAlan Stern return -EBUSY;
9043ea15966SAlan Stern
9051da177e4SLinus Torvalds msleep(15);
9061da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED)
9071da177e4SLinus Torvalds return -ENODEV;
9081da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED)
9091da177e4SLinus Torvalds return -EHOSTUNREACH;
910011b15dfSAlan Stern if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
911011b15dfSAlan Stern iface->condition == USB_INTERFACE_UNBOUND))
9121da177e4SLinus Torvalds return -EINTR;
9131da177e4SLinus Torvalds }
914011b15dfSAlan Stern return 0;
9151da177e4SLinus Torvalds }
916782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
9171da177e4SLinus Torvalds
9181da177e4SLinus Torvalds /**
9191da177e4SLinus Torvalds * usb_get_current_frame_number - return current bus frame number
9201da177e4SLinus Torvalds * @dev: the device whose bus is being queried
9211da177e4SLinus Torvalds *
922626f090cSYacine Belkadi * Return: The current frame number for the USB host controller used
923626f090cSYacine Belkadi * with the given USB device. This can be used when scheduling
9241da177e4SLinus Torvalds * isochronous requests.
9251da177e4SLinus Torvalds *
926626f090cSYacine Belkadi * Note: Different kinds of host controller have different "scheduling
927626f090cSYacine Belkadi * horizons". While one type might support scheduling only 32 frames
928626f090cSYacine Belkadi * into the future, others could support scheduling up to 1024 frames
929626f090cSYacine Belkadi * into the future.
930626f090cSYacine Belkadi *
9311da177e4SLinus Torvalds */
usb_get_current_frame_number(struct usb_device * dev)9321da177e4SLinus Torvalds int usb_get_current_frame_number(struct usb_device *dev)
9331da177e4SLinus Torvalds {
934a6d2bb9fSAlan Stern return usb_hcd_get_frame_number(dev);
9351da177e4SLinus Torvalds }
936782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
9371da177e4SLinus Torvalds
9381da177e4SLinus Torvalds /*-------------------------------------------------------------------*/
9391da177e4SLinus Torvalds /*
9401da177e4SLinus Torvalds * __usb_get_extra_descriptor() finds a descriptor of specific type in the
9411da177e4SLinus Torvalds * extra field of the interface and endpoint descriptor structs.
9421da177e4SLinus Torvalds */
9431da177e4SLinus Torvalds
__usb_get_extra_descriptor(char * buffer,unsigned size,unsigned char type,void ** ptr,size_t minsize)9441da177e4SLinus Torvalds int __usb_get_extra_descriptor(char *buffer, unsigned size,
945704620afSMathias Payer unsigned char type, void **ptr, size_t minsize)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds struct usb_descriptor_header *header;
9481da177e4SLinus Torvalds
9491da177e4SLinus Torvalds while (size >= sizeof(struct usb_descriptor_header)) {
9501da177e4SLinus Torvalds header = (struct usb_descriptor_header *)buffer;
9511da177e4SLinus Torvalds
952704620afSMathias Payer if (header->bLength < 2 || header->bLength > size) {
9531da177e4SLinus Torvalds printk(KERN_ERR
9541da177e4SLinus Torvalds "%s: bogus descriptor, type %d length %d\n",
9551da177e4SLinus Torvalds usbcore_name,
9561da177e4SLinus Torvalds header->bDescriptorType,
9571da177e4SLinus Torvalds header->bLength);
9581da177e4SLinus Torvalds return -1;
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds
961704620afSMathias Payer if (header->bDescriptorType == type && header->bLength >= minsize) {
9621da177e4SLinus Torvalds *ptr = header;
9631da177e4SLinus Torvalds return 0;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds
9661da177e4SLinus Torvalds buffer += header->bLength;
9671da177e4SLinus Torvalds size -= header->bLength;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds return -1;
9701da177e4SLinus Torvalds }
971782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
9721da177e4SLinus Torvalds
9731da177e4SLinus Torvalds /**
974073900a2SDaniel Mack * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
9751da177e4SLinus Torvalds * @dev: device the buffer will be used with
9761da177e4SLinus Torvalds * @size: requested buffer size
9771da177e4SLinus Torvalds * @mem_flags: affect whether allocation may block
9781da177e4SLinus Torvalds * @dma: used to return DMA address of buffer
9791da177e4SLinus Torvalds *
980626f090cSYacine Belkadi * Return: Either null (indicating no buffer could be allocated), or the
981626f090cSYacine Belkadi * cpu-space pointer to a buffer that may be used to perform DMA to the
9821da177e4SLinus Torvalds * specified device. Such cpu-space buffers are returned along with the DMA
9831da177e4SLinus Torvalds * address (through the pointer provided).
9841da177e4SLinus Torvalds *
985626f090cSYacine Belkadi * Note:
9861da177e4SLinus Torvalds * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
987fbf54dd3SDavid Brownell * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
988fbf54dd3SDavid Brownell * hardware during URB completion/resubmit. The implementation varies between
9891da177e4SLinus Torvalds * platforms, depending on details of how DMA will work to this device.
990fbf54dd3SDavid Brownell * Using these buffers also eliminates cacheline sharing problems on
991fbf54dd3SDavid Brownell * architectures where CPU caches are not DMA-coherent. On systems without
992fbf54dd3SDavid Brownell * bus-snooping caches, these buffers are uncached.
9931da177e4SLinus Torvalds *
994073900a2SDaniel Mack * When the buffer is no longer used, free it with usb_free_coherent().
9951da177e4SLinus Torvalds */
usb_alloc_coherent(struct usb_device * dev,size_t size,gfp_t mem_flags,dma_addr_t * dma)996073900a2SDaniel Mack void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
9972c044a48SGreg Kroah-Hartman dma_addr_t *dma)
9981da177e4SLinus Torvalds {
999a6d2bb9fSAlan Stern if (!dev || !dev->bus)
10001da177e4SLinus Torvalds return NULL;
1001a6d2bb9fSAlan Stern return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
10021da177e4SLinus Torvalds }
1003073900a2SDaniel Mack EXPORT_SYMBOL_GPL(usb_alloc_coherent);
10041da177e4SLinus Torvalds
10051da177e4SLinus Torvalds /**
1006073900a2SDaniel Mack * usb_free_coherent - free memory allocated with usb_alloc_coherent()
10071da177e4SLinus Torvalds * @dev: device the buffer was used with
10081da177e4SLinus Torvalds * @size: requested buffer size
10091da177e4SLinus Torvalds * @addr: CPU address of buffer
10101da177e4SLinus Torvalds * @dma: DMA address of buffer
10111da177e4SLinus Torvalds *
10121da177e4SLinus Torvalds * This reclaims an I/O buffer, letting it be reused. The memory must have
1013073900a2SDaniel Mack * been allocated using usb_alloc_coherent(), and the parameters must match
10141da177e4SLinus Torvalds * those provided in that allocation request.
10151da177e4SLinus Torvalds */
usb_free_coherent(struct usb_device * dev,size_t size,void * addr,dma_addr_t dma)1016073900a2SDaniel Mack void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
10172c044a48SGreg Kroah-Hartman dma_addr_t dma)
10181da177e4SLinus Torvalds {
1019a6d2bb9fSAlan Stern if (!dev || !dev->bus)
10201da177e4SLinus Torvalds return;
1021b94badbbSDmitry Torokhov if (!addr)
1022b94badbbSDmitry Torokhov return;
1023a6d2bb9fSAlan Stern hcd_buffer_free(dev->bus, size, addr, dma);
10241da177e4SLinus Torvalds }
1025073900a2SDaniel Mack EXPORT_SYMBOL_GPL(usb_free_coherent);
10261da177e4SLinus Torvalds
10271da177e4SLinus Torvalds /*
10283b23dd6fSAlan Stern * Notifications of device and interface registration
10293b23dd6fSAlan Stern */
usb_bus_notify(struct notifier_block * nb,unsigned long action,void * data)10303b23dd6fSAlan Stern static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
10313b23dd6fSAlan Stern void *data)
10323b23dd6fSAlan Stern {
10333b23dd6fSAlan Stern struct device *dev = data;
10343b23dd6fSAlan Stern
10353b23dd6fSAlan Stern switch (action) {
10363b23dd6fSAlan Stern case BUS_NOTIFY_ADD_DEVICE:
10373b23dd6fSAlan Stern if (dev->type == &usb_device_type)
10383b23dd6fSAlan Stern (void) usb_create_sysfs_dev_files(to_usb_device(dev));
10393b23dd6fSAlan Stern else if (dev->type == &usb_if_device_type)
1040643de624SMichal Nazarewicz usb_create_sysfs_intf_files(to_usb_interface(dev));
10413b23dd6fSAlan Stern break;
10423b23dd6fSAlan Stern
10433b23dd6fSAlan Stern case BUS_NOTIFY_DEL_DEVICE:
10443b23dd6fSAlan Stern if (dev->type == &usb_device_type)
10453b23dd6fSAlan Stern usb_remove_sysfs_dev_files(to_usb_device(dev));
10463b23dd6fSAlan Stern else if (dev->type == &usb_if_device_type)
10473b23dd6fSAlan Stern usb_remove_sysfs_intf_files(to_usb_interface(dev));
10483b23dd6fSAlan Stern break;
10493b23dd6fSAlan Stern }
10503b23dd6fSAlan Stern return 0;
10513b23dd6fSAlan Stern }
10523b23dd6fSAlan Stern
10533b23dd6fSAlan Stern static struct notifier_block usb_bus_nb = {
10543b23dd6fSAlan Stern .notifier_call = usb_bus_notify,
10553b23dd6fSAlan Stern };
10563b23dd6fSAlan Stern
usb_debugfs_init(void)1057b708692dSGreg Kroah-Hartman static void usb_debugfs_init(void)
105800048b8bSGreg Kroah-Hartman {
10599c174b57SGreg Kroah-Hartman debugfs_create_file("devices", 0444, usb_debug_root, NULL,
10609c174b57SGreg Kroah-Hartman &usbfs_devices_fops);
106100048b8bSGreg Kroah-Hartman }
106200048b8bSGreg Kroah-Hartman
usb_debugfs_cleanup(void)106300048b8bSGreg Kroah-Hartman static void usb_debugfs_cleanup(void)
106400048b8bSGreg Kroah-Hartman {
106530374434SGreg Kroah-Hartman debugfs_lookup_and_remove("devices", usb_debug_root);
106600048b8bSGreg Kroah-Hartman }
106700048b8bSGreg Kroah-Hartman
10683b23dd6fSAlan Stern /*
10691da177e4SLinus Torvalds * Init
10701da177e4SLinus Torvalds */
usb_init(void)10711da177e4SLinus Torvalds static int __init usb_init(void)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds int retval;
10741da47f54SViresh Kumar if (usb_disabled()) {
10751da177e4SLinus Torvalds pr_info("%s: USB support disabled\n", usbcore_name);
10761da177e4SLinus Torvalds return 0;
10771da177e4SLinus Torvalds }
10785efd2ea8SSebastian Andrzej Siewior usb_init_pool_max();
10791da177e4SLinus Torvalds
1080b708692dSGreg Kroah-Hartman usb_debugfs_init();
108100048b8bSGreg Kroah-Hartman
1082ea79c2edSSasha Levin usb_acpi_register();
1083bd859281SAlan Stern retval = bus_register(&usb_bus_type);
1084bd859281SAlan Stern if (retval)
1085bd859281SAlan Stern goto bus_register_failed;
10863b23dd6fSAlan Stern retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
10873b23dd6fSAlan Stern if (retval)
10883b23dd6fSAlan Stern goto bus_notifier_failed;
10891da177e4SLinus Torvalds retval = usb_major_init();
10901da177e4SLinus Torvalds if (retval)
10911da177e4SLinus Torvalds goto major_init_failed;
1092015fbddeSIvan Orlov retval = class_register(&usbmisc_class);
1093015fbddeSIvan Orlov if (retval)
1094015fbddeSIvan Orlov goto class_register_failed;
1095fbf82fd2SKay Sievers retval = usb_register(&usbfs_driver);
1096fbf82fd2SKay Sievers if (retval)
1097fbf82fd2SKay Sievers goto driver_register_failed;
10989f8b17e6SKay Sievers retval = usb_devio_init();
1099fbf82fd2SKay Sievers if (retval)
11009f8b17e6SKay Sievers goto usb_devio_init_failed;
11011da177e4SLinus Torvalds retval = usb_hub_init();
11021da177e4SLinus Torvalds if (retval)
11031da177e4SLinus Torvalds goto hub_init_failed;
11048bb54ab5SAlan Stern retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
11051da177e4SLinus Torvalds if (!retval)
11061da177e4SLinus Torvalds goto out;
11071da177e4SLinus Torvalds
11081da177e4SLinus Torvalds usb_hub_cleanup();
11091da177e4SLinus Torvalds hub_init_failed:
11109f8b17e6SKay Sievers usb_devio_cleanup();
11119f8b17e6SKay Sievers usb_devio_init_failed:
1112fbf82fd2SKay Sievers usb_deregister(&usbfs_driver);
1113fbf82fd2SKay Sievers driver_register_failed:
1114015fbddeSIvan Orlov class_unregister(&usbmisc_class);
1115015fbddeSIvan Orlov class_register_failed:
11161da177e4SLinus Torvalds usb_major_cleanup();
11171da177e4SLinus Torvalds major_init_failed:
11183b23dd6fSAlan Stern bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
11193b23dd6fSAlan Stern bus_notifier_failed:
11201da177e4SLinus Torvalds bus_unregister(&usb_bus_type);
1121bd859281SAlan Stern bus_register_failed:
1122da0af6e7SMatthew Garrett usb_acpi_unregister();
11239bbdf1e0SAlan Stern usb_debugfs_cleanup();
11241da177e4SLinus Torvalds out:
11251da177e4SLinus Torvalds return retval;
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds * Cleanup
11301da177e4SLinus Torvalds */
usb_exit(void)11311da177e4SLinus Torvalds static void __exit usb_exit(void)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds /* This will matter if shutdown/reboot does exitcalls. */
11341da47f54SViresh Kumar if (usb_disabled())
11351da177e4SLinus Torvalds return;
11361da177e4SLinus Torvalds
1137027bd6caSKai-Heng Feng usb_release_quirk_list();
11388bb54ab5SAlan Stern usb_deregister_device_driver(&usb_generic_driver);
11391da177e4SLinus Torvalds usb_major_cleanup();
1140fbf82fd2SKay Sievers usb_deregister(&usbfs_driver);
11419f8b17e6SKay Sievers usb_devio_cleanup();
11421da177e4SLinus Torvalds usb_hub_cleanup();
1143015fbddeSIvan Orlov class_unregister(&usbmisc_class);
11443b23dd6fSAlan Stern bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
11451da177e4SLinus Torvalds bus_unregister(&usb_bus_type);
1146da0af6e7SMatthew Garrett usb_acpi_unregister();
114700048b8bSGreg Kroah-Hartman usb_debugfs_cleanup();
11485363de75SHeiner Kallweit idr_destroy(&usb_bus_idr);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds
11511da177e4SLinus Torvalds subsys_initcall(usb_init);
11521da177e4SLinus Torvalds module_exit(usb_exit);
11531da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1154