Commit 98d9a82e5f753a2483d7b4638802d60e94e5d2e4

Authored by Oliver Neukum
Committed by Greg Kroah-Hartman
1 parent 1493138af1

USB: cleanup the handling of the PM complete call

This eliminates the last instance of a function's behavior
controlled by a parameter as Linus hates such things.

Signed-off-by: Oliver Neukum <oneukum@suse.de>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 3 changed files with 22 additions and 18 deletions Inline Diff

drivers/usb/core/driver.c
1 /* 1 /*
2 * drivers/usb/driver.c - most of the driver model stuff for usb 2 * drivers/usb/driver.c - most of the driver model stuff for usb
3 * 3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de> 4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 * 5 *
6 * based on drivers/usb/usb.c which had the following copyrights: 6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999 7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001 8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999 9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999 10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture) 11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000 12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004 13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000 14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter) 15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003 16 * (C) Copyright Greg Kroah-Hartman 2002-2003
17 * 17 *
18 * NOTE! This is not actually a driver at all, rather this is 18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the 19 * just a collection of helper routines that implement the
20 * matching, probing, releasing, suspending and resuming for 20 * matching, probing, releasing, suspending and resuming for
21 * real drivers. 21 * real drivers.
22 * 22 *
23 */ 23 */
24 24
25 #include <linux/device.h> 25 #include <linux/device.h>
26 #include <linux/slab.h> 26 #include <linux/slab.h>
27 #include <linux/export.h> 27 #include <linux/export.h>
28 #include <linux/usb.h> 28 #include <linux/usb.h>
29 #include <linux/usb/quirks.h> 29 #include <linux/usb/quirks.h>
30 #include <linux/usb/hcd.h> 30 #include <linux/usb/hcd.h>
31 31
32 #include "usb.h" 32 #include "usb.h"
33 33
34 34
35 #ifdef CONFIG_HOTPLUG 35 #ifdef CONFIG_HOTPLUG
36 36
37 /* 37 /*
38 * Adds a new dynamic USBdevice ID to this driver, 38 * Adds a new dynamic USBdevice ID to this driver,
39 * and cause the driver to probe for all devices again. 39 * and cause the driver to probe for all devices again.
40 */ 40 */
41 ssize_t usb_store_new_id(struct usb_dynids *dynids, 41 ssize_t usb_store_new_id(struct usb_dynids *dynids,
42 struct device_driver *driver, 42 struct device_driver *driver,
43 const char *buf, size_t count) 43 const char *buf, size_t count)
44 { 44 {
45 struct usb_dynid *dynid; 45 struct usb_dynid *dynid;
46 u32 idVendor = 0; 46 u32 idVendor = 0;
47 u32 idProduct = 0; 47 u32 idProduct = 0;
48 unsigned int bInterfaceClass = 0; 48 unsigned int bInterfaceClass = 0;
49 int fields = 0; 49 int fields = 0;
50 int retval = 0; 50 int retval = 0;
51 51
52 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct, 52 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass); 53 &bInterfaceClass);
54 if (fields < 2) 54 if (fields < 2)
55 return -EINVAL; 55 return -EINVAL;
56 56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); 57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid) 58 if (!dynid)
59 return -ENOMEM; 59 return -ENOMEM;
60 60
61 INIT_LIST_HEAD(&dynid->node); 61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor; 62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct; 63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; 64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
65 if (fields == 3) { 65 if (fields == 3) {
66 dynid->id.bInterfaceClass = (u8)bInterfaceClass; 66 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
67 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; 67 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
68 } 68 }
69 69
70 spin_lock(&dynids->lock); 70 spin_lock(&dynids->lock);
71 list_add_tail(&dynid->node, &dynids->list); 71 list_add_tail(&dynid->node, &dynids->list);
72 spin_unlock(&dynids->lock); 72 spin_unlock(&dynids->lock);
73 73
74 if (get_driver(driver)) { 74 if (get_driver(driver)) {
75 retval = driver_attach(driver); 75 retval = driver_attach(driver);
76 put_driver(driver); 76 put_driver(driver);
77 } 77 }
78 78
79 if (retval) 79 if (retval)
80 return retval; 80 return retval;
81 return count; 81 return count;
82 } 82 }
83 EXPORT_SYMBOL_GPL(usb_store_new_id); 83 EXPORT_SYMBOL_GPL(usb_store_new_id);
84 84
85 static ssize_t store_new_id(struct device_driver *driver, 85 static ssize_t store_new_id(struct device_driver *driver,
86 const char *buf, size_t count) 86 const char *buf, size_t count)
87 { 87 {
88 struct usb_driver *usb_drv = to_usb_driver(driver); 88 struct usb_driver *usb_drv = to_usb_driver(driver);
89 89
90 return usb_store_new_id(&usb_drv->dynids, driver, buf, count); 90 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
91 } 91 }
92 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); 92 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
93 93
94 /** 94 /**
95 * store_remove_id - remove a USB device ID from this driver 95 * store_remove_id - remove a USB device ID from this driver
96 * @driver: target device driver 96 * @driver: target device driver
97 * @buf: buffer for scanning device ID data 97 * @buf: buffer for scanning device ID data
98 * @count: input size 98 * @count: input size
99 * 99 *
100 * Removes a dynamic usb device ID from this driver. 100 * Removes a dynamic usb device ID from this driver.
101 */ 101 */
102 static ssize_t 102 static ssize_t
103 store_remove_id(struct device_driver *driver, const char *buf, size_t count) 103 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
104 { 104 {
105 struct usb_dynid *dynid, *n; 105 struct usb_dynid *dynid, *n;
106 struct usb_driver *usb_driver = to_usb_driver(driver); 106 struct usb_driver *usb_driver = to_usb_driver(driver);
107 u32 idVendor = 0; 107 u32 idVendor = 0;
108 u32 idProduct = 0; 108 u32 idProduct = 0;
109 int fields = 0; 109 int fields = 0;
110 int retval = 0; 110 int retval = 0;
111 111
112 fields = sscanf(buf, "%x %x", &idVendor, &idProduct); 112 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
113 if (fields < 2) 113 if (fields < 2)
114 return -EINVAL; 114 return -EINVAL;
115 115
116 spin_lock(&usb_driver->dynids.lock); 116 spin_lock(&usb_driver->dynids.lock);
117 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) { 117 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
118 struct usb_device_id *id = &dynid->id; 118 struct usb_device_id *id = &dynid->id;
119 if ((id->idVendor == idVendor) && 119 if ((id->idVendor == idVendor) &&
120 (id->idProduct == idProduct)) { 120 (id->idProduct == idProduct)) {
121 list_del(&dynid->node); 121 list_del(&dynid->node);
122 kfree(dynid); 122 kfree(dynid);
123 retval = 0; 123 retval = 0;
124 break; 124 break;
125 } 125 }
126 } 126 }
127 spin_unlock(&usb_driver->dynids.lock); 127 spin_unlock(&usb_driver->dynids.lock);
128 128
129 if (retval) 129 if (retval)
130 return retval; 130 return retval;
131 return count; 131 return count;
132 } 132 }
133 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id); 133 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
134 134
135 static int usb_create_newid_file(struct usb_driver *usb_drv) 135 static int usb_create_newid_file(struct usb_driver *usb_drv)
136 { 136 {
137 int error = 0; 137 int error = 0;
138 138
139 if (usb_drv->no_dynamic_id) 139 if (usb_drv->no_dynamic_id)
140 goto exit; 140 goto exit;
141 141
142 if (usb_drv->probe != NULL) 142 if (usb_drv->probe != NULL)
143 error = driver_create_file(&usb_drv->drvwrap.driver, 143 error = driver_create_file(&usb_drv->drvwrap.driver,
144 &driver_attr_new_id); 144 &driver_attr_new_id);
145 exit: 145 exit:
146 return error; 146 return error;
147 } 147 }
148 148
149 static void usb_remove_newid_file(struct usb_driver *usb_drv) 149 static void usb_remove_newid_file(struct usb_driver *usb_drv)
150 { 150 {
151 if (usb_drv->no_dynamic_id) 151 if (usb_drv->no_dynamic_id)
152 return; 152 return;
153 153
154 if (usb_drv->probe != NULL) 154 if (usb_drv->probe != NULL)
155 driver_remove_file(&usb_drv->drvwrap.driver, 155 driver_remove_file(&usb_drv->drvwrap.driver,
156 &driver_attr_new_id); 156 &driver_attr_new_id);
157 } 157 }
158 158
159 static int 159 static int
160 usb_create_removeid_file(struct usb_driver *drv) 160 usb_create_removeid_file(struct usb_driver *drv)
161 { 161 {
162 int error = 0; 162 int error = 0;
163 if (drv->probe != NULL) 163 if (drv->probe != NULL)
164 error = driver_create_file(&drv->drvwrap.driver, 164 error = driver_create_file(&drv->drvwrap.driver,
165 &driver_attr_remove_id); 165 &driver_attr_remove_id);
166 return error; 166 return error;
167 } 167 }
168 168
169 static void usb_remove_removeid_file(struct usb_driver *drv) 169 static void usb_remove_removeid_file(struct usb_driver *drv)
170 { 170 {
171 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id); 171 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
172 } 172 }
173 173
174 static void usb_free_dynids(struct usb_driver *usb_drv) 174 static void usb_free_dynids(struct usb_driver *usb_drv)
175 { 175 {
176 struct usb_dynid *dynid, *n; 176 struct usb_dynid *dynid, *n;
177 177
178 spin_lock(&usb_drv->dynids.lock); 178 spin_lock(&usb_drv->dynids.lock);
179 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { 179 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
180 list_del(&dynid->node); 180 list_del(&dynid->node);
181 kfree(dynid); 181 kfree(dynid);
182 } 182 }
183 spin_unlock(&usb_drv->dynids.lock); 183 spin_unlock(&usb_drv->dynids.lock);
184 } 184 }
185 #else 185 #else
186 static inline int usb_create_newid_file(struct usb_driver *usb_drv) 186 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
187 { 187 {
188 return 0; 188 return 0;
189 } 189 }
190 190
191 static void usb_remove_newid_file(struct usb_driver *usb_drv) 191 static void usb_remove_newid_file(struct usb_driver *usb_drv)
192 { 192 {
193 } 193 }
194 194
195 static int 195 static int
196 usb_create_removeid_file(struct usb_driver *drv) 196 usb_create_removeid_file(struct usb_driver *drv)
197 { 197 {
198 return 0; 198 return 0;
199 } 199 }
200 200
201 static void usb_remove_removeid_file(struct usb_driver *drv) 201 static void usb_remove_removeid_file(struct usb_driver *drv)
202 { 202 {
203 } 203 }
204 204
205 static inline void usb_free_dynids(struct usb_driver *usb_drv) 205 static inline void usb_free_dynids(struct usb_driver *usb_drv)
206 { 206 {
207 } 207 }
208 #endif 208 #endif
209 209
210 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, 210 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
211 struct usb_driver *drv) 211 struct usb_driver *drv)
212 { 212 {
213 struct usb_dynid *dynid; 213 struct usb_dynid *dynid;
214 214
215 spin_lock(&drv->dynids.lock); 215 spin_lock(&drv->dynids.lock);
216 list_for_each_entry(dynid, &drv->dynids.list, node) { 216 list_for_each_entry(dynid, &drv->dynids.list, node) {
217 if (usb_match_one_id(intf, &dynid->id)) { 217 if (usb_match_one_id(intf, &dynid->id)) {
218 spin_unlock(&drv->dynids.lock); 218 spin_unlock(&drv->dynids.lock);
219 return &dynid->id; 219 return &dynid->id;
220 } 220 }
221 } 221 }
222 spin_unlock(&drv->dynids.lock); 222 spin_unlock(&drv->dynids.lock);
223 return NULL; 223 return NULL;
224 } 224 }
225 225
226 226
227 /* called from driver core with dev locked */ 227 /* called from driver core with dev locked */
228 static int usb_probe_device(struct device *dev) 228 static int usb_probe_device(struct device *dev)
229 { 229 {
230 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 230 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
231 struct usb_device *udev = to_usb_device(dev); 231 struct usb_device *udev = to_usb_device(dev);
232 int error = 0; 232 int error = 0;
233 233
234 dev_dbg(dev, "%s\n", __func__); 234 dev_dbg(dev, "%s\n", __func__);
235 235
236 /* TODO: Add real matching code */ 236 /* TODO: Add real matching code */
237 237
238 /* The device should always appear to be in use 238 /* The device should always appear to be in use
239 * unless the driver suports autosuspend. 239 * unless the driver suports autosuspend.
240 */ 240 */
241 if (!udriver->supports_autosuspend) 241 if (!udriver->supports_autosuspend)
242 error = usb_autoresume_device(udev); 242 error = usb_autoresume_device(udev);
243 243
244 if (!error) 244 if (!error)
245 error = udriver->probe(udev); 245 error = udriver->probe(udev);
246 return error; 246 return error;
247 } 247 }
248 248
249 /* called from driver core with dev locked */ 249 /* called from driver core with dev locked */
250 static int usb_unbind_device(struct device *dev) 250 static int usb_unbind_device(struct device *dev)
251 { 251 {
252 struct usb_device *udev = to_usb_device(dev); 252 struct usb_device *udev = to_usb_device(dev);
253 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 253 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
254 254
255 udriver->disconnect(udev); 255 udriver->disconnect(udev);
256 if (!udriver->supports_autosuspend) 256 if (!udriver->supports_autosuspend)
257 usb_autosuspend_device(udev); 257 usb_autosuspend_device(udev);
258 return 0; 258 return 0;
259 } 259 }
260 260
261 /* 261 /*
262 * Cancel any pending scheduled resets 262 * Cancel any pending scheduled resets
263 * 263 *
264 * [see usb_queue_reset_device()] 264 * [see usb_queue_reset_device()]
265 * 265 *
266 * Called after unconfiguring / when releasing interfaces. See 266 * Called after unconfiguring / when releasing interfaces. See
267 * comments in __usb_queue_reset_device() regarding 267 * comments in __usb_queue_reset_device() regarding
268 * udev->reset_running. 268 * udev->reset_running.
269 */ 269 */
270 static void usb_cancel_queued_reset(struct usb_interface *iface) 270 static void usb_cancel_queued_reset(struct usb_interface *iface)
271 { 271 {
272 if (iface->reset_running == 0) 272 if (iface->reset_running == 0)
273 cancel_work_sync(&iface->reset_ws); 273 cancel_work_sync(&iface->reset_ws);
274 } 274 }
275 275
276 /* called from driver core with dev locked */ 276 /* called from driver core with dev locked */
277 static int usb_probe_interface(struct device *dev) 277 static int usb_probe_interface(struct device *dev)
278 { 278 {
279 struct usb_driver *driver = to_usb_driver(dev->driver); 279 struct usb_driver *driver = to_usb_driver(dev->driver);
280 struct usb_interface *intf = to_usb_interface(dev); 280 struct usb_interface *intf = to_usb_interface(dev);
281 struct usb_device *udev = interface_to_usbdev(intf); 281 struct usb_device *udev = interface_to_usbdev(intf);
282 const struct usb_device_id *id; 282 const struct usb_device_id *id;
283 int error = -ENODEV; 283 int error = -ENODEV;
284 284
285 dev_dbg(dev, "%s\n", __func__); 285 dev_dbg(dev, "%s\n", __func__);
286 286
287 intf->needs_binding = 0; 287 intf->needs_binding = 0;
288 288
289 if (usb_device_is_owned(udev)) 289 if (usb_device_is_owned(udev))
290 return error; 290 return error;
291 291
292 if (udev->authorized == 0) { 292 if (udev->authorized == 0) {
293 dev_err(&intf->dev, "Device is not authorized for usage\n"); 293 dev_err(&intf->dev, "Device is not authorized for usage\n");
294 return error; 294 return error;
295 } 295 }
296 296
297 id = usb_match_id(intf, driver->id_table); 297 id = usb_match_id(intf, driver->id_table);
298 if (!id) 298 if (!id)
299 id = usb_match_dynamic_id(intf, driver); 299 id = usb_match_dynamic_id(intf, driver);
300 if (!id) 300 if (!id)
301 return error; 301 return error;
302 302
303 dev_dbg(dev, "%s - got id\n", __func__); 303 dev_dbg(dev, "%s - got id\n", __func__);
304 304
305 error = usb_autoresume_device(udev); 305 error = usb_autoresume_device(udev);
306 if (error) 306 if (error)
307 return error; 307 return error;
308 308
309 intf->condition = USB_INTERFACE_BINDING; 309 intf->condition = USB_INTERFACE_BINDING;
310 310
311 /* Probed interfaces are initially active. They are 311 /* Probed interfaces are initially active. They are
312 * runtime-PM-enabled only if the driver has autosuspend support. 312 * runtime-PM-enabled only if the driver has autosuspend support.
313 * They are sensitive to their children's power states. 313 * They are sensitive to their children's power states.
314 */ 314 */
315 pm_runtime_set_active(dev); 315 pm_runtime_set_active(dev);
316 pm_suspend_ignore_children(dev, false); 316 pm_suspend_ignore_children(dev, false);
317 if (driver->supports_autosuspend) 317 if (driver->supports_autosuspend)
318 pm_runtime_enable(dev); 318 pm_runtime_enable(dev);
319 319
320 /* Carry out a deferred switch to altsetting 0 */ 320 /* Carry out a deferred switch to altsetting 0 */
321 if (intf->needs_altsetting0) { 321 if (intf->needs_altsetting0) {
322 error = usb_set_interface(udev, intf->altsetting[0]. 322 error = usb_set_interface(udev, intf->altsetting[0].
323 desc.bInterfaceNumber, 0); 323 desc.bInterfaceNumber, 0);
324 if (error < 0) 324 if (error < 0)
325 goto err; 325 goto err;
326 intf->needs_altsetting0 = 0; 326 intf->needs_altsetting0 = 0;
327 } 327 }
328 328
329 error = driver->probe(intf, id); 329 error = driver->probe(intf, id);
330 if (error) 330 if (error)
331 goto err; 331 goto err;
332 332
333 intf->condition = USB_INTERFACE_BOUND; 333 intf->condition = USB_INTERFACE_BOUND;
334 usb_autosuspend_device(udev); 334 usb_autosuspend_device(udev);
335 return error; 335 return error;
336 336
337 err: 337 err:
338 intf->needs_remote_wakeup = 0; 338 intf->needs_remote_wakeup = 0;
339 intf->condition = USB_INTERFACE_UNBOUND; 339 intf->condition = USB_INTERFACE_UNBOUND;
340 usb_cancel_queued_reset(intf); 340 usb_cancel_queued_reset(intf);
341 341
342 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 342 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
343 if (driver->supports_autosuspend) 343 if (driver->supports_autosuspend)
344 pm_runtime_disable(dev); 344 pm_runtime_disable(dev);
345 pm_runtime_set_suspended(dev); 345 pm_runtime_set_suspended(dev);
346 346
347 usb_autosuspend_device(udev); 347 usb_autosuspend_device(udev);
348 return error; 348 return error;
349 } 349 }
350 350
351 /* called from driver core with dev locked */ 351 /* called from driver core with dev locked */
352 static int usb_unbind_interface(struct device *dev) 352 static int usb_unbind_interface(struct device *dev)
353 { 353 {
354 struct usb_driver *driver = to_usb_driver(dev->driver); 354 struct usb_driver *driver = to_usb_driver(dev->driver);
355 struct usb_interface *intf = to_usb_interface(dev); 355 struct usb_interface *intf = to_usb_interface(dev);
356 struct usb_device *udev; 356 struct usb_device *udev;
357 int error, r; 357 int error, r;
358 358
359 intf->condition = USB_INTERFACE_UNBINDING; 359 intf->condition = USB_INTERFACE_UNBINDING;
360 360
361 /* Autoresume for set_interface call below */ 361 /* Autoresume for set_interface call below */
362 udev = interface_to_usbdev(intf); 362 udev = interface_to_usbdev(intf);
363 error = usb_autoresume_device(udev); 363 error = usb_autoresume_device(udev);
364 364
365 /* Terminate all URBs for this interface unless the driver 365 /* Terminate all URBs for this interface unless the driver
366 * supports "soft" unbinding. 366 * supports "soft" unbinding.
367 */ 367 */
368 if (!driver->soft_unbind) 368 if (!driver->soft_unbind)
369 usb_disable_interface(udev, intf, false); 369 usb_disable_interface(udev, intf, false);
370 370
371 driver->disconnect(intf); 371 driver->disconnect(intf);
372 usb_cancel_queued_reset(intf); 372 usb_cancel_queued_reset(intf);
373 373
374 /* Reset other interface state. 374 /* Reset other interface state.
375 * We cannot do a Set-Interface if the device is suspended or 375 * We cannot do a Set-Interface if the device is suspended or
376 * if it is prepared for a system sleep (since installing a new 376 * if it is prepared for a system sleep (since installing a new
377 * altsetting means creating new endpoint device entries). 377 * altsetting means creating new endpoint device entries).
378 * When either of these happens, defer the Set-Interface. 378 * When either of these happens, defer the Set-Interface.
379 */ 379 */
380 if (intf->cur_altsetting->desc.bAlternateSetting == 0) { 380 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
381 /* Already in altsetting 0 so skip Set-Interface. 381 /* Already in altsetting 0 so skip Set-Interface.
382 * Just re-enable it without affecting the endpoint toggles. 382 * Just re-enable it without affecting the endpoint toggles.
383 */ 383 */
384 usb_enable_interface(udev, intf, false); 384 usb_enable_interface(udev, intf, false);
385 } else if (!error && !intf->dev.power.is_prepared) { 385 } else if (!error && !intf->dev.power.is_prepared) {
386 r = usb_set_interface(udev, intf->altsetting[0]. 386 r = usb_set_interface(udev, intf->altsetting[0].
387 desc.bInterfaceNumber, 0); 387 desc.bInterfaceNumber, 0);
388 if (r < 0) 388 if (r < 0)
389 intf->needs_altsetting0 = 1; 389 intf->needs_altsetting0 = 1;
390 } else { 390 } else {
391 intf->needs_altsetting0 = 1; 391 intf->needs_altsetting0 = 1;
392 } 392 }
393 usb_set_intfdata(intf, NULL); 393 usb_set_intfdata(intf, NULL);
394 394
395 intf->condition = USB_INTERFACE_UNBOUND; 395 intf->condition = USB_INTERFACE_UNBOUND;
396 intf->needs_remote_wakeup = 0; 396 intf->needs_remote_wakeup = 0;
397 397
398 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 398 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
399 if (driver->supports_autosuspend) 399 if (driver->supports_autosuspend)
400 pm_runtime_disable(dev); 400 pm_runtime_disable(dev);
401 pm_runtime_set_suspended(dev); 401 pm_runtime_set_suspended(dev);
402 402
403 /* Undo any residual pm_autopm_get_interface_* calls */ 403 /* Undo any residual pm_autopm_get_interface_* calls */
404 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r) 404 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
405 usb_autopm_put_interface_no_suspend(intf); 405 usb_autopm_put_interface_no_suspend(intf);
406 atomic_set(&intf->pm_usage_cnt, 0); 406 atomic_set(&intf->pm_usage_cnt, 0);
407 407
408 if (!error) 408 if (!error)
409 usb_autosuspend_device(udev); 409 usb_autosuspend_device(udev);
410 410
411 return 0; 411 return 0;
412 } 412 }
413 413
414 /** 414 /**
415 * usb_driver_claim_interface - bind a driver to an interface 415 * usb_driver_claim_interface - bind a driver to an interface
416 * @driver: the driver to be bound 416 * @driver: the driver to be bound
417 * @iface: the interface to which it will be bound; must be in the 417 * @iface: the interface to which it will be bound; must be in the
418 * usb device's active configuration 418 * usb device's active configuration
419 * @priv: driver data associated with that interface 419 * @priv: driver data associated with that interface
420 * 420 *
421 * This is used by usb device drivers that need to claim more than one 421 * This is used by usb device drivers that need to claim more than one
422 * interface on a device when probing (audio and acm are current examples). 422 * interface on a device when probing (audio and acm are current examples).
423 * No device driver should directly modify internal usb_interface or 423 * No device driver should directly modify internal usb_interface or
424 * usb_device structure members. 424 * usb_device structure members.
425 * 425 *
426 * Few drivers should need to use this routine, since the most natural 426 * Few drivers should need to use this routine, since the most natural
427 * way to bind to an interface is to return the private data from 427 * way to bind to an interface is to return the private data from
428 * the driver's probe() method. 428 * the driver's probe() method.
429 * 429 *
430 * Callers must own the device lock, so driver probe() entries don't need 430 * Callers must own the device lock, so driver probe() entries don't need
431 * extra locking, but other call contexts may need to explicitly claim that 431 * extra locking, but other call contexts may need to explicitly claim that
432 * lock. 432 * lock.
433 */ 433 */
434 int usb_driver_claim_interface(struct usb_driver *driver, 434 int usb_driver_claim_interface(struct usb_driver *driver,
435 struct usb_interface *iface, void *priv) 435 struct usb_interface *iface, void *priv)
436 { 436 {
437 struct device *dev = &iface->dev; 437 struct device *dev = &iface->dev;
438 int retval = 0; 438 int retval = 0;
439 439
440 if (dev->driver) 440 if (dev->driver)
441 return -EBUSY; 441 return -EBUSY;
442 442
443 dev->driver = &driver->drvwrap.driver; 443 dev->driver = &driver->drvwrap.driver;
444 usb_set_intfdata(iface, priv); 444 usb_set_intfdata(iface, priv);
445 iface->needs_binding = 0; 445 iface->needs_binding = 0;
446 446
447 iface->condition = USB_INTERFACE_BOUND; 447 iface->condition = USB_INTERFACE_BOUND;
448 448
449 /* Claimed interfaces are initially inactive (suspended) and 449 /* Claimed interfaces are initially inactive (suspended) and
450 * runtime-PM-enabled, but only if the driver has autosuspend 450 * runtime-PM-enabled, but only if the driver has autosuspend
451 * support. Otherwise they are marked active, to prevent the 451 * support. Otherwise they are marked active, to prevent the
452 * device from being autosuspended, but left disabled. In either 452 * device from being autosuspended, but left disabled. In either
453 * case they are sensitive to their children's power states. 453 * case they are sensitive to their children's power states.
454 */ 454 */
455 pm_suspend_ignore_children(dev, false); 455 pm_suspend_ignore_children(dev, false);
456 if (driver->supports_autosuspend) 456 if (driver->supports_autosuspend)
457 pm_runtime_enable(dev); 457 pm_runtime_enable(dev);
458 else 458 else
459 pm_runtime_set_active(dev); 459 pm_runtime_set_active(dev);
460 460
461 /* if interface was already added, bind now; else let 461 /* if interface was already added, bind now; else let
462 * the future device_add() bind it, bypassing probe() 462 * the future device_add() bind it, bypassing probe()
463 */ 463 */
464 if (device_is_registered(dev)) 464 if (device_is_registered(dev))
465 retval = device_bind_driver(dev); 465 retval = device_bind_driver(dev);
466 466
467 return retval; 467 return retval;
468 } 468 }
469 EXPORT_SYMBOL_GPL(usb_driver_claim_interface); 469 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
470 470
471 /** 471 /**
472 * usb_driver_release_interface - unbind a driver from an interface 472 * usb_driver_release_interface - unbind a driver from an interface
473 * @driver: the driver to be unbound 473 * @driver: the driver to be unbound
474 * @iface: the interface from which it will be unbound 474 * @iface: the interface from which it will be unbound
475 * 475 *
476 * This can be used by drivers to release an interface without waiting 476 * This can be used by drivers to release an interface without waiting
477 * for their disconnect() methods to be called. In typical cases this 477 * for their disconnect() methods to be called. In typical cases this
478 * also causes the driver disconnect() method to be called. 478 * also causes the driver disconnect() method to be called.
479 * 479 *
480 * This call is synchronous, and may not be used in an interrupt context. 480 * This call is synchronous, and may not be used in an interrupt context.
481 * Callers must own the device lock, so driver disconnect() entries don't 481 * Callers must own the device lock, so driver disconnect() entries don't
482 * need extra locking, but other call contexts may need to explicitly claim 482 * need extra locking, but other call contexts may need to explicitly claim
483 * that lock. 483 * that lock.
484 */ 484 */
485 void usb_driver_release_interface(struct usb_driver *driver, 485 void usb_driver_release_interface(struct usb_driver *driver,
486 struct usb_interface *iface) 486 struct usb_interface *iface)
487 { 487 {
488 struct device *dev = &iface->dev; 488 struct device *dev = &iface->dev;
489 489
490 /* this should never happen, don't release something that's not ours */ 490 /* this should never happen, don't release something that's not ours */
491 if (!dev->driver || dev->driver != &driver->drvwrap.driver) 491 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
492 return; 492 return;
493 493
494 /* don't release from within disconnect() */ 494 /* don't release from within disconnect() */
495 if (iface->condition != USB_INTERFACE_BOUND) 495 if (iface->condition != USB_INTERFACE_BOUND)
496 return; 496 return;
497 iface->condition = USB_INTERFACE_UNBINDING; 497 iface->condition = USB_INTERFACE_UNBINDING;
498 498
499 /* Release via the driver core only if the interface 499 /* Release via the driver core only if the interface
500 * has already been registered 500 * has already been registered
501 */ 501 */
502 if (device_is_registered(dev)) { 502 if (device_is_registered(dev)) {
503 device_release_driver(dev); 503 device_release_driver(dev);
504 } else { 504 } else {
505 device_lock(dev); 505 device_lock(dev);
506 usb_unbind_interface(dev); 506 usb_unbind_interface(dev);
507 dev->driver = NULL; 507 dev->driver = NULL;
508 device_unlock(dev); 508 device_unlock(dev);
509 } 509 }
510 } 510 }
511 EXPORT_SYMBOL_GPL(usb_driver_release_interface); 511 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
512 512
513 /* returns 0 if no match, 1 if match */ 513 /* returns 0 if no match, 1 if match */
514 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id) 514 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
515 { 515 {
516 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 516 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
517 id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) 517 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
518 return 0; 518 return 0;
519 519
520 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 520 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
521 id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) 521 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
522 return 0; 522 return 0;
523 523
524 /* No need to test id->bcdDevice_lo != 0, since 0 is never 524 /* No need to test id->bcdDevice_lo != 0, since 0 is never
525 greater than any unsigned number. */ 525 greater than any unsigned number. */
526 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 526 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
527 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) 527 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
528 return 0; 528 return 0;
529 529
530 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 530 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
531 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) 531 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
532 return 0; 532 return 0;
533 533
534 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 534 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
535 (id->bDeviceClass != dev->descriptor.bDeviceClass)) 535 (id->bDeviceClass != dev->descriptor.bDeviceClass))
536 return 0; 536 return 0;
537 537
538 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 538 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
539 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass)) 539 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
540 return 0; 540 return 0;
541 541
542 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 542 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
543 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) 543 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
544 return 0; 544 return 0;
545 545
546 return 1; 546 return 1;
547 } 547 }
548 548
549 /* returns 0 if no match, 1 if match */ 549 /* returns 0 if no match, 1 if match */
550 int usb_match_one_id(struct usb_interface *interface, 550 int usb_match_one_id(struct usb_interface *interface,
551 const struct usb_device_id *id) 551 const struct usb_device_id *id)
552 { 552 {
553 struct usb_host_interface *intf; 553 struct usb_host_interface *intf;
554 struct usb_device *dev; 554 struct usb_device *dev;
555 555
556 /* proc_connectinfo in devio.c may call us with id == NULL. */ 556 /* proc_connectinfo in devio.c may call us with id == NULL. */
557 if (id == NULL) 557 if (id == NULL)
558 return 0; 558 return 0;
559 559
560 intf = interface->cur_altsetting; 560 intf = interface->cur_altsetting;
561 dev = interface_to_usbdev(interface); 561 dev = interface_to_usbdev(interface);
562 562
563 if (!usb_match_device(dev, id)) 563 if (!usb_match_device(dev, id))
564 return 0; 564 return 0;
565 565
566 /* The interface class, subclass, and protocol should never be 566 /* The interface class, subclass, and protocol should never be
567 * checked for a match if the device class is Vendor Specific, 567 * checked for a match if the device class is Vendor Specific,
568 * unless the match record specifies the Vendor ID. */ 568 * unless the match record specifies the Vendor ID. */
569 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && 569 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
570 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 570 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
571 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 571 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
572 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 572 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
573 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) 573 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
574 return 0; 574 return 0;
575 575
576 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 576 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
577 (id->bInterfaceClass != intf->desc.bInterfaceClass)) 577 (id->bInterfaceClass != intf->desc.bInterfaceClass))
578 return 0; 578 return 0;
579 579
580 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 580 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
581 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) 581 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
582 return 0; 582 return 0;
583 583
584 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 584 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
585 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) 585 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
586 return 0; 586 return 0;
587 587
588 return 1; 588 return 1;
589 } 589 }
590 EXPORT_SYMBOL_GPL(usb_match_one_id); 590 EXPORT_SYMBOL_GPL(usb_match_one_id);
591 591
592 /** 592 /**
593 * usb_match_id - find first usb_device_id matching device or interface 593 * usb_match_id - find first usb_device_id matching device or interface
594 * @interface: the interface of interest 594 * @interface: the interface of interest
595 * @id: array of usb_device_id structures, terminated by zero entry 595 * @id: array of usb_device_id structures, terminated by zero entry
596 * 596 *
597 * usb_match_id searches an array of usb_device_id's and returns 597 * usb_match_id searches an array of usb_device_id's and returns
598 * the first one matching the device or interface, or null. 598 * the first one matching the device or interface, or null.
599 * This is used when binding (or rebinding) a driver to an interface. 599 * This is used when binding (or rebinding) a driver to an interface.
600 * Most USB device drivers will use this indirectly, through the usb core, 600 * Most USB device drivers will use this indirectly, through the usb core,
601 * but some layered driver frameworks use it directly. 601 * but some layered driver frameworks use it directly.
602 * These device tables are exported with MODULE_DEVICE_TABLE, through 602 * These device tables are exported with MODULE_DEVICE_TABLE, through
603 * modutils, to support the driver loading functionality of USB hotplugging. 603 * modutils, to support the driver loading functionality of USB hotplugging.
604 * 604 *
605 * What Matches: 605 * What Matches:
606 * 606 *
607 * The "match_flags" element in a usb_device_id controls which 607 * The "match_flags" element in a usb_device_id controls which
608 * members are used. If the corresponding bit is set, the 608 * members are used. If the corresponding bit is set, the
609 * value in the device_id must match its corresponding member 609 * value in the device_id must match its corresponding member
610 * in the device or interface descriptor, or else the device_id 610 * in the device or interface descriptor, or else the device_id
611 * does not match. 611 * does not match.
612 * 612 *
613 * "driver_info" is normally used only by device drivers, 613 * "driver_info" is normally used only by device drivers,
614 * but you can create a wildcard "matches anything" usb_device_id 614 * but you can create a wildcard "matches anything" usb_device_id
615 * as a driver's "modules.usbmap" entry if you provide an id with 615 * as a driver's "modules.usbmap" entry if you provide an id with
616 * only a nonzero "driver_info" field. If you do this, the USB device 616 * only a nonzero "driver_info" field. If you do this, the USB device
617 * driver's probe() routine should use additional intelligence to 617 * driver's probe() routine should use additional intelligence to
618 * decide whether to bind to the specified interface. 618 * decide whether to bind to the specified interface.
619 * 619 *
620 * What Makes Good usb_device_id Tables: 620 * What Makes Good usb_device_id Tables:
621 * 621 *
622 * The match algorithm is very simple, so that intelligence in 622 * The match algorithm is very simple, so that intelligence in
623 * driver selection must come from smart driver id records. 623 * driver selection must come from smart driver id records.
624 * Unless you have good reasons to use another selection policy, 624 * Unless you have good reasons to use another selection policy,
625 * provide match elements only in related groups, and order match 625 * provide match elements only in related groups, and order match
626 * specifiers from specific to general. Use the macros provided 626 * specifiers from specific to general. Use the macros provided
627 * for that purpose if you can. 627 * for that purpose if you can.
628 * 628 *
629 * The most specific match specifiers use device descriptor 629 * The most specific match specifiers use device descriptor
630 * data. These are commonly used with product-specific matches; 630 * data. These are commonly used with product-specific matches;
631 * the USB_DEVICE macro lets you provide vendor and product IDs, 631 * the USB_DEVICE macro lets you provide vendor and product IDs,
632 * and you can also match against ranges of product revisions. 632 * and you can also match against ranges of product revisions.
633 * These are widely used for devices with application or vendor 633 * These are widely used for devices with application or vendor
634 * specific bDeviceClass values. 634 * specific bDeviceClass values.
635 * 635 *
636 * Matches based on device class/subclass/protocol specifications 636 * Matches based on device class/subclass/protocol specifications
637 * are slightly more general; use the USB_DEVICE_INFO macro, or 637 * are slightly more general; use the USB_DEVICE_INFO macro, or
638 * its siblings. These are used with single-function devices 638 * its siblings. These are used with single-function devices
639 * where bDeviceClass doesn't specify that each interface has 639 * where bDeviceClass doesn't specify that each interface has
640 * its own class. 640 * its own class.
641 * 641 *
642 * Matches based on interface class/subclass/protocol are the 642 * Matches based on interface class/subclass/protocol are the
643 * most general; they let drivers bind to any interface on a 643 * most general; they let drivers bind to any interface on a
644 * multiple-function device. Use the USB_INTERFACE_INFO 644 * multiple-function device. Use the USB_INTERFACE_INFO
645 * macro, or its siblings, to match class-per-interface style 645 * macro, or its siblings, to match class-per-interface style
646 * devices (as recorded in bInterfaceClass). 646 * devices (as recorded in bInterfaceClass).
647 * 647 *
648 * Note that an entry created by USB_INTERFACE_INFO won't match 648 * Note that an entry created by USB_INTERFACE_INFO won't match
649 * any interface if the device class is set to Vendor-Specific. 649 * any interface if the device class is set to Vendor-Specific.
650 * This is deliberate; according to the USB spec the meanings of 650 * This is deliberate; according to the USB spec the meanings of
651 * the interface class/subclass/protocol for these devices are also 651 * the interface class/subclass/protocol for these devices are also
652 * vendor-specific, and hence matching against a standard product 652 * vendor-specific, and hence matching against a standard product
653 * class wouldn't work anyway. If you really want to use an 653 * class wouldn't work anyway. If you really want to use an
654 * interface-based match for such a device, create a match record 654 * interface-based match for such a device, create a match record
655 * that also specifies the vendor ID. (Unforunately there isn't a 655 * that also specifies the vendor ID. (Unforunately there isn't a
656 * standard macro for creating records like this.) 656 * standard macro for creating records like this.)
657 * 657 *
658 * Within those groups, remember that not all combinations are 658 * Within those groups, remember that not all combinations are
659 * meaningful. For example, don't give a product version range 659 * meaningful. For example, don't give a product version range
660 * without vendor and product IDs; or specify a protocol without 660 * without vendor and product IDs; or specify a protocol without
661 * its associated class and subclass. 661 * its associated class and subclass.
662 */ 662 */
663 const struct usb_device_id *usb_match_id(struct usb_interface *interface, 663 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
664 const struct usb_device_id *id) 664 const struct usb_device_id *id)
665 { 665 {
666 /* proc_connectinfo in devio.c may call us with id == NULL. */ 666 /* proc_connectinfo in devio.c may call us with id == NULL. */
667 if (id == NULL) 667 if (id == NULL)
668 return NULL; 668 return NULL;
669 669
670 /* It is important to check that id->driver_info is nonzero, 670 /* It is important to check that id->driver_info is nonzero,
671 since an entry that is all zeroes except for a nonzero 671 since an entry that is all zeroes except for a nonzero
672 id->driver_info is the way to create an entry that 672 id->driver_info is the way to create an entry that
673 indicates that the driver want to examine every 673 indicates that the driver want to examine every
674 device and interface. */ 674 device and interface. */
675 for (; id->idVendor || id->idProduct || id->bDeviceClass || 675 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
676 id->bInterfaceClass || id->driver_info; id++) { 676 id->bInterfaceClass || id->driver_info; id++) {
677 if (usb_match_one_id(interface, id)) 677 if (usb_match_one_id(interface, id))
678 return id; 678 return id;
679 } 679 }
680 680
681 return NULL; 681 return NULL;
682 } 682 }
683 EXPORT_SYMBOL_GPL(usb_match_id); 683 EXPORT_SYMBOL_GPL(usb_match_id);
684 684
685 static int usb_device_match(struct device *dev, struct device_driver *drv) 685 static int usb_device_match(struct device *dev, struct device_driver *drv)
686 { 686 {
687 /* devices and interfaces are handled separately */ 687 /* devices and interfaces are handled separately */
688 if (is_usb_device(dev)) { 688 if (is_usb_device(dev)) {
689 689
690 /* interface drivers never match devices */ 690 /* interface drivers never match devices */
691 if (!is_usb_device_driver(drv)) 691 if (!is_usb_device_driver(drv))
692 return 0; 692 return 0;
693 693
694 /* TODO: Add real matching code */ 694 /* TODO: Add real matching code */
695 return 1; 695 return 1;
696 696
697 } else if (is_usb_interface(dev)) { 697 } else if (is_usb_interface(dev)) {
698 struct usb_interface *intf; 698 struct usb_interface *intf;
699 struct usb_driver *usb_drv; 699 struct usb_driver *usb_drv;
700 const struct usb_device_id *id; 700 const struct usb_device_id *id;
701 701
702 /* device drivers never match interfaces */ 702 /* device drivers never match interfaces */
703 if (is_usb_device_driver(drv)) 703 if (is_usb_device_driver(drv))
704 return 0; 704 return 0;
705 705
706 intf = to_usb_interface(dev); 706 intf = to_usb_interface(dev);
707 usb_drv = to_usb_driver(drv); 707 usb_drv = to_usb_driver(drv);
708 708
709 id = usb_match_id(intf, usb_drv->id_table); 709 id = usb_match_id(intf, usb_drv->id_table);
710 if (id) 710 if (id)
711 return 1; 711 return 1;
712 712
713 id = usb_match_dynamic_id(intf, usb_drv); 713 id = usb_match_dynamic_id(intf, usb_drv);
714 if (id) 714 if (id)
715 return 1; 715 return 1;
716 } 716 }
717 717
718 return 0; 718 return 0;
719 } 719 }
720 720
721 #ifdef CONFIG_HOTPLUG 721 #ifdef CONFIG_HOTPLUG
722 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) 722 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
723 { 723 {
724 struct usb_device *usb_dev; 724 struct usb_device *usb_dev;
725 725
726 if (is_usb_device(dev)) { 726 if (is_usb_device(dev)) {
727 usb_dev = to_usb_device(dev); 727 usb_dev = to_usb_device(dev);
728 } else if (is_usb_interface(dev)) { 728 } else if (is_usb_interface(dev)) {
729 struct usb_interface *intf = to_usb_interface(dev); 729 struct usb_interface *intf = to_usb_interface(dev);
730 730
731 usb_dev = interface_to_usbdev(intf); 731 usb_dev = interface_to_usbdev(intf);
732 } else { 732 } else {
733 return 0; 733 return 0;
734 } 734 }
735 735
736 if (usb_dev->devnum < 0) { 736 if (usb_dev->devnum < 0) {
737 /* driver is often null here; dev_dbg() would oops */ 737 /* driver is often null here; dev_dbg() would oops */
738 pr_debug("usb %s: already deleted?\n", dev_name(dev)); 738 pr_debug("usb %s: already deleted?\n", dev_name(dev));
739 return -ENODEV; 739 return -ENODEV;
740 } 740 }
741 if (!usb_dev->bus) { 741 if (!usb_dev->bus) {
742 pr_debug("usb %s: bus removed?\n", dev_name(dev)); 742 pr_debug("usb %s: bus removed?\n", dev_name(dev));
743 return -ENODEV; 743 return -ENODEV;
744 } 744 }
745 745
746 #ifdef CONFIG_USB_DEVICEFS 746 #ifdef CONFIG_USB_DEVICEFS
747 /* If this is available, userspace programs can directly read 747 /* If this is available, userspace programs can directly read
748 * all the device descriptors we don't tell them about. Or 748 * all the device descriptors we don't tell them about. Or
749 * act as usermode drivers. 749 * act as usermode drivers.
750 */ 750 */
751 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d", 751 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
752 usb_dev->bus->busnum, usb_dev->devnum)) 752 usb_dev->bus->busnum, usb_dev->devnum))
753 return -ENOMEM; 753 return -ENOMEM;
754 #endif 754 #endif
755 755
756 /* per-device configurations are common */ 756 /* per-device configurations are common */
757 if (add_uevent_var(env, "PRODUCT=%x/%x/%x", 757 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
758 le16_to_cpu(usb_dev->descriptor.idVendor), 758 le16_to_cpu(usb_dev->descriptor.idVendor),
759 le16_to_cpu(usb_dev->descriptor.idProduct), 759 le16_to_cpu(usb_dev->descriptor.idProduct),
760 le16_to_cpu(usb_dev->descriptor.bcdDevice))) 760 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
761 return -ENOMEM; 761 return -ENOMEM;
762 762
763 /* class-based driver binding models */ 763 /* class-based driver binding models */
764 if (add_uevent_var(env, "TYPE=%d/%d/%d", 764 if (add_uevent_var(env, "TYPE=%d/%d/%d",
765 usb_dev->descriptor.bDeviceClass, 765 usb_dev->descriptor.bDeviceClass,
766 usb_dev->descriptor.bDeviceSubClass, 766 usb_dev->descriptor.bDeviceSubClass,
767 usb_dev->descriptor.bDeviceProtocol)) 767 usb_dev->descriptor.bDeviceProtocol))
768 return -ENOMEM; 768 return -ENOMEM;
769 769
770 return 0; 770 return 0;
771 } 771 }
772 772
773 #else 773 #else
774 774
775 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) 775 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
776 { 776 {
777 return -ENODEV; 777 return -ENODEV;
778 } 778 }
779 #endif /* CONFIG_HOTPLUG */ 779 #endif /* CONFIG_HOTPLUG */
780 780
781 /** 781 /**
782 * usb_register_device_driver - register a USB device (not interface) driver 782 * usb_register_device_driver - register a USB device (not interface) driver
783 * @new_udriver: USB operations for the device driver 783 * @new_udriver: USB operations for the device driver
784 * @owner: module owner of this driver. 784 * @owner: module owner of this driver.
785 * 785 *
786 * Registers a USB device driver with the USB core. The list of 786 * Registers a USB device driver with the USB core. The list of
787 * unattached devices will be rescanned whenever a new driver is 787 * unattached devices will be rescanned whenever a new driver is
788 * added, allowing the new driver to attach to any recognized devices. 788 * added, allowing the new driver to attach to any recognized devices.
789 * Returns a negative error code on failure and 0 on success. 789 * Returns a negative error code on failure and 0 on success.
790 */ 790 */
791 int usb_register_device_driver(struct usb_device_driver *new_udriver, 791 int usb_register_device_driver(struct usb_device_driver *new_udriver,
792 struct module *owner) 792 struct module *owner)
793 { 793 {
794 int retval = 0; 794 int retval = 0;
795 795
796 if (usb_disabled()) 796 if (usb_disabled())
797 return -ENODEV; 797 return -ENODEV;
798 798
799 new_udriver->drvwrap.for_devices = 1; 799 new_udriver->drvwrap.for_devices = 1;
800 new_udriver->drvwrap.driver.name = (char *) new_udriver->name; 800 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
801 new_udriver->drvwrap.driver.bus = &usb_bus_type; 801 new_udriver->drvwrap.driver.bus = &usb_bus_type;
802 new_udriver->drvwrap.driver.probe = usb_probe_device; 802 new_udriver->drvwrap.driver.probe = usb_probe_device;
803 new_udriver->drvwrap.driver.remove = usb_unbind_device; 803 new_udriver->drvwrap.driver.remove = usb_unbind_device;
804 new_udriver->drvwrap.driver.owner = owner; 804 new_udriver->drvwrap.driver.owner = owner;
805 805
806 retval = driver_register(&new_udriver->drvwrap.driver); 806 retval = driver_register(&new_udriver->drvwrap.driver);
807 807
808 if (!retval) { 808 if (!retval) {
809 pr_info("%s: registered new device driver %s\n", 809 pr_info("%s: registered new device driver %s\n",
810 usbcore_name, new_udriver->name); 810 usbcore_name, new_udriver->name);
811 usbfs_update_special(); 811 usbfs_update_special();
812 } else { 812 } else {
813 printk(KERN_ERR "%s: error %d registering device " 813 printk(KERN_ERR "%s: error %d registering device "
814 " driver %s\n", 814 " driver %s\n",
815 usbcore_name, retval, new_udriver->name); 815 usbcore_name, retval, new_udriver->name);
816 } 816 }
817 817
818 return retval; 818 return retval;
819 } 819 }
820 EXPORT_SYMBOL_GPL(usb_register_device_driver); 820 EXPORT_SYMBOL_GPL(usb_register_device_driver);
821 821
822 /** 822 /**
823 * usb_deregister_device_driver - unregister a USB device (not interface) driver 823 * usb_deregister_device_driver - unregister a USB device (not interface) driver
824 * @udriver: USB operations of the device driver to unregister 824 * @udriver: USB operations of the device driver to unregister
825 * Context: must be able to sleep 825 * Context: must be able to sleep
826 * 826 *
827 * Unlinks the specified driver from the internal USB driver list. 827 * Unlinks the specified driver from the internal USB driver list.
828 */ 828 */
829 void usb_deregister_device_driver(struct usb_device_driver *udriver) 829 void usb_deregister_device_driver(struct usb_device_driver *udriver)
830 { 830 {
831 pr_info("%s: deregistering device driver %s\n", 831 pr_info("%s: deregistering device driver %s\n",
832 usbcore_name, udriver->name); 832 usbcore_name, udriver->name);
833 833
834 driver_unregister(&udriver->drvwrap.driver); 834 driver_unregister(&udriver->drvwrap.driver);
835 usbfs_update_special(); 835 usbfs_update_special();
836 } 836 }
837 EXPORT_SYMBOL_GPL(usb_deregister_device_driver); 837 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
838 838
839 /** 839 /**
840 * usb_register_driver - register a USB interface driver 840 * usb_register_driver - register a USB interface driver
841 * @new_driver: USB operations for the interface driver 841 * @new_driver: USB operations for the interface driver
842 * @owner: module owner of this driver. 842 * @owner: module owner of this driver.
843 * @mod_name: module name string 843 * @mod_name: module name string
844 * 844 *
845 * Registers a USB interface driver with the USB core. The list of 845 * Registers a USB interface driver with the USB core. The list of
846 * unattached interfaces will be rescanned whenever a new driver is 846 * unattached interfaces will be rescanned whenever a new driver is
847 * added, allowing the new driver to attach to any recognized interfaces. 847 * added, allowing the new driver to attach to any recognized interfaces.
848 * Returns a negative error code on failure and 0 on success. 848 * Returns a negative error code on failure and 0 on success.
849 * 849 *
850 * NOTE: if you want your driver to use the USB major number, you must call 850 * NOTE: if you want your driver to use the USB major number, you must call
851 * usb_register_dev() to enable that functionality. This function no longer 851 * usb_register_dev() to enable that functionality. This function no longer
852 * takes care of that. 852 * takes care of that.
853 */ 853 */
854 int usb_register_driver(struct usb_driver *new_driver, struct module *owner, 854 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
855 const char *mod_name) 855 const char *mod_name)
856 { 856 {
857 int retval = 0; 857 int retval = 0;
858 858
859 if (usb_disabled()) 859 if (usb_disabled())
860 return -ENODEV; 860 return -ENODEV;
861 861
862 new_driver->drvwrap.for_devices = 0; 862 new_driver->drvwrap.for_devices = 0;
863 new_driver->drvwrap.driver.name = (char *) new_driver->name; 863 new_driver->drvwrap.driver.name = (char *) new_driver->name;
864 new_driver->drvwrap.driver.bus = &usb_bus_type; 864 new_driver->drvwrap.driver.bus = &usb_bus_type;
865 new_driver->drvwrap.driver.probe = usb_probe_interface; 865 new_driver->drvwrap.driver.probe = usb_probe_interface;
866 new_driver->drvwrap.driver.remove = usb_unbind_interface; 866 new_driver->drvwrap.driver.remove = usb_unbind_interface;
867 new_driver->drvwrap.driver.owner = owner; 867 new_driver->drvwrap.driver.owner = owner;
868 new_driver->drvwrap.driver.mod_name = mod_name; 868 new_driver->drvwrap.driver.mod_name = mod_name;
869 spin_lock_init(&new_driver->dynids.lock); 869 spin_lock_init(&new_driver->dynids.lock);
870 INIT_LIST_HEAD(&new_driver->dynids.list); 870 INIT_LIST_HEAD(&new_driver->dynids.list);
871 871
872 retval = driver_register(&new_driver->drvwrap.driver); 872 retval = driver_register(&new_driver->drvwrap.driver);
873 if (retval) 873 if (retval)
874 goto out; 874 goto out;
875 875
876 usbfs_update_special(); 876 usbfs_update_special();
877 877
878 retval = usb_create_newid_file(new_driver); 878 retval = usb_create_newid_file(new_driver);
879 if (retval) 879 if (retval)
880 goto out_newid; 880 goto out_newid;
881 881
882 retval = usb_create_removeid_file(new_driver); 882 retval = usb_create_removeid_file(new_driver);
883 if (retval) 883 if (retval)
884 goto out_removeid; 884 goto out_removeid;
885 885
886 pr_info("%s: registered new interface driver %s\n", 886 pr_info("%s: registered new interface driver %s\n",
887 usbcore_name, new_driver->name); 887 usbcore_name, new_driver->name);
888 888
889 out: 889 out:
890 return retval; 890 return retval;
891 891
892 out_removeid: 892 out_removeid:
893 usb_remove_newid_file(new_driver); 893 usb_remove_newid_file(new_driver);
894 out_newid: 894 out_newid:
895 driver_unregister(&new_driver->drvwrap.driver); 895 driver_unregister(&new_driver->drvwrap.driver);
896 896
897 printk(KERN_ERR "%s: error %d registering interface " 897 printk(KERN_ERR "%s: error %d registering interface "
898 " driver %s\n", 898 " driver %s\n",
899 usbcore_name, retval, new_driver->name); 899 usbcore_name, retval, new_driver->name);
900 goto out; 900 goto out;
901 } 901 }
902 EXPORT_SYMBOL_GPL(usb_register_driver); 902 EXPORT_SYMBOL_GPL(usb_register_driver);
903 903
904 /** 904 /**
905 * usb_deregister - unregister a USB interface driver 905 * usb_deregister - unregister a USB interface driver
906 * @driver: USB operations of the interface driver to unregister 906 * @driver: USB operations of the interface driver to unregister
907 * Context: must be able to sleep 907 * Context: must be able to sleep
908 * 908 *
909 * Unlinks the specified driver from the internal USB driver list. 909 * Unlinks the specified driver from the internal USB driver list.
910 * 910 *
911 * NOTE: If you called usb_register_dev(), you still need to call 911 * NOTE: If you called usb_register_dev(), you still need to call
912 * usb_deregister_dev() to clean up your driver's allocated minor numbers, 912 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
913 * this * call will no longer do it for you. 913 * this * call will no longer do it for you.
914 */ 914 */
915 void usb_deregister(struct usb_driver *driver) 915 void usb_deregister(struct usb_driver *driver)
916 { 916 {
917 pr_info("%s: deregistering interface driver %s\n", 917 pr_info("%s: deregistering interface driver %s\n",
918 usbcore_name, driver->name); 918 usbcore_name, driver->name);
919 919
920 usb_remove_removeid_file(driver); 920 usb_remove_removeid_file(driver);
921 usb_remove_newid_file(driver); 921 usb_remove_newid_file(driver);
922 usb_free_dynids(driver); 922 usb_free_dynids(driver);
923 driver_unregister(&driver->drvwrap.driver); 923 driver_unregister(&driver->drvwrap.driver);
924 924
925 usbfs_update_special(); 925 usbfs_update_special();
926 } 926 }
927 EXPORT_SYMBOL_GPL(usb_deregister); 927 EXPORT_SYMBOL_GPL(usb_deregister);
928 928
929 /* Forced unbinding of a USB interface driver, either because 929 /* Forced unbinding of a USB interface driver, either because
930 * it doesn't support pre_reset/post_reset/reset_resume or 930 * it doesn't support pre_reset/post_reset/reset_resume or
931 * because it doesn't support suspend/resume. 931 * because it doesn't support suspend/resume.
932 * 932 *
933 * The caller must hold @intf's device's lock, but not its pm_mutex 933 * The caller must hold @intf's device's lock, but not its pm_mutex
934 * and not @intf->dev.sem. 934 * and not @intf->dev.sem.
935 */ 935 */
936 void usb_forced_unbind_intf(struct usb_interface *intf) 936 void usb_forced_unbind_intf(struct usb_interface *intf)
937 { 937 {
938 struct usb_driver *driver = to_usb_driver(intf->dev.driver); 938 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
939 939
940 dev_dbg(&intf->dev, "forced unbind\n"); 940 dev_dbg(&intf->dev, "forced unbind\n");
941 usb_driver_release_interface(driver, intf); 941 usb_driver_release_interface(driver, intf);
942 942
943 /* Mark the interface for later rebinding */ 943 /* Mark the interface for later rebinding */
944 intf->needs_binding = 1; 944 intf->needs_binding = 1;
945 } 945 }
946 946
947 /* Delayed forced unbinding of a USB interface driver and scan 947 /* Delayed forced unbinding of a USB interface driver and scan
948 * for rebinding. 948 * for rebinding.
949 * 949 *
950 * The caller must hold @intf's device's lock, but not its pm_mutex 950 * The caller must hold @intf's device's lock, but not its pm_mutex
951 * and not @intf->dev.sem. 951 * and not @intf->dev.sem.
952 * 952 *
953 * Note: Rebinds will be skipped if a system sleep transition is in 953 * Note: Rebinds will be skipped if a system sleep transition is in
954 * progress and the PM "complete" callback hasn't occurred yet. 954 * progress and the PM "complete" callback hasn't occurred yet.
955 */ 955 */
956 void usb_rebind_intf(struct usb_interface *intf) 956 void usb_rebind_intf(struct usb_interface *intf)
957 { 957 {
958 int rc; 958 int rc;
959 959
960 /* Delayed unbind of an existing driver */ 960 /* Delayed unbind of an existing driver */
961 if (intf->dev.driver) 961 if (intf->dev.driver)
962 usb_forced_unbind_intf(intf); 962 usb_forced_unbind_intf(intf);
963 963
964 /* Try to rebind the interface */ 964 /* Try to rebind the interface */
965 if (!intf->dev.power.is_prepared) { 965 if (!intf->dev.power.is_prepared) {
966 intf->needs_binding = 0; 966 intf->needs_binding = 0;
967 rc = device_attach(&intf->dev); 967 rc = device_attach(&intf->dev);
968 if (rc < 0) 968 if (rc < 0)
969 dev_warn(&intf->dev, "rebind failed: %d\n", rc); 969 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
970 } 970 }
971 } 971 }
972 972
973 #ifdef CONFIG_PM 973 #ifdef CONFIG_PM
974 974
975 /* Unbind drivers for @udev's interfaces that don't support suspend/resume 975 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
976 * There is no check for reset_resume here because it can be determined 976 * There is no check for reset_resume here because it can be determined
977 * only during resume whether reset_resume is needed. 977 * only during resume whether reset_resume is needed.
978 * 978 *
979 * The caller must hold @udev's device lock. 979 * The caller must hold @udev's device lock.
980 */ 980 */
981 static void unbind_no_pm_drivers_interfaces(struct usb_device *udev) 981 static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
982 { 982 {
983 struct usb_host_config *config; 983 struct usb_host_config *config;
984 int i; 984 int i;
985 struct usb_interface *intf; 985 struct usb_interface *intf;
986 struct usb_driver *drv; 986 struct usb_driver *drv;
987 987
988 config = udev->actconfig; 988 config = udev->actconfig;
989 if (config) { 989 if (config) {
990 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 990 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
991 intf = config->interface[i]; 991 intf = config->interface[i];
992 992
993 if (intf->dev.driver) { 993 if (intf->dev.driver) {
994 drv = to_usb_driver(intf->dev.driver); 994 drv = to_usb_driver(intf->dev.driver);
995 if (!drv->suspend || !drv->resume) 995 if (!drv->suspend || !drv->resume)
996 usb_forced_unbind_intf(intf); 996 usb_forced_unbind_intf(intf);
997 } 997 }
998 } 998 }
999 } 999 }
1000 } 1000 }
1001 1001
1002 /* Unbind drivers for @udev's interfaces that failed to support reset-resume. 1002 /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1003 * These interfaces have the needs_binding flag set by usb_resume_interface(). 1003 * These interfaces have the needs_binding flag set by usb_resume_interface().
1004 * 1004 *
1005 * The caller must hold @udev's device lock. 1005 * The caller must hold @udev's device lock.
1006 */ 1006 */
1007 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev) 1007 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1008 { 1008 {
1009 struct usb_host_config *config; 1009 struct usb_host_config *config;
1010 int i; 1010 int i;
1011 struct usb_interface *intf; 1011 struct usb_interface *intf;
1012 1012
1013 config = udev->actconfig; 1013 config = udev->actconfig;
1014 if (config) { 1014 if (config) {
1015 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 1015 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1016 intf = config->interface[i]; 1016 intf = config->interface[i];
1017 if (intf->dev.driver && intf->needs_binding) 1017 if (intf->dev.driver && intf->needs_binding)
1018 usb_forced_unbind_intf(intf); 1018 usb_forced_unbind_intf(intf);
1019 } 1019 }
1020 } 1020 }
1021 } 1021 }
1022 1022
1023 static void do_rebind_interfaces(struct usb_device *udev) 1023 static void do_rebind_interfaces(struct usb_device *udev)
1024 { 1024 {
1025 struct usb_host_config *config; 1025 struct usb_host_config *config;
1026 int i; 1026 int i;
1027 struct usb_interface *intf; 1027 struct usb_interface *intf;
1028 1028
1029 config = udev->actconfig; 1029 config = udev->actconfig;
1030 if (config) { 1030 if (config) {
1031 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 1031 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1032 intf = config->interface[i]; 1032 intf = config->interface[i];
1033 if (intf->needs_binding) 1033 if (intf->needs_binding)
1034 usb_rebind_intf(intf); 1034 usb_rebind_intf(intf);
1035 } 1035 }
1036 } 1036 }
1037 } 1037 }
1038 1038
1039 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1039 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1040 { 1040 {
1041 struct usb_device_driver *udriver; 1041 struct usb_device_driver *udriver;
1042 int status = 0; 1042 int status = 0;
1043 1043
1044 if (udev->state == USB_STATE_NOTATTACHED || 1044 if (udev->state == USB_STATE_NOTATTACHED ||
1045 udev->state == USB_STATE_SUSPENDED) 1045 udev->state == USB_STATE_SUSPENDED)
1046 goto done; 1046 goto done;
1047 1047
1048 /* For devices that don't have a driver, we do a generic suspend. */ 1048 /* For devices that don't have a driver, we do a generic suspend. */
1049 if (udev->dev.driver) 1049 if (udev->dev.driver)
1050 udriver = to_usb_device_driver(udev->dev.driver); 1050 udriver = to_usb_device_driver(udev->dev.driver);
1051 else { 1051 else {
1052 udev->do_remote_wakeup = 0; 1052 udev->do_remote_wakeup = 0;
1053 udriver = &usb_generic_driver; 1053 udriver = &usb_generic_driver;
1054 } 1054 }
1055 status = udriver->suspend(udev, msg); 1055 status = udriver->suspend(udev, msg);
1056 1056
1057 done: 1057 done:
1058 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1058 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1059 return status; 1059 return status;
1060 } 1060 }
1061 1061
1062 static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1062 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1063 { 1063 {
1064 struct usb_device_driver *udriver; 1064 struct usb_device_driver *udriver;
1065 int status = 0; 1065 int status = 0;
1066 1066
1067 if (udev->state == USB_STATE_NOTATTACHED) 1067 if (udev->state == USB_STATE_NOTATTACHED)
1068 goto done; 1068 goto done;
1069 1069
1070 /* Can't resume it if it doesn't have a driver. */ 1070 /* Can't resume it if it doesn't have a driver. */
1071 if (udev->dev.driver == NULL) { 1071 if (udev->dev.driver == NULL) {
1072 status = -ENOTCONN; 1072 status = -ENOTCONN;
1073 goto done; 1073 goto done;
1074 } 1074 }
1075 1075
1076 /* Non-root devices on a full/low-speed bus must wait for their 1076 /* Non-root devices on a full/low-speed bus must wait for their
1077 * companion high-speed root hub, in case a handoff is needed. 1077 * companion high-speed root hub, in case a handoff is needed.
1078 */ 1078 */
1079 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion) 1079 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1080 device_pm_wait_for_dev(&udev->dev, 1080 device_pm_wait_for_dev(&udev->dev,
1081 &udev->bus->hs_companion->root_hub->dev); 1081 &udev->bus->hs_companion->root_hub->dev);
1082 1082
1083 if (udev->quirks & USB_QUIRK_RESET_RESUME) 1083 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1084 udev->reset_resume = 1; 1084 udev->reset_resume = 1;
1085 1085
1086 udriver = to_usb_device_driver(udev->dev.driver); 1086 udriver = to_usb_device_driver(udev->dev.driver);
1087 status = udriver->resume(udev, msg); 1087 status = udriver->resume(udev, msg);
1088 1088
1089 done: 1089 done:
1090 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1090 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1091 return status; 1091 return status;
1092 } 1092 }
1093 1093
1094 static int usb_suspend_interface(struct usb_device *udev, 1094 static int usb_suspend_interface(struct usb_device *udev,
1095 struct usb_interface *intf, pm_message_t msg) 1095 struct usb_interface *intf, pm_message_t msg)
1096 { 1096 {
1097 struct usb_driver *driver; 1097 struct usb_driver *driver;
1098 int status = 0; 1098 int status = 0;
1099 1099
1100 if (udev->state == USB_STATE_NOTATTACHED || 1100 if (udev->state == USB_STATE_NOTATTACHED ||
1101 intf->condition == USB_INTERFACE_UNBOUND) 1101 intf->condition == USB_INTERFACE_UNBOUND)
1102 goto done; 1102 goto done;
1103 driver = to_usb_driver(intf->dev.driver); 1103 driver = to_usb_driver(intf->dev.driver);
1104 1104
1105 /* at this time we know the driver supports suspend */ 1105 /* at this time we know the driver supports suspend */
1106 status = driver->suspend(intf, msg); 1106 status = driver->suspend(intf, msg);
1107 if (status && !PMSG_IS_AUTO(msg)) 1107 if (status && !PMSG_IS_AUTO(msg))
1108 dev_err(&intf->dev, "suspend error %d\n", status); 1108 dev_err(&intf->dev, "suspend error %d\n", status);
1109 1109
1110 done: 1110 done:
1111 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1111 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1112 return status; 1112 return status;
1113 } 1113 }
1114 1114
1115 static int usb_resume_interface(struct usb_device *udev, 1115 static int usb_resume_interface(struct usb_device *udev,
1116 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1116 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1117 { 1117 {
1118 struct usb_driver *driver; 1118 struct usb_driver *driver;
1119 int status = 0; 1119 int status = 0;
1120 1120
1121 if (udev->state == USB_STATE_NOTATTACHED) 1121 if (udev->state == USB_STATE_NOTATTACHED)
1122 goto done; 1122 goto done;
1123 1123
1124 /* Don't let autoresume interfere with unbinding */ 1124 /* Don't let autoresume interfere with unbinding */
1125 if (intf->condition == USB_INTERFACE_UNBINDING) 1125 if (intf->condition == USB_INTERFACE_UNBINDING)
1126 goto done; 1126 goto done;
1127 1127
1128 /* Can't resume it if it doesn't have a driver. */ 1128 /* Can't resume it if it doesn't have a driver. */
1129 if (intf->condition == USB_INTERFACE_UNBOUND) { 1129 if (intf->condition == USB_INTERFACE_UNBOUND) {
1130 1130
1131 /* Carry out a deferred switch to altsetting 0 */ 1131 /* Carry out a deferred switch to altsetting 0 */
1132 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) { 1132 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1133 usb_set_interface(udev, intf->altsetting[0]. 1133 usb_set_interface(udev, intf->altsetting[0].
1134 desc.bInterfaceNumber, 0); 1134 desc.bInterfaceNumber, 0);
1135 intf->needs_altsetting0 = 0; 1135 intf->needs_altsetting0 = 0;
1136 } 1136 }
1137 goto done; 1137 goto done;
1138 } 1138 }
1139 1139
1140 /* Don't resume if the interface is marked for rebinding */ 1140 /* Don't resume if the interface is marked for rebinding */
1141 if (intf->needs_binding) 1141 if (intf->needs_binding)
1142 goto done; 1142 goto done;
1143 driver = to_usb_driver(intf->dev.driver); 1143 driver = to_usb_driver(intf->dev.driver);
1144 1144
1145 if (reset_resume) { 1145 if (reset_resume) {
1146 if (driver->reset_resume) { 1146 if (driver->reset_resume) {
1147 status = driver->reset_resume(intf); 1147 status = driver->reset_resume(intf);
1148 if (status) 1148 if (status)
1149 dev_err(&intf->dev, "%s error %d\n", 1149 dev_err(&intf->dev, "%s error %d\n",
1150 "reset_resume", status); 1150 "reset_resume", status);
1151 } else { 1151 } else {
1152 intf->needs_binding = 1; 1152 intf->needs_binding = 1;
1153 dev_warn(&intf->dev, "no %s for driver %s?\n", 1153 dev_warn(&intf->dev, "no %s for driver %s?\n",
1154 "reset_resume", driver->name); 1154 "reset_resume", driver->name);
1155 } 1155 }
1156 } else { 1156 } else {
1157 status = driver->resume(intf); 1157 status = driver->resume(intf);
1158 if (status) 1158 if (status)
1159 dev_err(&intf->dev, "resume error %d\n", status); 1159 dev_err(&intf->dev, "resume error %d\n", status);
1160 } 1160 }
1161 1161
1162 done: 1162 done:
1163 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1163 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1164 1164
1165 /* Later we will unbind the driver and/or reprobe, if necessary */ 1165 /* Later we will unbind the driver and/or reprobe, if necessary */
1166 return status; 1166 return status;
1167 } 1167 }
1168 1168
1169 /** 1169 /**
1170 * usb_suspend_both - suspend a USB device and its interfaces 1170 * usb_suspend_both - suspend a USB device and its interfaces
1171 * @udev: the usb_device to suspend 1171 * @udev: the usb_device to suspend
1172 * @msg: Power Management message describing this state transition 1172 * @msg: Power Management message describing this state transition
1173 * 1173 *
1174 * This is the central routine for suspending USB devices. It calls the 1174 * This is the central routine for suspending USB devices. It calls the
1175 * suspend methods for all the interface drivers in @udev and then calls 1175 * suspend methods for all the interface drivers in @udev and then calls
1176 * the suspend method for @udev itself. If an error occurs at any stage, 1176 * the suspend method for @udev itself. If an error occurs at any stage,
1177 * all the interfaces which were suspended are resumed so that they remain 1177 * all the interfaces which were suspended are resumed so that they remain
1178 * in the same state as the device. 1178 * in the same state as the device.
1179 * 1179 *
1180 * Autosuspend requests originating from a child device or an interface 1180 * Autosuspend requests originating from a child device or an interface
1181 * driver may be made without the protection of @udev's device lock, but 1181 * driver may be made without the protection of @udev's device lock, but
1182 * all other suspend calls will hold the lock. Usbcore will insure that 1182 * all other suspend calls will hold the lock. Usbcore will insure that
1183 * method calls do not arrive during bind, unbind, or reset operations. 1183 * method calls do not arrive during bind, unbind, or reset operations.
1184 * However drivers must be prepared to handle suspend calls arriving at 1184 * However drivers must be prepared to handle suspend calls arriving at
1185 * unpredictable times. 1185 * unpredictable times.
1186 * 1186 *
1187 * This routine can run only in process context. 1187 * This routine can run only in process context.
1188 */ 1188 */
1189 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg) 1189 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1190 { 1190 {
1191 int status = 0; 1191 int status = 0;
1192 int i = 0, n = 0; 1192 int i = 0, n = 0;
1193 struct usb_interface *intf; 1193 struct usb_interface *intf;
1194 1194
1195 if (udev->state == USB_STATE_NOTATTACHED || 1195 if (udev->state == USB_STATE_NOTATTACHED ||
1196 udev->state == USB_STATE_SUSPENDED) 1196 udev->state == USB_STATE_SUSPENDED)
1197 goto done; 1197 goto done;
1198 1198
1199 /* Suspend all the interfaces and then udev itself */ 1199 /* Suspend all the interfaces and then udev itself */
1200 if (udev->actconfig) { 1200 if (udev->actconfig) {
1201 n = udev->actconfig->desc.bNumInterfaces; 1201 n = udev->actconfig->desc.bNumInterfaces;
1202 for (i = n - 1; i >= 0; --i) { 1202 for (i = n - 1; i >= 0; --i) {
1203 intf = udev->actconfig->interface[i]; 1203 intf = udev->actconfig->interface[i];
1204 status = usb_suspend_interface(udev, intf, msg); 1204 status = usb_suspend_interface(udev, intf, msg);
1205 1205
1206 /* Ignore errors during system sleep transitions */ 1206 /* Ignore errors during system sleep transitions */
1207 if (!PMSG_IS_AUTO(msg)) 1207 if (!PMSG_IS_AUTO(msg))
1208 status = 0; 1208 status = 0;
1209 if (status != 0) 1209 if (status != 0)
1210 break; 1210 break;
1211 } 1211 }
1212 } 1212 }
1213 if (status == 0) { 1213 if (status == 0) {
1214 status = usb_suspend_device(udev, msg); 1214 status = usb_suspend_device(udev, msg);
1215 1215
1216 /* Again, ignore errors during system sleep transitions */ 1216 /* Again, ignore errors during system sleep transitions */
1217 if (!PMSG_IS_AUTO(msg)) 1217 if (!PMSG_IS_AUTO(msg))
1218 status = 0; 1218 status = 0;
1219 } 1219 }
1220 1220
1221 /* If the suspend failed, resume interfaces that did get suspended */ 1221 /* If the suspend failed, resume interfaces that did get suspended */
1222 if (status != 0) { 1222 if (status != 0) {
1223 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME); 1223 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1224 while (++i < n) { 1224 while (++i < n) {
1225 intf = udev->actconfig->interface[i]; 1225 intf = udev->actconfig->interface[i];
1226 usb_resume_interface(udev, intf, msg, 0); 1226 usb_resume_interface(udev, intf, msg, 0);
1227 } 1227 }
1228 1228
1229 /* If the suspend succeeded then prevent any more URB submissions 1229 /* If the suspend succeeded then prevent any more URB submissions
1230 * and flush any outstanding URBs. 1230 * and flush any outstanding URBs.
1231 */ 1231 */
1232 } else { 1232 } else {
1233 udev->can_submit = 0; 1233 udev->can_submit = 0;
1234 for (i = 0; i < 16; ++i) { 1234 for (i = 0; i < 16; ++i) {
1235 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1235 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1236 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1236 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1237 } 1237 }
1238 } 1238 }
1239 1239
1240 done: 1240 done:
1241 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1241 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1242 return status; 1242 return status;
1243 } 1243 }
1244 1244
1245 /** 1245 /**
1246 * usb_resume_both - resume a USB device and its interfaces 1246 * usb_resume_both - resume a USB device and its interfaces
1247 * @udev: the usb_device to resume 1247 * @udev: the usb_device to resume
1248 * @msg: Power Management message describing this state transition 1248 * @msg: Power Management message describing this state transition
1249 * 1249 *
1250 * This is the central routine for resuming USB devices. It calls the 1250 * This is the central routine for resuming USB devices. It calls the
1251 * the resume method for @udev and then calls the resume methods for all 1251 * the resume method for @udev and then calls the resume methods for all
1252 * the interface drivers in @udev. 1252 * the interface drivers in @udev.
1253 * 1253 *
1254 * Autoresume requests originating from a child device or an interface 1254 * Autoresume requests originating from a child device or an interface
1255 * driver may be made without the protection of @udev's device lock, but 1255 * driver may be made without the protection of @udev's device lock, but
1256 * all other resume calls will hold the lock. Usbcore will insure that 1256 * all other resume calls will hold the lock. Usbcore will insure that
1257 * method calls do not arrive during bind, unbind, or reset operations. 1257 * method calls do not arrive during bind, unbind, or reset operations.
1258 * However drivers must be prepared to handle resume calls arriving at 1258 * However drivers must be prepared to handle resume calls arriving at
1259 * unpredictable times. 1259 * unpredictable times.
1260 * 1260 *
1261 * This routine can run only in process context. 1261 * This routine can run only in process context.
1262 */ 1262 */
1263 static int usb_resume_both(struct usb_device *udev, pm_message_t msg) 1263 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1264 { 1264 {
1265 int status = 0; 1265 int status = 0;
1266 int i; 1266 int i;
1267 struct usb_interface *intf; 1267 struct usb_interface *intf;
1268 1268
1269 if (udev->state == USB_STATE_NOTATTACHED) { 1269 if (udev->state == USB_STATE_NOTATTACHED) {
1270 status = -ENODEV; 1270 status = -ENODEV;
1271 goto done; 1271 goto done;
1272 } 1272 }
1273 udev->can_submit = 1; 1273 udev->can_submit = 1;
1274 1274
1275 /* Resume the device */ 1275 /* Resume the device */
1276 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) 1276 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1277 status = usb_resume_device(udev, msg); 1277 status = usb_resume_device(udev, msg);
1278 1278
1279 /* Resume the interfaces */ 1279 /* Resume the interfaces */
1280 if (status == 0 && udev->actconfig) { 1280 if (status == 0 && udev->actconfig) {
1281 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1281 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1282 intf = udev->actconfig->interface[i]; 1282 intf = udev->actconfig->interface[i];
1283 usb_resume_interface(udev, intf, msg, 1283 usb_resume_interface(udev, intf, msg,
1284 udev->reset_resume); 1284 udev->reset_resume);
1285 } 1285 }
1286 } 1286 }
1287 usb_mark_last_busy(udev); 1287 usb_mark_last_busy(udev);
1288 1288
1289 done: 1289 done:
1290 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1290 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1291 if (!status) 1291 if (!status)
1292 udev->reset_resume = 0; 1292 udev->reset_resume = 0;
1293 return status; 1293 return status;
1294 } 1294 }
1295 1295
1296 static void choose_wakeup(struct usb_device *udev, pm_message_t msg) 1296 static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1297 { 1297 {
1298 int w; 1298 int w;
1299 1299
1300 /* Remote wakeup is needed only when we actually go to sleep. 1300 /* Remote wakeup is needed only when we actually go to sleep.
1301 * For things like FREEZE and QUIESCE, if the device is already 1301 * For things like FREEZE and QUIESCE, if the device is already
1302 * autosuspended then its current wakeup setting is okay. 1302 * autosuspended then its current wakeup setting is okay.
1303 */ 1303 */
1304 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) { 1304 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1305 if (udev->state != USB_STATE_SUSPENDED) 1305 if (udev->state != USB_STATE_SUSPENDED)
1306 udev->do_remote_wakeup = 0; 1306 udev->do_remote_wakeup = 0;
1307 return; 1307 return;
1308 } 1308 }
1309 1309
1310 /* Enable remote wakeup if it is allowed, even if no interface drivers 1310 /* Enable remote wakeup if it is allowed, even if no interface drivers
1311 * actually want it. 1311 * actually want it.
1312 */ 1312 */
1313 w = device_may_wakeup(&udev->dev); 1313 w = device_may_wakeup(&udev->dev);
1314 1314
1315 /* If the device is autosuspended with the wrong wakeup setting, 1315 /* If the device is autosuspended with the wrong wakeup setting,
1316 * autoresume now so the setting can be changed. 1316 * autoresume now so the setting can be changed.
1317 */ 1317 */
1318 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup) 1318 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1319 pm_runtime_resume(&udev->dev); 1319 pm_runtime_resume(&udev->dev);
1320 udev->do_remote_wakeup = w; 1320 udev->do_remote_wakeup = w;
1321 } 1321 }
1322 1322
1323 /* The device lock is held by the PM core */ 1323 /* The device lock is held by the PM core */
1324 int usb_suspend(struct device *dev, pm_message_t msg) 1324 int usb_suspend(struct device *dev, pm_message_t msg)
1325 { 1325 {
1326 struct usb_device *udev = to_usb_device(dev); 1326 struct usb_device *udev = to_usb_device(dev);
1327 1327
1328 unbind_no_pm_drivers_interfaces(udev); 1328 unbind_no_pm_drivers_interfaces(udev);
1329 1329
1330 /* From now on we are sure all drivers support suspend/resume 1330 /* From now on we are sure all drivers support suspend/resume
1331 * but not necessarily reset_resume() 1331 * but not necessarily reset_resume()
1332 * so we may still need to unbind and rebind upon resume 1332 * so we may still need to unbind and rebind upon resume
1333 */ 1333 */
1334 choose_wakeup(udev, msg); 1334 choose_wakeup(udev, msg);
1335 return usb_suspend_both(udev, msg); 1335 return usb_suspend_both(udev, msg);
1336 } 1336 }
1337 1337
1338 /* The device lock is held by the PM core */ 1338 /* The device lock is held by the PM core */
1339 int usb_resume(struct device *dev, pm_message_t msg) 1339 int usb_resume_complete(struct device *dev)
1340 { 1340 {
1341 struct usb_device *udev = to_usb_device(dev); 1341 struct usb_device *udev = to_usb_device(dev);
1342 int status;
1343 1342
1344 /* For PM complete calls, all we do is rebind interfaces 1343 /* For PM complete calls, all we do is rebind interfaces
1345 * whose needs_binding flag is set 1344 * whose needs_binding flag is set
1346 */ 1345 */
1347 if (msg.event == PM_EVENT_ON) { 1346 if (udev->state != USB_STATE_NOTATTACHED)
1348 if (udev->state != USB_STATE_NOTATTACHED) 1347 do_rebind_interfaces(udev);
1349 do_rebind_interfaces(udev); 1348 return 0;
1350 status = 0; 1349 }
1351 1350
1352 /* For all other calls, take the device back to full power and 1351 /* The device lock is held by the PM core */
1352 int usb_resume(struct device *dev, pm_message_t msg)
1353 {
1354 struct usb_device *udev = to_usb_device(dev);
1355 int status;
1356
1357 /* For all calls, take the device back to full power and
1353 * tell the PM core in case it was autosuspended previously. 1358 * tell the PM core in case it was autosuspended previously.
1354 * Unbind the interfaces that will need rebinding later, 1359 * Unbind the interfaces that will need rebinding later,
1355 * because they fail to support reset_resume. 1360 * because they fail to support reset_resume.
1356 * (This can't be done in usb_resume_interface() 1361 * (This can't be done in usb_resume_interface()
1357 * above because it doesn't own the right set of locks.) 1362 * above because it doesn't own the right set of locks.)
1358 */ 1363 */
1359 } else { 1364 status = usb_resume_both(udev, msg);
1360 status = usb_resume_both(udev, msg); 1365 if (status == 0) {
1361 if (status == 0) { 1366 pm_runtime_disable(dev);
1362 pm_runtime_disable(dev); 1367 pm_runtime_set_active(dev);
1363 pm_runtime_set_active(dev); 1368 pm_runtime_enable(dev);
1364 pm_runtime_enable(dev); 1369 unbind_no_reset_resume_drivers_interfaces(udev);
1365 unbind_no_reset_resume_drivers_interfaces(udev);
1366 }
1367 } 1370 }
1368 1371
1369 /* Avoid PM error messages for devices disconnected while suspended 1372 /* Avoid PM error messages for devices disconnected while suspended
1370 * as we'll display regular disconnect messages just a bit later. 1373 * as we'll display regular disconnect messages just a bit later.
1371 */ 1374 */
1372 if (status == -ENODEV || status == -ESHUTDOWN) 1375 if (status == -ENODEV || status == -ESHUTDOWN)
1373 status = 0; 1376 status = 0;
1374 return status; 1377 return status;
1375 } 1378 }
1376 1379
1377 #endif /* CONFIG_PM */ 1380 #endif /* CONFIG_PM */
1378 1381
1379 #ifdef CONFIG_USB_SUSPEND 1382 #ifdef CONFIG_USB_SUSPEND
1380 1383
1381 /** 1384 /**
1382 * usb_enable_autosuspend - allow a USB device to be autosuspended 1385 * usb_enable_autosuspend - allow a USB device to be autosuspended
1383 * @udev: the USB device which may be autosuspended 1386 * @udev: the USB device which may be autosuspended
1384 * 1387 *
1385 * This routine allows @udev to be autosuspended. An autosuspend won't 1388 * This routine allows @udev to be autosuspended. An autosuspend won't
1386 * take place until the autosuspend_delay has elapsed and all the other 1389 * take place until the autosuspend_delay has elapsed and all the other
1387 * necessary conditions are satisfied. 1390 * necessary conditions are satisfied.
1388 * 1391 *
1389 * The caller must hold @udev's device lock. 1392 * The caller must hold @udev's device lock.
1390 */ 1393 */
1391 void usb_enable_autosuspend(struct usb_device *udev) 1394 void usb_enable_autosuspend(struct usb_device *udev)
1392 { 1395 {
1393 pm_runtime_allow(&udev->dev); 1396 pm_runtime_allow(&udev->dev);
1394 } 1397 }
1395 EXPORT_SYMBOL_GPL(usb_enable_autosuspend); 1398 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1396 1399
1397 /** 1400 /**
1398 * usb_disable_autosuspend - prevent a USB device from being autosuspended 1401 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1399 * @udev: the USB device which may not be autosuspended 1402 * @udev: the USB device which may not be autosuspended
1400 * 1403 *
1401 * This routine prevents @udev from being autosuspended and wakes it up 1404 * This routine prevents @udev from being autosuspended and wakes it up
1402 * if it is already autosuspended. 1405 * if it is already autosuspended.
1403 * 1406 *
1404 * The caller must hold @udev's device lock. 1407 * The caller must hold @udev's device lock.
1405 */ 1408 */
1406 void usb_disable_autosuspend(struct usb_device *udev) 1409 void usb_disable_autosuspend(struct usb_device *udev)
1407 { 1410 {
1408 pm_runtime_forbid(&udev->dev); 1411 pm_runtime_forbid(&udev->dev);
1409 } 1412 }
1410 EXPORT_SYMBOL_GPL(usb_disable_autosuspend); 1413 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1411 1414
1412 /** 1415 /**
1413 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1416 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1414 * @udev: the usb_device to autosuspend 1417 * @udev: the usb_device to autosuspend
1415 * 1418 *
1416 * This routine should be called when a core subsystem is finished using 1419 * This routine should be called when a core subsystem is finished using
1417 * @udev and wants to allow it to autosuspend. Examples would be when 1420 * @udev and wants to allow it to autosuspend. Examples would be when
1418 * @udev's device file in usbfs is closed or after a configuration change. 1421 * @udev's device file in usbfs is closed or after a configuration change.
1419 * 1422 *
1420 * @udev's usage counter is decremented; if it drops to 0 and all the 1423 * @udev's usage counter is decremented; if it drops to 0 and all the
1421 * interfaces are inactive then a delayed autosuspend will be attempted. 1424 * interfaces are inactive then a delayed autosuspend will be attempted.
1422 * The attempt may fail (see autosuspend_check()). 1425 * The attempt may fail (see autosuspend_check()).
1423 * 1426 *
1424 * The caller must hold @udev's device lock. 1427 * The caller must hold @udev's device lock.
1425 * 1428 *
1426 * This routine can run only in process context. 1429 * This routine can run only in process context.
1427 */ 1430 */
1428 void usb_autosuspend_device(struct usb_device *udev) 1431 void usb_autosuspend_device(struct usb_device *udev)
1429 { 1432 {
1430 int status; 1433 int status;
1431 1434
1432 usb_mark_last_busy(udev); 1435 usb_mark_last_busy(udev);
1433 status = pm_runtime_put_sync_autosuspend(&udev->dev); 1436 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1434 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1437 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1435 __func__, atomic_read(&udev->dev.power.usage_count), 1438 __func__, atomic_read(&udev->dev.power.usage_count),
1436 status); 1439 status);
1437 } 1440 }
1438 1441
1439 /** 1442 /**
1440 * usb_autoresume_device - immediately autoresume a USB device and its interfaces 1443 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1441 * @udev: the usb_device to autoresume 1444 * @udev: the usb_device to autoresume
1442 * 1445 *
1443 * This routine should be called when a core subsystem wants to use @udev 1446 * This routine should be called when a core subsystem wants to use @udev
1444 * and needs to guarantee that it is not suspended. No autosuspend will 1447 * and needs to guarantee that it is not suspended. No autosuspend will
1445 * occur until usb_autosuspend_device() is called. (Note that this will 1448 * occur until usb_autosuspend_device() is called. (Note that this will
1446 * not prevent suspend events originating in the PM core.) Examples would 1449 * not prevent suspend events originating in the PM core.) Examples would
1447 * be when @udev's device file in usbfs is opened or when a remote-wakeup 1450 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1448 * request is received. 1451 * request is received.
1449 * 1452 *
1450 * @udev's usage counter is incremented to prevent subsequent autosuspends. 1453 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1451 * However if the autoresume fails then the usage counter is re-decremented. 1454 * However if the autoresume fails then the usage counter is re-decremented.
1452 * 1455 *
1453 * The caller must hold @udev's device lock. 1456 * The caller must hold @udev's device lock.
1454 * 1457 *
1455 * This routine can run only in process context. 1458 * This routine can run only in process context.
1456 */ 1459 */
1457 int usb_autoresume_device(struct usb_device *udev) 1460 int usb_autoresume_device(struct usb_device *udev)
1458 { 1461 {
1459 int status; 1462 int status;
1460 1463
1461 status = pm_runtime_get_sync(&udev->dev); 1464 status = pm_runtime_get_sync(&udev->dev);
1462 if (status < 0) 1465 if (status < 0)
1463 pm_runtime_put_sync(&udev->dev); 1466 pm_runtime_put_sync(&udev->dev);
1464 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1467 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1465 __func__, atomic_read(&udev->dev.power.usage_count), 1468 __func__, atomic_read(&udev->dev.power.usage_count),
1466 status); 1469 status);
1467 if (status > 0) 1470 if (status > 0)
1468 status = 0; 1471 status = 0;
1469 return status; 1472 return status;
1470 } 1473 }
1471 1474
1472 /** 1475 /**
1473 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter 1476 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1474 * @intf: the usb_interface whose counter should be decremented 1477 * @intf: the usb_interface whose counter should be decremented
1475 * 1478 *
1476 * This routine should be called by an interface driver when it is 1479 * This routine should be called by an interface driver when it is
1477 * finished using @intf and wants to allow it to autosuspend. A typical 1480 * finished using @intf and wants to allow it to autosuspend. A typical
1478 * example would be a character-device driver when its device file is 1481 * example would be a character-device driver when its device file is
1479 * closed. 1482 * closed.
1480 * 1483 *
1481 * The routine decrements @intf's usage counter. When the counter reaches 1484 * The routine decrements @intf's usage counter. When the counter reaches
1482 * 0, a delayed autosuspend request for @intf's device is attempted. The 1485 * 0, a delayed autosuspend request for @intf's device is attempted. The
1483 * attempt may fail (see autosuspend_check()). 1486 * attempt may fail (see autosuspend_check()).
1484 * 1487 *
1485 * This routine can run only in process context. 1488 * This routine can run only in process context.
1486 */ 1489 */
1487 void usb_autopm_put_interface(struct usb_interface *intf) 1490 void usb_autopm_put_interface(struct usb_interface *intf)
1488 { 1491 {
1489 struct usb_device *udev = interface_to_usbdev(intf); 1492 struct usb_device *udev = interface_to_usbdev(intf);
1490 int status; 1493 int status;
1491 1494
1492 usb_mark_last_busy(udev); 1495 usb_mark_last_busy(udev);
1493 atomic_dec(&intf->pm_usage_cnt); 1496 atomic_dec(&intf->pm_usage_cnt);
1494 status = pm_runtime_put_sync(&intf->dev); 1497 status = pm_runtime_put_sync(&intf->dev);
1495 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1498 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1496 __func__, atomic_read(&intf->dev.power.usage_count), 1499 __func__, atomic_read(&intf->dev.power.usage_count),
1497 status); 1500 status);
1498 } 1501 }
1499 EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1502 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1500 1503
1501 /** 1504 /**
1502 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1505 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1503 * @intf: the usb_interface whose counter should be decremented 1506 * @intf: the usb_interface whose counter should be decremented
1504 * 1507 *
1505 * This routine does much the same thing as usb_autopm_put_interface(): 1508 * This routine does much the same thing as usb_autopm_put_interface():
1506 * It decrements @intf's usage counter and schedules a delayed 1509 * It decrements @intf's usage counter and schedules a delayed
1507 * autosuspend request if the counter is <= 0. The difference is that it 1510 * autosuspend request if the counter is <= 0. The difference is that it
1508 * does not perform any synchronization; callers should hold a private 1511 * does not perform any synchronization; callers should hold a private
1509 * lock and handle all synchronization issues themselves. 1512 * lock and handle all synchronization issues themselves.
1510 * 1513 *
1511 * Typically a driver would call this routine during an URB's completion 1514 * Typically a driver would call this routine during an URB's completion
1512 * handler, if no more URBs were pending. 1515 * handler, if no more URBs were pending.
1513 * 1516 *
1514 * This routine can run in atomic context. 1517 * This routine can run in atomic context.
1515 */ 1518 */
1516 void usb_autopm_put_interface_async(struct usb_interface *intf) 1519 void usb_autopm_put_interface_async(struct usb_interface *intf)
1517 { 1520 {
1518 struct usb_device *udev = interface_to_usbdev(intf); 1521 struct usb_device *udev = interface_to_usbdev(intf);
1519 int status; 1522 int status;
1520 1523
1521 usb_mark_last_busy(udev); 1524 usb_mark_last_busy(udev);
1522 atomic_dec(&intf->pm_usage_cnt); 1525 atomic_dec(&intf->pm_usage_cnt);
1523 status = pm_runtime_put(&intf->dev); 1526 status = pm_runtime_put(&intf->dev);
1524 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1527 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1525 __func__, atomic_read(&intf->dev.power.usage_count), 1528 __func__, atomic_read(&intf->dev.power.usage_count),
1526 status); 1529 status);
1527 } 1530 }
1528 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1531 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1529 1532
1530 /** 1533 /**
1531 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter 1534 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1532 * @intf: the usb_interface whose counter should be decremented 1535 * @intf: the usb_interface whose counter should be decremented
1533 * 1536 *
1534 * This routine decrements @intf's usage counter but does not carry out an 1537 * This routine decrements @intf's usage counter but does not carry out an
1535 * autosuspend. 1538 * autosuspend.
1536 * 1539 *
1537 * This routine can run in atomic context. 1540 * This routine can run in atomic context.
1538 */ 1541 */
1539 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf) 1542 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1540 { 1543 {
1541 struct usb_device *udev = interface_to_usbdev(intf); 1544 struct usb_device *udev = interface_to_usbdev(intf);
1542 1545
1543 usb_mark_last_busy(udev); 1546 usb_mark_last_busy(udev);
1544 atomic_dec(&intf->pm_usage_cnt); 1547 atomic_dec(&intf->pm_usage_cnt);
1545 pm_runtime_put_noidle(&intf->dev); 1548 pm_runtime_put_noidle(&intf->dev);
1546 } 1549 }
1547 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend); 1550 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1548 1551
1549 /** 1552 /**
1550 * usb_autopm_get_interface - increment a USB interface's PM-usage counter 1553 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1551 * @intf: the usb_interface whose counter should be incremented 1554 * @intf: the usb_interface whose counter should be incremented
1552 * 1555 *
1553 * This routine should be called by an interface driver when it wants to 1556 * This routine should be called by an interface driver when it wants to
1554 * use @intf and needs to guarantee that it is not suspended. In addition, 1557 * use @intf and needs to guarantee that it is not suspended. In addition,
1555 * the routine prevents @intf from being autosuspended subsequently. (Note 1558 * the routine prevents @intf from being autosuspended subsequently. (Note
1556 * that this will not prevent suspend events originating in the PM core.) 1559 * that this will not prevent suspend events originating in the PM core.)
1557 * This prevention will persist until usb_autopm_put_interface() is called 1560 * This prevention will persist until usb_autopm_put_interface() is called
1558 * or @intf is unbound. A typical example would be a character-device 1561 * or @intf is unbound. A typical example would be a character-device
1559 * driver when its device file is opened. 1562 * driver when its device file is opened.
1560 * 1563 *
1561 * @intf's usage counter is incremented to prevent subsequent autosuspends. 1564 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1562 * However if the autoresume fails then the counter is re-decremented. 1565 * However if the autoresume fails then the counter is re-decremented.
1563 * 1566 *
1564 * This routine can run only in process context. 1567 * This routine can run only in process context.
1565 */ 1568 */
1566 int usb_autopm_get_interface(struct usb_interface *intf) 1569 int usb_autopm_get_interface(struct usb_interface *intf)
1567 { 1570 {
1568 int status; 1571 int status;
1569 1572
1570 status = pm_runtime_get_sync(&intf->dev); 1573 status = pm_runtime_get_sync(&intf->dev);
1571 if (status < 0) 1574 if (status < 0)
1572 pm_runtime_put_sync(&intf->dev); 1575 pm_runtime_put_sync(&intf->dev);
1573 else 1576 else
1574 atomic_inc(&intf->pm_usage_cnt); 1577 atomic_inc(&intf->pm_usage_cnt);
1575 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1578 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1576 __func__, atomic_read(&intf->dev.power.usage_count), 1579 __func__, atomic_read(&intf->dev.power.usage_count),
1577 status); 1580 status);
1578 if (status > 0) 1581 if (status > 0)
1579 status = 0; 1582 status = 0;
1580 return status; 1583 return status;
1581 } 1584 }
1582 EXPORT_SYMBOL_GPL(usb_autopm_get_interface); 1585 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1583 1586
1584 /** 1587 /**
1585 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter 1588 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1586 * @intf: the usb_interface whose counter should be incremented 1589 * @intf: the usb_interface whose counter should be incremented
1587 * 1590 *
1588 * This routine does much the same thing as 1591 * This routine does much the same thing as
1589 * usb_autopm_get_interface(): It increments @intf's usage counter and 1592 * usb_autopm_get_interface(): It increments @intf's usage counter and
1590 * queues an autoresume request if the device is suspended. The 1593 * queues an autoresume request if the device is suspended. The
1591 * differences are that it does not perform any synchronization (callers 1594 * differences are that it does not perform any synchronization (callers
1592 * should hold a private lock and handle all synchronization issues 1595 * should hold a private lock and handle all synchronization issues
1593 * themselves), and it does not autoresume the device directly (it only 1596 * themselves), and it does not autoresume the device directly (it only
1594 * queues a request). After a successful call, the device may not yet be 1597 * queues a request). After a successful call, the device may not yet be
1595 * resumed. 1598 * resumed.
1596 * 1599 *
1597 * This routine can run in atomic context. 1600 * This routine can run in atomic context.
1598 */ 1601 */
1599 int usb_autopm_get_interface_async(struct usb_interface *intf) 1602 int usb_autopm_get_interface_async(struct usb_interface *intf)
1600 { 1603 {
1601 int status; 1604 int status;
1602 1605
1603 status = pm_runtime_get(&intf->dev); 1606 status = pm_runtime_get(&intf->dev);
1604 if (status < 0 && status != -EINPROGRESS) 1607 if (status < 0 && status != -EINPROGRESS)
1605 pm_runtime_put_noidle(&intf->dev); 1608 pm_runtime_put_noidle(&intf->dev);
1606 else 1609 else
1607 atomic_inc(&intf->pm_usage_cnt); 1610 atomic_inc(&intf->pm_usage_cnt);
1608 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1611 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1609 __func__, atomic_read(&intf->dev.power.usage_count), 1612 __func__, atomic_read(&intf->dev.power.usage_count),
1610 status); 1613 status);
1611 if (status > 0 || status == -EINPROGRESS) 1614 if (status > 0 || status == -EINPROGRESS)
1612 status = 0; 1615 status = 0;
1613 return status; 1616 return status;
1614 } 1617 }
1615 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1618 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1616 1619
1617 /** 1620 /**
1618 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter 1621 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1619 * @intf: the usb_interface whose counter should be incremented 1622 * @intf: the usb_interface whose counter should be incremented
1620 * 1623 *
1621 * This routine increments @intf's usage counter but does not carry out an 1624 * This routine increments @intf's usage counter but does not carry out an
1622 * autoresume. 1625 * autoresume.
1623 * 1626 *
1624 * This routine can run in atomic context. 1627 * This routine can run in atomic context.
1625 */ 1628 */
1626 void usb_autopm_get_interface_no_resume(struct usb_interface *intf) 1629 void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1627 { 1630 {
1628 struct usb_device *udev = interface_to_usbdev(intf); 1631 struct usb_device *udev = interface_to_usbdev(intf);
1629 1632
1630 usb_mark_last_busy(udev); 1633 usb_mark_last_busy(udev);
1631 atomic_inc(&intf->pm_usage_cnt); 1634 atomic_inc(&intf->pm_usage_cnt);
1632 pm_runtime_get_noresume(&intf->dev); 1635 pm_runtime_get_noresume(&intf->dev);
1633 } 1636 }
1634 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume); 1637 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1635 1638
1636 /* Internal routine to check whether we may autosuspend a device. */ 1639 /* Internal routine to check whether we may autosuspend a device. */
1637 static int autosuspend_check(struct usb_device *udev) 1640 static int autosuspend_check(struct usb_device *udev)
1638 { 1641 {
1639 int w, i; 1642 int w, i;
1640 struct usb_interface *intf; 1643 struct usb_interface *intf;
1641 1644
1642 /* Fail if autosuspend is disabled, or any interfaces are in use, or 1645 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1643 * any interface drivers require remote wakeup but it isn't available. 1646 * any interface drivers require remote wakeup but it isn't available.
1644 */ 1647 */
1645 w = 0; 1648 w = 0;
1646 if (udev->actconfig) { 1649 if (udev->actconfig) {
1647 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1650 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1648 intf = udev->actconfig->interface[i]; 1651 intf = udev->actconfig->interface[i];
1649 1652
1650 /* We don't need to check interfaces that are 1653 /* We don't need to check interfaces that are
1651 * disabled for runtime PM. Either they are unbound 1654 * disabled for runtime PM. Either they are unbound
1652 * or else their drivers don't support autosuspend 1655 * or else their drivers don't support autosuspend
1653 * and so they are permanently active. 1656 * and so they are permanently active.
1654 */ 1657 */
1655 if (intf->dev.power.disable_depth) 1658 if (intf->dev.power.disable_depth)
1656 continue; 1659 continue;
1657 if (atomic_read(&intf->dev.power.usage_count) > 0) 1660 if (atomic_read(&intf->dev.power.usage_count) > 0)
1658 return -EBUSY; 1661 return -EBUSY;
1659 w |= intf->needs_remote_wakeup; 1662 w |= intf->needs_remote_wakeup;
1660 1663
1661 /* Don't allow autosuspend if the device will need 1664 /* Don't allow autosuspend if the device will need
1662 * a reset-resume and any of its interface drivers 1665 * a reset-resume and any of its interface drivers
1663 * doesn't include support or needs remote wakeup. 1666 * doesn't include support or needs remote wakeup.
1664 */ 1667 */
1665 if (udev->quirks & USB_QUIRK_RESET_RESUME) { 1668 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1666 struct usb_driver *driver; 1669 struct usb_driver *driver;
1667 1670
1668 driver = to_usb_driver(intf->dev.driver); 1671 driver = to_usb_driver(intf->dev.driver);
1669 if (!driver->reset_resume || 1672 if (!driver->reset_resume ||
1670 intf->needs_remote_wakeup) 1673 intf->needs_remote_wakeup)
1671 return -EOPNOTSUPP; 1674 return -EOPNOTSUPP;
1672 } 1675 }
1673 } 1676 }
1674 } 1677 }
1675 if (w && !device_can_wakeup(&udev->dev)) { 1678 if (w && !device_can_wakeup(&udev->dev)) {
1676 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n"); 1679 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1677 return -EOPNOTSUPP; 1680 return -EOPNOTSUPP;
1678 } 1681 }
1679 udev->do_remote_wakeup = w; 1682 udev->do_remote_wakeup = w;
1680 return 0; 1683 return 0;
1681 } 1684 }
1682 1685
1683 int usb_runtime_suspend(struct device *dev) 1686 int usb_runtime_suspend(struct device *dev)
1684 { 1687 {
1685 struct usb_device *udev = to_usb_device(dev); 1688 struct usb_device *udev = to_usb_device(dev);
1686 int status; 1689 int status;
1687 1690
1688 /* A USB device can be suspended if it passes the various autosuspend 1691 /* A USB device can be suspended if it passes the various autosuspend
1689 * checks. Runtime suspend for a USB device means suspending all the 1692 * checks. Runtime suspend for a USB device means suspending all the
1690 * interfaces and then the device itself. 1693 * interfaces and then the device itself.
1691 */ 1694 */
1692 if (autosuspend_check(udev) != 0) 1695 if (autosuspend_check(udev) != 0)
1693 return -EAGAIN; 1696 return -EAGAIN;
1694 1697
1695 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1698 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1696 1699
1697 /* Allow a retry if autosuspend failed temporarily */ 1700 /* Allow a retry if autosuspend failed temporarily */
1698 if (status == -EAGAIN || status == -EBUSY) 1701 if (status == -EAGAIN || status == -EBUSY)
1699 usb_mark_last_busy(udev); 1702 usb_mark_last_busy(udev);
1700 1703
1701 /* The PM core reacts badly unless the return code is 0, 1704 /* The PM core reacts badly unless the return code is 0,
1702 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error. 1705 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1703 */ 1706 */
1704 if (status != 0) 1707 if (status != 0)
1705 return -EBUSY; 1708 return -EBUSY;
1706 return status; 1709 return status;
1707 } 1710 }
1708 1711
1709 int usb_runtime_resume(struct device *dev) 1712 int usb_runtime_resume(struct device *dev)
1710 { 1713 {
1711 struct usb_device *udev = to_usb_device(dev); 1714 struct usb_device *udev = to_usb_device(dev);
1712 int status; 1715 int status;
1713 1716
1714 /* Runtime resume for a USB device means resuming both the device 1717 /* Runtime resume for a USB device means resuming both the device
1715 * and all its interfaces. 1718 * and all its interfaces.
1716 */ 1719 */
1717 status = usb_resume_both(udev, PMSG_AUTO_RESUME); 1720 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1718 return status; 1721 return status;
1719 } 1722 }
1720 1723
1721 int usb_runtime_idle(struct device *dev) 1724 int usb_runtime_idle(struct device *dev)
1722 { 1725 {
1723 struct usb_device *udev = to_usb_device(dev); 1726 struct usb_device *udev = to_usb_device(dev);
1724 1727
1725 /* An idle USB device can be suspended if it passes the various 1728 /* An idle USB device can be suspended if it passes the various
1726 * autosuspend checks. 1729 * autosuspend checks.
1727 */ 1730 */
1728 if (autosuspend_check(udev) == 0) 1731 if (autosuspend_check(udev) == 0)
1729 pm_runtime_autosuspend(dev); 1732 pm_runtime_autosuspend(dev);
1730 return 0; 1733 return 0;
1731 } 1734 }
1732 1735
1733 int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable) 1736 int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1734 { 1737 {
1735 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 1738 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1736 int ret = -EPERM; 1739 int ret = -EPERM;
1737 1740
1738 if (hcd->driver->set_usb2_hw_lpm) { 1741 if (hcd->driver->set_usb2_hw_lpm) {
1739 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable); 1742 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1740 if (!ret) 1743 if (!ret)
1741 udev->usb2_hw_lpm_enabled = enable; 1744 udev->usb2_hw_lpm_enabled = enable;
1742 } 1745 }
1743 1746
1744 return ret; 1747 return ret;
1745 } 1748 }
1746 1749
1747 #endif /* CONFIG_USB_SUSPEND */ 1750 #endif /* CONFIG_USB_SUSPEND */
1748 1751
1749 struct bus_type usb_bus_type = { 1752 struct bus_type usb_bus_type = {
1750 .name = "usb", 1753 .name = "usb",
1751 .match = usb_device_match, 1754 .match = usb_device_match,
drivers/usb/core/usb.c
1 /* 1 /*
2 * drivers/usb/core/usb.c 2 * drivers/usb/core/usb.c
3 * 3 *
4 * (C) Copyright Linus Torvalds 1999 4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001 5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999 6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999 7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture) 8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000 9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000-2004 10 * (C) Copyright David Brownell 2000-2004
11 * (C) Copyright Yggdrasil Computing, Inc. 2000 11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter) 12 * (usb_device_id matching changes by Adam J. Richter)
13 * (C) Copyright Greg Kroah-Hartman 2002-2003 13 * (C) Copyright Greg Kroah-Hartman 2002-2003
14 * 14 *
15 * NOTE! This is not actually a driver at all, rather this is 15 * NOTE! This is not actually a driver at all, rather this is
16 * just a collection of helper routines that implement the 16 * just a collection of helper routines that implement the
17 * generic USB things that the real drivers can use.. 17 * generic USB things that the real drivers can use..
18 * 18 *
19 * Think of this as a "USB library" rather than anything else. 19 * Think of this as a "USB library" rather than anything else.
20 * It should be considered a slave, with no callbacks. Callbacks 20 * It should be considered a slave, with no callbacks. Callbacks
21 * are evil. 21 * are evil.
22 */ 22 */
23 23
24 #include <linux/module.h> 24 #include <linux/module.h>
25 #include <linux/moduleparam.h> 25 #include <linux/moduleparam.h>
26 #include <linux/string.h> 26 #include <linux/string.h>
27 #include <linux/bitops.h> 27 #include <linux/bitops.h>
28 #include <linux/slab.h> 28 #include <linux/slab.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */ 29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/kmod.h> 30 #include <linux/kmod.h>
31 #include <linux/init.h> 31 #include <linux/init.h>
32 #include <linux/spinlock.h> 32 #include <linux/spinlock.h>
33 #include <linux/errno.h> 33 #include <linux/errno.h>
34 #include <linux/usb.h> 34 #include <linux/usb.h>
35 #include <linux/usb/hcd.h> 35 #include <linux/usb/hcd.h>
36 #include <linux/mutex.h> 36 #include <linux/mutex.h>
37 #include <linux/workqueue.h> 37 #include <linux/workqueue.h>
38 #include <linux/debugfs.h> 38 #include <linux/debugfs.h>
39 39
40 #include <asm/io.h> 40 #include <asm/io.h>
41 #include <linux/scatterlist.h> 41 #include <linux/scatterlist.h>
42 #include <linux/mm.h> 42 #include <linux/mm.h>
43 #include <linux/dma-mapping.h> 43 #include <linux/dma-mapping.h>
44 44
45 #include "usb.h" 45 #include "usb.h"
46 46
47 47
48 const char *usbcore_name = "usbcore"; 48 const char *usbcore_name = "usbcore";
49 49
50 static bool nousb; /* Disable USB when built into kernel image */ 50 static bool nousb; /* Disable USB when built into kernel image */
51 51
52 #ifdef CONFIG_USB_SUSPEND 52 #ifdef CONFIG_USB_SUSPEND
53 static int usb_autosuspend_delay = 2; /* Default delay value, 53 static int usb_autosuspend_delay = 2; /* Default delay value,
54 * in seconds */ 54 * in seconds */
55 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644); 55 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
56 MODULE_PARM_DESC(autosuspend, "default autosuspend delay"); 56 MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
57 57
58 #else 58 #else
59 #define usb_autosuspend_delay 0 59 #define usb_autosuspend_delay 0
60 #endif 60 #endif
61 61
62 62
63 /** 63 /**
64 * usb_find_alt_setting() - Given a configuration, find the alternate setting 64 * usb_find_alt_setting() - Given a configuration, find the alternate setting
65 * for the given interface. 65 * for the given interface.
66 * @config: the configuration to search (not necessarily the current config). 66 * @config: the configuration to search (not necessarily the current config).
67 * @iface_num: interface number to search in 67 * @iface_num: interface number to search in
68 * @alt_num: alternate interface setting number to search for. 68 * @alt_num: alternate interface setting number to search for.
69 * 69 *
70 * Search the configuration's interface cache for the given alt setting. 70 * Search the configuration's interface cache for the given alt setting.
71 */ 71 */
72 struct usb_host_interface *usb_find_alt_setting( 72 struct usb_host_interface *usb_find_alt_setting(
73 struct usb_host_config *config, 73 struct usb_host_config *config,
74 unsigned int iface_num, 74 unsigned int iface_num,
75 unsigned int alt_num) 75 unsigned int alt_num)
76 { 76 {
77 struct usb_interface_cache *intf_cache = NULL; 77 struct usb_interface_cache *intf_cache = NULL;
78 int i; 78 int i;
79 79
80 for (i = 0; i < config->desc.bNumInterfaces; i++) { 80 for (i = 0; i < config->desc.bNumInterfaces; i++) {
81 if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber 81 if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
82 == iface_num) { 82 == iface_num) {
83 intf_cache = config->intf_cache[i]; 83 intf_cache = config->intf_cache[i];
84 break; 84 break;
85 } 85 }
86 } 86 }
87 if (!intf_cache) 87 if (!intf_cache)
88 return NULL; 88 return NULL;
89 for (i = 0; i < intf_cache->num_altsetting; i++) 89 for (i = 0; i < intf_cache->num_altsetting; i++)
90 if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num) 90 if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
91 return &intf_cache->altsetting[i]; 91 return &intf_cache->altsetting[i];
92 92
93 printk(KERN_DEBUG "Did not find alt setting %u for intf %u, " 93 printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
94 "config %u\n", alt_num, iface_num, 94 "config %u\n", alt_num, iface_num,
95 config->desc.bConfigurationValue); 95 config->desc.bConfigurationValue);
96 return NULL; 96 return NULL;
97 } 97 }
98 EXPORT_SYMBOL_GPL(usb_find_alt_setting); 98 EXPORT_SYMBOL_GPL(usb_find_alt_setting);
99 99
100 /** 100 /**
101 * usb_ifnum_to_if - get the interface object with a given interface number 101 * usb_ifnum_to_if - get the interface object with a given interface number
102 * @dev: the device whose current configuration is considered 102 * @dev: the device whose current configuration is considered
103 * @ifnum: the desired interface 103 * @ifnum: the desired interface
104 * 104 *
105 * This walks the device descriptor for the currently active configuration 105 * This walks the device descriptor for the currently active configuration
106 * and returns a pointer to the interface with that particular interface 106 * and returns a pointer to the interface with that particular interface
107 * number, or null. 107 * number, or null.
108 * 108 *
109 * Note that configuration descriptors are not required to assign interface 109 * Note that configuration descriptors are not required to assign interface
110 * numbers sequentially, so that it would be incorrect to assume that 110 * numbers sequentially, so that it would be incorrect to assume that
111 * the first interface in that descriptor corresponds to interface zero. 111 * the first interface in that descriptor corresponds to interface zero.
112 * This routine helps device drivers avoid such mistakes. 112 * This routine helps device drivers avoid such mistakes.
113 * However, you should make sure that you do the right thing with any 113 * However, you should make sure that you do the right thing with any
114 * alternate settings available for this interfaces. 114 * alternate settings available for this interfaces.
115 * 115 *
116 * Don't call this function unless you are bound to one of the interfaces 116 * Don't call this function unless you are bound to one of the interfaces
117 * on this device or you have locked the device! 117 * on this device or you have locked the device!
118 */ 118 */
119 struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev, 119 struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
120 unsigned ifnum) 120 unsigned ifnum)
121 { 121 {
122 struct usb_host_config *config = dev->actconfig; 122 struct usb_host_config *config = dev->actconfig;
123 int i; 123 int i;
124 124
125 if (!config) 125 if (!config)
126 return NULL; 126 return NULL;
127 for (i = 0; i < config->desc.bNumInterfaces; i++) 127 for (i = 0; i < config->desc.bNumInterfaces; i++)
128 if (config->interface[i]->altsetting[0] 128 if (config->interface[i]->altsetting[0]
129 .desc.bInterfaceNumber == ifnum) 129 .desc.bInterfaceNumber == ifnum)
130 return config->interface[i]; 130 return config->interface[i];
131 131
132 return NULL; 132 return NULL;
133 } 133 }
134 EXPORT_SYMBOL_GPL(usb_ifnum_to_if); 134 EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
135 135
136 /** 136 /**
137 * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number. 137 * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
138 * @intf: the interface containing the altsetting in question 138 * @intf: the interface containing the altsetting in question
139 * @altnum: the desired alternate setting number 139 * @altnum: the desired alternate setting number
140 * 140 *
141 * This searches the altsetting array of the specified interface for 141 * This searches the altsetting array of the specified interface for
142 * an entry with the correct bAlternateSetting value and returns a pointer 142 * an entry with the correct bAlternateSetting value and returns a pointer
143 * to that entry, or null. 143 * to that entry, or null.
144 * 144 *
145 * Note that altsettings need not be stored sequentially by number, so 145 * Note that altsettings need not be stored sequentially by number, so
146 * it would be incorrect to assume that the first altsetting entry in 146 * it would be incorrect to assume that the first altsetting entry in
147 * the array corresponds to altsetting zero. This routine helps device 147 * the array corresponds to altsetting zero. This routine helps device
148 * drivers avoid such mistakes. 148 * drivers avoid such mistakes.
149 * 149 *
150 * Don't call this function unless you are bound to the intf interface 150 * Don't call this function unless you are bound to the intf interface
151 * or you have locked the device! 151 * or you have locked the device!
152 */ 152 */
153 struct usb_host_interface *usb_altnum_to_altsetting( 153 struct usb_host_interface *usb_altnum_to_altsetting(
154 const struct usb_interface *intf, 154 const struct usb_interface *intf,
155 unsigned int altnum) 155 unsigned int altnum)
156 { 156 {
157 int i; 157 int i;
158 158
159 for (i = 0; i < intf->num_altsetting; i++) { 159 for (i = 0; i < intf->num_altsetting; i++) {
160 if (intf->altsetting[i].desc.bAlternateSetting == altnum) 160 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
161 return &intf->altsetting[i]; 161 return &intf->altsetting[i];
162 } 162 }
163 return NULL; 163 return NULL;
164 } 164 }
165 EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting); 165 EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
166 166
167 struct find_interface_arg { 167 struct find_interface_arg {
168 int minor; 168 int minor;
169 struct device_driver *drv; 169 struct device_driver *drv;
170 }; 170 };
171 171
172 static int __find_interface(struct device *dev, void *data) 172 static int __find_interface(struct device *dev, void *data)
173 { 173 {
174 struct find_interface_arg *arg = data; 174 struct find_interface_arg *arg = data;
175 struct usb_interface *intf; 175 struct usb_interface *intf;
176 176
177 if (!is_usb_interface(dev)) 177 if (!is_usb_interface(dev))
178 return 0; 178 return 0;
179 179
180 if (dev->driver != arg->drv) 180 if (dev->driver != arg->drv)
181 return 0; 181 return 0;
182 intf = to_usb_interface(dev); 182 intf = to_usb_interface(dev);
183 return intf->minor == arg->minor; 183 return intf->minor == arg->minor;
184 } 184 }
185 185
186 /** 186 /**
187 * usb_find_interface - find usb_interface pointer for driver and device 187 * usb_find_interface - find usb_interface pointer for driver and device
188 * @drv: the driver whose current configuration is considered 188 * @drv: the driver whose current configuration is considered
189 * @minor: the minor number of the desired device 189 * @minor: the minor number of the desired device
190 * 190 *
191 * This walks the bus device list and returns a pointer to the interface 191 * This walks the bus device list and returns a pointer to the interface
192 * with the matching minor and driver. Note, this only works for devices 192 * with the matching minor and driver. Note, this only works for devices
193 * that share the USB major number. 193 * that share the USB major number.
194 */ 194 */
195 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) 195 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
196 { 196 {
197 struct find_interface_arg argb; 197 struct find_interface_arg argb;
198 struct device *dev; 198 struct device *dev;
199 199
200 argb.minor = minor; 200 argb.minor = minor;
201 argb.drv = &drv->drvwrap.driver; 201 argb.drv = &drv->drvwrap.driver;
202 202
203 dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface); 203 dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
204 204
205 /* Drop reference count from bus_find_device */ 205 /* Drop reference count from bus_find_device */
206 put_device(dev); 206 put_device(dev);
207 207
208 return dev ? to_usb_interface(dev) : NULL; 208 return dev ? to_usb_interface(dev) : NULL;
209 } 209 }
210 EXPORT_SYMBOL_GPL(usb_find_interface); 210 EXPORT_SYMBOL_GPL(usb_find_interface);
211 211
212 /** 212 /**
213 * usb_release_dev - free a usb device structure when all users of it are finished. 213 * usb_release_dev - free a usb device structure when all users of it are finished.
214 * @dev: device that's been disconnected 214 * @dev: device that's been disconnected
215 * 215 *
216 * Will be called only by the device core when all users of this usb device are 216 * Will be called only by the device core when all users of this usb device are
217 * done. 217 * done.
218 */ 218 */
219 static void usb_release_dev(struct device *dev) 219 static void usb_release_dev(struct device *dev)
220 { 220 {
221 struct usb_device *udev; 221 struct usb_device *udev;
222 struct usb_hcd *hcd; 222 struct usb_hcd *hcd;
223 223
224 udev = to_usb_device(dev); 224 udev = to_usb_device(dev);
225 hcd = bus_to_hcd(udev->bus); 225 hcd = bus_to_hcd(udev->bus);
226 226
227 usb_destroy_configuration(udev); 227 usb_destroy_configuration(udev);
228 usb_release_bos_descriptor(udev); 228 usb_release_bos_descriptor(udev);
229 usb_put_hcd(hcd); 229 usb_put_hcd(hcd);
230 kfree(udev->product); 230 kfree(udev->product);
231 kfree(udev->manufacturer); 231 kfree(udev->manufacturer);
232 kfree(udev->serial); 232 kfree(udev->serial);
233 kfree(udev); 233 kfree(udev);
234 } 234 }
235 235
236 #ifdef CONFIG_HOTPLUG 236 #ifdef CONFIG_HOTPLUG
237 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env) 237 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
238 { 238 {
239 struct usb_device *usb_dev; 239 struct usb_device *usb_dev;
240 240
241 usb_dev = to_usb_device(dev); 241 usb_dev = to_usb_device(dev);
242 242
243 if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum)) 243 if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
244 return -ENOMEM; 244 return -ENOMEM;
245 245
246 if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum)) 246 if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
247 return -ENOMEM; 247 return -ENOMEM;
248 248
249 return 0; 249 return 0;
250 } 250 }
251 251
252 #else 252 #else
253 253
254 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env) 254 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
255 { 255 {
256 return -ENODEV; 256 return -ENODEV;
257 } 257 }
258 #endif /* CONFIG_HOTPLUG */ 258 #endif /* CONFIG_HOTPLUG */
259 259
260 #ifdef CONFIG_PM 260 #ifdef CONFIG_PM
261 261
262 /* USB device Power-Management thunks. 262 /* USB device Power-Management thunks.
263 * There's no need to distinguish here between quiescing a USB device 263 * There's no need to distinguish here between quiescing a USB device
264 * and powering it down; the generic_suspend() routine takes care of 264 * and powering it down; the generic_suspend() routine takes care of
265 * it by skipping the usb_port_suspend() call for a quiesce. And for 265 * it by skipping the usb_port_suspend() call for a quiesce. And for
266 * USB interfaces there's no difference at all. 266 * USB interfaces there's no difference at all.
267 */ 267 */
268 268
269 static int usb_dev_prepare(struct device *dev) 269 static int usb_dev_prepare(struct device *dev)
270 { 270 {
271 return 0; /* Implement eventually? */ 271 return 0; /* Implement eventually? */
272 } 272 }
273 273
274 static void usb_dev_complete(struct device *dev) 274 static void usb_dev_complete(struct device *dev)
275 { 275 {
276 /* Currently used only for rebinding interfaces */ 276 /* Currently used only for rebinding interfaces */
277 usb_resume(dev, PMSG_ON); /* FIXME: change to PMSG_COMPLETE */ 277 usb_resume_complete(dev);
278 } 278 }
279 279
280 static int usb_dev_suspend(struct device *dev) 280 static int usb_dev_suspend(struct device *dev)
281 { 281 {
282 return usb_suspend(dev, PMSG_SUSPEND); 282 return usb_suspend(dev, PMSG_SUSPEND);
283 } 283 }
284 284
285 static int usb_dev_resume(struct device *dev) 285 static int usb_dev_resume(struct device *dev)
286 { 286 {
287 return usb_resume(dev, PMSG_RESUME); 287 return usb_resume(dev, PMSG_RESUME);
288 } 288 }
289 289
290 static int usb_dev_freeze(struct device *dev) 290 static int usb_dev_freeze(struct device *dev)
291 { 291 {
292 return usb_suspend(dev, PMSG_FREEZE); 292 return usb_suspend(dev, PMSG_FREEZE);
293 } 293 }
294 294
295 static int usb_dev_thaw(struct device *dev) 295 static int usb_dev_thaw(struct device *dev)
296 { 296 {
297 return usb_resume(dev, PMSG_THAW); 297 return usb_resume(dev, PMSG_THAW);
298 } 298 }
299 299
300 static int usb_dev_poweroff(struct device *dev) 300 static int usb_dev_poweroff(struct device *dev)
301 { 301 {
302 return usb_suspend(dev, PMSG_HIBERNATE); 302 return usb_suspend(dev, PMSG_HIBERNATE);
303 } 303 }
304 304
305 static int usb_dev_restore(struct device *dev) 305 static int usb_dev_restore(struct device *dev)
306 { 306 {
307 return usb_resume(dev, PMSG_RESTORE); 307 return usb_resume(dev, PMSG_RESTORE);
308 } 308 }
309 309
310 static const struct dev_pm_ops usb_device_pm_ops = { 310 static const struct dev_pm_ops usb_device_pm_ops = {
311 .prepare = usb_dev_prepare, 311 .prepare = usb_dev_prepare,
312 .complete = usb_dev_complete, 312 .complete = usb_dev_complete,
313 .suspend = usb_dev_suspend, 313 .suspend = usb_dev_suspend,
314 .resume = usb_dev_resume, 314 .resume = usb_dev_resume,
315 .freeze = usb_dev_freeze, 315 .freeze = usb_dev_freeze,
316 .thaw = usb_dev_thaw, 316 .thaw = usb_dev_thaw,
317 .poweroff = usb_dev_poweroff, 317 .poweroff = usb_dev_poweroff,
318 .restore = usb_dev_restore, 318 .restore = usb_dev_restore,
319 #ifdef CONFIG_USB_SUSPEND 319 #ifdef CONFIG_USB_SUSPEND
320 .runtime_suspend = usb_runtime_suspend, 320 .runtime_suspend = usb_runtime_suspend,
321 .runtime_resume = usb_runtime_resume, 321 .runtime_resume = usb_runtime_resume,
322 .runtime_idle = usb_runtime_idle, 322 .runtime_idle = usb_runtime_idle,
323 #endif 323 #endif
324 }; 324 };
325 325
326 #endif /* CONFIG_PM */ 326 #endif /* CONFIG_PM */
327 327
328 328
329 static char *usb_devnode(struct device *dev, umode_t *mode) 329 static char *usb_devnode(struct device *dev, umode_t *mode)
330 { 330 {
331 struct usb_device *usb_dev; 331 struct usb_device *usb_dev;
332 332
333 usb_dev = to_usb_device(dev); 333 usb_dev = to_usb_device(dev);
334 return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d", 334 return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
335 usb_dev->bus->busnum, usb_dev->devnum); 335 usb_dev->bus->busnum, usb_dev->devnum);
336 } 336 }
337 337
338 struct device_type usb_device_type = { 338 struct device_type usb_device_type = {
339 .name = "usb_device", 339 .name = "usb_device",
340 .release = usb_release_dev, 340 .release = usb_release_dev,
341 .uevent = usb_dev_uevent, 341 .uevent = usb_dev_uevent,
342 .devnode = usb_devnode, 342 .devnode = usb_devnode,
343 #ifdef CONFIG_PM 343 #ifdef CONFIG_PM
344 .pm = &usb_device_pm_ops, 344 .pm = &usb_device_pm_ops,
345 #endif 345 #endif
346 }; 346 };
347 347
348 348
349 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */ 349 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
350 static unsigned usb_bus_is_wusb(struct usb_bus *bus) 350 static unsigned usb_bus_is_wusb(struct usb_bus *bus)
351 { 351 {
352 struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self); 352 struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self);
353 return hcd->wireless; 353 return hcd->wireless;
354 } 354 }
355 355
356 356
357 /** 357 /**
358 * usb_alloc_dev - usb device constructor (usbcore-internal) 358 * usb_alloc_dev - usb device constructor (usbcore-internal)
359 * @parent: hub to which device is connected; null to allocate a root hub 359 * @parent: hub to which device is connected; null to allocate a root hub
360 * @bus: bus used to access the device 360 * @bus: bus used to access the device
361 * @port1: one-based index of port; ignored for root hubs 361 * @port1: one-based index of port; ignored for root hubs
362 * Context: !in_interrupt() 362 * Context: !in_interrupt()
363 * 363 *
364 * Only hub drivers (including virtual root hub drivers for host 364 * Only hub drivers (including virtual root hub drivers for host
365 * controllers) should ever call this. 365 * controllers) should ever call this.
366 * 366 *
367 * This call may not be used in a non-sleeping context. 367 * This call may not be used in a non-sleeping context.
368 */ 368 */
369 struct usb_device *usb_alloc_dev(struct usb_device *parent, 369 struct usb_device *usb_alloc_dev(struct usb_device *parent,
370 struct usb_bus *bus, unsigned port1) 370 struct usb_bus *bus, unsigned port1)
371 { 371 {
372 struct usb_device *dev; 372 struct usb_device *dev;
373 struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self); 373 struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
374 unsigned root_hub = 0; 374 unsigned root_hub = 0;
375 375
376 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 376 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
377 if (!dev) 377 if (!dev)
378 return NULL; 378 return NULL;
379 379
380 if (!usb_get_hcd(bus_to_hcd(bus))) { 380 if (!usb_get_hcd(bus_to_hcd(bus))) {
381 kfree(dev); 381 kfree(dev);
382 return NULL; 382 return NULL;
383 } 383 }
384 /* Root hubs aren't true devices, so don't allocate HCD resources */ 384 /* Root hubs aren't true devices, so don't allocate HCD resources */
385 if (usb_hcd->driver->alloc_dev && parent && 385 if (usb_hcd->driver->alloc_dev && parent &&
386 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) { 386 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
387 usb_put_hcd(bus_to_hcd(bus)); 387 usb_put_hcd(bus_to_hcd(bus));
388 kfree(dev); 388 kfree(dev);
389 return NULL; 389 return NULL;
390 } 390 }
391 391
392 device_initialize(&dev->dev); 392 device_initialize(&dev->dev);
393 dev->dev.bus = &usb_bus_type; 393 dev->dev.bus = &usb_bus_type;
394 dev->dev.type = &usb_device_type; 394 dev->dev.type = &usb_device_type;
395 dev->dev.groups = usb_device_groups; 395 dev->dev.groups = usb_device_groups;
396 dev->dev.dma_mask = bus->controller->dma_mask; 396 dev->dev.dma_mask = bus->controller->dma_mask;
397 set_dev_node(&dev->dev, dev_to_node(bus->controller)); 397 set_dev_node(&dev->dev, dev_to_node(bus->controller));
398 dev->state = USB_STATE_ATTACHED; 398 dev->state = USB_STATE_ATTACHED;
399 atomic_set(&dev->urbnum, 0); 399 atomic_set(&dev->urbnum, 0);
400 400
401 INIT_LIST_HEAD(&dev->ep0.urb_list); 401 INIT_LIST_HEAD(&dev->ep0.urb_list);
402 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; 402 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
403 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; 403 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
404 /* ep0 maxpacket comes later, from device descriptor */ 404 /* ep0 maxpacket comes later, from device descriptor */
405 usb_enable_endpoint(dev, &dev->ep0, false); 405 usb_enable_endpoint(dev, &dev->ep0, false);
406 dev->can_submit = 1; 406 dev->can_submit = 1;
407 407
408 /* Save readable and stable topology id, distinguishing devices 408 /* Save readable and stable topology id, distinguishing devices
409 * by location for diagnostics, tools, driver model, etc. The 409 * by location for diagnostics, tools, driver model, etc. The
410 * string is a path along hub ports, from the root. Each device's 410 * string is a path along hub ports, from the root. Each device's
411 * dev->devpath will be stable until USB is re-cabled, and hubs 411 * dev->devpath will be stable until USB is re-cabled, and hubs
412 * are often labeled with these port numbers. The name isn't 412 * are often labeled with these port numbers. The name isn't
413 * as stable: bus->busnum changes easily from modprobe order, 413 * as stable: bus->busnum changes easily from modprobe order,
414 * cardbus or pci hotplugging, and so on. 414 * cardbus or pci hotplugging, and so on.
415 */ 415 */
416 if (unlikely(!parent)) { 416 if (unlikely(!parent)) {
417 dev->devpath[0] = '0'; 417 dev->devpath[0] = '0';
418 dev->route = 0; 418 dev->route = 0;
419 419
420 dev->dev.parent = bus->controller; 420 dev->dev.parent = bus->controller;
421 dev_set_name(&dev->dev, "usb%d", bus->busnum); 421 dev_set_name(&dev->dev, "usb%d", bus->busnum);
422 root_hub = 1; 422 root_hub = 1;
423 } else { 423 } else {
424 /* match any labeling on the hubs; it's one-based */ 424 /* match any labeling on the hubs; it's one-based */
425 if (parent->devpath[0] == '0') { 425 if (parent->devpath[0] == '0') {
426 snprintf(dev->devpath, sizeof dev->devpath, 426 snprintf(dev->devpath, sizeof dev->devpath,
427 "%d", port1); 427 "%d", port1);
428 /* Root ports are not counted in route string */ 428 /* Root ports are not counted in route string */
429 dev->route = 0; 429 dev->route = 0;
430 } else { 430 } else {
431 snprintf(dev->devpath, sizeof dev->devpath, 431 snprintf(dev->devpath, sizeof dev->devpath,
432 "%s.%d", parent->devpath, port1); 432 "%s.%d", parent->devpath, port1);
433 /* Route string assumes hubs have less than 16 ports */ 433 /* Route string assumes hubs have less than 16 ports */
434 if (port1 < 15) 434 if (port1 < 15)
435 dev->route = parent->route + 435 dev->route = parent->route +
436 (port1 << ((parent->level - 1)*4)); 436 (port1 << ((parent->level - 1)*4));
437 else 437 else
438 dev->route = parent->route + 438 dev->route = parent->route +
439 (15 << ((parent->level - 1)*4)); 439 (15 << ((parent->level - 1)*4));
440 } 440 }
441 441
442 dev->dev.parent = &parent->dev; 442 dev->dev.parent = &parent->dev;
443 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath); 443 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
444 444
445 /* hub driver sets up TT records */ 445 /* hub driver sets up TT records */
446 } 446 }
447 447
448 dev->portnum = port1; 448 dev->portnum = port1;
449 dev->bus = bus; 449 dev->bus = bus;
450 dev->parent = parent; 450 dev->parent = parent;
451 INIT_LIST_HEAD(&dev->filelist); 451 INIT_LIST_HEAD(&dev->filelist);
452 452
453 #ifdef CONFIG_PM 453 #ifdef CONFIG_PM
454 pm_runtime_set_autosuspend_delay(&dev->dev, 454 pm_runtime_set_autosuspend_delay(&dev->dev,
455 usb_autosuspend_delay * 1000); 455 usb_autosuspend_delay * 1000);
456 dev->connect_time = jiffies; 456 dev->connect_time = jiffies;
457 dev->active_duration = -jiffies; 457 dev->active_duration = -jiffies;
458 #endif 458 #endif
459 if (root_hub) /* Root hub always ok [and always wired] */ 459 if (root_hub) /* Root hub always ok [and always wired] */
460 dev->authorized = 1; 460 dev->authorized = 1;
461 else { 461 else {
462 dev->authorized = usb_hcd->authorized_default; 462 dev->authorized = usb_hcd->authorized_default;
463 dev->wusb = usb_bus_is_wusb(bus)? 1 : 0; 463 dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
464 } 464 }
465 return dev; 465 return dev;
466 } 466 }
467 467
468 /** 468 /**
469 * usb_get_dev - increments the reference count of the usb device structure 469 * usb_get_dev - increments the reference count of the usb device structure
470 * @dev: the device being referenced 470 * @dev: the device being referenced
471 * 471 *
472 * Each live reference to a device should be refcounted. 472 * Each live reference to a device should be refcounted.
473 * 473 *
474 * Drivers for USB interfaces should normally record such references in 474 * Drivers for USB interfaces should normally record such references in
475 * their probe() methods, when they bind to an interface, and release 475 * their probe() methods, when they bind to an interface, and release
476 * them by calling usb_put_dev(), in their disconnect() methods. 476 * them by calling usb_put_dev(), in their disconnect() methods.
477 * 477 *
478 * A pointer to the device with the incremented reference counter is returned. 478 * A pointer to the device with the incremented reference counter is returned.
479 */ 479 */
480 struct usb_device *usb_get_dev(struct usb_device *dev) 480 struct usb_device *usb_get_dev(struct usb_device *dev)
481 { 481 {
482 if (dev) 482 if (dev)
483 get_device(&dev->dev); 483 get_device(&dev->dev);
484 return dev; 484 return dev;
485 } 485 }
486 EXPORT_SYMBOL_GPL(usb_get_dev); 486 EXPORT_SYMBOL_GPL(usb_get_dev);
487 487
488 /** 488 /**
489 * usb_put_dev - release a use of the usb device structure 489 * usb_put_dev - release a use of the usb device structure
490 * @dev: device that's been disconnected 490 * @dev: device that's been disconnected
491 * 491 *
492 * Must be called when a user of a device is finished with it. When the last 492 * Must be called when a user of a device is finished with it. When the last
493 * user of the device calls this function, the memory of the device is freed. 493 * user of the device calls this function, the memory of the device is freed.
494 */ 494 */
495 void usb_put_dev(struct usb_device *dev) 495 void usb_put_dev(struct usb_device *dev)
496 { 496 {
497 if (dev) 497 if (dev)
498 put_device(&dev->dev); 498 put_device(&dev->dev);
499 } 499 }
500 EXPORT_SYMBOL_GPL(usb_put_dev); 500 EXPORT_SYMBOL_GPL(usb_put_dev);
501 501
502 /** 502 /**
503 * usb_get_intf - increments the reference count of the usb interface structure 503 * usb_get_intf - increments the reference count of the usb interface structure
504 * @intf: the interface being referenced 504 * @intf: the interface being referenced
505 * 505 *
506 * Each live reference to a interface must be refcounted. 506 * Each live reference to a interface must be refcounted.
507 * 507 *
508 * Drivers for USB interfaces should normally record such references in 508 * Drivers for USB interfaces should normally record such references in
509 * their probe() methods, when they bind to an interface, and release 509 * their probe() methods, when they bind to an interface, and release
510 * them by calling usb_put_intf(), in their disconnect() methods. 510 * them by calling usb_put_intf(), in their disconnect() methods.
511 * 511 *
512 * A pointer to the interface with the incremented reference counter is 512 * A pointer to the interface with the incremented reference counter is
513 * returned. 513 * returned.
514 */ 514 */
515 struct usb_interface *usb_get_intf(struct usb_interface *intf) 515 struct usb_interface *usb_get_intf(struct usb_interface *intf)
516 { 516 {
517 if (intf) 517 if (intf)
518 get_device(&intf->dev); 518 get_device(&intf->dev);
519 return intf; 519 return intf;
520 } 520 }
521 EXPORT_SYMBOL_GPL(usb_get_intf); 521 EXPORT_SYMBOL_GPL(usb_get_intf);
522 522
523 /** 523 /**
524 * usb_put_intf - release a use of the usb interface structure 524 * usb_put_intf - release a use of the usb interface structure
525 * @intf: interface that's been decremented 525 * @intf: interface that's been decremented
526 * 526 *
527 * Must be called when a user of an interface is finished with it. When the 527 * Must be called when a user of an interface is finished with it. When the
528 * last user of the interface calls this function, the memory of the interface 528 * last user of the interface calls this function, the memory of the interface
529 * is freed. 529 * is freed.
530 */ 530 */
531 void usb_put_intf(struct usb_interface *intf) 531 void usb_put_intf(struct usb_interface *intf)
532 { 532 {
533 if (intf) 533 if (intf)
534 put_device(&intf->dev); 534 put_device(&intf->dev);
535 } 535 }
536 EXPORT_SYMBOL_GPL(usb_put_intf); 536 EXPORT_SYMBOL_GPL(usb_put_intf);
537 537
538 /* USB device locking 538 /* USB device locking
539 * 539 *
540 * USB devices and interfaces are locked using the semaphore in their 540 * USB devices and interfaces are locked using the semaphore in their
541 * embedded struct device. The hub driver guarantees that whenever a 541 * embedded struct device. The hub driver guarantees that whenever a
542 * device is connected or disconnected, drivers are called with the 542 * device is connected or disconnected, drivers are called with the
543 * USB device locked as well as their particular interface. 543 * USB device locked as well as their particular interface.
544 * 544 *
545 * Complications arise when several devices are to be locked at the same 545 * Complications arise when several devices are to be locked at the same
546 * time. Only hub-aware drivers that are part of usbcore ever have to 546 * time. Only hub-aware drivers that are part of usbcore ever have to
547 * do this; nobody else needs to worry about it. The rule for locking 547 * do this; nobody else needs to worry about it. The rule for locking
548 * is simple: 548 * is simple:
549 * 549 *
550 * When locking both a device and its parent, always lock the 550 * When locking both a device and its parent, always lock the
551 * the parent first. 551 * the parent first.
552 */ 552 */
553 553
554 /** 554 /**
555 * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure 555 * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
556 * @udev: device that's being locked 556 * @udev: device that's being locked
557 * @iface: interface bound to the driver making the request (optional) 557 * @iface: interface bound to the driver making the request (optional)
558 * 558 *
559 * Attempts to acquire the device lock, but fails if the device is 559 * Attempts to acquire the device lock, but fails if the device is
560 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface 560 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
561 * is neither BINDING nor BOUND. Rather than sleeping to wait for the 561 * is neither BINDING nor BOUND. Rather than sleeping to wait for the
562 * lock, the routine polls repeatedly. This is to prevent deadlock with 562 * lock, the routine polls repeatedly. This is to prevent deadlock with
563 * disconnect; in some drivers (such as usb-storage) the disconnect() 563 * disconnect; in some drivers (such as usb-storage) the disconnect()
564 * or suspend() method will block waiting for a device reset to complete. 564 * or suspend() method will block waiting for a device reset to complete.
565 * 565 *
566 * Returns a negative error code for failure, otherwise 0. 566 * Returns a negative error code for failure, otherwise 0.
567 */ 567 */
568 int usb_lock_device_for_reset(struct usb_device *udev, 568 int usb_lock_device_for_reset(struct usb_device *udev,
569 const struct usb_interface *iface) 569 const struct usb_interface *iface)
570 { 570 {
571 unsigned long jiffies_expire = jiffies + HZ; 571 unsigned long jiffies_expire = jiffies + HZ;
572 572
573 if (udev->state == USB_STATE_NOTATTACHED) 573 if (udev->state == USB_STATE_NOTATTACHED)
574 return -ENODEV; 574 return -ENODEV;
575 if (udev->state == USB_STATE_SUSPENDED) 575 if (udev->state == USB_STATE_SUSPENDED)
576 return -EHOSTUNREACH; 576 return -EHOSTUNREACH;
577 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || 577 if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
578 iface->condition == USB_INTERFACE_UNBOUND)) 578 iface->condition == USB_INTERFACE_UNBOUND))
579 return -EINTR; 579 return -EINTR;
580 580
581 while (!usb_trylock_device(udev)) { 581 while (!usb_trylock_device(udev)) {
582 582
583 /* If we can't acquire the lock after waiting one second, 583 /* If we can't acquire the lock after waiting one second,
584 * we're probably deadlocked */ 584 * we're probably deadlocked */
585 if (time_after(jiffies, jiffies_expire)) 585 if (time_after(jiffies, jiffies_expire))
586 return -EBUSY; 586 return -EBUSY;
587 587
588 msleep(15); 588 msleep(15);
589 if (udev->state == USB_STATE_NOTATTACHED) 589 if (udev->state == USB_STATE_NOTATTACHED)
590 return -ENODEV; 590 return -ENODEV;
591 if (udev->state == USB_STATE_SUSPENDED) 591 if (udev->state == USB_STATE_SUSPENDED)
592 return -EHOSTUNREACH; 592 return -EHOSTUNREACH;
593 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || 593 if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
594 iface->condition == USB_INTERFACE_UNBOUND)) 594 iface->condition == USB_INTERFACE_UNBOUND))
595 return -EINTR; 595 return -EINTR;
596 } 596 }
597 return 0; 597 return 0;
598 } 598 }
599 EXPORT_SYMBOL_GPL(usb_lock_device_for_reset); 599 EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
600 600
601 /** 601 /**
602 * usb_get_current_frame_number - return current bus frame number 602 * usb_get_current_frame_number - return current bus frame number
603 * @dev: the device whose bus is being queried 603 * @dev: the device whose bus is being queried
604 * 604 *
605 * Returns the current frame number for the USB host controller 605 * Returns the current frame number for the USB host controller
606 * used with the given USB device. This can be used when scheduling 606 * used with the given USB device. This can be used when scheduling
607 * isochronous requests. 607 * isochronous requests.
608 * 608 *
609 * Note that different kinds of host controller have different 609 * Note that different kinds of host controller have different
610 * "scheduling horizons". While one type might support scheduling only 610 * "scheduling horizons". While one type might support scheduling only
611 * 32 frames into the future, others could support scheduling up to 611 * 32 frames into the future, others could support scheduling up to
612 * 1024 frames into the future. 612 * 1024 frames into the future.
613 */ 613 */
614 int usb_get_current_frame_number(struct usb_device *dev) 614 int usb_get_current_frame_number(struct usb_device *dev)
615 { 615 {
616 return usb_hcd_get_frame_number(dev); 616 return usb_hcd_get_frame_number(dev);
617 } 617 }
618 EXPORT_SYMBOL_GPL(usb_get_current_frame_number); 618 EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
619 619
620 /*-------------------------------------------------------------------*/ 620 /*-------------------------------------------------------------------*/
621 /* 621 /*
622 * __usb_get_extra_descriptor() finds a descriptor of specific type in the 622 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
623 * extra field of the interface and endpoint descriptor structs. 623 * extra field of the interface and endpoint descriptor structs.
624 */ 624 */
625 625
626 int __usb_get_extra_descriptor(char *buffer, unsigned size, 626 int __usb_get_extra_descriptor(char *buffer, unsigned size,
627 unsigned char type, void **ptr) 627 unsigned char type, void **ptr)
628 { 628 {
629 struct usb_descriptor_header *header; 629 struct usb_descriptor_header *header;
630 630
631 while (size >= sizeof(struct usb_descriptor_header)) { 631 while (size >= sizeof(struct usb_descriptor_header)) {
632 header = (struct usb_descriptor_header *)buffer; 632 header = (struct usb_descriptor_header *)buffer;
633 633
634 if (header->bLength < 2) { 634 if (header->bLength < 2) {
635 printk(KERN_ERR 635 printk(KERN_ERR
636 "%s: bogus descriptor, type %d length %d\n", 636 "%s: bogus descriptor, type %d length %d\n",
637 usbcore_name, 637 usbcore_name,
638 header->bDescriptorType, 638 header->bDescriptorType,
639 header->bLength); 639 header->bLength);
640 return -1; 640 return -1;
641 } 641 }
642 642
643 if (header->bDescriptorType == type) { 643 if (header->bDescriptorType == type) {
644 *ptr = header; 644 *ptr = header;
645 return 0; 645 return 0;
646 } 646 }
647 647
648 buffer += header->bLength; 648 buffer += header->bLength;
649 size -= header->bLength; 649 size -= header->bLength;
650 } 650 }
651 return -1; 651 return -1;
652 } 652 }
653 EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor); 653 EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
654 654
655 /** 655 /**
656 * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP 656 * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
657 * @dev: device the buffer will be used with 657 * @dev: device the buffer will be used with
658 * @size: requested buffer size 658 * @size: requested buffer size
659 * @mem_flags: affect whether allocation may block 659 * @mem_flags: affect whether allocation may block
660 * @dma: used to return DMA address of buffer 660 * @dma: used to return DMA address of buffer
661 * 661 *
662 * Return value is either null (indicating no buffer could be allocated), or 662 * Return value is either null (indicating no buffer could be allocated), or
663 * the cpu-space pointer to a buffer that may be used to perform DMA to the 663 * the cpu-space pointer to a buffer that may be used to perform DMA to the
664 * specified device. Such cpu-space buffers are returned along with the DMA 664 * specified device. Such cpu-space buffers are returned along with the DMA
665 * address (through the pointer provided). 665 * address (through the pointer provided).
666 * 666 *
667 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags 667 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
668 * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU 668 * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
669 * hardware during URB completion/resubmit. The implementation varies between 669 * hardware during URB completion/resubmit. The implementation varies between
670 * platforms, depending on details of how DMA will work to this device. 670 * platforms, depending on details of how DMA will work to this device.
671 * Using these buffers also eliminates cacheline sharing problems on 671 * Using these buffers also eliminates cacheline sharing problems on
672 * architectures where CPU caches are not DMA-coherent. On systems without 672 * architectures where CPU caches are not DMA-coherent. On systems without
673 * bus-snooping caches, these buffers are uncached. 673 * bus-snooping caches, these buffers are uncached.
674 * 674 *
675 * When the buffer is no longer used, free it with usb_free_coherent(). 675 * When the buffer is no longer used, free it with usb_free_coherent().
676 */ 676 */
677 void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags, 677 void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
678 dma_addr_t *dma) 678 dma_addr_t *dma)
679 { 679 {
680 if (!dev || !dev->bus) 680 if (!dev || !dev->bus)
681 return NULL; 681 return NULL;
682 return hcd_buffer_alloc(dev->bus, size, mem_flags, dma); 682 return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
683 } 683 }
684 EXPORT_SYMBOL_GPL(usb_alloc_coherent); 684 EXPORT_SYMBOL_GPL(usb_alloc_coherent);
685 685
686 /** 686 /**
687 * usb_free_coherent - free memory allocated with usb_alloc_coherent() 687 * usb_free_coherent - free memory allocated with usb_alloc_coherent()
688 * @dev: device the buffer was used with 688 * @dev: device the buffer was used with
689 * @size: requested buffer size 689 * @size: requested buffer size
690 * @addr: CPU address of buffer 690 * @addr: CPU address of buffer
691 * @dma: DMA address of buffer 691 * @dma: DMA address of buffer
692 * 692 *
693 * This reclaims an I/O buffer, letting it be reused. The memory must have 693 * This reclaims an I/O buffer, letting it be reused. The memory must have
694 * been allocated using usb_alloc_coherent(), and the parameters must match 694 * been allocated using usb_alloc_coherent(), and the parameters must match
695 * those provided in that allocation request. 695 * those provided in that allocation request.
696 */ 696 */
697 void usb_free_coherent(struct usb_device *dev, size_t size, void *addr, 697 void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
698 dma_addr_t dma) 698 dma_addr_t dma)
699 { 699 {
700 if (!dev || !dev->bus) 700 if (!dev || !dev->bus)
701 return; 701 return;
702 if (!addr) 702 if (!addr)
703 return; 703 return;
704 hcd_buffer_free(dev->bus, size, addr, dma); 704 hcd_buffer_free(dev->bus, size, addr, dma);
705 } 705 }
706 EXPORT_SYMBOL_GPL(usb_free_coherent); 706 EXPORT_SYMBOL_GPL(usb_free_coherent);
707 707
708 /** 708 /**
709 * usb_buffer_map - create DMA mapping(s) for an urb 709 * usb_buffer_map - create DMA mapping(s) for an urb
710 * @urb: urb whose transfer_buffer/setup_packet will be mapped 710 * @urb: urb whose transfer_buffer/setup_packet will be mapped
711 * 711 *
712 * Return value is either null (indicating no buffer could be mapped), or 712 * Return value is either null (indicating no buffer could be mapped), or
713 * the parameter. URB_NO_TRANSFER_DMA_MAP is 713 * the parameter. URB_NO_TRANSFER_DMA_MAP is
714 * added to urb->transfer_flags if the operation succeeds. If the device 714 * added to urb->transfer_flags if the operation succeeds. If the device
715 * is connected to this system through a non-DMA controller, this operation 715 * is connected to this system through a non-DMA controller, this operation
716 * always succeeds. 716 * always succeeds.
717 * 717 *
718 * This call would normally be used for an urb which is reused, perhaps 718 * This call would normally be used for an urb which is reused, perhaps
719 * as the target of a large periodic transfer, with usb_buffer_dmasync() 719 * as the target of a large periodic transfer, with usb_buffer_dmasync()
720 * calls to synchronize memory and dma state. 720 * calls to synchronize memory and dma state.
721 * 721 *
722 * Reverse the effect of this call with usb_buffer_unmap(). 722 * Reverse the effect of this call with usb_buffer_unmap().
723 */ 723 */
724 #if 0 724 #if 0
725 struct urb *usb_buffer_map(struct urb *urb) 725 struct urb *usb_buffer_map(struct urb *urb)
726 { 726 {
727 struct usb_bus *bus; 727 struct usb_bus *bus;
728 struct device *controller; 728 struct device *controller;
729 729
730 if (!urb 730 if (!urb
731 || !urb->dev 731 || !urb->dev
732 || !(bus = urb->dev->bus) 732 || !(bus = urb->dev->bus)
733 || !(controller = bus->controller)) 733 || !(controller = bus->controller))
734 return NULL; 734 return NULL;
735 735
736 if (controller->dma_mask) { 736 if (controller->dma_mask) {
737 urb->transfer_dma = dma_map_single(controller, 737 urb->transfer_dma = dma_map_single(controller,
738 urb->transfer_buffer, urb->transfer_buffer_length, 738 urb->transfer_buffer, urb->transfer_buffer_length,
739 usb_pipein(urb->pipe) 739 usb_pipein(urb->pipe)
740 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 740 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
741 /* FIXME generic api broken like pci, can't report errors */ 741 /* FIXME generic api broken like pci, can't report errors */
742 /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */ 742 /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
743 } else 743 } else
744 urb->transfer_dma = ~0; 744 urb->transfer_dma = ~0;
745 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 745 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
746 return urb; 746 return urb;
747 } 747 }
748 EXPORT_SYMBOL_GPL(usb_buffer_map); 748 EXPORT_SYMBOL_GPL(usb_buffer_map);
749 #endif /* 0 */ 749 #endif /* 0 */
750 750
751 /* XXX DISABLED, no users currently. If you wish to re-enable this 751 /* XXX DISABLED, no users currently. If you wish to re-enable this
752 * XXX please determine whether the sync is to transfer ownership of 752 * XXX please determine whether the sync is to transfer ownership of
753 * XXX the buffer from device to cpu or vice verse, and thusly use the 753 * XXX the buffer from device to cpu or vice verse, and thusly use the
754 * XXX appropriate _for_{cpu,device}() method. -DaveM 754 * XXX appropriate _for_{cpu,device}() method. -DaveM
755 */ 755 */
756 #if 0 756 #if 0
757 757
758 /** 758 /**
759 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) 759 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
760 * @urb: urb whose transfer_buffer/setup_packet will be synchronized 760 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
761 */ 761 */
762 void usb_buffer_dmasync(struct urb *urb) 762 void usb_buffer_dmasync(struct urb *urb)
763 { 763 {
764 struct usb_bus *bus; 764 struct usb_bus *bus;
765 struct device *controller; 765 struct device *controller;
766 766
767 if (!urb 767 if (!urb
768 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) 768 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
769 || !urb->dev 769 || !urb->dev
770 || !(bus = urb->dev->bus) 770 || !(bus = urb->dev->bus)
771 || !(controller = bus->controller)) 771 || !(controller = bus->controller))
772 return; 772 return;
773 773
774 if (controller->dma_mask) { 774 if (controller->dma_mask) {
775 dma_sync_single_for_cpu(controller, 775 dma_sync_single_for_cpu(controller,
776 urb->transfer_dma, urb->transfer_buffer_length, 776 urb->transfer_dma, urb->transfer_buffer_length,
777 usb_pipein(urb->pipe) 777 usb_pipein(urb->pipe)
778 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 778 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
779 if (usb_pipecontrol(urb->pipe)) 779 if (usb_pipecontrol(urb->pipe))
780 dma_sync_single_for_cpu(controller, 780 dma_sync_single_for_cpu(controller,
781 urb->setup_dma, 781 urb->setup_dma,
782 sizeof(struct usb_ctrlrequest), 782 sizeof(struct usb_ctrlrequest),
783 DMA_TO_DEVICE); 783 DMA_TO_DEVICE);
784 } 784 }
785 } 785 }
786 EXPORT_SYMBOL_GPL(usb_buffer_dmasync); 786 EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
787 #endif 787 #endif
788 788
789 /** 789 /**
790 * usb_buffer_unmap - free DMA mapping(s) for an urb 790 * usb_buffer_unmap - free DMA mapping(s) for an urb
791 * @urb: urb whose transfer_buffer will be unmapped 791 * @urb: urb whose transfer_buffer will be unmapped
792 * 792 *
793 * Reverses the effect of usb_buffer_map(). 793 * Reverses the effect of usb_buffer_map().
794 */ 794 */
795 #if 0 795 #if 0
796 void usb_buffer_unmap(struct urb *urb) 796 void usb_buffer_unmap(struct urb *urb)
797 { 797 {
798 struct usb_bus *bus; 798 struct usb_bus *bus;
799 struct device *controller; 799 struct device *controller;
800 800
801 if (!urb 801 if (!urb
802 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) 802 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
803 || !urb->dev 803 || !urb->dev
804 || !(bus = urb->dev->bus) 804 || !(bus = urb->dev->bus)
805 || !(controller = bus->controller)) 805 || !(controller = bus->controller))
806 return; 806 return;
807 807
808 if (controller->dma_mask) { 808 if (controller->dma_mask) {
809 dma_unmap_single(controller, 809 dma_unmap_single(controller,
810 urb->transfer_dma, urb->transfer_buffer_length, 810 urb->transfer_dma, urb->transfer_buffer_length,
811 usb_pipein(urb->pipe) 811 usb_pipein(urb->pipe)
812 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 812 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
813 } 813 }
814 urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP; 814 urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
815 } 815 }
816 EXPORT_SYMBOL_GPL(usb_buffer_unmap); 816 EXPORT_SYMBOL_GPL(usb_buffer_unmap);
817 #endif /* 0 */ 817 #endif /* 0 */
818 818
819 #if 0 819 #if 0
820 /** 820 /**
821 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint 821 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
822 * @dev: device to which the scatterlist will be mapped 822 * @dev: device to which the scatterlist will be mapped
823 * @is_in: mapping transfer direction 823 * @is_in: mapping transfer direction
824 * @sg: the scatterlist to map 824 * @sg: the scatterlist to map
825 * @nents: the number of entries in the scatterlist 825 * @nents: the number of entries in the scatterlist
826 * 826 *
827 * Return value is either < 0 (indicating no buffers could be mapped), or 827 * Return value is either < 0 (indicating no buffers could be mapped), or
828 * the number of DMA mapping array entries in the scatterlist. 828 * the number of DMA mapping array entries in the scatterlist.
829 * 829 *
830 * The caller is responsible for placing the resulting DMA addresses from 830 * The caller is responsible for placing the resulting DMA addresses from
831 * the scatterlist into URB transfer buffer pointers, and for setting the 831 * the scatterlist into URB transfer buffer pointers, and for setting the
832 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs. 832 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
833 * 833 *
834 * Top I/O rates come from queuing URBs, instead of waiting for each one 834 * Top I/O rates come from queuing URBs, instead of waiting for each one
835 * to complete before starting the next I/O. This is particularly easy 835 * to complete before starting the next I/O. This is particularly easy
836 * to do with scatterlists. Just allocate and submit one URB for each DMA 836 * to do with scatterlists. Just allocate and submit one URB for each DMA
837 * mapping entry returned, stopping on the first error or when all succeed. 837 * mapping entry returned, stopping on the first error or when all succeed.
838 * Better yet, use the usb_sg_*() calls, which do that (and more) for you. 838 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
839 * 839 *
840 * This call would normally be used when translating scatterlist requests, 840 * This call would normally be used when translating scatterlist requests,
841 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it 841 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
842 * may be able to coalesce mappings for improved I/O efficiency. 842 * may be able to coalesce mappings for improved I/O efficiency.
843 * 843 *
844 * Reverse the effect of this call with usb_buffer_unmap_sg(). 844 * Reverse the effect of this call with usb_buffer_unmap_sg().
845 */ 845 */
846 int usb_buffer_map_sg(const struct usb_device *dev, int is_in, 846 int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
847 struct scatterlist *sg, int nents) 847 struct scatterlist *sg, int nents)
848 { 848 {
849 struct usb_bus *bus; 849 struct usb_bus *bus;
850 struct device *controller; 850 struct device *controller;
851 851
852 if (!dev 852 if (!dev
853 || !(bus = dev->bus) 853 || !(bus = dev->bus)
854 || !(controller = bus->controller) 854 || !(controller = bus->controller)
855 || !controller->dma_mask) 855 || !controller->dma_mask)
856 return -EINVAL; 856 return -EINVAL;
857 857
858 /* FIXME generic api broken like pci, can't report errors */ 858 /* FIXME generic api broken like pci, can't report errors */
859 return dma_map_sg(controller, sg, nents, 859 return dma_map_sg(controller, sg, nents,
860 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM; 860 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM;
861 } 861 }
862 EXPORT_SYMBOL_GPL(usb_buffer_map_sg); 862 EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
863 #endif 863 #endif
864 864
865 /* XXX DISABLED, no users currently. If you wish to re-enable this 865 /* XXX DISABLED, no users currently. If you wish to re-enable this
866 * XXX please determine whether the sync is to transfer ownership of 866 * XXX please determine whether the sync is to transfer ownership of
867 * XXX the buffer from device to cpu or vice verse, and thusly use the 867 * XXX the buffer from device to cpu or vice verse, and thusly use the
868 * XXX appropriate _for_{cpu,device}() method. -DaveM 868 * XXX appropriate _for_{cpu,device}() method. -DaveM
869 */ 869 */
870 #if 0 870 #if 0
871 871
872 /** 872 /**
873 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s) 873 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
874 * @dev: device to which the scatterlist will be mapped 874 * @dev: device to which the scatterlist will be mapped
875 * @is_in: mapping transfer direction 875 * @is_in: mapping transfer direction
876 * @sg: the scatterlist to synchronize 876 * @sg: the scatterlist to synchronize
877 * @n_hw_ents: the positive return value from usb_buffer_map_sg 877 * @n_hw_ents: the positive return value from usb_buffer_map_sg
878 * 878 *
879 * Use this when you are re-using a scatterlist's data buffers for 879 * Use this when you are re-using a scatterlist's data buffers for
880 * another USB request. 880 * another USB request.
881 */ 881 */
882 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in, 882 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
883 struct scatterlist *sg, int n_hw_ents) 883 struct scatterlist *sg, int n_hw_ents)
884 { 884 {
885 struct usb_bus *bus; 885 struct usb_bus *bus;
886 struct device *controller; 886 struct device *controller;
887 887
888 if (!dev 888 if (!dev
889 || !(bus = dev->bus) 889 || !(bus = dev->bus)
890 || !(controller = bus->controller) 890 || !(controller = bus->controller)
891 || !controller->dma_mask) 891 || !controller->dma_mask)
892 return; 892 return;
893 893
894 dma_sync_sg_for_cpu(controller, sg, n_hw_ents, 894 dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
895 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 895 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
896 } 896 }
897 EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg); 897 EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
898 #endif 898 #endif
899 899
900 #if 0 900 #if 0
901 /** 901 /**
902 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist 902 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
903 * @dev: device to which the scatterlist will be mapped 903 * @dev: device to which the scatterlist will be mapped
904 * @is_in: mapping transfer direction 904 * @is_in: mapping transfer direction
905 * @sg: the scatterlist to unmap 905 * @sg: the scatterlist to unmap
906 * @n_hw_ents: the positive return value from usb_buffer_map_sg 906 * @n_hw_ents: the positive return value from usb_buffer_map_sg
907 * 907 *
908 * Reverses the effect of usb_buffer_map_sg(). 908 * Reverses the effect of usb_buffer_map_sg().
909 */ 909 */
910 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in, 910 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
911 struct scatterlist *sg, int n_hw_ents) 911 struct scatterlist *sg, int n_hw_ents)
912 { 912 {
913 struct usb_bus *bus; 913 struct usb_bus *bus;
914 struct device *controller; 914 struct device *controller;
915 915
916 if (!dev 916 if (!dev
917 || !(bus = dev->bus) 917 || !(bus = dev->bus)
918 || !(controller = bus->controller) 918 || !(controller = bus->controller)
919 || !controller->dma_mask) 919 || !controller->dma_mask)
920 return; 920 return;
921 921
922 dma_unmap_sg(controller, sg, n_hw_ents, 922 dma_unmap_sg(controller, sg, n_hw_ents,
923 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 923 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
924 } 924 }
925 EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg); 925 EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
926 #endif 926 #endif
927 927
928 /* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */ 928 /* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */
929 #ifdef MODULE 929 #ifdef MODULE
930 module_param(nousb, bool, 0444); 930 module_param(nousb, bool, 0444);
931 #else 931 #else
932 core_param(nousb, nousb, bool, 0444); 932 core_param(nousb, nousb, bool, 0444);
933 #endif 933 #endif
934 934
935 /* 935 /*
936 * for external read access to <nousb> 936 * for external read access to <nousb>
937 */ 937 */
938 int usb_disabled(void) 938 int usb_disabled(void)
939 { 939 {
940 return nousb; 940 return nousb;
941 } 941 }
942 EXPORT_SYMBOL_GPL(usb_disabled); 942 EXPORT_SYMBOL_GPL(usb_disabled);
943 943
944 /* 944 /*
945 * Notifications of device and interface registration 945 * Notifications of device and interface registration
946 */ 946 */
947 static int usb_bus_notify(struct notifier_block *nb, unsigned long action, 947 static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
948 void *data) 948 void *data)
949 { 949 {
950 struct device *dev = data; 950 struct device *dev = data;
951 951
952 switch (action) { 952 switch (action) {
953 case BUS_NOTIFY_ADD_DEVICE: 953 case BUS_NOTIFY_ADD_DEVICE:
954 if (dev->type == &usb_device_type) 954 if (dev->type == &usb_device_type)
955 (void) usb_create_sysfs_dev_files(to_usb_device(dev)); 955 (void) usb_create_sysfs_dev_files(to_usb_device(dev));
956 else if (dev->type == &usb_if_device_type) 956 else if (dev->type == &usb_if_device_type)
957 usb_create_sysfs_intf_files(to_usb_interface(dev)); 957 usb_create_sysfs_intf_files(to_usb_interface(dev));
958 break; 958 break;
959 959
960 case BUS_NOTIFY_DEL_DEVICE: 960 case BUS_NOTIFY_DEL_DEVICE:
961 if (dev->type == &usb_device_type) 961 if (dev->type == &usb_device_type)
962 usb_remove_sysfs_dev_files(to_usb_device(dev)); 962 usb_remove_sysfs_dev_files(to_usb_device(dev));
963 else if (dev->type == &usb_if_device_type) 963 else if (dev->type == &usb_if_device_type)
964 usb_remove_sysfs_intf_files(to_usb_interface(dev)); 964 usb_remove_sysfs_intf_files(to_usb_interface(dev));
965 break; 965 break;
966 } 966 }
967 return 0; 967 return 0;
968 } 968 }
969 969
970 static struct notifier_block usb_bus_nb = { 970 static struct notifier_block usb_bus_nb = {
971 .notifier_call = usb_bus_notify, 971 .notifier_call = usb_bus_notify,
972 }; 972 };
973 973
974 struct dentry *usb_debug_root; 974 struct dentry *usb_debug_root;
975 EXPORT_SYMBOL_GPL(usb_debug_root); 975 EXPORT_SYMBOL_GPL(usb_debug_root);
976 976
977 static struct dentry *usb_debug_devices; 977 static struct dentry *usb_debug_devices;
978 978
979 static int usb_debugfs_init(void) 979 static int usb_debugfs_init(void)
980 { 980 {
981 usb_debug_root = debugfs_create_dir("usb", NULL); 981 usb_debug_root = debugfs_create_dir("usb", NULL);
982 if (!usb_debug_root) 982 if (!usb_debug_root)
983 return -ENOENT; 983 return -ENOENT;
984 984
985 usb_debug_devices = debugfs_create_file("devices", 0444, 985 usb_debug_devices = debugfs_create_file("devices", 0444,
986 usb_debug_root, NULL, 986 usb_debug_root, NULL,
987 &usbfs_devices_fops); 987 &usbfs_devices_fops);
988 if (!usb_debug_devices) { 988 if (!usb_debug_devices) {
989 debugfs_remove(usb_debug_root); 989 debugfs_remove(usb_debug_root);
990 usb_debug_root = NULL; 990 usb_debug_root = NULL;
991 return -ENOENT; 991 return -ENOENT;
992 } 992 }
993 993
994 return 0; 994 return 0;
995 } 995 }
996 996
997 static void usb_debugfs_cleanup(void) 997 static void usb_debugfs_cleanup(void)
998 { 998 {
999 debugfs_remove(usb_debug_devices); 999 debugfs_remove(usb_debug_devices);
1000 debugfs_remove(usb_debug_root); 1000 debugfs_remove(usb_debug_root);
1001 } 1001 }
1002 1002
1003 /* 1003 /*
1004 * Init 1004 * Init
1005 */ 1005 */
1006 static int __init usb_init(void) 1006 static int __init usb_init(void)
1007 { 1007 {
1008 int retval; 1008 int retval;
1009 if (nousb) { 1009 if (nousb) {
1010 pr_info("%s: USB support disabled\n", usbcore_name); 1010 pr_info("%s: USB support disabled\n", usbcore_name);
1011 return 0; 1011 return 0;
1012 } 1012 }
1013 1013
1014 retval = usb_debugfs_init(); 1014 retval = usb_debugfs_init();
1015 if (retval) 1015 if (retval)
1016 goto out; 1016 goto out;
1017 1017
1018 retval = bus_register(&usb_bus_type); 1018 retval = bus_register(&usb_bus_type);
1019 if (retval) 1019 if (retval)
1020 goto bus_register_failed; 1020 goto bus_register_failed;
1021 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb); 1021 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1022 if (retval) 1022 if (retval)
1023 goto bus_notifier_failed; 1023 goto bus_notifier_failed;
1024 retval = usb_major_init(); 1024 retval = usb_major_init();
1025 if (retval) 1025 if (retval)
1026 goto major_init_failed; 1026 goto major_init_failed;
1027 retval = usb_register(&usbfs_driver); 1027 retval = usb_register(&usbfs_driver);
1028 if (retval) 1028 if (retval)
1029 goto driver_register_failed; 1029 goto driver_register_failed;
1030 retval = usb_devio_init(); 1030 retval = usb_devio_init();
1031 if (retval) 1031 if (retval)
1032 goto usb_devio_init_failed; 1032 goto usb_devio_init_failed;
1033 retval = usbfs_init(); 1033 retval = usbfs_init();
1034 if (retval) 1034 if (retval)
1035 goto fs_init_failed; 1035 goto fs_init_failed;
1036 retval = usb_hub_init(); 1036 retval = usb_hub_init();
1037 if (retval) 1037 if (retval)
1038 goto hub_init_failed; 1038 goto hub_init_failed;
1039 retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); 1039 retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
1040 if (!retval) 1040 if (!retval)
1041 goto out; 1041 goto out;
1042 1042
1043 usb_hub_cleanup(); 1043 usb_hub_cleanup();
1044 hub_init_failed: 1044 hub_init_failed:
1045 usbfs_cleanup(); 1045 usbfs_cleanup();
1046 fs_init_failed: 1046 fs_init_failed:
1047 usb_devio_cleanup(); 1047 usb_devio_cleanup();
1048 usb_devio_init_failed: 1048 usb_devio_init_failed:
1049 usb_deregister(&usbfs_driver); 1049 usb_deregister(&usbfs_driver);
1050 driver_register_failed: 1050 driver_register_failed:
1051 usb_major_cleanup(); 1051 usb_major_cleanup();
1052 major_init_failed: 1052 major_init_failed:
1053 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1053 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1054 bus_notifier_failed: 1054 bus_notifier_failed:
1055 bus_unregister(&usb_bus_type); 1055 bus_unregister(&usb_bus_type);
1056 bus_register_failed: 1056 bus_register_failed:
1057 usb_debugfs_cleanup(); 1057 usb_debugfs_cleanup();
1058 out: 1058 out:
1059 return retval; 1059 return retval;
1060 } 1060 }
1061 1061
1062 /* 1062 /*
1063 * Cleanup 1063 * Cleanup
1064 */ 1064 */
1065 static void __exit usb_exit(void) 1065 static void __exit usb_exit(void)
1066 { 1066 {
1067 /* This will matter if shutdown/reboot does exitcalls. */ 1067 /* This will matter if shutdown/reboot does exitcalls. */
1068 if (nousb) 1068 if (nousb)
1069 return; 1069 return;
1070 1070
1071 usb_deregister_device_driver(&usb_generic_driver); 1071 usb_deregister_device_driver(&usb_generic_driver);
1072 usb_major_cleanup(); 1072 usb_major_cleanup();
1073 usbfs_cleanup(); 1073 usbfs_cleanup();
1074 usb_deregister(&usbfs_driver); 1074 usb_deregister(&usbfs_driver);
1075 usb_devio_cleanup(); 1075 usb_devio_cleanup();
1076 usb_hub_cleanup(); 1076 usb_hub_cleanup();
1077 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1077 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1078 bus_unregister(&usb_bus_type); 1078 bus_unregister(&usb_bus_type);
1079 usb_debugfs_cleanup(); 1079 usb_debugfs_cleanup();
1080 } 1080 }
1081 1081
1082 subsys_initcall(usb_init); 1082 subsys_initcall(usb_init);
1083 module_exit(usb_exit); 1083 module_exit(usb_exit);
1084 MODULE_LICENSE("GPL"); 1084 MODULE_LICENSE("GPL");
1085 1085
drivers/usb/core/usb.h
1 #include <linux/pm.h> 1 #include <linux/pm.h>
2 2
3 /* Functions local to drivers/usb/core/ */ 3 /* Functions local to drivers/usb/core/ */
4 4
5 extern int usb_create_sysfs_dev_files(struct usb_device *dev); 5 extern int usb_create_sysfs_dev_files(struct usb_device *dev);
6 extern void usb_remove_sysfs_dev_files(struct usb_device *dev); 6 extern void usb_remove_sysfs_dev_files(struct usb_device *dev);
7 extern void usb_create_sysfs_intf_files(struct usb_interface *intf); 7 extern void usb_create_sysfs_intf_files(struct usb_interface *intf);
8 extern void usb_remove_sysfs_intf_files(struct usb_interface *intf); 8 extern void usb_remove_sysfs_intf_files(struct usb_interface *intf);
9 extern int usb_create_ep_devs(struct device *parent, 9 extern int usb_create_ep_devs(struct device *parent,
10 struct usb_host_endpoint *endpoint, 10 struct usb_host_endpoint *endpoint,
11 struct usb_device *udev); 11 struct usb_device *udev);
12 extern void usb_remove_ep_devs(struct usb_host_endpoint *endpoint); 12 extern void usb_remove_ep_devs(struct usb_host_endpoint *endpoint);
13 13
14 extern void usb_enable_endpoint(struct usb_device *dev, 14 extern void usb_enable_endpoint(struct usb_device *dev,
15 struct usb_host_endpoint *ep, bool reset_toggle); 15 struct usb_host_endpoint *ep, bool reset_toggle);
16 extern void usb_enable_interface(struct usb_device *dev, 16 extern void usb_enable_interface(struct usb_device *dev,
17 struct usb_interface *intf, bool reset_toggles); 17 struct usb_interface *intf, bool reset_toggles);
18 extern void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr, 18 extern void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
19 bool reset_hardware); 19 bool reset_hardware);
20 extern void usb_disable_interface(struct usb_device *dev, 20 extern void usb_disable_interface(struct usb_device *dev,
21 struct usb_interface *intf, bool reset_hardware); 21 struct usb_interface *intf, bool reset_hardware);
22 extern void usb_release_interface_cache(struct kref *ref); 22 extern void usb_release_interface_cache(struct kref *ref);
23 extern void usb_disable_device(struct usb_device *dev, int skip_ep0); 23 extern void usb_disable_device(struct usb_device *dev, int skip_ep0);
24 extern int usb_deauthorize_device(struct usb_device *); 24 extern int usb_deauthorize_device(struct usb_device *);
25 extern int usb_authorize_device(struct usb_device *); 25 extern int usb_authorize_device(struct usb_device *);
26 extern void usb_detect_quirks(struct usb_device *udev); 26 extern void usb_detect_quirks(struct usb_device *udev);
27 extern int usb_remove_device(struct usb_device *udev); 27 extern int usb_remove_device(struct usb_device *udev);
28 28
29 extern int usb_get_device_descriptor(struct usb_device *dev, 29 extern int usb_get_device_descriptor(struct usb_device *dev,
30 unsigned int size); 30 unsigned int size);
31 extern int usb_get_bos_descriptor(struct usb_device *dev); 31 extern int usb_get_bos_descriptor(struct usb_device *dev);
32 extern void usb_release_bos_descriptor(struct usb_device *dev); 32 extern void usb_release_bos_descriptor(struct usb_device *dev);
33 extern char *usb_cache_string(struct usb_device *udev, int index); 33 extern char *usb_cache_string(struct usb_device *udev, int index);
34 extern int usb_set_configuration(struct usb_device *dev, int configuration); 34 extern int usb_set_configuration(struct usb_device *dev, int configuration);
35 extern int usb_choose_configuration(struct usb_device *udev); 35 extern int usb_choose_configuration(struct usb_device *udev);
36 36
37 extern void usb_kick_khubd(struct usb_device *dev); 37 extern void usb_kick_khubd(struct usb_device *dev);
38 extern int usb_match_device(struct usb_device *dev, 38 extern int usb_match_device(struct usb_device *dev,
39 const struct usb_device_id *id); 39 const struct usb_device_id *id);
40 extern void usb_forced_unbind_intf(struct usb_interface *intf); 40 extern void usb_forced_unbind_intf(struct usb_interface *intf);
41 extern void usb_rebind_intf(struct usb_interface *intf); 41 extern void usb_rebind_intf(struct usb_interface *intf);
42 42
43 extern int usb_hub_claim_port(struct usb_device *hdev, unsigned port, 43 extern int usb_hub_claim_port(struct usb_device *hdev, unsigned port,
44 void *owner); 44 void *owner);
45 extern int usb_hub_release_port(struct usb_device *hdev, unsigned port, 45 extern int usb_hub_release_port(struct usb_device *hdev, unsigned port,
46 void *owner); 46 void *owner);
47 extern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner); 47 extern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner);
48 extern bool usb_device_is_owned(struct usb_device *udev); 48 extern bool usb_device_is_owned(struct usb_device *udev);
49 49
50 extern int usb_hub_init(void); 50 extern int usb_hub_init(void);
51 extern void usb_hub_cleanup(void); 51 extern void usb_hub_cleanup(void);
52 extern int usb_major_init(void); 52 extern int usb_major_init(void);
53 extern void usb_major_cleanup(void); 53 extern void usb_major_cleanup(void);
54 54
55 #ifdef CONFIG_PM 55 #ifdef CONFIG_PM
56 56
57 extern int usb_suspend(struct device *dev, pm_message_t msg); 57 extern int usb_suspend(struct device *dev, pm_message_t msg);
58 extern int usb_resume(struct device *dev, pm_message_t msg); 58 extern int usb_resume(struct device *dev, pm_message_t msg);
59 extern int usb_resume_complete(struct device *dev);
59 60
60 extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg); 61 extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg);
61 extern int usb_port_resume(struct usb_device *dev, pm_message_t msg); 62 extern int usb_port_resume(struct usb_device *dev, pm_message_t msg);
62 63
63 #else 64 #else
64 65
65 static inline int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 66 static inline int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
66 { 67 {
67 return 0; 68 return 0;
68 } 69 }
69 70
70 static inline int usb_port_resume(struct usb_device *udev, pm_message_t msg) 71 static inline int usb_port_resume(struct usb_device *udev, pm_message_t msg)
71 { 72 {
72 return 0; 73 return 0;
73 } 74 }
74 75
75 #endif 76 #endif
76 77
77 #ifdef CONFIG_USB_SUSPEND 78 #ifdef CONFIG_USB_SUSPEND
78 79
79 extern void usb_autosuspend_device(struct usb_device *udev); 80 extern void usb_autosuspend_device(struct usb_device *udev);
80 extern int usb_autoresume_device(struct usb_device *udev); 81 extern int usb_autoresume_device(struct usb_device *udev);
81 extern int usb_remote_wakeup(struct usb_device *dev); 82 extern int usb_remote_wakeup(struct usb_device *dev);
82 extern int usb_runtime_suspend(struct device *dev); 83 extern int usb_runtime_suspend(struct device *dev);
83 extern int usb_runtime_resume(struct device *dev); 84 extern int usb_runtime_resume(struct device *dev);
84 extern int usb_runtime_idle(struct device *dev); 85 extern int usb_runtime_idle(struct device *dev);
85 extern int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable); 86 extern int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable);
86 87
87 #else 88 #else
88 89
89 #define usb_autosuspend_device(udev) do {} while (0) 90 #define usb_autosuspend_device(udev) do {} while (0)
90 static inline int usb_autoresume_device(struct usb_device *udev) 91 static inline int usb_autoresume_device(struct usb_device *udev)
91 { 92 {
92 return 0; 93 return 0;
93 } 94 }
94 95
95 static inline int usb_remote_wakeup(struct usb_device *udev) 96 static inline int usb_remote_wakeup(struct usb_device *udev)
96 { 97 {
97 return 0; 98 return 0;
98 } 99 }
99 100
100 static inline int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable) 101 static inline int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
101 { 102 {
102 return 0; 103 return 0;
103 } 104 }
104 #endif 105 #endif
105 106
106 extern struct bus_type usb_bus_type; 107 extern struct bus_type usb_bus_type;
107 extern struct device_type usb_device_type; 108 extern struct device_type usb_device_type;
108 extern struct device_type usb_if_device_type; 109 extern struct device_type usb_if_device_type;
109 extern struct device_type usb_ep_device_type; 110 extern struct device_type usb_ep_device_type;
110 extern struct usb_device_driver usb_generic_driver; 111 extern struct usb_device_driver usb_generic_driver;
111 112
112 static inline int is_usb_device(const struct device *dev) 113 static inline int is_usb_device(const struct device *dev)
113 { 114 {
114 return dev->type == &usb_device_type; 115 return dev->type == &usb_device_type;
115 } 116 }
116 117
117 static inline int is_usb_interface(const struct device *dev) 118 static inline int is_usb_interface(const struct device *dev)
118 { 119 {
119 return dev->type == &usb_if_device_type; 120 return dev->type == &usb_if_device_type;
120 } 121 }
121 122
122 static inline int is_usb_endpoint(const struct device *dev) 123 static inline int is_usb_endpoint(const struct device *dev)
123 { 124 {
124 return dev->type == &usb_ep_device_type; 125 return dev->type == &usb_ep_device_type;
125 } 126 }
126 127
127 /* Do the same for device drivers and interface drivers. */ 128 /* Do the same for device drivers and interface drivers. */
128 129
129 static inline int is_usb_device_driver(struct device_driver *drv) 130 static inline int is_usb_device_driver(struct device_driver *drv)
130 { 131 {
131 return container_of(drv, struct usbdrv_wrap, driver)-> 132 return container_of(drv, struct usbdrv_wrap, driver)->
132 for_devices; 133 for_devices;
133 } 134 }
134 135
135 /* for labeling diagnostics */ 136 /* for labeling diagnostics */
136 extern const char *usbcore_name; 137 extern const char *usbcore_name;
137 138
138 /* sysfs stuff */ 139 /* sysfs stuff */
139 extern const struct attribute_group *usb_device_groups[]; 140 extern const struct attribute_group *usb_device_groups[];
140 extern const struct attribute_group *usb_interface_groups[]; 141 extern const struct attribute_group *usb_interface_groups[];
141 142
142 /* usbfs stuff */ 143 /* usbfs stuff */
143 extern struct mutex usbfs_mutex; 144 extern struct mutex usbfs_mutex;
144 extern struct usb_driver usbfs_driver; 145 extern struct usb_driver usbfs_driver;
145 extern const struct file_operations usbfs_devices_fops; 146 extern const struct file_operations usbfs_devices_fops;
146 extern const struct file_operations usbdev_file_operations; 147 extern const struct file_operations usbdev_file_operations;
147 extern void usbfs_conn_disc_event(void); 148 extern void usbfs_conn_disc_event(void);
148 149
149 extern int usb_devio_init(void); 150 extern int usb_devio_init(void);
150 extern void usb_devio_cleanup(void); 151 extern void usb_devio_cleanup(void);
151 152
152 /* internal notify stuff */ 153 /* internal notify stuff */
153 extern void usb_notify_add_device(struct usb_device *udev); 154 extern void usb_notify_add_device(struct usb_device *udev);
154 extern void usb_notify_remove_device(struct usb_device *udev); 155 extern void usb_notify_remove_device(struct usb_device *udev);
155 extern void usb_notify_add_bus(struct usb_bus *ubus); 156 extern void usb_notify_add_bus(struct usb_bus *ubus);
156 extern void usb_notify_remove_bus(struct usb_bus *ubus); 157 extern void usb_notify_remove_bus(struct usb_bus *ubus);
157 158
158 159