hc
2024-05-10 748e4f3d702def1a4bff191e0cf93b6a05340f01
kernel/drivers/usb/core/driver.c
....@@ -34,6 +34,7 @@
3434
3535 #include "usb.h"
3636
37
+#include <trace/hooks/usb.h>
3738
3839 /*
3940 * Adds a new dynamic USBdevice ID to this driver,
....@@ -261,9 +262,41 @@
261262 */
262263 if (!udriver->supports_autosuspend)
263264 error = usb_autoresume_device(udev);
265
+ if (error)
266
+ return error;
264267
265
- if (!error)
266
- error = udriver->probe(udev);
268
+ if (udriver->generic_subclass)
269
+ error = usb_generic_driver_probe(udev);
270
+ if (error)
271
+ return error;
272
+
273
+ /* Probe the USB device with the driver in hand, but only
274
+ * defer to a generic driver in case the current USB
275
+ * device driver has an id_table or a match function; i.e.,
276
+ * when the device driver was explicitly matched against
277
+ * a device.
278
+ *
279
+ * If the device driver does not have either of these,
280
+ * then we assume that it can bind to any device and is
281
+ * not truly a more specialized/non-generic driver, so a
282
+ * return value of -ENODEV should not force the device
283
+ * to be handled by the generic USB driver, as there
284
+ * can still be another, more specialized, device driver.
285
+ *
286
+ * This accommodates the usbip driver.
287
+ *
288
+ * TODO: What if, in the future, there are multiple
289
+ * specialized USB device drivers for a particular device?
290
+ * In such cases, there is a need to try all matching
291
+ * specialised device drivers prior to setting the
292
+ * use_generic_driver bit.
293
+ */
294
+ error = udriver->probe(udev);
295
+ if (error == -ENODEV && udriver != &usb_generic_driver &&
296
+ (udriver->id_table || udriver->match)) {
297
+ udev->use_generic_driver = 1;
298
+ return -EPROBE_DEFER;
299
+ }
267300 return error;
268301 }
269302
....@@ -273,7 +306,10 @@
273306 struct usb_device *udev = to_usb_device(dev);
274307 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
275308
276
- udriver->disconnect(udev);
309
+ if (udriver->disconnect)
310
+ udriver->disconnect(udev);
311
+ if (udriver->generic_subclass)
312
+ usb_generic_driver_disconnect(udev);
277313 if (!udriver->supports_autosuspend)
278314 usb_autosuspend_device(udev);
279315 return 0;
....@@ -505,7 +541,6 @@
505541 struct usb_interface *iface, void *priv)
506542 {
507543 struct device *dev;
508
- struct usb_device *udev;
509544 int retval = 0;
510545
511546 if (!iface)
....@@ -518,8 +553,6 @@
518553 /* reject claim if interface is not authorized */
519554 if (!iface->authorized)
520555 return -ENODEV;
521
-
522
- udev = interface_to_usbdev(iface);
523556
524557 dev->driver = &driver->drvwrap.driver;
525558 usb_set_intfdata(iface, priv);
....@@ -793,17 +826,58 @@
793826 }
794827 EXPORT_SYMBOL_GPL(usb_match_id);
795828
829
+const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
830
+ const struct usb_device_id *id)
831
+{
832
+ if (!id)
833
+ return NULL;
834
+
835
+ for (; id->idVendor || id->idProduct ; id++) {
836
+ if (usb_match_device(udev, id))
837
+ return id;
838
+ }
839
+
840
+ return NULL;
841
+}
842
+
843
+bool usb_driver_applicable(struct usb_device *udev,
844
+ struct usb_device_driver *udrv)
845
+{
846
+ if (udrv->id_table && udrv->match)
847
+ return usb_device_match_id(udev, udrv->id_table) != NULL &&
848
+ udrv->match(udev);
849
+
850
+ if (udrv->id_table)
851
+ return usb_device_match_id(udev, udrv->id_table) != NULL;
852
+
853
+ if (udrv->match)
854
+ return udrv->match(udev);
855
+
856
+ return false;
857
+}
858
+
796859 static int usb_device_match(struct device *dev, struct device_driver *drv)
797860 {
798861 /* devices and interfaces are handled separately */
799862 if (is_usb_device(dev)) {
863
+ struct usb_device *udev;
864
+ struct usb_device_driver *udrv;
800865
801866 /* interface drivers never match devices */
802867 if (!is_usb_device_driver(drv))
803868 return 0;
804869
805
- /* TODO: Add real matching code */
806
- return 1;
870
+ udev = to_usb_device(dev);
871
+ udrv = to_usb_device_driver(drv);
872
+
873
+ /* If the device driver under consideration does not have a
874
+ * id_table or a match function, then let the driver's probe
875
+ * function decide.
876
+ */
877
+ if (!udrv->id_table && !udrv->match)
878
+ return 1;
879
+
880
+ return usb_driver_applicable(udev, udrv);
807881
808882 } else if (is_usb_interface(dev)) {
809883 struct usb_interface *intf;
....@@ -870,6 +944,27 @@
870944 return 0;
871945 }
872946
947
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
948
+{
949
+ struct usb_device_driver *new_udriver = data;
950
+ struct usb_device *udev;
951
+ int ret;
952
+
953
+ /* Don't reprobe if current driver isn't usb_generic_driver */
954
+ if (dev->driver != &usb_generic_driver.drvwrap.driver)
955
+ return 0;
956
+
957
+ udev = to_usb_device(dev);
958
+ if (!usb_driver_applicable(udev, new_udriver))
959
+ return 0;
960
+
961
+ ret = device_reprobe(dev);
962
+ if (ret && ret != -EPROBE_DEFER)
963
+ dev_err(dev, "Failed to reprobe device (error %d)\n", ret);
964
+
965
+ return 0;
966
+}
967
+
873968 /**
874969 * usb_register_device_driver - register a USB device (not interface) driver
875970 * @new_udriver: USB operations for the device driver
....@@ -895,16 +990,23 @@
895990 new_udriver->drvwrap.driver.probe = usb_probe_device;
896991 new_udriver->drvwrap.driver.remove = usb_unbind_device;
897992 new_udriver->drvwrap.driver.owner = owner;
993
+ new_udriver->drvwrap.driver.dev_groups = new_udriver->dev_groups;
898994
899995 retval = driver_register(&new_udriver->drvwrap.driver);
900996
901
- if (!retval)
997
+ if (!retval) {
902998 pr_info("%s: registered new device driver %s\n",
903999 usbcore_name, new_udriver->name);
904
- else
905
- printk(KERN_ERR "%s: error %d registering device "
906
- " driver %s\n",
1000
+ /*
1001
+ * Check whether any device could be better served with
1002
+ * this new driver
1003
+ */
1004
+ bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
1005
+ __usb_bus_reprobe_drivers);
1006
+ } else {
1007
+ pr_err("%s: error %d registering device driver %s\n",
9071008 usbcore_name, retval, new_udriver->name);
1009
+ }
9081010
9091011 return retval;
9101012 }
....@@ -957,6 +1059,7 @@
9571059 new_driver->drvwrap.driver.remove = usb_unbind_interface;
9581060 new_driver->drvwrap.driver.owner = owner;
9591061 new_driver->drvwrap.driver.mod_name = mod_name;
1062
+ new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups;
9601063 spin_lock_init(&new_driver->dynids.lock);
9611064 INIT_LIST_HEAD(&new_driver->dynids.list);
9621065
....@@ -977,9 +1080,8 @@
9771080 out_newid:
9781081 driver_unregister(&new_driver->drvwrap.driver);
9791082
980
- printk(KERN_ERR "%s: error %d registering interface "
981
- " driver %s\n",
982
- usbcore_name, retval, new_driver->name);
1083
+ pr_err("%s: error %d registering interface driver %s\n",
1084
+ usbcore_name, retval, new_driver->name);
9831085 goto out;
9841086 }
9851087 EXPORT_SYMBOL_GPL(usb_register_driver);
....@@ -1150,7 +1252,10 @@
11501252 udev->do_remote_wakeup = 0;
11511253 udriver = &usb_generic_driver;
11521254 }
1153
- status = udriver->suspend(udev, msg);
1255
+ if (udriver->suspend)
1256
+ status = udriver->suspend(udev, msg);
1257
+ if (status == 0 && udriver->generic_subclass)
1258
+ status = usb_generic_driver_suspend(udev, msg);
11541259
11551260 done:
11561261 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
....@@ -1182,7 +1287,10 @@
11821287 udev->reset_resume = 1;
11831288
11841289 udriver = to_usb_device_driver(udev->dev.driver);
1185
- status = udriver->resume(udev, msg);
1290
+ if (udriver->generic_subclass)
1291
+ status = usb_generic_driver_resume(udev, msg);
1292
+ if (status == 0 && udriver->resume)
1293
+ status = udriver->resume(udev, msg);
11861294
11871295 done:
11881296 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
....@@ -1296,9 +1404,14 @@
12961404 int status = 0;
12971405 int i = 0, n = 0;
12981406 struct usb_interface *intf;
1407
+ int bypass = 0;
12991408
13001409 if (udev->state == USB_STATE_NOTATTACHED ||
13011410 udev->state == USB_STATE_SUSPENDED)
1411
+ goto done;
1412
+
1413
+ trace_android_vh_usb_dev_suspend(udev, msg, &bypass);
1414
+ if (bypass)
13021415 goto done;
13031416
13041417 /* Suspend all the interfaces and then udev itself */
....@@ -1397,11 +1510,17 @@
13971510 int status = 0;
13981511 int i;
13991512 struct usb_interface *intf;
1513
+ int bypass = 0;
14001514
14011515 if (udev->state == USB_STATE_NOTATTACHED) {
14021516 status = -ENODEV;
14031517 goto done;
14041518 }
1519
+
1520
+ trace_android_vh_usb_dev_resume(udev, msg, &bypass);
1521
+ if (bypass)
1522
+ goto done;
1523
+
14051524 udev->can_submit = 1;
14061525
14071526 /* Resume the device */
....@@ -1458,9 +1577,6 @@
14581577 struct usb_device *udev = to_usb_device(dev);
14591578 int r;
14601579
1461
- if (udev->bus->skip_resume && udev->state == USB_STATE_SUSPENDED)
1462
- return 0;
1463
-
14641580 unbind_no_pm_drivers_interfaces(udev);
14651581
14661582 /* From now on we are sure all drivers support suspend/resume
....@@ -1496,15 +1612,6 @@
14961612 {
14971613 struct usb_device *udev = to_usb_device(dev);
14981614 int status;
1499
-
1500
- /*
1501
- * Some buses would like to keep their devices in suspend
1502
- * state after system resume. Their resume happen when
1503
- * a remote wakeup is detected or interface driver start
1504
- * I/O.
1505
- */
1506
- if (udev->bus->skip_resume)
1507
- return 0;
15081615
15091616 /* For all calls, take the device back to full power and
15101617 * tell the PM core in case it was autosuspended previously.