1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/device.h>
26#include <linux/slab.h>
27#include <linux/export.h>
28#include <linux/usb.h>
29#include <linux/usb/quirks.h>
30#include <linux/usb/hcd.h>
31
32#include "usb.h"
33
34
35
36
37
38
39ssize_t usb_store_new_id(struct usb_dynids *dynids,
40 const struct usb_device_id *id_table,
41 struct device_driver *driver,
42 const char *buf, size_t count)
43{
44 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
47 unsigned int bInterfaceClass = 0;
48 u32 refVendor, refProduct;
49 int fields = 0;
50 int retval = 0;
51
52 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
54 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
65 if (fields > 2 && bInterfaceClass) {
66 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
70
71 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
74
75 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
78 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
82
83 for (; id->match_flags; id++)
84 if (id->idVendor == refVendor && id->idProduct == refProduct)
85 break;
86
87 if (id->match_flags) {
88 dynid->id.driver_info = id->driver_info;
89 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
93 }
94
95 spin_lock(&dynids->lock);
96 list_add_tail(&dynid->node, &dynids->list);
97 spin_unlock(&dynids->lock);
98
99 retval = driver_attach(driver);
100
101 if (retval)
102 return retval;
103 return count;
104
105fail:
106 kfree(dynid);
107 return retval;
108}
109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
112{
113 struct usb_dynid *dynid;
114 size_t count = 0;
115
116 list_for_each_entry(dynid, &dynids->list, node)
117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
128static ssize_t new_id_show(struct device_driver *driver, char *buf)
129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
134
135static ssize_t new_id_store(struct device_driver *driver,
136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
141}
142static DRIVER_ATTR_RW(new_id);
143
144
145
146
147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
152 u32 idVendor;
153 u32 idProduct;
154 int fields;
155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
163 if ((id->idVendor == idVendor) &&
164 (id->idProduct == idProduct)) {
165 list_del(&dynid->node);
166 kfree(dynid);
167 break;
168 }
169 }
170 spin_unlock(&usb_driver->dynids.lock);
171 return count;
172}
173
174static ssize_t remove_id_show(struct device_driver *driver, char *buf)
175{
176 return new_id_show(driver, buf);
177}
178static DRIVER_ATTR_RW(remove_id);
179
180static int usb_create_newid_files(struct usb_driver *usb_drv)
181{
182 int error = 0;
183
184 if (usb_drv->no_dynamic_id)
185 goto exit;
186
187 if (usb_drv->probe != NULL) {
188 error = driver_create_file(&usb_drv->drvwrap.driver,
189 &driver_attr_new_id);
190 if (error == 0) {
191 error = driver_create_file(&usb_drv->drvwrap.driver,
192 &driver_attr_remove_id);
193 if (error)
194 driver_remove_file(&usb_drv->drvwrap.driver,
195 &driver_attr_new_id);
196 }
197 }
198exit:
199 return error;
200}
201
202static void usb_remove_newid_files(struct usb_driver *usb_drv)
203{
204 if (usb_drv->no_dynamic_id)
205 return;
206
207 if (usb_drv->probe != NULL) {
208 driver_remove_file(&usb_drv->drvwrap.driver,
209 &driver_attr_remove_id);
210 driver_remove_file(&usb_drv->drvwrap.driver,
211 &driver_attr_new_id);
212 }
213}
214
215static void usb_free_dynids(struct usb_driver *usb_drv)
216{
217 struct usb_dynid *dynid, *n;
218
219 spin_lock(&usb_drv->dynids.lock);
220 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
221 list_del(&dynid->node);
222 kfree(dynid);
223 }
224 spin_unlock(&usb_drv->dynids.lock);
225}
226
227static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
228 struct usb_driver *drv)
229{
230 struct usb_dynid *dynid;
231
232 spin_lock(&drv->dynids.lock);
233 list_for_each_entry(dynid, &drv->dynids.list, node) {
234 if (usb_match_one_id(intf, &dynid->id)) {
235 spin_unlock(&drv->dynids.lock);
236 return &dynid->id;
237 }
238 }
239 spin_unlock(&drv->dynids.lock);
240 return NULL;
241}
242
243
244
245static int usb_probe_device(struct device *dev)
246{
247 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
248 struct usb_device *udev = to_usb_device(dev);
249 int error = 0;
250
251 dev_dbg(dev, "%s\n", __func__);
252
253
254
255
256
257
258 if (!udriver->supports_autosuspend)
259 error = usb_autoresume_device(udev);
260
261 if (!error)
262 error = udriver->probe(udev);
263 return error;
264}
265
266
267static int usb_unbind_device(struct device *dev)
268{
269 struct usb_device *udev = to_usb_device(dev);
270 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
271
272 udriver->disconnect(udev);
273 if (!udriver->supports_autosuspend)
274 usb_autosuspend_device(udev);
275 return 0;
276}
277
278
279
280
281
282
283
284
285
286
287static void usb_cancel_queued_reset(struct usb_interface *iface)
288{
289 if (iface->reset_running == 0)
290 cancel_work_sync(&iface->reset_ws);
291}
292
293
294static int usb_probe_interface(struct device *dev)
295{
296 struct usb_driver *driver = to_usb_driver(dev->driver);
297 struct usb_interface *intf = to_usb_interface(dev);
298 struct usb_device *udev = interface_to_usbdev(intf);
299 const struct usb_device_id *id;
300 int error = -ENODEV;
301 int lpm_disable_error;
302
303 dev_dbg(dev, "%s\n", __func__);
304
305 intf->needs_binding = 0;
306
307 if (usb_device_is_owned(udev))
308 return error;
309
310 if (udev->authorized == 0) {
311 dev_err(&intf->dev, "Device is not authorized for usage\n");
312 return error;
313 }
314
315 id = usb_match_id(intf, driver->id_table);
316 if (!id)
317 id = usb_match_dynamic_id(intf, driver);
318 if (!id)
319 return error;
320
321 dev_dbg(dev, "%s - got id\n", __func__);
322
323 error = usb_autoresume_device(udev);
324 if (error)
325 return error;
326
327 intf->condition = USB_INTERFACE_BINDING;
328
329
330
331
332
333 pm_runtime_set_active(dev);
334 pm_suspend_ignore_children(dev, false);
335 if (driver->supports_autosuspend)
336 pm_runtime_enable(dev);
337
338
339
340
341
342
343
344
345
346
347
348
349 lpm_disable_error = usb_unlocked_disable_lpm(udev);
350 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
351 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
352 __func__, driver->name);
353 error = lpm_disable_error;
354 goto err;
355 }
356
357
358 if (intf->needs_altsetting0) {
359 error = usb_set_interface(udev, intf->altsetting[0].
360 desc.bInterfaceNumber, 0);
361 if (error < 0)
362 goto err;
363 intf->needs_altsetting0 = 0;
364 }
365
366 error = driver->probe(intf, id);
367 if (error)
368 goto err;
369
370 intf->condition = USB_INTERFACE_BOUND;
371
372
373 if (!lpm_disable_error)
374 usb_unlocked_enable_lpm(udev);
375
376 usb_autosuspend_device(udev);
377 return error;
378
379 err:
380 usb_set_intfdata(intf, NULL);
381 intf->needs_remote_wakeup = 0;
382 intf->condition = USB_INTERFACE_UNBOUND;
383 usb_cancel_queued_reset(intf);
384
385
386 if (!lpm_disable_error)
387 usb_unlocked_enable_lpm(udev);
388
389
390 if (driver->supports_autosuspend)
391 pm_runtime_disable(dev);
392 pm_runtime_set_suspended(dev);
393
394 usb_autosuspend_device(udev);
395 return error;
396}
397
398
399static int usb_unbind_interface(struct device *dev)
400{
401 struct usb_driver *driver = to_usb_driver(dev->driver);
402 struct usb_interface *intf = to_usb_interface(dev);
403 struct usb_device *udev;
404 int error, r, lpm_disable_error;
405
406 intf->condition = USB_INTERFACE_UNBINDING;
407
408
409 udev = interface_to_usbdev(intf);
410 error = usb_autoresume_device(udev);
411
412
413
414
415
416
417 lpm_disable_error = usb_unlocked_disable_lpm(udev);
418
419
420
421
422 if (!driver->soft_unbind)
423 usb_disable_interface(udev, intf, false);
424
425 driver->disconnect(intf);
426 usb_cancel_queued_reset(intf);
427
428
429
430
431
432
433
434 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
435
436
437
438 usb_enable_interface(udev, intf, false);
439 } else if (!error && !intf->dev.power.is_prepared) {
440 r = usb_set_interface(udev, intf->altsetting[0].
441 desc.bInterfaceNumber, 0);
442 if (r < 0)
443 intf->needs_altsetting0 = 1;
444 } else {
445 intf->needs_altsetting0 = 1;
446 }
447 usb_set_intfdata(intf, NULL);
448
449 intf->condition = USB_INTERFACE_UNBOUND;
450 intf->needs_remote_wakeup = 0;
451
452
453 if (!lpm_disable_error)
454 usb_unlocked_enable_lpm(udev);
455
456
457 if (driver->supports_autosuspend)
458 pm_runtime_disable(dev);
459 pm_runtime_set_suspended(dev);
460
461
462 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
463 usb_autopm_put_interface_no_suspend(intf);
464 atomic_set(&intf->pm_usage_cnt, 0);
465
466 if (!error)
467 usb_autosuspend_device(udev);
468
469 return 0;
470}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494int usb_driver_claim_interface(struct usb_driver *driver,
495 struct usb_interface *iface, void *priv)
496{
497 struct device *dev = &iface->dev;
498 struct usb_device *udev;
499 int retval = 0;
500 int lpm_disable_error;
501
502 if (dev->driver)
503 return -EBUSY;
504
505 udev = interface_to_usbdev(iface);
506
507 dev->driver = &driver->drvwrap.driver;
508 usb_set_intfdata(iface, priv);
509 iface->needs_binding = 0;
510
511 iface->condition = USB_INTERFACE_BOUND;
512
513
514 lpm_disable_error = usb_unlocked_disable_lpm(udev);
515 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
516 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
517 __func__, driver->name);
518 return -ENOMEM;
519 }
520
521
522
523
524
525
526
527 pm_suspend_ignore_children(dev, false);
528 if (driver->supports_autosuspend)
529 pm_runtime_enable(dev);
530 else
531 pm_runtime_set_active(dev);
532
533
534
535
536 if (device_is_registered(dev))
537 retval = device_bind_driver(dev);
538
539
540 if (!lpm_disable_error)
541 usb_unlocked_enable_lpm(udev);
542
543 return retval;
544}
545EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561void usb_driver_release_interface(struct usb_driver *driver,
562 struct usb_interface *iface)
563{
564 struct device *dev = &iface->dev;
565
566
567 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
568 return;
569
570
571 if (iface->condition != USB_INTERFACE_BOUND)
572 return;
573 iface->condition = USB_INTERFACE_UNBINDING;
574
575
576
577
578 if (device_is_registered(dev)) {
579 device_release_driver(dev);
580 } else {
581 device_lock(dev);
582 usb_unbind_interface(dev);
583 dev->driver = NULL;
584 device_unlock(dev);
585 }
586}
587EXPORT_SYMBOL_GPL(usb_driver_release_interface);
588
589
590int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
591{
592 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
593 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
594 return 0;
595
596 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
597 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
598 return 0;
599
600
601
602 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
603 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
604 return 0;
605
606 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
607 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
608 return 0;
609
610 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
611 (id->bDeviceClass != dev->descriptor.bDeviceClass))
612 return 0;
613
614 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
615 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
616 return 0;
617
618 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
619 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
620 return 0;
621
622 return 1;
623}
624
625
626int usb_match_one_id_intf(struct usb_device *dev,
627 struct usb_host_interface *intf,
628 const struct usb_device_id *id)
629{
630
631
632
633 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
634 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
635 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
636 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
637 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
638 USB_DEVICE_ID_MATCH_INT_NUMBER)))
639 return 0;
640
641 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
642 (id->bInterfaceClass != intf->desc.bInterfaceClass))
643 return 0;
644
645 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
646 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
647 return 0;
648
649 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
650 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
651 return 0;
652
653 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
654 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
655 return 0;
656
657 return 1;
658}
659
660
661int usb_match_one_id(struct usb_interface *interface,
662 const struct usb_device_id *id)
663{
664 struct usb_host_interface *intf;
665 struct usb_device *dev;
666
667
668 if (id == NULL)
669 return 0;
670
671 intf = interface->cur_altsetting;
672 dev = interface_to_usbdev(interface);
673
674 if (!usb_match_device(dev, id))
675 return 0;
676
677 return usb_match_one_id_intf(dev, intf, id);
678}
679EXPORT_SYMBOL_GPL(usb_match_one_id);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754const struct usb_device_id *usb_match_id(struct usb_interface *interface,
755 const struct usb_device_id *id)
756{
757
758 if (id == NULL)
759 return NULL;
760
761
762
763
764
765
766 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
767 id->bInterfaceClass || id->driver_info; id++) {
768 if (usb_match_one_id(interface, id))
769 return id;
770 }
771
772 return NULL;
773}
774EXPORT_SYMBOL_GPL(usb_match_id);
775
776static int usb_device_match(struct device *dev, struct device_driver *drv)
777{
778
779 if (is_usb_device(dev)) {
780
781
782 if (!is_usb_device_driver(drv))
783 return 0;
784
785
786 return 1;
787
788 } else if (is_usb_interface(dev)) {
789 struct usb_interface *intf;
790 struct usb_driver *usb_drv;
791 const struct usb_device_id *id;
792
793
794 if (is_usb_device_driver(drv))
795 return 0;
796
797 intf = to_usb_interface(dev);
798 usb_drv = to_usb_driver(drv);
799
800 id = usb_match_id(intf, usb_drv->id_table);
801 if (id)
802 return 1;
803
804 id = usb_match_dynamic_id(intf, usb_drv);
805 if (id)
806 return 1;
807 }
808
809 return 0;
810}
811
812static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
813{
814 struct usb_device *usb_dev;
815
816 if (is_usb_device(dev)) {
817 usb_dev = to_usb_device(dev);
818 } else if (is_usb_interface(dev)) {
819 struct usb_interface *intf = to_usb_interface(dev);
820
821 usb_dev = interface_to_usbdev(intf);
822 } else {
823 return 0;
824 }
825
826 if (usb_dev->devnum < 0) {
827
828 pr_debug("usb %s: already deleted?\n", dev_name(dev));
829 return -ENODEV;
830 }
831 if (!usb_dev->bus) {
832 pr_debug("usb %s: bus removed?\n", dev_name(dev));
833 return -ENODEV;
834 }
835
836
837 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
838 le16_to_cpu(usb_dev->descriptor.idVendor),
839 le16_to_cpu(usb_dev->descriptor.idProduct),
840 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
841 return -ENOMEM;
842
843
844 if (add_uevent_var(env, "TYPE=%d/%d/%d",
845 usb_dev->descriptor.bDeviceClass,
846 usb_dev->descriptor.bDeviceSubClass,
847 usb_dev->descriptor.bDeviceProtocol))
848 return -ENOMEM;
849
850 return 0;
851}
852
853
854
855
856
857
858
859
860
861
862
863
864int usb_register_device_driver(struct usb_device_driver *new_udriver,
865 struct module *owner)
866{
867 int retval = 0;
868
869 if (usb_disabled())
870 return -ENODEV;
871
872 new_udriver->drvwrap.for_devices = 1;
873 new_udriver->drvwrap.driver.name = new_udriver->name;
874 new_udriver->drvwrap.driver.bus = &usb_bus_type;
875 new_udriver->drvwrap.driver.probe = usb_probe_device;
876 new_udriver->drvwrap.driver.remove = usb_unbind_device;
877 new_udriver->drvwrap.driver.owner = owner;
878
879 retval = driver_register(&new_udriver->drvwrap.driver);
880
881 if (!retval)
882 pr_info("%s: registered new device driver %s\n",
883 usbcore_name, new_udriver->name);
884 else
885 printk(KERN_ERR "%s: error %d registering device "
886 " driver %s\n",
887 usbcore_name, retval, new_udriver->name);
888
889 return retval;
890}
891EXPORT_SYMBOL_GPL(usb_register_device_driver);
892
893
894
895
896
897
898
899
900void usb_deregister_device_driver(struct usb_device_driver *udriver)
901{
902 pr_info("%s: deregistering device driver %s\n",
903 usbcore_name, udriver->name);
904
905 driver_unregister(&udriver->drvwrap.driver);
906}
907EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
926 const char *mod_name)
927{
928 int retval = 0;
929
930 if (usb_disabled())
931 return -ENODEV;
932
933 new_driver->drvwrap.for_devices = 0;
934 new_driver->drvwrap.driver.name = new_driver->name;
935 new_driver->drvwrap.driver.bus = &usb_bus_type;
936 new_driver->drvwrap.driver.probe = usb_probe_interface;
937 new_driver->drvwrap.driver.remove = usb_unbind_interface;
938 new_driver->drvwrap.driver.owner = owner;
939 new_driver->drvwrap.driver.mod_name = mod_name;
940 spin_lock_init(&new_driver->dynids.lock);
941 INIT_LIST_HEAD(&new_driver->dynids.list);
942
943 retval = driver_register(&new_driver->drvwrap.driver);
944 if (retval)
945 goto out;
946
947 retval = usb_create_newid_files(new_driver);
948 if (retval)
949 goto out_newid;
950
951 pr_info("%s: registered new interface driver %s\n",
952 usbcore_name, new_driver->name);
953
954out:
955 return retval;
956
957out_newid:
958 driver_unregister(&new_driver->drvwrap.driver);
959
960 printk(KERN_ERR "%s: error %d registering interface "
961 " driver %s\n",
962 usbcore_name, retval, new_driver->name);
963 goto out;
964}
965EXPORT_SYMBOL_GPL(usb_register_driver);
966
967
968
969
970
971
972
973
974
975
976
977
978void usb_deregister(struct usb_driver *driver)
979{
980 pr_info("%s: deregistering interface driver %s\n",
981 usbcore_name, driver->name);
982
983 usb_remove_newid_files(driver);
984 driver_unregister(&driver->drvwrap.driver);
985 usb_free_dynids(driver);
986}
987EXPORT_SYMBOL_GPL(usb_deregister);
988
989
990
991
992
993
994
995
996void usb_forced_unbind_intf(struct usb_interface *intf)
997{
998 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
999
1000 dev_dbg(&intf->dev, "forced unbind\n");
1001 usb_driver_release_interface(driver, intf);
1002
1003
1004 intf->needs_binding = 1;
1005}
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016void usb_rebind_intf(struct usb_interface *intf)
1017{
1018 int rc;
1019
1020
1021 if (intf->dev.driver)
1022 usb_forced_unbind_intf(intf);
1023
1024
1025 if (!intf->dev.power.is_prepared) {
1026 intf->needs_binding = 0;
1027 rc = device_attach(&intf->dev);
1028 if (rc < 0)
1029 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1030 }
1031}
1032
1033#ifdef CONFIG_PM
1034
1035
1036
1037
1038
1039
1040
1041static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
1042{
1043 struct usb_host_config *config;
1044 int i;
1045 struct usb_interface *intf;
1046 struct usb_driver *drv;
1047
1048 config = udev->actconfig;
1049 if (config) {
1050 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1051 intf = config->interface[i];
1052
1053 if (intf->dev.driver) {
1054 drv = to_usb_driver(intf->dev.driver);
1055 if (!drv->suspend || !drv->resume)
1056 usb_forced_unbind_intf(intf);
1057 }
1058 }
1059 }
1060}
1061
1062
1063
1064
1065
1066
1067static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1068{
1069 struct usb_host_config *config;
1070 int i;
1071 struct usb_interface *intf;
1072
1073 config = udev->actconfig;
1074 if (config) {
1075 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1076 intf = config->interface[i];
1077 if (intf->dev.driver && intf->needs_binding)
1078 usb_forced_unbind_intf(intf);
1079 }
1080 }
1081}
1082
1083static void do_rebind_interfaces(struct usb_device *udev)
1084{
1085 struct usb_host_config *config;
1086 int i;
1087 struct usb_interface *intf;
1088
1089 config = udev->actconfig;
1090 if (config) {
1091 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1092 intf = config->interface[i];
1093 if (intf->needs_binding)
1094 usb_rebind_intf(intf);
1095 }
1096 }
1097}
1098
1099static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1100{
1101 struct usb_device_driver *udriver;
1102 int status = 0;
1103
1104 if (udev->state == USB_STATE_NOTATTACHED ||
1105 udev->state == USB_STATE_SUSPENDED)
1106 goto done;
1107
1108
1109 if (udev->dev.driver)
1110 udriver = to_usb_device_driver(udev->dev.driver);
1111 else {
1112 udev->do_remote_wakeup = 0;
1113 udriver = &usb_generic_driver;
1114 }
1115 status = udriver->suspend(udev, msg);
1116
1117 done:
1118 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1119 return status;
1120}
1121
1122static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1123{
1124 struct usb_device_driver *udriver;
1125 int status = 0;
1126
1127 if (udev->state == USB_STATE_NOTATTACHED)
1128 goto done;
1129
1130
1131 if (udev->dev.driver == NULL) {
1132 status = -ENOTCONN;
1133 goto done;
1134 }
1135
1136
1137
1138
1139 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1140 device_pm_wait_for_dev(&udev->dev,
1141 &udev->bus->hs_companion->root_hub->dev);
1142
1143 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1144 udev->reset_resume = 1;
1145
1146 udriver = to_usb_device_driver(udev->dev.driver);
1147 status = udriver->resume(udev, msg);
1148
1149 done:
1150 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1151 return status;
1152}
1153
1154static int usb_suspend_interface(struct usb_device *udev,
1155 struct usb_interface *intf, pm_message_t msg)
1156{
1157 struct usb_driver *driver;
1158 int status = 0;
1159
1160 if (udev->state == USB_STATE_NOTATTACHED ||
1161 intf->condition == USB_INTERFACE_UNBOUND)
1162 goto done;
1163 driver = to_usb_driver(intf->dev.driver);
1164
1165
1166 status = driver->suspend(intf, msg);
1167 if (status && !PMSG_IS_AUTO(msg))
1168 dev_err(&intf->dev, "suspend error %d\n", status);
1169
1170 done:
1171 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1172 return status;
1173}
1174
1175static int usb_resume_interface(struct usb_device *udev,
1176 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1177{
1178 struct usb_driver *driver;
1179 int status = 0;
1180
1181 if (udev->state == USB_STATE_NOTATTACHED)
1182 goto done;
1183
1184
1185 if (intf->condition == USB_INTERFACE_UNBINDING)
1186 goto done;
1187
1188
1189 if (intf->condition == USB_INTERFACE_UNBOUND) {
1190
1191
1192 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1193 usb_set_interface(udev, intf->altsetting[0].
1194 desc.bInterfaceNumber, 0);
1195 intf->needs_altsetting0 = 0;
1196 }
1197 goto done;
1198 }
1199
1200
1201 if (intf->needs_binding)
1202 goto done;
1203 driver = to_usb_driver(intf->dev.driver);
1204
1205 if (reset_resume) {
1206 if (driver->reset_resume) {
1207 status = driver->reset_resume(intf);
1208 if (status)
1209 dev_err(&intf->dev, "%s error %d\n",
1210 "reset_resume", status);
1211 } else {
1212 intf->needs_binding = 1;
1213 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1214 driver->name);
1215 }
1216 } else {
1217 status = driver->resume(intf);
1218 if (status)
1219 dev_err(&intf->dev, "resume error %d\n", status);
1220 }
1221
1222done:
1223 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1224
1225
1226 return status;
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1257{
1258 int status = 0;
1259 int i = 0, n = 0;
1260 struct usb_interface *intf;
1261
1262 if (udev->state == USB_STATE_NOTATTACHED ||
1263 udev->state == USB_STATE_SUSPENDED)
1264 goto done;
1265
1266
1267 if (udev->actconfig) {
1268 n = udev->actconfig->desc.bNumInterfaces;
1269 for (i = n - 1; i >= 0; --i) {
1270 intf = udev->actconfig->interface[i];
1271 status = usb_suspend_interface(udev, intf, msg);
1272
1273
1274 if (!PMSG_IS_AUTO(msg))
1275 status = 0;
1276 if (status != 0)
1277 break;
1278 }
1279 }
1280 if (status == 0) {
1281 status = usb_suspend_device(udev, msg);
1282
1283
1284
1285
1286
1287
1288
1289 if (udev->parent && !PMSG_IS_AUTO(msg))
1290 status = 0;
1291 }
1292
1293
1294 if (status != 0) {
1295 if (udev->actconfig) {
1296 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1297 while (++i < n) {
1298 intf = udev->actconfig->interface[i];
1299 usb_resume_interface(udev, intf, msg, 0);
1300 }
1301 }
1302
1303
1304
1305
1306 } else {
1307 udev->can_submit = 0;
1308 for (i = 0; i < 16; ++i) {
1309 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1310 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1311 }
1312 }
1313
1314 done:
1315 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1316 return status;
1317}
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1340{
1341 int status = 0;
1342 int i;
1343 struct usb_interface *intf;
1344
1345 if (udev->state == USB_STATE_NOTATTACHED) {
1346 status = -ENODEV;
1347 goto done;
1348 }
1349 udev->can_submit = 1;
1350
1351
1352 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1353 status = usb_resume_device(udev, msg);
1354
1355
1356 if (status == 0 && udev->actconfig) {
1357 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1358 intf = udev->actconfig->interface[i];
1359 usb_resume_interface(udev, intf, msg,
1360 udev->reset_resume);
1361 }
1362 }
1363 usb_mark_last_busy(udev);
1364
1365 done:
1366 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1367 if (!status)
1368 udev->reset_resume = 0;
1369 return status;
1370}
1371
1372static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1373{
1374 int w;
1375
1376
1377
1378
1379
1380 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1381 if (udev->state != USB_STATE_SUSPENDED)
1382 udev->do_remote_wakeup = 0;
1383 return;
1384 }
1385
1386
1387
1388
1389 w = device_may_wakeup(&udev->dev);
1390
1391
1392
1393
1394 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1395 pm_runtime_resume(&udev->dev);
1396 udev->do_remote_wakeup = w;
1397}
1398
1399
1400int usb_suspend(struct device *dev, pm_message_t msg)
1401{
1402 struct usb_device *udev = to_usb_device(dev);
1403
1404 unbind_no_pm_drivers_interfaces(udev);
1405
1406
1407
1408
1409
1410 choose_wakeup(udev, msg);
1411 return usb_suspend_both(udev, msg);
1412}
1413
1414
1415int usb_resume_complete(struct device *dev)
1416{
1417 struct usb_device *udev = to_usb_device(dev);
1418
1419
1420
1421
1422 if (udev->state != USB_STATE_NOTATTACHED)
1423 do_rebind_interfaces(udev);
1424 return 0;
1425}
1426
1427
1428int usb_resume(struct device *dev, pm_message_t msg)
1429{
1430 struct usb_device *udev = to_usb_device(dev);
1431 int status;
1432
1433
1434
1435
1436
1437
1438
1439
1440 status = usb_resume_both(udev, msg);
1441 if (status == 0) {
1442 pm_runtime_disable(dev);
1443 pm_runtime_set_active(dev);
1444 pm_runtime_enable(dev);
1445 unbind_no_reset_resume_drivers_interfaces(udev);
1446 }
1447
1448
1449
1450
1451 if (status == -ENODEV || status == -ESHUTDOWN)
1452 status = 0;
1453 return status;
1454}
1455
1456#endif
1457
1458#ifdef CONFIG_PM_RUNTIME
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470void usb_enable_autosuspend(struct usb_device *udev)
1471{
1472 pm_runtime_allow(&udev->dev);
1473}
1474EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485void usb_disable_autosuspend(struct usb_device *udev)
1486{
1487 pm_runtime_forbid(&udev->dev);
1488}
1489EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507void usb_autosuspend_device(struct usb_device *udev)
1508{
1509 int status;
1510
1511 usb_mark_last_busy(udev);
1512 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1513 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1514 __func__, atomic_read(&udev->dev.power.usage_count),
1515 status);
1516}
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538int usb_autoresume_device(struct usb_device *udev)
1539{
1540 int status;
1541
1542 status = pm_runtime_get_sync(&udev->dev);
1543 if (status < 0)
1544 pm_runtime_put_sync(&udev->dev);
1545 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1546 __func__, atomic_read(&udev->dev.power.usage_count),
1547 status);
1548 if (status > 0)
1549 status = 0;
1550 return status;
1551}
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568void usb_autopm_put_interface(struct usb_interface *intf)
1569{
1570 struct usb_device *udev = interface_to_usbdev(intf);
1571 int status;
1572
1573 usb_mark_last_busy(udev);
1574 atomic_dec(&intf->pm_usage_cnt);
1575 status = pm_runtime_put_sync(&intf->dev);
1576 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1577 __func__, atomic_read(&intf->dev.power.usage_count),
1578 status);
1579}
1580EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597void usb_autopm_put_interface_async(struct usb_interface *intf)
1598{
1599 struct usb_device *udev = interface_to_usbdev(intf);
1600 int status;
1601
1602 usb_mark_last_busy(udev);
1603 atomic_dec(&intf->pm_usage_cnt);
1604 status = pm_runtime_put(&intf->dev);
1605 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1606 __func__, atomic_read(&intf->dev.power.usage_count),
1607 status);
1608}
1609EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1621{
1622 struct usb_device *udev = interface_to_usbdev(intf);
1623
1624 usb_mark_last_busy(udev);
1625 atomic_dec(&intf->pm_usage_cnt);
1626 pm_runtime_put_noidle(&intf->dev);
1627}
1628EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649int usb_autopm_get_interface(struct usb_interface *intf)
1650{
1651 int status;
1652
1653 status = pm_runtime_get_sync(&intf->dev);
1654 if (status < 0)
1655 pm_runtime_put_sync(&intf->dev);
1656 else
1657 atomic_inc(&intf->pm_usage_cnt);
1658 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1659 __func__, atomic_read(&intf->dev.power.usage_count),
1660 status);
1661 if (status > 0)
1662 status = 0;
1663 return status;
1664}
1665EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684int usb_autopm_get_interface_async(struct usb_interface *intf)
1685{
1686 int status;
1687
1688 status = pm_runtime_get(&intf->dev);
1689 if (status < 0 && status != -EINPROGRESS)
1690 pm_runtime_put_noidle(&intf->dev);
1691 else
1692 atomic_inc(&intf->pm_usage_cnt);
1693 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1694 __func__, atomic_read(&intf->dev.power.usage_count),
1695 status);
1696 if (status > 0 || status == -EINPROGRESS)
1697 status = 0;
1698 return status;
1699}
1700EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1712{
1713 struct usb_device *udev = interface_to_usbdev(intf);
1714
1715 usb_mark_last_busy(udev);
1716 atomic_inc(&intf->pm_usage_cnt);
1717 pm_runtime_get_noresume(&intf->dev);
1718}
1719EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1720
1721
1722static int autosuspend_check(struct usb_device *udev)
1723{
1724 int w, i;
1725 struct usb_interface *intf;
1726
1727
1728
1729
1730 w = 0;
1731 if (udev->actconfig) {
1732 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1733 intf = udev->actconfig->interface[i];
1734
1735
1736
1737
1738
1739
1740 if (intf->dev.power.disable_depth)
1741 continue;
1742 if (atomic_read(&intf->dev.power.usage_count) > 0)
1743 return -EBUSY;
1744 w |= intf->needs_remote_wakeup;
1745
1746
1747
1748
1749
1750 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1751 struct usb_driver *driver;
1752
1753 driver = to_usb_driver(intf->dev.driver);
1754 if (!driver->reset_resume ||
1755 intf->needs_remote_wakeup)
1756 return -EOPNOTSUPP;
1757 }
1758 }
1759 }
1760 if (w && !device_can_wakeup(&udev->dev)) {
1761 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1762 return -EOPNOTSUPP;
1763 }
1764 udev->do_remote_wakeup = w;
1765 return 0;
1766}
1767
1768int usb_runtime_suspend(struct device *dev)
1769{
1770 struct usb_device *udev = to_usb_device(dev);
1771 int status;
1772
1773
1774
1775
1776
1777 if (autosuspend_check(udev) != 0)
1778 return -EAGAIN;
1779
1780 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1781
1782
1783 if (status == -EAGAIN || status == -EBUSY)
1784 usb_mark_last_busy(udev);
1785
1786
1787
1788
1789 if (status != 0)
1790 return -EBUSY;
1791 return status;
1792}
1793
1794int usb_runtime_resume(struct device *dev)
1795{
1796 struct usb_device *udev = to_usb_device(dev);
1797 int status;
1798
1799
1800
1801
1802 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1803 return status;
1804}
1805
1806int usb_runtime_idle(struct device *dev)
1807{
1808 struct usb_device *udev = to_usb_device(dev);
1809
1810
1811
1812
1813 if (autosuspend_check(udev) == 0)
1814 pm_runtime_autosuspend(dev);
1815
1816 return -EBUSY;
1817}
1818
1819int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1820{
1821 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1822 int ret = -EPERM;
1823
1824 if (enable && !udev->usb2_hw_lpm_allowed)
1825 return 0;
1826
1827 if (hcd->driver->set_usb2_hw_lpm) {
1828 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1829 if (!ret)
1830 udev->usb2_hw_lpm_enabled = enable;
1831 }
1832
1833 return ret;
1834}
1835
1836#endif
1837
1838struct bus_type usb_bus_type = {
1839 .name = "usb",
1840 .match = usb_device_match,
1841 .uevent = usb_uevent,
1842};
1843