1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * drivers/usb/core/usb.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright Linus Torvalds 1999
6*4882a593Smuzhiyun * (C) Copyright Johannes Erdfelt 1999-2001
7*4882a593Smuzhiyun * (C) Copyright Andreas Gal 1999
8*4882a593Smuzhiyun * (C) Copyright Gregory P. Smith 1999
9*4882a593Smuzhiyun * (C) Copyright Deti Fliegl 1999 (new USB architecture)
10*4882a593Smuzhiyun * (C) Copyright Randy Dunlap 2000
11*4882a593Smuzhiyun * (C) Copyright David Brownell 2000-2004
12*4882a593Smuzhiyun * (C) Copyright Yggdrasil Computing, Inc. 2000
13*4882a593Smuzhiyun * (usb_device_id matching changes by Adam J. Richter)
14*4882a593Smuzhiyun * (C) Copyright Greg Kroah-Hartman 2002-2003
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Released under the GPLv2 only.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * NOTE! This is not actually a driver at all, rather this is
19*4882a593Smuzhiyun * just a collection of helper routines that implement the
20*4882a593Smuzhiyun * generic USB things that the real drivers can use..
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Think of this as a "USB library" rather than anything else,
23*4882a593Smuzhiyun * with no callbacks. Callbacks are evil.
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/moduleparam.h>
28*4882a593Smuzhiyun #include <linux/string.h>
29*4882a593Smuzhiyun #include <linux/bitops.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/interrupt.h> /* for in_interrupt() */
32*4882a593Smuzhiyun #include <linux/kmod.h>
33*4882a593Smuzhiyun #include <linux/init.h>
34*4882a593Smuzhiyun #include <linux/spinlock.h>
35*4882a593Smuzhiyun #include <linux/errno.h>
36*4882a593Smuzhiyun #include <linux/usb.h>
37*4882a593Smuzhiyun #include <linux/usb/hcd.h>
38*4882a593Smuzhiyun #include <linux/mutex.h>
39*4882a593Smuzhiyun #include <linux/workqueue.h>
40*4882a593Smuzhiyun #include <linux/debugfs.h>
41*4882a593Smuzhiyun #include <linux/usb/of.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <asm/io.h>
44*4882a593Smuzhiyun #include <linux/scatterlist.h>
45*4882a593Smuzhiyun #include <linux/mm.h>
46*4882a593Smuzhiyun #include <linux/dma-mapping.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #include "hub.h"
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun const char *usbcore_name = "usbcore";
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static bool nousb; /* Disable USB when built into kernel image */
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun module_param(nousb, bool, 0444);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * for external read access to <nousb>
58*4882a593Smuzhiyun */
usb_disabled(void)59*4882a593Smuzhiyun int usb_disabled(void)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun return nousb;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_disabled);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #ifdef CONFIG_PM
66*4882a593Smuzhiyun /* Default delay value, in seconds */
67*4882a593Smuzhiyun static int usb_autosuspend_delay = CONFIG_USB_AUTOSUSPEND_DELAY;
68*4882a593Smuzhiyun module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
69*4882a593Smuzhiyun MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #else
72*4882a593Smuzhiyun #define usb_autosuspend_delay 0
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun
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)75*4882a593Smuzhiyun static bool match_endpoint(struct usb_endpoint_descriptor *epd,
76*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_in,
77*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_out,
78*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_in,
79*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_out)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun switch (usb_endpoint_type(epd)) {
82*4882a593Smuzhiyun case USB_ENDPOINT_XFER_BULK:
83*4882a593Smuzhiyun if (usb_endpoint_dir_in(epd)) {
84*4882a593Smuzhiyun if (bulk_in && !*bulk_in) {
85*4882a593Smuzhiyun *bulk_in = epd;
86*4882a593Smuzhiyun break;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun } else {
89*4882a593Smuzhiyun if (bulk_out && !*bulk_out) {
90*4882a593Smuzhiyun *bulk_out = epd;
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return false;
96*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
97*4882a593Smuzhiyun if (usb_endpoint_dir_in(epd)) {
98*4882a593Smuzhiyun if (int_in && !*int_in) {
99*4882a593Smuzhiyun *int_in = epd;
100*4882a593Smuzhiyun break;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun } else {
103*4882a593Smuzhiyun if (int_out && !*int_out) {
104*4882a593Smuzhiyun *int_out = epd;
105*4882a593Smuzhiyun break;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return false;
110*4882a593Smuzhiyun default:
111*4882a593Smuzhiyun return false;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&
115*4882a593Smuzhiyun (!int_in || *int_in) && (!int_out || *int_out);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun * usb_find_common_endpoints() -- look up common endpoint descriptors
120*4882a593Smuzhiyun * @alt: alternate setting to search
121*4882a593Smuzhiyun * @bulk_in: pointer to descriptor pointer, or NULL
122*4882a593Smuzhiyun * @bulk_out: pointer to descriptor pointer, or NULL
123*4882a593Smuzhiyun * @int_in: pointer to descriptor pointer, or NULL
124*4882a593Smuzhiyun * @int_out: pointer to descriptor pointer, or NULL
125*4882a593Smuzhiyun *
126*4882a593Smuzhiyun * Search the alternate setting's endpoint descriptors for the first bulk-in,
127*4882a593Smuzhiyun * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
128*4882a593Smuzhiyun * provided pointers (unless they are NULL).
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * If a requested endpoint is not found, the corresponding pointer is set to
131*4882a593Smuzhiyun * NULL.
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
134*4882a593Smuzhiyun */
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)135*4882a593Smuzhiyun int usb_find_common_endpoints(struct usb_host_interface *alt,
136*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_in,
137*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_out,
138*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_in,
139*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_out)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun struct usb_endpoint_descriptor *epd;
142*4882a593Smuzhiyun int i;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun if (bulk_in)
145*4882a593Smuzhiyun *bulk_in = NULL;
146*4882a593Smuzhiyun if (bulk_out)
147*4882a593Smuzhiyun *bulk_out = NULL;
148*4882a593Smuzhiyun if (int_in)
149*4882a593Smuzhiyun *int_in = NULL;
150*4882a593Smuzhiyun if (int_out)
151*4882a593Smuzhiyun *int_out = NULL;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
154*4882a593Smuzhiyun epd = &alt->endpoint[i].desc;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
157*4882a593Smuzhiyun return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return -ENXIO;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
166*4882a593Smuzhiyun * @alt: alternate setting to search
167*4882a593Smuzhiyun * @bulk_in: pointer to descriptor pointer, or NULL
168*4882a593Smuzhiyun * @bulk_out: pointer to descriptor pointer, or NULL
169*4882a593Smuzhiyun * @int_in: pointer to descriptor pointer, or NULL
170*4882a593Smuzhiyun * @int_out: pointer to descriptor pointer, or NULL
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * Search the alternate setting's endpoint descriptors for the last bulk-in,
173*4882a593Smuzhiyun * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
174*4882a593Smuzhiyun * provided pointers (unless they are NULL).
175*4882a593Smuzhiyun *
176*4882a593Smuzhiyun * If a requested endpoint is not found, the corresponding pointer is set to
177*4882a593Smuzhiyun * NULL.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
180*4882a593Smuzhiyun */
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)181*4882a593Smuzhiyun int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
182*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_in,
183*4882a593Smuzhiyun struct usb_endpoint_descriptor **bulk_out,
184*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_in,
185*4882a593Smuzhiyun struct usb_endpoint_descriptor **int_out)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun struct usb_endpoint_descriptor *epd;
188*4882a593Smuzhiyun int i;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if (bulk_in)
191*4882a593Smuzhiyun *bulk_in = NULL;
192*4882a593Smuzhiyun if (bulk_out)
193*4882a593Smuzhiyun *bulk_out = NULL;
194*4882a593Smuzhiyun if (int_in)
195*4882a593Smuzhiyun *int_in = NULL;
196*4882a593Smuzhiyun if (int_out)
197*4882a593Smuzhiyun *int_out = NULL;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {
200*4882a593Smuzhiyun epd = &alt->endpoint[i].desc;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
203*4882a593Smuzhiyun return 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun return -ENXIO;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun * usb_find_alt_setting() - Given a configuration, find the alternate setting
212*4882a593Smuzhiyun * for the given interface.
213*4882a593Smuzhiyun * @config: the configuration to search (not necessarily the current config).
214*4882a593Smuzhiyun * @iface_num: interface number to search in
215*4882a593Smuzhiyun * @alt_num: alternate interface setting number to search for.
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * Search the configuration's interface cache for the given alt setting.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * Return: The alternate setting, if found. %NULL otherwise.
220*4882a593Smuzhiyun */
usb_find_alt_setting(struct usb_host_config * config,unsigned int iface_num,unsigned int alt_num)221*4882a593Smuzhiyun struct usb_host_interface *usb_find_alt_setting(
222*4882a593Smuzhiyun struct usb_host_config *config,
223*4882a593Smuzhiyun unsigned int iface_num,
224*4882a593Smuzhiyun unsigned int alt_num)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun struct usb_interface_cache *intf_cache = NULL;
227*4882a593Smuzhiyun int i;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (!config)
230*4882a593Smuzhiyun return NULL;
231*4882a593Smuzhiyun for (i = 0; i < config->desc.bNumInterfaces; i++) {
232*4882a593Smuzhiyun if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
233*4882a593Smuzhiyun == iface_num) {
234*4882a593Smuzhiyun intf_cache = config->intf_cache[i];
235*4882a593Smuzhiyun break;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun if (!intf_cache)
239*4882a593Smuzhiyun return NULL;
240*4882a593Smuzhiyun for (i = 0; i < intf_cache->num_altsetting; i++)
241*4882a593Smuzhiyun if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
242*4882a593Smuzhiyun return &intf_cache->altsetting[i];
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
245*4882a593Smuzhiyun "config %u\n", alt_num, iface_num,
246*4882a593Smuzhiyun config->desc.bConfigurationValue);
247*4882a593Smuzhiyun return NULL;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_find_alt_setting);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun * usb_ifnum_to_if - get the interface object with a given interface number
253*4882a593Smuzhiyun * @dev: the device whose current configuration is considered
254*4882a593Smuzhiyun * @ifnum: the desired interface
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * This walks the device descriptor for the currently active configuration
257*4882a593Smuzhiyun * to find the interface object with the particular interface number.
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Note that configuration descriptors are not required to assign interface
260*4882a593Smuzhiyun * numbers sequentially, so that it would be incorrect to assume that
261*4882a593Smuzhiyun * the first interface in that descriptor corresponds to interface zero.
262*4882a593Smuzhiyun * This routine helps device drivers avoid such mistakes.
263*4882a593Smuzhiyun * However, you should make sure that you do the right thing with any
264*4882a593Smuzhiyun * alternate settings available for this interfaces.
265*4882a593Smuzhiyun *
266*4882a593Smuzhiyun * Don't call this function unless you are bound to one of the interfaces
267*4882a593Smuzhiyun * on this device or you have locked the device!
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * Return: A pointer to the interface that has @ifnum as interface number,
270*4882a593Smuzhiyun * if found. %NULL otherwise.
271*4882a593Smuzhiyun */
usb_ifnum_to_if(const struct usb_device * dev,unsigned ifnum)272*4882a593Smuzhiyun struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
273*4882a593Smuzhiyun unsigned ifnum)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun struct usb_host_config *config = dev->actconfig;
276*4882a593Smuzhiyun int i;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (!config)
279*4882a593Smuzhiyun return NULL;
280*4882a593Smuzhiyun for (i = 0; i < config->desc.bNumInterfaces; i++)
281*4882a593Smuzhiyun if (config->interface[i]->altsetting[0]
282*4882a593Smuzhiyun .desc.bInterfaceNumber == ifnum)
283*4882a593Smuzhiyun return config->interface[i];
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return NULL;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /**
290*4882a593Smuzhiyun * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
291*4882a593Smuzhiyun * @intf: the interface containing the altsetting in question
292*4882a593Smuzhiyun * @altnum: the desired alternate setting number
293*4882a593Smuzhiyun *
294*4882a593Smuzhiyun * This searches the altsetting array of the specified interface for
295*4882a593Smuzhiyun * an entry with the correct bAlternateSetting value.
296*4882a593Smuzhiyun *
297*4882a593Smuzhiyun * Note that altsettings need not be stored sequentially by number, so
298*4882a593Smuzhiyun * it would be incorrect to assume that the first altsetting entry in
299*4882a593Smuzhiyun * the array corresponds to altsetting zero. This routine helps device
300*4882a593Smuzhiyun * drivers avoid such mistakes.
301*4882a593Smuzhiyun *
302*4882a593Smuzhiyun * Don't call this function unless you are bound to the intf interface
303*4882a593Smuzhiyun * or you have locked the device!
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * Return: A pointer to the entry of the altsetting array of @intf that
306*4882a593Smuzhiyun * has @altnum as the alternate setting number. %NULL if not found.
307*4882a593Smuzhiyun */
usb_altnum_to_altsetting(const struct usb_interface * intf,unsigned int altnum)308*4882a593Smuzhiyun struct usb_host_interface *usb_altnum_to_altsetting(
309*4882a593Smuzhiyun const struct usb_interface *intf,
310*4882a593Smuzhiyun unsigned int altnum)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun int i;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun for (i = 0; i < intf->num_altsetting; i++) {
315*4882a593Smuzhiyun if (intf->altsetting[i].desc.bAlternateSetting == altnum)
316*4882a593Smuzhiyun return &intf->altsetting[i];
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun return NULL;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun struct find_interface_arg {
323*4882a593Smuzhiyun int minor;
324*4882a593Smuzhiyun struct device_driver *drv;
325*4882a593Smuzhiyun };
326*4882a593Smuzhiyun
__find_interface(struct device * dev,const void * data)327*4882a593Smuzhiyun static int __find_interface(struct device *dev, const void *data)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun const struct find_interface_arg *arg = data;
330*4882a593Smuzhiyun struct usb_interface *intf;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (!is_usb_interface(dev))
333*4882a593Smuzhiyun return 0;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun if (dev->driver != arg->drv)
336*4882a593Smuzhiyun return 0;
337*4882a593Smuzhiyun intf = to_usb_interface(dev);
338*4882a593Smuzhiyun return intf->minor == arg->minor;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /**
342*4882a593Smuzhiyun * usb_find_interface - find usb_interface pointer for driver and device
343*4882a593Smuzhiyun * @drv: the driver whose current configuration is considered
344*4882a593Smuzhiyun * @minor: the minor number of the desired device
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * This walks the bus device list and returns a pointer to the interface
347*4882a593Smuzhiyun * with the matching minor and driver. Note, this only works for devices
348*4882a593Smuzhiyun * that share the USB major number.
349*4882a593Smuzhiyun *
350*4882a593Smuzhiyun * Return: A pointer to the interface with the matching major and @minor.
351*4882a593Smuzhiyun */
usb_find_interface(struct usb_driver * drv,int minor)352*4882a593Smuzhiyun struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun struct find_interface_arg argb;
355*4882a593Smuzhiyun struct device *dev;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun argb.minor = minor;
358*4882a593Smuzhiyun argb.drv = &drv->drvwrap.driver;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /* Drop reference count from bus_find_device */
363*4882a593Smuzhiyun put_device(dev);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return dev ? to_usb_interface(dev) : NULL;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_find_interface);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun struct each_dev_arg {
370*4882a593Smuzhiyun void *data;
371*4882a593Smuzhiyun int (*fn)(struct usb_device *, void *);
372*4882a593Smuzhiyun };
373*4882a593Smuzhiyun
__each_dev(struct device * dev,void * data)374*4882a593Smuzhiyun static int __each_dev(struct device *dev, void *data)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun struct each_dev_arg *arg = (struct each_dev_arg *)data;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* There are struct usb_interface on the same bus, filter them out */
379*4882a593Smuzhiyun if (!is_usb_device(dev))
380*4882a593Smuzhiyun return 0;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun return arg->fn(to_usb_device(dev), arg->data);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun * usb_for_each_dev - iterate over all USB devices in the system
387*4882a593Smuzhiyun * @data: data pointer that will be handed to the callback function
388*4882a593Smuzhiyun * @fn: callback function to be called for each USB device
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * Iterate over all USB devices and call @fn for each, passing it @data. If it
391*4882a593Smuzhiyun * returns anything other than 0, we break the iteration prematurely and return
392*4882a593Smuzhiyun * that value.
393*4882a593Smuzhiyun */
usb_for_each_dev(void * data,int (* fn)(struct usb_device *,void *))394*4882a593Smuzhiyun int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *))
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun struct each_dev_arg arg = {data, fn};
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun return bus_for_each_dev(&usb_bus_type, NULL, &arg, __each_dev);
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_for_each_dev);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /**
403*4882a593Smuzhiyun * usb_release_dev - free a usb device structure when all users of it are finished.
404*4882a593Smuzhiyun * @dev: device that's been disconnected
405*4882a593Smuzhiyun *
406*4882a593Smuzhiyun * Will be called only by the device core when all users of this usb device are
407*4882a593Smuzhiyun * done.
408*4882a593Smuzhiyun */
usb_release_dev(struct device * dev)409*4882a593Smuzhiyun static void usb_release_dev(struct device *dev)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun struct usb_device *udev;
412*4882a593Smuzhiyun struct usb_hcd *hcd;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun udev = to_usb_device(dev);
415*4882a593Smuzhiyun hcd = bus_to_hcd(udev->bus);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun usb_destroy_configuration(udev);
418*4882a593Smuzhiyun usb_release_bos_descriptor(udev);
419*4882a593Smuzhiyun of_node_put(dev->of_node);
420*4882a593Smuzhiyun usb_put_hcd(hcd);
421*4882a593Smuzhiyun kfree(udev->product);
422*4882a593Smuzhiyun kfree(udev->manufacturer);
423*4882a593Smuzhiyun kfree(udev->serial);
424*4882a593Smuzhiyun kfree(udev);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
usb_dev_uevent(struct device * dev,struct kobj_uevent_env * env)427*4882a593Smuzhiyun static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct usb_device *usb_dev;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun usb_dev = to_usb_device(dev);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
434*4882a593Smuzhiyun return -ENOMEM;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
437*4882a593Smuzhiyun return -ENOMEM;
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun return 0;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun #ifdef CONFIG_PM
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /* USB device Power-Management thunks.
445*4882a593Smuzhiyun * There's no need to distinguish here between quiescing a USB device
446*4882a593Smuzhiyun * and powering it down; the generic_suspend() routine takes care of
447*4882a593Smuzhiyun * it by skipping the usb_port_suspend() call for a quiesce. And for
448*4882a593Smuzhiyun * USB interfaces there's no difference at all.
449*4882a593Smuzhiyun */
450*4882a593Smuzhiyun
usb_dev_prepare(struct device * dev)451*4882a593Smuzhiyun static int usb_dev_prepare(struct device *dev)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun return 0; /* Implement eventually? */
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
usb_dev_complete(struct device * dev)456*4882a593Smuzhiyun static void usb_dev_complete(struct device *dev)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun /* Currently used only for rebinding interfaces */
459*4882a593Smuzhiyun usb_resume_complete(dev);
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
usb_dev_suspend(struct device * dev)462*4882a593Smuzhiyun static int usb_dev_suspend(struct device *dev)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun return usb_suspend(dev, PMSG_SUSPEND);
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
usb_dev_resume(struct device * dev)467*4882a593Smuzhiyun static int usb_dev_resume(struct device *dev)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun return usb_resume(dev, PMSG_RESUME);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
usb_dev_freeze(struct device * dev)472*4882a593Smuzhiyun static int usb_dev_freeze(struct device *dev)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun return usb_suspend(dev, PMSG_FREEZE);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
usb_dev_thaw(struct device * dev)477*4882a593Smuzhiyun static int usb_dev_thaw(struct device *dev)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun return usb_resume(dev, PMSG_THAW);
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun
usb_dev_poweroff(struct device * dev)482*4882a593Smuzhiyun static int usb_dev_poweroff(struct device *dev)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun return usb_suspend(dev, PMSG_HIBERNATE);
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
usb_dev_restore(struct device * dev)487*4882a593Smuzhiyun static int usb_dev_restore(struct device *dev)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun return usb_resume(dev, PMSG_RESTORE);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun static const struct dev_pm_ops usb_device_pm_ops = {
493*4882a593Smuzhiyun .prepare = usb_dev_prepare,
494*4882a593Smuzhiyun .complete = usb_dev_complete,
495*4882a593Smuzhiyun .suspend = usb_dev_suspend,
496*4882a593Smuzhiyun .resume = usb_dev_resume,
497*4882a593Smuzhiyun .freeze = usb_dev_freeze,
498*4882a593Smuzhiyun .thaw = usb_dev_thaw,
499*4882a593Smuzhiyun .poweroff = usb_dev_poweroff,
500*4882a593Smuzhiyun .restore = usb_dev_restore,
501*4882a593Smuzhiyun .runtime_suspend = usb_runtime_suspend,
502*4882a593Smuzhiyun .runtime_resume = usb_runtime_resume,
503*4882a593Smuzhiyun .runtime_idle = usb_runtime_idle,
504*4882a593Smuzhiyun };
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun #endif /* CONFIG_PM */
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun
usb_devnode(struct device * dev,umode_t * mode,kuid_t * uid,kgid_t * gid)509*4882a593Smuzhiyun static char *usb_devnode(struct device *dev,
510*4882a593Smuzhiyun umode_t *mode, kuid_t *uid, kgid_t *gid)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct usb_device *usb_dev;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun usb_dev = to_usb_device(dev);
515*4882a593Smuzhiyun return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
516*4882a593Smuzhiyun usb_dev->bus->busnum, usb_dev->devnum);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun struct device_type usb_device_type = {
520*4882a593Smuzhiyun .name = "usb_device",
521*4882a593Smuzhiyun .release = usb_release_dev,
522*4882a593Smuzhiyun .uevent = usb_dev_uevent,
523*4882a593Smuzhiyun .devnode = usb_devnode,
524*4882a593Smuzhiyun #ifdef CONFIG_PM
525*4882a593Smuzhiyun .pm = &usb_device_pm_ops,
526*4882a593Smuzhiyun #endif
527*4882a593Smuzhiyun };
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
usb_bus_is_wusb(struct usb_bus * bus)531*4882a593Smuzhiyun static unsigned usb_bus_is_wusb(struct usb_bus *bus)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun struct usb_hcd *hcd = bus_to_hcd(bus);
534*4882a593Smuzhiyun return hcd->wireless;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
usb_dev_authorized(struct usb_device * dev,struct usb_hcd * hcd)537*4882a593Smuzhiyun static bool usb_dev_authorized(struct usb_device *dev, struct usb_hcd *hcd)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun struct usb_hub *hub;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun if (!dev->parent)
542*4882a593Smuzhiyun return true; /* Root hub always ok [and always wired] */
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun switch (hcd->dev_policy) {
545*4882a593Smuzhiyun case USB_DEVICE_AUTHORIZE_NONE:
546*4882a593Smuzhiyun default:
547*4882a593Smuzhiyun return false;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun case USB_DEVICE_AUTHORIZE_ALL:
550*4882a593Smuzhiyun return true;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun case USB_DEVICE_AUTHORIZE_INTERNAL:
553*4882a593Smuzhiyun hub = usb_hub_to_struct_hub(dev->parent);
554*4882a593Smuzhiyun return hub->ports[dev->portnum - 1]->connect_type ==
555*4882a593Smuzhiyun USB_PORT_CONNECT_TYPE_HARD_WIRED;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /**
560*4882a593Smuzhiyun * usb_alloc_dev - usb device constructor (usbcore-internal)
561*4882a593Smuzhiyun * @parent: hub to which device is connected; null to allocate a root hub
562*4882a593Smuzhiyun * @bus: bus used to access the device
563*4882a593Smuzhiyun * @port1: one-based index of port; ignored for root hubs
564*4882a593Smuzhiyun * Context: !in_interrupt()
565*4882a593Smuzhiyun *
566*4882a593Smuzhiyun * Only hub drivers (including virtual root hub drivers for host
567*4882a593Smuzhiyun * controllers) should ever call this.
568*4882a593Smuzhiyun *
569*4882a593Smuzhiyun * This call may not be used in a non-sleeping context.
570*4882a593Smuzhiyun *
571*4882a593Smuzhiyun * Return: On success, a pointer to the allocated usb device. %NULL on
572*4882a593Smuzhiyun * failure.
573*4882a593Smuzhiyun */
usb_alloc_dev(struct usb_device * parent,struct usb_bus * bus,unsigned port1)574*4882a593Smuzhiyun struct usb_device *usb_alloc_dev(struct usb_device *parent,
575*4882a593Smuzhiyun struct usb_bus *bus, unsigned port1)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun struct usb_device *dev;
578*4882a593Smuzhiyun struct usb_hcd *usb_hcd = bus_to_hcd(bus);
579*4882a593Smuzhiyun unsigned root_hub = 0;
580*4882a593Smuzhiyun unsigned raw_port = port1;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun dev = kzalloc(sizeof(*dev), GFP_KERNEL);
583*4882a593Smuzhiyun if (!dev)
584*4882a593Smuzhiyun return NULL;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun if (!usb_get_hcd(usb_hcd)) {
587*4882a593Smuzhiyun kfree(dev);
588*4882a593Smuzhiyun return NULL;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun /* Root hubs aren't true devices, so don't allocate HCD resources */
591*4882a593Smuzhiyun if (usb_hcd->driver->alloc_dev && parent &&
592*4882a593Smuzhiyun !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
593*4882a593Smuzhiyun usb_put_hcd(bus_to_hcd(bus));
594*4882a593Smuzhiyun kfree(dev);
595*4882a593Smuzhiyun return NULL;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun device_initialize(&dev->dev);
599*4882a593Smuzhiyun dev->dev.bus = &usb_bus_type;
600*4882a593Smuzhiyun dev->dev.type = &usb_device_type;
601*4882a593Smuzhiyun dev->dev.groups = usb_device_groups;
602*4882a593Smuzhiyun set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
603*4882a593Smuzhiyun dev->state = USB_STATE_ATTACHED;
604*4882a593Smuzhiyun dev->lpm_disable_count = 1;
605*4882a593Smuzhiyun atomic_set(&dev->urbnum, 0);
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun INIT_LIST_HEAD(&dev->ep0.urb_list);
608*4882a593Smuzhiyun dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
609*4882a593Smuzhiyun dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
610*4882a593Smuzhiyun /* ep0 maxpacket comes later, from device descriptor */
611*4882a593Smuzhiyun usb_enable_endpoint(dev, &dev->ep0, false);
612*4882a593Smuzhiyun dev->can_submit = 1;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun /* Save readable and stable topology id, distinguishing devices
615*4882a593Smuzhiyun * by location for diagnostics, tools, driver model, etc. The
616*4882a593Smuzhiyun * string is a path along hub ports, from the root. Each device's
617*4882a593Smuzhiyun * dev->devpath will be stable until USB is re-cabled, and hubs
618*4882a593Smuzhiyun * are often labeled with these port numbers. The name isn't
619*4882a593Smuzhiyun * as stable: bus->busnum changes easily from modprobe order,
620*4882a593Smuzhiyun * cardbus or pci hotplugging, and so on.
621*4882a593Smuzhiyun */
622*4882a593Smuzhiyun if (unlikely(!parent)) {
623*4882a593Smuzhiyun dev->devpath[0] = '0';
624*4882a593Smuzhiyun dev->route = 0;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun dev->dev.parent = bus->controller;
627*4882a593Smuzhiyun device_set_of_node_from_dev(&dev->dev, bus->sysdev);
628*4882a593Smuzhiyun dev_set_name(&dev->dev, "usb%d", bus->busnum);
629*4882a593Smuzhiyun root_hub = 1;
630*4882a593Smuzhiyun } else {
631*4882a593Smuzhiyun /* match any labeling on the hubs; it's one-based */
632*4882a593Smuzhiyun if (parent->devpath[0] == '0') {
633*4882a593Smuzhiyun snprintf(dev->devpath, sizeof dev->devpath,
634*4882a593Smuzhiyun "%d", port1);
635*4882a593Smuzhiyun /* Root ports are not counted in route string */
636*4882a593Smuzhiyun dev->route = 0;
637*4882a593Smuzhiyun } else {
638*4882a593Smuzhiyun snprintf(dev->devpath, sizeof dev->devpath,
639*4882a593Smuzhiyun "%s.%d", parent->devpath, port1);
640*4882a593Smuzhiyun /* Route string assumes hubs have less than 16 ports */
641*4882a593Smuzhiyun if (port1 < 15)
642*4882a593Smuzhiyun dev->route = parent->route +
643*4882a593Smuzhiyun (port1 << ((parent->level - 1)*4));
644*4882a593Smuzhiyun else
645*4882a593Smuzhiyun dev->route = parent->route +
646*4882a593Smuzhiyun (15 << ((parent->level - 1)*4));
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun dev->dev.parent = &parent->dev;
650*4882a593Smuzhiyun dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun if (!parent->parent) {
653*4882a593Smuzhiyun /* device under root hub's port */
654*4882a593Smuzhiyun raw_port = usb_hcd_find_raw_port_number(usb_hcd,
655*4882a593Smuzhiyun port1);
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun dev->dev.of_node = usb_of_get_device_node(parent, raw_port);
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /* hub driver sets up TT records */
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun dev->portnum = port1;
663*4882a593Smuzhiyun dev->bus = bus;
664*4882a593Smuzhiyun dev->parent = parent;
665*4882a593Smuzhiyun INIT_LIST_HEAD(&dev->filelist);
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun #ifdef CONFIG_PM
668*4882a593Smuzhiyun pm_runtime_set_autosuspend_delay(&dev->dev,
669*4882a593Smuzhiyun usb_autosuspend_delay * 1000);
670*4882a593Smuzhiyun dev->connect_time = jiffies;
671*4882a593Smuzhiyun dev->active_duration = -jiffies;
672*4882a593Smuzhiyun #endif
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun dev->authorized = usb_dev_authorized(dev, usb_hcd);
675*4882a593Smuzhiyun if (!root_hub)
676*4882a593Smuzhiyun dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun return dev;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_alloc_dev);
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /**
683*4882a593Smuzhiyun * usb_get_dev - increments the reference count of the usb device structure
684*4882a593Smuzhiyun * @dev: the device being referenced
685*4882a593Smuzhiyun *
686*4882a593Smuzhiyun * Each live reference to a device should be refcounted.
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * Drivers for USB interfaces should normally record such references in
689*4882a593Smuzhiyun * their probe() methods, when they bind to an interface, and release
690*4882a593Smuzhiyun * them by calling usb_put_dev(), in their disconnect() methods.
691*4882a593Smuzhiyun *
692*4882a593Smuzhiyun * Return: A pointer to the device with the incremented reference counter.
693*4882a593Smuzhiyun */
usb_get_dev(struct usb_device * dev)694*4882a593Smuzhiyun struct usb_device *usb_get_dev(struct usb_device *dev)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun if (dev)
697*4882a593Smuzhiyun get_device(&dev->dev);
698*4882a593Smuzhiyun return dev;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_get_dev);
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /**
703*4882a593Smuzhiyun * usb_put_dev - release a use of the usb device structure
704*4882a593Smuzhiyun * @dev: device that's been disconnected
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * Must be called when a user of a device is finished with it. When the last
707*4882a593Smuzhiyun * user of the device calls this function, the memory of the device is freed.
708*4882a593Smuzhiyun */
usb_put_dev(struct usb_device * dev)709*4882a593Smuzhiyun void usb_put_dev(struct usb_device *dev)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun if (dev)
712*4882a593Smuzhiyun put_device(&dev->dev);
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_put_dev);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun /**
717*4882a593Smuzhiyun * usb_get_intf - increments the reference count of the usb interface structure
718*4882a593Smuzhiyun * @intf: the interface being referenced
719*4882a593Smuzhiyun *
720*4882a593Smuzhiyun * Each live reference to a interface must be refcounted.
721*4882a593Smuzhiyun *
722*4882a593Smuzhiyun * Drivers for USB interfaces should normally record such references in
723*4882a593Smuzhiyun * their probe() methods, when they bind to an interface, and release
724*4882a593Smuzhiyun * them by calling usb_put_intf(), in their disconnect() methods.
725*4882a593Smuzhiyun *
726*4882a593Smuzhiyun * Return: A pointer to the interface with the incremented reference counter.
727*4882a593Smuzhiyun */
usb_get_intf(struct usb_interface * intf)728*4882a593Smuzhiyun struct usb_interface *usb_get_intf(struct usb_interface *intf)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun if (intf)
731*4882a593Smuzhiyun get_device(&intf->dev);
732*4882a593Smuzhiyun return intf;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_get_intf);
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun /**
737*4882a593Smuzhiyun * usb_put_intf - release a use of the usb interface structure
738*4882a593Smuzhiyun * @intf: interface that's been decremented
739*4882a593Smuzhiyun *
740*4882a593Smuzhiyun * Must be called when a user of an interface is finished with it. When the
741*4882a593Smuzhiyun * last user of the interface calls this function, the memory of the interface
742*4882a593Smuzhiyun * is freed.
743*4882a593Smuzhiyun */
usb_put_intf(struct usb_interface * intf)744*4882a593Smuzhiyun void usb_put_intf(struct usb_interface *intf)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun if (intf)
747*4882a593Smuzhiyun put_device(&intf->dev);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_put_intf);
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun /**
752*4882a593Smuzhiyun * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
753*4882a593Smuzhiyun * @intf: the usb interface
754*4882a593Smuzhiyun *
755*4882a593Smuzhiyun * While a USB device cannot perform DMA operations by itself, many USB
756*4882a593Smuzhiyun * controllers can. A call to usb_intf_get_dma_device() returns the DMA endpoint
757*4882a593Smuzhiyun * for the given USB interface, if any. The returned device structure must be
758*4882a593Smuzhiyun * released with put_device().
759*4882a593Smuzhiyun *
760*4882a593Smuzhiyun * See also usb_get_dma_device().
761*4882a593Smuzhiyun *
762*4882a593Smuzhiyun * Returns: A reference to the usb interface's DMA endpoint; or NULL if none
763*4882a593Smuzhiyun * exists.
764*4882a593Smuzhiyun */
usb_intf_get_dma_device(struct usb_interface * intf)765*4882a593Smuzhiyun struct device *usb_intf_get_dma_device(struct usb_interface *intf)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(intf);
768*4882a593Smuzhiyun struct device *dmadev;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun if (!udev->bus)
771*4882a593Smuzhiyun return NULL;
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun dmadev = get_device(udev->bus->sysdev);
774*4882a593Smuzhiyun if (!dmadev || !dmadev->dma_mask) {
775*4882a593Smuzhiyun put_device(dmadev);
776*4882a593Smuzhiyun return NULL;
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun return dmadev;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_intf_get_dma_device);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /* USB device locking
784*4882a593Smuzhiyun *
785*4882a593Smuzhiyun * USB devices and interfaces are locked using the semaphore in their
786*4882a593Smuzhiyun * embedded struct device. The hub driver guarantees that whenever a
787*4882a593Smuzhiyun * device is connected or disconnected, drivers are called with the
788*4882a593Smuzhiyun * USB device locked as well as their particular interface.
789*4882a593Smuzhiyun *
790*4882a593Smuzhiyun * Complications arise when several devices are to be locked at the same
791*4882a593Smuzhiyun * time. Only hub-aware drivers that are part of usbcore ever have to
792*4882a593Smuzhiyun * do this; nobody else needs to worry about it. The rule for locking
793*4882a593Smuzhiyun * is simple:
794*4882a593Smuzhiyun *
795*4882a593Smuzhiyun * When locking both a device and its parent, always lock the
796*4882a593Smuzhiyun * the parent first.
797*4882a593Smuzhiyun */
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun /**
800*4882a593Smuzhiyun * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
801*4882a593Smuzhiyun * @udev: device that's being locked
802*4882a593Smuzhiyun * @iface: interface bound to the driver making the request (optional)
803*4882a593Smuzhiyun *
804*4882a593Smuzhiyun * Attempts to acquire the device lock, but fails if the device is
805*4882a593Smuzhiyun * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
806*4882a593Smuzhiyun * is neither BINDING nor BOUND. Rather than sleeping to wait for the
807*4882a593Smuzhiyun * lock, the routine polls repeatedly. This is to prevent deadlock with
808*4882a593Smuzhiyun * disconnect; in some drivers (such as usb-storage) the disconnect()
809*4882a593Smuzhiyun * or suspend() method will block waiting for a device reset to complete.
810*4882a593Smuzhiyun *
811*4882a593Smuzhiyun * Return: A negative error code for failure, otherwise 0.
812*4882a593Smuzhiyun */
usb_lock_device_for_reset(struct usb_device * udev,const struct usb_interface * iface)813*4882a593Smuzhiyun int usb_lock_device_for_reset(struct usb_device *udev,
814*4882a593Smuzhiyun const struct usb_interface *iface)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun unsigned long jiffies_expire = jiffies + HZ;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun if (udev->state == USB_STATE_NOTATTACHED)
819*4882a593Smuzhiyun return -ENODEV;
820*4882a593Smuzhiyun if (udev->state == USB_STATE_SUSPENDED)
821*4882a593Smuzhiyun return -EHOSTUNREACH;
822*4882a593Smuzhiyun if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
823*4882a593Smuzhiyun iface->condition == USB_INTERFACE_UNBOUND))
824*4882a593Smuzhiyun return -EINTR;
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun while (!usb_trylock_device(udev)) {
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun /* If we can't acquire the lock after waiting one second,
829*4882a593Smuzhiyun * we're probably deadlocked */
830*4882a593Smuzhiyun if (time_after(jiffies, jiffies_expire))
831*4882a593Smuzhiyun return -EBUSY;
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun msleep(15);
834*4882a593Smuzhiyun if (udev->state == USB_STATE_NOTATTACHED)
835*4882a593Smuzhiyun return -ENODEV;
836*4882a593Smuzhiyun if (udev->state == USB_STATE_SUSPENDED)
837*4882a593Smuzhiyun return -EHOSTUNREACH;
838*4882a593Smuzhiyun if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
839*4882a593Smuzhiyun iface->condition == USB_INTERFACE_UNBOUND))
840*4882a593Smuzhiyun return -EINTR;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun return 0;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /**
847*4882a593Smuzhiyun * usb_get_current_frame_number - return current bus frame number
848*4882a593Smuzhiyun * @dev: the device whose bus is being queried
849*4882a593Smuzhiyun *
850*4882a593Smuzhiyun * Return: The current frame number for the USB host controller used
851*4882a593Smuzhiyun * with the given USB device. This can be used when scheduling
852*4882a593Smuzhiyun * isochronous requests.
853*4882a593Smuzhiyun *
854*4882a593Smuzhiyun * Note: Different kinds of host controller have different "scheduling
855*4882a593Smuzhiyun * horizons". While one type might support scheduling only 32 frames
856*4882a593Smuzhiyun * into the future, others could support scheduling up to 1024 frames
857*4882a593Smuzhiyun * into the future.
858*4882a593Smuzhiyun *
859*4882a593Smuzhiyun */
usb_get_current_frame_number(struct usb_device * dev)860*4882a593Smuzhiyun int usb_get_current_frame_number(struct usb_device *dev)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun return usb_hcd_get_frame_number(dev);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /*-------------------------------------------------------------------*/
867*4882a593Smuzhiyun /*
868*4882a593Smuzhiyun * __usb_get_extra_descriptor() finds a descriptor of specific type in the
869*4882a593Smuzhiyun * extra field of the interface and endpoint descriptor structs.
870*4882a593Smuzhiyun */
871*4882a593Smuzhiyun
__usb_get_extra_descriptor(char * buffer,unsigned size,unsigned char type,void ** ptr,size_t minsize)872*4882a593Smuzhiyun int __usb_get_extra_descriptor(char *buffer, unsigned size,
873*4882a593Smuzhiyun unsigned char type, void **ptr, size_t minsize)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun struct usb_descriptor_header *header;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun while (size >= sizeof(struct usb_descriptor_header)) {
878*4882a593Smuzhiyun header = (struct usb_descriptor_header *)buffer;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun if (header->bLength < 2 || header->bLength > size) {
881*4882a593Smuzhiyun printk(KERN_ERR
882*4882a593Smuzhiyun "%s: bogus descriptor, type %d length %d\n",
883*4882a593Smuzhiyun usbcore_name,
884*4882a593Smuzhiyun header->bDescriptorType,
885*4882a593Smuzhiyun header->bLength);
886*4882a593Smuzhiyun return -1;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun if (header->bDescriptorType == type && header->bLength >= minsize) {
890*4882a593Smuzhiyun *ptr = header;
891*4882a593Smuzhiyun return 0;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun buffer += header->bLength;
895*4882a593Smuzhiyun size -= header->bLength;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun return -1;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun /**
902*4882a593Smuzhiyun * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
903*4882a593Smuzhiyun * @dev: device the buffer will be used with
904*4882a593Smuzhiyun * @size: requested buffer size
905*4882a593Smuzhiyun * @mem_flags: affect whether allocation may block
906*4882a593Smuzhiyun * @dma: used to return DMA address of buffer
907*4882a593Smuzhiyun *
908*4882a593Smuzhiyun * Return: Either null (indicating no buffer could be allocated), or the
909*4882a593Smuzhiyun * cpu-space pointer to a buffer that may be used to perform DMA to the
910*4882a593Smuzhiyun * specified device. Such cpu-space buffers are returned along with the DMA
911*4882a593Smuzhiyun * address (through the pointer provided).
912*4882a593Smuzhiyun *
913*4882a593Smuzhiyun * Note:
914*4882a593Smuzhiyun * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
915*4882a593Smuzhiyun * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
916*4882a593Smuzhiyun * hardware during URB completion/resubmit. The implementation varies between
917*4882a593Smuzhiyun * platforms, depending on details of how DMA will work to this device.
918*4882a593Smuzhiyun * Using these buffers also eliminates cacheline sharing problems on
919*4882a593Smuzhiyun * architectures where CPU caches are not DMA-coherent. On systems without
920*4882a593Smuzhiyun * bus-snooping caches, these buffers are uncached.
921*4882a593Smuzhiyun *
922*4882a593Smuzhiyun * When the buffer is no longer used, free it with usb_free_coherent().
923*4882a593Smuzhiyun */
usb_alloc_coherent(struct usb_device * dev,size_t size,gfp_t mem_flags,dma_addr_t * dma)924*4882a593Smuzhiyun void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
925*4882a593Smuzhiyun dma_addr_t *dma)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun if (!dev || !dev->bus)
928*4882a593Smuzhiyun return NULL;
929*4882a593Smuzhiyun return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_alloc_coherent);
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun /**
934*4882a593Smuzhiyun * usb_free_coherent - free memory allocated with usb_alloc_coherent()
935*4882a593Smuzhiyun * @dev: device the buffer was used with
936*4882a593Smuzhiyun * @size: requested buffer size
937*4882a593Smuzhiyun * @addr: CPU address of buffer
938*4882a593Smuzhiyun * @dma: DMA address of buffer
939*4882a593Smuzhiyun *
940*4882a593Smuzhiyun * This reclaims an I/O buffer, letting it be reused. The memory must have
941*4882a593Smuzhiyun * been allocated using usb_alloc_coherent(), and the parameters must match
942*4882a593Smuzhiyun * those provided in that allocation request.
943*4882a593Smuzhiyun */
usb_free_coherent(struct usb_device * dev,size_t size,void * addr,dma_addr_t dma)944*4882a593Smuzhiyun void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
945*4882a593Smuzhiyun dma_addr_t dma)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun if (!dev || !dev->bus)
948*4882a593Smuzhiyun return;
949*4882a593Smuzhiyun if (!addr)
950*4882a593Smuzhiyun return;
951*4882a593Smuzhiyun hcd_buffer_free(dev->bus, size, addr, dma);
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_free_coherent);
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /*
956*4882a593Smuzhiyun * Notifications of device and interface registration
957*4882a593Smuzhiyun */
usb_bus_notify(struct notifier_block * nb,unsigned long action,void * data)958*4882a593Smuzhiyun static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
959*4882a593Smuzhiyun void *data)
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun struct device *dev = data;
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun switch (action) {
964*4882a593Smuzhiyun case BUS_NOTIFY_ADD_DEVICE:
965*4882a593Smuzhiyun if (dev->type == &usb_device_type)
966*4882a593Smuzhiyun (void) usb_create_sysfs_dev_files(to_usb_device(dev));
967*4882a593Smuzhiyun else if (dev->type == &usb_if_device_type)
968*4882a593Smuzhiyun usb_create_sysfs_intf_files(to_usb_interface(dev));
969*4882a593Smuzhiyun break;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun case BUS_NOTIFY_DEL_DEVICE:
972*4882a593Smuzhiyun if (dev->type == &usb_device_type)
973*4882a593Smuzhiyun usb_remove_sysfs_dev_files(to_usb_device(dev));
974*4882a593Smuzhiyun else if (dev->type == &usb_if_device_type)
975*4882a593Smuzhiyun usb_remove_sysfs_intf_files(to_usb_interface(dev));
976*4882a593Smuzhiyun break;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun static struct notifier_block usb_bus_nb = {
982*4882a593Smuzhiyun .notifier_call = usb_bus_notify,
983*4882a593Smuzhiyun };
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun static struct dentry *usb_devices_root;
986*4882a593Smuzhiyun
usb_debugfs_init(void)987*4882a593Smuzhiyun static void usb_debugfs_init(void)
988*4882a593Smuzhiyun {
989*4882a593Smuzhiyun usb_devices_root = debugfs_create_file("devices", 0444, usb_debug_root,
990*4882a593Smuzhiyun NULL, &usbfs_devices_fops);
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun
usb_debugfs_cleanup(void)993*4882a593Smuzhiyun static void usb_debugfs_cleanup(void)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun debugfs_remove(usb_devices_root);
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun /*
999*4882a593Smuzhiyun * Init
1000*4882a593Smuzhiyun */
usb_init(void)1001*4882a593Smuzhiyun static int __init usb_init(void)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun int retval;
1004*4882a593Smuzhiyun if (usb_disabled()) {
1005*4882a593Smuzhiyun pr_info("%s: USB support disabled\n", usbcore_name);
1006*4882a593Smuzhiyun return 0;
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun usb_init_pool_max();
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun usb_debugfs_init();
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun usb_acpi_register();
1013*4882a593Smuzhiyun retval = bus_register(&usb_bus_type);
1014*4882a593Smuzhiyun if (retval)
1015*4882a593Smuzhiyun goto bus_register_failed;
1016*4882a593Smuzhiyun retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1017*4882a593Smuzhiyun if (retval)
1018*4882a593Smuzhiyun goto bus_notifier_failed;
1019*4882a593Smuzhiyun retval = usb_major_init();
1020*4882a593Smuzhiyun if (retval)
1021*4882a593Smuzhiyun goto major_init_failed;
1022*4882a593Smuzhiyun retval = usb_register(&usbfs_driver);
1023*4882a593Smuzhiyun if (retval)
1024*4882a593Smuzhiyun goto driver_register_failed;
1025*4882a593Smuzhiyun retval = usb_devio_init();
1026*4882a593Smuzhiyun if (retval)
1027*4882a593Smuzhiyun goto usb_devio_init_failed;
1028*4882a593Smuzhiyun retval = usb_hub_init();
1029*4882a593Smuzhiyun if (retval)
1030*4882a593Smuzhiyun goto hub_init_failed;
1031*4882a593Smuzhiyun retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
1032*4882a593Smuzhiyun if (!retval)
1033*4882a593Smuzhiyun goto out;
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun usb_hub_cleanup();
1036*4882a593Smuzhiyun hub_init_failed:
1037*4882a593Smuzhiyun usb_devio_cleanup();
1038*4882a593Smuzhiyun usb_devio_init_failed:
1039*4882a593Smuzhiyun usb_deregister(&usbfs_driver);
1040*4882a593Smuzhiyun driver_register_failed:
1041*4882a593Smuzhiyun usb_major_cleanup();
1042*4882a593Smuzhiyun major_init_failed:
1043*4882a593Smuzhiyun bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1044*4882a593Smuzhiyun bus_notifier_failed:
1045*4882a593Smuzhiyun bus_unregister(&usb_bus_type);
1046*4882a593Smuzhiyun bus_register_failed:
1047*4882a593Smuzhiyun usb_acpi_unregister();
1048*4882a593Smuzhiyun usb_debugfs_cleanup();
1049*4882a593Smuzhiyun out:
1050*4882a593Smuzhiyun return retval;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun /*
1054*4882a593Smuzhiyun * Cleanup
1055*4882a593Smuzhiyun */
usb_exit(void)1056*4882a593Smuzhiyun static void __exit usb_exit(void)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun /* This will matter if shutdown/reboot does exitcalls. */
1059*4882a593Smuzhiyun if (usb_disabled())
1060*4882a593Smuzhiyun return;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun usb_release_quirk_list();
1063*4882a593Smuzhiyun usb_deregister_device_driver(&usb_generic_driver);
1064*4882a593Smuzhiyun usb_major_cleanup();
1065*4882a593Smuzhiyun usb_deregister(&usbfs_driver);
1066*4882a593Smuzhiyun usb_devio_cleanup();
1067*4882a593Smuzhiyun usb_hub_cleanup();
1068*4882a593Smuzhiyun bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1069*4882a593Smuzhiyun bus_unregister(&usb_bus_type);
1070*4882a593Smuzhiyun usb_acpi_unregister();
1071*4882a593Smuzhiyun usb_debugfs_cleanup();
1072*4882a593Smuzhiyun idr_destroy(&usb_bus_idr);
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun subsys_initcall(usb_init);
1076*4882a593Smuzhiyun module_exit(usb_exit);
1077*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1078