diff options
author | Alan Stern <stern@rowland.harvard.edu> | 2006-08-30 15:47:02 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2006-09-27 14:58:57 -0400 |
commit | 645daaab0b6adc35c1838df2a82f9d729fdb1767 (patch) | |
tree | 908d58f6c0ad01e9c1622a4c12c9c8080c629baa | |
parent | b6956ffa595db97656d5901ca8fec77ef272d41a (diff) |
usbcore: add autosuspend/autoresume infrastructure
This patch (as739) adds the basic infrastructure for USB autosuspend
and autoresume. The main features are:
PM usage counters added to struct usb_device and struct
usb_interface, indicating whether it's okay to autosuspend
them or they are currently in use.
Flag added to usb_device indicating whether the current
suspend/resume operation originated from outside or as an
autosuspend/autoresume.
Flag added to usb_driver indicating whether the driver
supports autosuspend. If not, no device bound to the driver
will be autosuspended.
Mutex added to usb_device for protecting PM operations.
Unlike the device semaphore, the locking rule for the pm_mutex
is that you must acquire the locks going _up_ the device tree.
New routines handling autosuspend/autoresume requests for
interfaces and devices.
Suspend and resume requests are propagated up the device tree
(but not outside the USB subsystem).
work_struct added to usb_device, for carrying out delayed
autosuspend requests.
Autoresume added (and autosuspend prevented) during probe and
disconnect.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r-- | drivers/usb/core/driver.c | 419 | ||||
-rw-r--r-- | drivers/usb/core/hub.c | 49 | ||||
-rw-r--r-- | drivers/usb/core/usb.c | 23 | ||||
-rw-r--r-- | drivers/usb/core/usb.h | 14 | ||||
-rw-r--r-- | include/linux/usb.h | 33 |
5 files changed, 493 insertions, 45 deletions
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index a5d11461f5a9..2b2000ac05ab 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c | |||
@@ -157,12 +157,13 @@ static int usb_probe_device(struct device *dev) | |||
157 | 157 | ||
158 | udev = to_usb_device(dev); | 158 | udev = to_usb_device(dev); |
159 | 159 | ||
160 | /* FIXME: resume a suspended device */ | ||
161 | if (udev->state == USB_STATE_SUSPENDED) | ||
162 | return -EHOSTUNREACH; | ||
163 | |||
164 | /* TODO: Add real matching code */ | 160 | /* TODO: Add real matching code */ |
165 | 161 | ||
162 | /* The device should always appear to be in use | ||
163 | * unless the driver suports autosuspend. | ||
164 | */ | ||
165 | udev->pm_usage_cnt = !(udriver->supports_autosuspend); | ||
166 | |||
166 | error = udriver->probe(udev); | 167 | error = udriver->probe(udev); |
167 | return error; | 168 | return error; |
168 | } | 169 | } |
@@ -182,6 +183,7 @@ static int usb_probe_interface(struct device *dev) | |||
182 | { | 183 | { |
183 | struct usb_driver *driver = to_usb_driver(dev->driver); | 184 | struct usb_driver *driver = to_usb_driver(dev->driver); |
184 | struct usb_interface *intf; | 185 | struct usb_interface *intf; |
186 | struct usb_device *udev; | ||
185 | const struct usb_device_id *id; | 187 | const struct usb_device_id *id; |
186 | int error = -ENODEV; | 188 | int error = -ENODEV; |
187 | 189 | ||
@@ -191,10 +193,7 @@ static int usb_probe_interface(struct device *dev) | |||
191 | return error; | 193 | return error; |
192 | 194 | ||
193 | intf = to_usb_interface(dev); | 195 | intf = to_usb_interface(dev); |
194 | 196 | udev = interface_to_usbdev(intf); | |
195 | /* FIXME we'd much prefer to just resume it ... */ | ||
196 | if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED) | ||
197 | return -EHOSTUNREACH; | ||
198 | 197 | ||
199 | id = usb_match_id(intf, driver->id_table); | 198 | id = usb_match_id(intf, driver->id_table); |
200 | if (!id) | 199 | if (!id) |
@@ -202,18 +201,31 @@ static int usb_probe_interface(struct device *dev) | |||
202 | if (id) { | 201 | if (id) { |
203 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); | 202 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); |
204 | 203 | ||
204 | error = usb_autoresume_device(udev, 1); | ||
205 | if (error) | ||
206 | return error; | ||
207 | |||
205 | /* Interface "power state" doesn't correspond to any hardware | 208 | /* Interface "power state" doesn't correspond to any hardware |
206 | * state whatsoever. We use it to record when it's bound to | 209 | * state whatsoever. We use it to record when it's bound to |
207 | * a driver that may start I/0: it's not frozen/quiesced. | 210 | * a driver that may start I/0: it's not frozen/quiesced. |
208 | */ | 211 | */ |
209 | mark_active(intf); | 212 | mark_active(intf); |
210 | intf->condition = USB_INTERFACE_BINDING; | 213 | intf->condition = USB_INTERFACE_BINDING; |
214 | |||
215 | /* The interface should always appear to be in use | ||
216 | * unless the driver suports autosuspend. | ||
217 | */ | ||
218 | intf->pm_usage_cnt = !(driver->supports_autosuspend); | ||
219 | |||
211 | error = driver->probe(intf, id); | 220 | error = driver->probe(intf, id); |
212 | if (error) { | 221 | if (error) { |
213 | mark_quiesced(intf); | 222 | mark_quiesced(intf); |
223 | intf->needs_remote_wakeup = 0; | ||
214 | intf->condition = USB_INTERFACE_UNBOUND; | 224 | intf->condition = USB_INTERFACE_UNBOUND; |
215 | } else | 225 | } else |
216 | intf->condition = USB_INTERFACE_BOUND; | 226 | intf->condition = USB_INTERFACE_BOUND; |
227 | |||
228 | usb_autosuspend_device(udev, 1); | ||
217 | } | 229 | } |
218 | 230 | ||
219 | return error; | 231 | return error; |
@@ -224,9 +236,15 @@ static int usb_unbind_interface(struct device *dev) | |||
224 | { | 236 | { |
225 | struct usb_driver *driver = to_usb_driver(dev->driver); | 237 | struct usb_driver *driver = to_usb_driver(dev->driver); |
226 | struct usb_interface *intf = to_usb_interface(dev); | 238 | struct usb_interface *intf = to_usb_interface(dev); |
239 | struct usb_device *udev; | ||
240 | int error; | ||
227 | 241 | ||
228 | intf->condition = USB_INTERFACE_UNBINDING; | 242 | intf->condition = USB_INTERFACE_UNBINDING; |
229 | 243 | ||
244 | /* Autoresume for set_interface call below */ | ||
245 | udev = interface_to_usbdev(intf); | ||
246 | error = usb_autoresume_device(udev, 1); | ||
247 | |||
230 | /* release all urbs for this interface */ | 248 | /* release all urbs for this interface */ |
231 | usb_disable_interface(interface_to_usbdev(intf), intf); | 249 | usb_disable_interface(interface_to_usbdev(intf), intf); |
232 | 250 | ||
@@ -237,8 +255,13 @@ static int usb_unbind_interface(struct device *dev) | |||
237 | intf->altsetting[0].desc.bInterfaceNumber, | 255 | intf->altsetting[0].desc.bInterfaceNumber, |
238 | 0); | 256 | 0); |
239 | usb_set_intfdata(intf, NULL); | 257 | usb_set_intfdata(intf, NULL); |
258 | |||
240 | intf->condition = USB_INTERFACE_UNBOUND; | 259 | intf->condition = USB_INTERFACE_UNBOUND; |
241 | mark_quiesced(intf); | 260 | mark_quiesced(intf); |
261 | intf->needs_remote_wakeup = 0; | ||
262 | |||
263 | if (!error) | ||
264 | usb_autosuspend_device(udev, 1); | ||
242 | 265 | ||
243 | return 0; | 266 | return 0; |
244 | } | 267 | } |
@@ -267,14 +290,19 @@ int usb_driver_claim_interface(struct usb_driver *driver, | |||
267 | struct usb_interface *iface, void* priv) | 290 | struct usb_interface *iface, void* priv) |
268 | { | 291 | { |
269 | struct device *dev = &iface->dev; | 292 | struct device *dev = &iface->dev; |
293 | struct usb_device *udev = interface_to_usbdev(iface); | ||
270 | 294 | ||
271 | if (dev->driver) | 295 | if (dev->driver) |
272 | return -EBUSY; | 296 | return -EBUSY; |
273 | 297 | ||
274 | dev->driver = &driver->drvwrap.driver; | 298 | dev->driver = &driver->drvwrap.driver; |
275 | usb_set_intfdata(iface, priv); | 299 | usb_set_intfdata(iface, priv); |
300 | |||
301 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
276 | iface->condition = USB_INTERFACE_BOUND; | 302 | iface->condition = USB_INTERFACE_BOUND; |
277 | mark_active(iface); | 303 | mark_active(iface); |
304 | iface->pm_usage_cnt = !(driver->supports_autosuspend); | ||
305 | mutex_unlock(&udev->pm_mutex); | ||
278 | 306 | ||
279 | /* if interface was already added, bind now; else let | 307 | /* if interface was already added, bind now; else let |
280 | * the future device_add() bind it, bypassing probe() | 308 | * the future device_add() bind it, bypassing probe() |
@@ -304,6 +332,7 @@ void usb_driver_release_interface(struct usb_driver *driver, | |||
304 | struct usb_interface *iface) | 332 | struct usb_interface *iface) |
305 | { | 333 | { |
306 | struct device *dev = &iface->dev; | 334 | struct device *dev = &iface->dev; |
335 | struct usb_device *udev = interface_to_usbdev(iface); | ||
307 | 336 | ||
308 | /* this should never happen, don't release something that's not ours */ | 337 | /* this should never happen, don't release something that's not ours */ |
309 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) | 338 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
@@ -321,8 +350,12 @@ void usb_driver_release_interface(struct usb_driver *driver, | |||
321 | 350 | ||
322 | dev->driver = NULL; | 351 | dev->driver = NULL; |
323 | usb_set_intfdata(iface, NULL); | 352 | usb_set_intfdata(iface, NULL); |
353 | |||
354 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
324 | iface->condition = USB_INTERFACE_UNBOUND; | 355 | iface->condition = USB_INTERFACE_UNBOUND; |
325 | mark_quiesced(iface); | 356 | mark_quiesced(iface); |
357 | iface->needs_remote_wakeup = 0; | ||
358 | mutex_unlock(&udev->pm_mutex); | ||
326 | } | 359 | } |
327 | EXPORT_SYMBOL(usb_driver_release_interface); | 360 | EXPORT_SYMBOL(usb_driver_release_interface); |
328 | 361 | ||
@@ -751,7 +784,7 @@ EXPORT_SYMBOL_GPL_FUTURE(usb_deregister); | |||
751 | 784 | ||
752 | #ifdef CONFIG_PM | 785 | #ifdef CONFIG_PM |
753 | 786 | ||
754 | /* Caller has locked udev */ | 787 | /* Caller has locked udev->pm_mutex */ |
755 | static int suspend_device(struct usb_device *udev, pm_message_t msg) | 788 | static int suspend_device(struct usb_device *udev, pm_message_t msg) |
756 | { | 789 | { |
757 | struct usb_device_driver *udriver; | 790 | struct usb_device_driver *udriver; |
@@ -763,6 +796,7 @@ static int suspend_device(struct usb_device *udev, pm_message_t msg) | |||
763 | 796 | ||
764 | /* For devices that don't have a driver, we do a standard suspend. */ | 797 | /* For devices that don't have a driver, we do a standard suspend. */ |
765 | if (udev->dev.driver == NULL) { | 798 | if (udev->dev.driver == NULL) { |
799 | udev->do_remote_wakeup = 0; | ||
766 | status = usb_port_suspend(udev); | 800 | status = usb_port_suspend(udev); |
767 | goto done; | 801 | goto done; |
768 | } | 802 | } |
@@ -771,12 +805,13 @@ static int suspend_device(struct usb_device *udev, pm_message_t msg) | |||
771 | status = udriver->suspend(udev, msg); | 805 | status = udriver->suspend(udev, msg); |
772 | 806 | ||
773 | done: | 807 | done: |
808 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | ||
774 | if (status == 0) | 809 | if (status == 0) |
775 | udev->dev.power.power_state.event = msg.event; | 810 | udev->dev.power.power_state.event = msg.event; |
776 | return status; | 811 | return status; |
777 | } | 812 | } |
778 | 813 | ||
779 | /* Caller has locked udev */ | 814 | /* Caller has locked udev->pm_mutex */ |
780 | static int resume_device(struct usb_device *udev) | 815 | static int resume_device(struct usb_device *udev) |
781 | { | 816 | { |
782 | struct usb_device_driver *udriver; | 817 | struct usb_device_driver *udriver; |
@@ -796,12 +831,13 @@ static int resume_device(struct usb_device *udev) | |||
796 | status = udriver->resume(udev); | 831 | status = udriver->resume(udev); |
797 | 832 | ||
798 | done: | 833 | done: |
834 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | ||
799 | if (status == 0) | 835 | if (status == 0) |
800 | udev->dev.power.power_state.event = PM_EVENT_ON; | 836 | udev->dev.power.power_state.event = PM_EVENT_ON; |
801 | return status; | 837 | return status; |
802 | } | 838 | } |
803 | 839 | ||
804 | /* Caller has locked intf's usb_device */ | 840 | /* Caller has locked intf's usb_device's pm_mutex */ |
805 | static int suspend_interface(struct usb_interface *intf, pm_message_t msg) | 841 | static int suspend_interface(struct usb_interface *intf, pm_message_t msg) |
806 | { | 842 | { |
807 | struct usb_driver *driver; | 843 | struct usb_driver *driver; |
@@ -812,31 +848,33 @@ static int suspend_interface(struct usb_interface *intf, pm_message_t msg) | |||
812 | !is_active(intf)) | 848 | !is_active(intf)) |
813 | goto done; | 849 | goto done; |
814 | 850 | ||
815 | if (intf->dev.driver == NULL) /* This can't happen */ | 851 | if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */ |
816 | goto done; | 852 | goto done; |
817 | driver = to_usb_driver(intf->dev.driver); | 853 | driver = to_usb_driver(intf->dev.driver); |
818 | 854 | ||
819 | if (driver->suspend && driver->resume) { | 855 | if (driver->suspend && driver->resume) { |
820 | status = driver->suspend(intf, msg); | 856 | status = driver->suspend(intf, msg); |
821 | if (status) | 857 | if (status == 0) |
858 | mark_quiesced(intf); | ||
859 | else if (!interface_to_usbdev(intf)->auto_pm) | ||
822 | dev_err(&intf->dev, "%s error %d\n", | 860 | dev_err(&intf->dev, "%s error %d\n", |
823 | "suspend", status); | 861 | "suspend", status); |
824 | else | ||
825 | mark_quiesced(intf); | ||
826 | } else { | 862 | } else { |
827 | // FIXME else if there's no suspend method, disconnect... | 863 | // FIXME else if there's no suspend method, disconnect... |
864 | // Not possible if auto_pm is set... | ||
828 | dev_warn(&intf->dev, "no suspend for driver %s?\n", | 865 | dev_warn(&intf->dev, "no suspend for driver %s?\n", |
829 | driver->name); | 866 | driver->name); |
830 | mark_quiesced(intf); | 867 | mark_quiesced(intf); |
831 | } | 868 | } |
832 | 869 | ||
833 | done: | 870 | done: |
871 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); | ||
834 | if (status == 0) | 872 | if (status == 0) |
835 | intf->dev.power.power_state.event = msg.event; | 873 | intf->dev.power.power_state.event = msg.event; |
836 | return status; | 874 | return status; |
837 | } | 875 | } |
838 | 876 | ||
839 | /* Caller has locked intf's usb_device */ | 877 | /* Caller has locked intf's usb_device's pm_mutex */ |
840 | static int resume_interface(struct usb_interface *intf) | 878 | static int resume_interface(struct usb_interface *intf) |
841 | { | 879 | { |
842 | struct usb_driver *driver; | 880 | struct usb_driver *driver; |
@@ -846,8 +884,12 @@ static int resume_interface(struct usb_interface *intf) | |||
846 | is_active(intf)) | 884 | is_active(intf)) |
847 | goto done; | 885 | goto done; |
848 | 886 | ||
887 | /* Don't let autoresume interfere with unbinding */ | ||
888 | if (intf->condition == USB_INTERFACE_UNBINDING) | ||
889 | goto done; | ||
890 | |||
849 | /* Can't resume it if it doesn't have a driver. */ | 891 | /* Can't resume it if it doesn't have a driver. */ |
850 | if (intf->dev.driver == NULL) { | 892 | if (intf->condition == USB_INTERFACE_UNBOUND) { |
851 | status = -ENOTCONN; | 893 | status = -ENOTCONN; |
852 | goto done; | 894 | goto done; |
853 | } | 895 | } |
@@ -867,18 +909,88 @@ static int resume_interface(struct usb_interface *intf) | |||
867 | } | 909 | } |
868 | 910 | ||
869 | done: | 911 | done: |
912 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); | ||
870 | if (status == 0) | 913 | if (status == 0) |
871 | intf->dev.power.power_state.event = PM_EVENT_ON; | 914 | intf->dev.power.power_state.event = PM_EVENT_ON; |
872 | return status; | 915 | return status; |
873 | } | 916 | } |
874 | 917 | ||
875 | /* Caller has locked udev */ | 918 | /** |
919 | * usb_suspend_both - suspend a USB device and its interfaces | ||
920 | * @udev: the usb_device to suspend | ||
921 | * @msg: Power Management message describing this state transition | ||
922 | * | ||
923 | * This is the central routine for suspending USB devices. It calls the | ||
924 | * suspend methods for all the interface drivers in @udev and then calls | ||
925 | * the suspend method for @udev itself. If an error occurs at any stage, | ||
926 | * all the interfaces which were suspended are resumed so that they remain | ||
927 | * in the same state as the device. | ||
928 | * | ||
929 | * If an autosuspend is in progress (@udev->auto_pm is set), the routine | ||
930 | * checks first to make sure that neither the device itself or any of its | ||
931 | * active interfaces is in use (pm_usage_cnt is greater than 0). If they | ||
932 | * are, the autosuspend fails. | ||
933 | * | ||
934 | * If the suspend succeeds, the routine recursively queues an autosuspend | ||
935 | * request for @udev's parent device, thereby propagating the change up | ||
936 | * the device tree. If all of the parent's children are now suspended, | ||
937 | * the parent will autosuspend in turn. | ||
938 | * | ||
939 | * The suspend method calls are subject to mutual exclusion under control | ||
940 | * of @udev's pm_mutex. Many of these calls are also under the protection | ||
941 | * of @udev's device lock (including all requests originating outside the | ||
942 | * USB subsystem), but autosuspend requests generated by a child device or | ||
943 | * interface driver may not be. Usbcore will insure that the method calls | ||
944 | * do not arrive during bind, unbind, or reset operations. However, drivers | ||
945 | * must be prepared to handle suspend calls arriving at unpredictable times. | ||
946 | * The only way to block such calls is to do an autoresume (preventing | ||
947 | * autosuspends) while holding @udev's device lock (preventing outside | ||
948 | * suspends). | ||
949 | * | ||
950 | * The caller must hold @udev->pm_mutex. | ||
951 | * | ||
952 | * This routine can run only in process context. | ||
953 | */ | ||
876 | int usb_suspend_both(struct usb_device *udev, pm_message_t msg) | 954 | int usb_suspend_both(struct usb_device *udev, pm_message_t msg) |
877 | { | 955 | { |
878 | int status = 0; | 956 | int status = 0; |
879 | int i = 0; | 957 | int i = 0; |
880 | struct usb_interface *intf; | 958 | struct usb_interface *intf; |
959 | struct usb_device *parent = udev->parent; | ||
960 | |||
961 | cancel_delayed_work(&udev->autosuspend); | ||
962 | if (udev->state == USB_STATE_NOTATTACHED) | ||
963 | return 0; | ||
964 | if (udev->state == USB_STATE_SUSPENDED) | ||
965 | return 0; | ||
881 | 966 | ||
967 | udev->do_remote_wakeup = device_may_wakeup(&udev->dev); | ||
968 | |||
969 | /* For autosuspend, fail fast if anything is in use. | ||
970 | * Also fail if any interfaces require remote wakeup but it | ||
971 | * isn't available. */ | ||
972 | if (udev->auto_pm) { | ||
973 | if (udev->pm_usage_cnt > 0) | ||
974 | return -EBUSY; | ||
975 | if (udev->actconfig) { | ||
976 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { | ||
977 | intf = udev->actconfig->interface[i]; | ||
978 | if (!is_active(intf)) | ||
979 | continue; | ||
980 | if (intf->pm_usage_cnt > 0) | ||
981 | return -EBUSY; | ||
982 | if (intf->needs_remote_wakeup && | ||
983 | !udev->do_remote_wakeup) { | ||
984 | dev_dbg(&udev->dev, | ||
985 | "remote wakeup needed for autosuspend\n"); | ||
986 | return -EOPNOTSUPP; | ||
987 | } | ||
988 | } | ||
989 | i = 0; | ||
990 | } | ||
991 | } | ||
992 | |||
993 | /* Suspend all the interfaces and then udev itself */ | ||
882 | if (udev->actconfig) { | 994 | if (udev->actconfig) { |
883 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { | 995 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { |
884 | intf = udev->actconfig->interface[i]; | 996 | intf = udev->actconfig->interface[i]; |
@@ -896,40 +1008,282 @@ int usb_suspend_both(struct usb_device *udev, pm_message_t msg) | |||
896 | intf = udev->actconfig->interface[i]; | 1008 | intf = udev->actconfig->interface[i]; |
897 | resume_interface(intf); | 1009 | resume_interface(intf); |
898 | } | 1010 | } |
899 | } | 1011 | |
1012 | /* If the suspend succeeded, propagate it up the tree */ | ||
1013 | } else if (parent) | ||
1014 | usb_autosuspend_device(parent, 0); | ||
1015 | |||
1016 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | ||
900 | return status; | 1017 | return status; |
901 | } | 1018 | } |
902 | 1019 | ||
903 | /* Caller has locked udev */ | 1020 | /** |
1021 | * usb_resume_both - resume a USB device and its interfaces | ||
1022 | * @udev: the usb_device to resume | ||
1023 | * | ||
1024 | * This is the central routine for resuming USB devices. It calls the | ||
1025 | * the resume method for @udev and then calls the resume methods for all | ||
1026 | * the interface drivers in @udev. | ||
1027 | * | ||
1028 | * Before starting the resume, the routine calls itself recursively for | ||
1029 | * the parent device of @udev, thereby propagating the change up the device | ||
1030 | * tree and assuring that @udev will be able to resume. If the parent is | ||
1031 | * unable to resume successfully, the routine fails. | ||
1032 | * | ||
1033 | * The resume method calls are subject to mutual exclusion under control | ||
1034 | * of @udev's pm_mutex. Many of these calls are also under the protection | ||
1035 | * of @udev's device lock (including all requests originating outside the | ||
1036 | * USB subsystem), but autoresume requests generated by a child device or | ||
1037 | * interface driver may not be. Usbcore will insure that the method calls | ||
1038 | * do not arrive during bind, unbind, or reset operations. However, drivers | ||
1039 | * must be prepared to handle resume calls arriving at unpredictable times. | ||
1040 | * The only way to block such calls is to do an autoresume (preventing | ||
1041 | * other autoresumes) while holding @udev's device lock (preventing outside | ||
1042 | * resumes). | ||
1043 | * | ||
1044 | * The caller must hold @udev->pm_mutex. | ||
1045 | * | ||
1046 | * This routine can run only in process context. | ||
1047 | */ | ||
904 | int usb_resume_both(struct usb_device *udev) | 1048 | int usb_resume_both(struct usb_device *udev) |
905 | { | 1049 | { |
906 | int status; | 1050 | int status = 0; |
907 | int i; | 1051 | int i; |
908 | struct usb_interface *intf; | 1052 | struct usb_interface *intf; |
1053 | struct usb_device *parent = udev->parent; | ||
1054 | |||
1055 | cancel_delayed_work(&udev->autosuspend); | ||
1056 | if (udev->state == USB_STATE_NOTATTACHED) | ||
1057 | return -ENODEV; | ||
909 | 1058 | ||
910 | /* Can't resume if the parent is suspended */ | 1059 | /* Propagate the resume up the tree, if necessary */ |
911 | if (udev->parent && udev->parent->state == USB_STATE_SUSPENDED) { | 1060 | if (udev->state == USB_STATE_SUSPENDED) { |
912 | dev_warn(&udev->dev, "can't resume; parent is suspended\n"); | 1061 | if (parent) { |
913 | return -EHOSTUNREACH; | 1062 | mutex_lock_nested(&parent->pm_mutex, parent->level); |
1063 | parent->auto_pm = 1; | ||
1064 | status = usb_resume_both(parent); | ||
1065 | } else { | ||
1066 | |||
1067 | /* We can't progagate beyond the USB subsystem, | ||
1068 | * so if a root hub's controller is suspended | ||
1069 | * then we're stuck. */ | ||
1070 | if (udev->dev.parent->power.power_state.event != | ||
1071 | PM_EVENT_ON) | ||
1072 | status = -EHOSTUNREACH; | ||
1073 | } | ||
1074 | if (status == 0 && udev->state == USB_STATE_SUSPENDED) | ||
1075 | status = resume_device(udev); | ||
1076 | if (parent) | ||
1077 | mutex_unlock(&parent->pm_mutex); | ||
914 | } | 1078 | } |
915 | 1079 | ||
916 | status = resume_device(udev); | 1080 | /* Now the parent won't suspend until we are finished */ |
1081 | |||
917 | if (status == 0 && udev->actconfig) { | 1082 | if (status == 0 && udev->actconfig) { |
918 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { | 1083 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
919 | intf = udev->actconfig->interface[i]; | 1084 | intf = udev->actconfig->interface[i]; |
920 | resume_interface(intf); | 1085 | resume_interface(intf); |
921 | } | 1086 | } |
922 | } | 1087 | } |
1088 | |||
1089 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | ||
1090 | return status; | ||
1091 | } | ||
1092 | |||
1093 | #ifdef CONFIG_USB_SUSPEND | ||
1094 | |||
1095 | /** | ||
1096 | * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces | ||
1097 | * @udev - the usb_device to autosuspend | ||
1098 | * @dec_usage_cnt - flag to decrement @udev's PM-usage counter | ||
1099 | * | ||
1100 | * This routine should be called when a core subsystem is finished using | ||
1101 | * @udev and wants to allow it to autosuspend. Examples would be when | ||
1102 | * @udev's device file in usbfs is closed or after a configuration change. | ||
1103 | * | ||
1104 | * @dec_usage_cnt should be 1 if the subsystem previously incremented | ||
1105 | * @udev's usage counter (such as by passing 1 to usb_autoresume_device); | ||
1106 | * otherwise it should be 0. | ||
1107 | * | ||
1108 | * If the usage counter for @udev or any of its active interfaces is greater | ||
1109 | * than 0, the autosuspend request will not be queued. (If an interface | ||
1110 | * driver does not support autosuspend then its usage counter is permanently | ||
1111 | * positive.) Likewise, if an interface driver requires remote-wakeup | ||
1112 | * capability during autosuspend but remote wakeup is disabled, the | ||
1113 | * autosuspend will fail. | ||
1114 | * | ||
1115 | * Often the caller will hold @udev's device lock, but this is not | ||
1116 | * necessary. | ||
1117 | * | ||
1118 | * This routine can run only in process context. | ||
1119 | */ | ||
1120 | void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt) | ||
1121 | { | ||
1122 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1123 | udev->pm_usage_cnt -= dec_usage_cnt; | ||
1124 | if (udev->pm_usage_cnt <= 0) | ||
1125 | schedule_delayed_work(&udev->autosuspend, | ||
1126 | USB_AUTOSUSPEND_DELAY); | ||
1127 | mutex_unlock(&udev->pm_mutex); | ||
1128 | // dev_dbg(&udev->dev, "%s: cnt %d\n", | ||
1129 | // __FUNCTION__, udev->pm_usage_cnt); | ||
1130 | } | ||
1131 | |||
1132 | /** | ||
1133 | * usb_autoresume_device - immediately autoresume a USB device and its interfaces | ||
1134 | * @udev - the usb_device to autoresume | ||
1135 | * @inc_usage_cnt - flag to increment @udev's PM-usage counter | ||
1136 | * | ||
1137 | * This routine should be called when a core subsystem wants to use @udev | ||
1138 | * and needs to guarantee that it is not suspended. In addition, the | ||
1139 | * caller can prevent @udev from being autosuspended subsequently. (Note | ||
1140 | * that this will not prevent suspend events originating in the PM core.) | ||
1141 | * Examples would be when @udev's device file in usbfs is opened (autosuspend | ||
1142 | * should be prevented until the file is closed) or when a remote-wakeup | ||
1143 | * request is received (later autosuspends should not be prevented). | ||
1144 | * | ||
1145 | * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent | ||
1146 | * autosuspends. This prevention will persist until the usage counter is | ||
1147 | * decremented again (such as by passing 1 to usb_autosuspend_device). | ||
1148 | * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged. | ||
1149 | * Regardless, if the autoresume fails then the usage counter is not | ||
1150 | * incremented. | ||
1151 | * | ||
1152 | * Often the caller will hold @udev's device lock, but this is not | ||
1153 | * necessary (and attempting it might cause deadlock). | ||
1154 | * | ||
1155 | * This routine can run only in process context. | ||
1156 | */ | ||
1157 | int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt) | ||
1158 | { | ||
1159 | int status; | ||
1160 | |||
1161 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1162 | udev->pm_usage_cnt += inc_usage_cnt; | ||
1163 | udev->auto_pm = 1; | ||
1164 | status = usb_resume_both(udev); | ||
1165 | if (status != 0) | ||
1166 | udev->pm_usage_cnt -= inc_usage_cnt; | ||
1167 | mutex_unlock(&udev->pm_mutex); | ||
1168 | // dev_dbg(&udev->dev, "%s: status %d cnt %d\n", | ||
1169 | // __FUNCTION__, status, udev->pm_usage_cnt); | ||
1170 | return status; | ||
1171 | } | ||
1172 | |||
1173 | /** | ||
1174 | * usb_autopm_put_interface - decrement a USB interface's PM-usage counter | ||
1175 | * @intf - the usb_interface whose counter should be decremented | ||
1176 | * | ||
1177 | * This routine should be called by an interface driver when it is | ||
1178 | * finished using @intf and wants to allow it to autosuspend. A typical | ||
1179 | * example would be a character-device driver when its device file is | ||
1180 | * closed. | ||
1181 | * | ||
1182 | * The routine decrements @intf's usage counter. When the counter reaches | ||
1183 | * 0, a delayed autosuspend request for @intf's device is queued. When | ||
1184 | * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all | ||
1185 | * the other usage counters for the sibling interfaces and @intf's | ||
1186 | * usb_device, the device and all its interfaces will be autosuspended. | ||
1187 | * | ||
1188 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The | ||
1189 | * core will not change its value other than the increment and decrement | ||
1190 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver | ||
1191 | * may use this simple counter-oriented discipline or may set the value | ||
1192 | * any way it likes. | ||
1193 | * | ||
1194 | * If the driver has set @intf->needs_remote_wakeup then autosuspend will | ||
1195 | * take place only if the device's remote-wakeup facility is enabled. | ||
1196 | * | ||
1197 | * Suspend method calls queued by this routine can arrive at any time | ||
1198 | * while @intf is resumed and its usage counter is equal to 0. They are | ||
1199 | * not protected by the usb_device's lock but only by its pm_mutex. | ||
1200 | * Drivers must provide their own synchronization. | ||
1201 | * | ||
1202 | * This routine can run only in process context. | ||
1203 | */ | ||
1204 | void usb_autopm_put_interface(struct usb_interface *intf) | ||
1205 | { | ||
1206 | struct usb_device *udev = interface_to_usbdev(intf); | ||
1207 | |||
1208 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1209 | if (intf->condition != USB_INTERFACE_UNBOUND) { | ||
1210 | if (--intf->pm_usage_cnt <= 0) | ||
1211 | schedule_delayed_work(&udev->autosuspend, | ||
1212 | USB_AUTOSUSPEND_DELAY); | ||
1213 | } | ||
1214 | mutex_unlock(&udev->pm_mutex); | ||
1215 | // dev_dbg(&intf->dev, "%s: cnt %d\n", | ||
1216 | // __FUNCTION__, intf->pm_usage_cnt); | ||
1217 | } | ||
1218 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); | ||
1219 | |||
1220 | /** | ||
1221 | * usb_autopm_get_interface - increment a USB interface's PM-usage counter | ||
1222 | * @intf - the usb_interface whose counter should be incremented | ||
1223 | * | ||
1224 | * This routine should be called by an interface driver when it wants to | ||
1225 | * use @intf and needs to guarantee that it is not suspended. In addition, | ||
1226 | * the routine prevents @intf from being autosuspended subsequently. (Note | ||
1227 | * that this will not prevent suspend events originating in the PM core.) | ||
1228 | * This prevention will persist until usb_autopm_put_interface() is called | ||
1229 | * or @intf is unbound. A typical example would be a character-device | ||
1230 | * driver when its device file is opened. | ||
1231 | * | ||
1232 | * The routine increments @intf's usage counter. So long as the counter | ||
1233 | * is greater than 0, autosuspend will not be allowed for @intf or its | ||
1234 | * usb_device. When the driver is finished using @intf it should call | ||
1235 | * usb_autopm_put_interface() to decrement the usage counter and queue | ||
1236 | * a delayed autosuspend request (if the counter is <= 0). | ||
1237 | * | ||
1238 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The | ||
1239 | * core will not change its value other than the increment and decrement | ||
1240 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver | ||
1241 | * may use this simple counter-oriented discipline or may set the value | ||
1242 | * any way it likes. | ||
1243 | * | ||
1244 | * Resume method calls generated by this routine can arrive at any time | ||
1245 | * while @intf is suspended. They are not protected by the usb_device's | ||
1246 | * lock but only by its pm_mutex. Drivers must provide their own | ||
1247 | * synchronization. | ||
1248 | * | ||
1249 | * This routine can run only in process context. | ||
1250 | */ | ||
1251 | int usb_autopm_get_interface(struct usb_interface *intf) | ||
1252 | { | ||
1253 | struct usb_device *udev = interface_to_usbdev(intf); | ||
1254 | int status; | ||
1255 | |||
1256 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1257 | if (intf->condition == USB_INTERFACE_UNBOUND) | ||
1258 | status = -ENODEV; | ||
1259 | else { | ||
1260 | ++intf->pm_usage_cnt; | ||
1261 | udev->auto_pm = 1; | ||
1262 | status = usb_resume_both(udev); | ||
1263 | if (status != 0) | ||
1264 | --intf->pm_usage_cnt; | ||
1265 | } | ||
1266 | mutex_unlock(&udev->pm_mutex); | ||
1267 | // dev_dbg(&intf->dev, "%s: status %d cnt %d\n", | ||
1268 | // __FUNCTION__, status, intf->pm_usage_cnt); | ||
923 | return status; | 1269 | return status; |
924 | } | 1270 | } |
1271 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); | ||
1272 | |||
1273 | #endif /* CONFIG_USB_SUSPEND */ | ||
925 | 1274 | ||
926 | static int usb_suspend(struct device *dev, pm_message_t message) | 1275 | static int usb_suspend(struct device *dev, pm_message_t message) |
927 | { | 1276 | { |
928 | int status; | 1277 | int status; |
929 | 1278 | ||
930 | if (is_usb_device(dev)) | 1279 | if (is_usb_device(dev)) { |
931 | status = usb_suspend_both(to_usb_device(dev), message); | 1280 | struct usb_device *udev = to_usb_device(dev); |
932 | else | 1281 | |
1282 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1283 | udev->auto_pm = 0; | ||
1284 | status = usb_suspend_both(udev, message); | ||
1285 | mutex_unlock(&udev->pm_mutex); | ||
1286 | } else | ||
933 | status = 0; | 1287 | status = 0; |
934 | return status; | 1288 | return status; |
935 | } | 1289 | } |
@@ -939,7 +1293,12 @@ static int usb_resume(struct device *dev) | |||
939 | int status; | 1293 | int status; |
940 | 1294 | ||
941 | if (is_usb_device(dev)) { | 1295 | if (is_usb_device(dev)) { |
942 | status = usb_resume_both(to_usb_device(dev)); | 1296 | struct usb_device *udev = to_usb_device(dev); |
1297 | |||
1298 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1299 | udev->auto_pm = 0; | ||
1300 | status = usb_resume_both(udev); | ||
1301 | mutex_unlock(&udev->pm_mutex); | ||
943 | 1302 | ||
944 | /* Rebind drivers that had no suspend method? */ | 1303 | /* Rebind drivers that had no suspend method? */ |
945 | } else | 1304 | } else |
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 78e910b2046c..dee812bc6c43 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c | |||
@@ -1017,19 +1017,22 @@ void usb_set_device_state(struct usb_device *udev, | |||
1017 | if (udev->state == USB_STATE_NOTATTACHED) | 1017 | if (udev->state == USB_STATE_NOTATTACHED) |
1018 | ; /* do nothing */ | 1018 | ; /* do nothing */ |
1019 | else if (new_state != USB_STATE_NOTATTACHED) { | 1019 | else if (new_state != USB_STATE_NOTATTACHED) { |
1020 | udev->state = new_state; | ||
1021 | 1020 | ||
1022 | /* root hub wakeup capabilities are managed out-of-band | 1021 | /* root hub wakeup capabilities are managed out-of-band |
1023 | * and may involve silicon errata ... ignore them here. | 1022 | * and may involve silicon errata ... ignore them here. |
1024 | */ | 1023 | */ |
1025 | if (udev->parent) { | 1024 | if (udev->parent) { |
1026 | if (new_state == USB_STATE_CONFIGURED) | 1025 | if (udev->state == USB_STATE_SUSPENDED |
1026 | || new_state == USB_STATE_SUSPENDED) | ||
1027 | ; /* No change to wakeup settings */ | ||
1028 | else if (new_state == USB_STATE_CONFIGURED) | ||
1027 | device_init_wakeup(&udev->dev, | 1029 | device_init_wakeup(&udev->dev, |
1028 | (udev->actconfig->desc.bmAttributes | 1030 | (udev->actconfig->desc.bmAttributes |
1029 | & USB_CONFIG_ATT_WAKEUP)); | 1031 | & USB_CONFIG_ATT_WAKEUP)); |
1030 | else if (new_state != USB_STATE_SUSPENDED) | 1032 | else |
1031 | device_init_wakeup(&udev->dev, 0); | 1033 | device_init_wakeup(&udev->dev, 0); |
1032 | } | 1034 | } |
1035 | udev->state = new_state; | ||
1033 | } else | 1036 | } else |
1034 | recursively_mark_NOTATTACHED(udev); | 1037 | recursively_mark_NOTATTACHED(udev); |
1035 | spin_unlock_irqrestore(&device_state_lock, flags); | 1038 | spin_unlock_irqrestore(&device_state_lock, flags); |
@@ -1507,7 +1510,7 @@ static int hub_port_suspend(struct usb_hub *hub, int port1, | |||
1507 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when | 1510 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when |
1508 | * we don't explicitly enable it here. | 1511 | * we don't explicitly enable it here. |
1509 | */ | 1512 | */ |
1510 | if (device_may_wakeup(&udev->dev)) { | 1513 | if (udev->do_remote_wakeup) { |
1511 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | 1514 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
1512 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, | 1515 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, |
1513 | USB_DEVICE_REMOTE_WAKEUP, 0, | 1516 | USB_DEVICE_REMOTE_WAKEUP, 0, |
@@ -1533,7 +1536,8 @@ static int hub_port_suspend(struct usb_hub *hub, int port1, | |||
1533 | USB_CTRL_SET_TIMEOUT); | 1536 | USB_CTRL_SET_TIMEOUT); |
1534 | } else { | 1537 | } else { |
1535 | /* device has up to 10 msec to fully suspend */ | 1538 | /* device has up to 10 msec to fully suspend */ |
1536 | dev_dbg(&udev->dev, "usb suspend\n"); | 1539 | dev_dbg(&udev->dev, "usb %ssuspend\n", |
1540 | udev->auto_pm ? "auto-" : ""); | ||
1537 | usb_set_device_state(udev, USB_STATE_SUSPENDED); | 1541 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
1538 | msleep(10); | 1542 | msleep(10); |
1539 | } | 1543 | } |
@@ -1573,7 +1577,8 @@ static int __usb_port_suspend (struct usb_device *udev, int port1) | |||
1573 | status = hub_port_suspend(hdev_to_hub(udev->parent), port1, | 1577 | status = hub_port_suspend(hdev_to_hub(udev->parent), port1, |
1574 | udev); | 1578 | udev); |
1575 | else { | 1579 | else { |
1576 | dev_dbg(&udev->dev, "usb suspend\n"); | 1580 | dev_dbg(&udev->dev, "usb %ssuspend\n", |
1581 | udev->auto_pm ? "auto-" : ""); | ||
1577 | usb_set_device_state(udev, USB_STATE_SUSPENDED); | 1582 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
1578 | } | 1583 | } |
1579 | return status; | 1584 | return status; |
@@ -1687,7 +1692,8 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev) | |||
1687 | 1692 | ||
1688 | /* drive resume for at least 20 msec */ | 1693 | /* drive resume for at least 20 msec */ |
1689 | if (udev) | 1694 | if (udev) |
1690 | dev_dbg(&udev->dev, "RESUME\n"); | 1695 | dev_dbg(&udev->dev, "usb %sresume\n", |
1696 | udev->auto_pm ? "auto-" : ""); | ||
1691 | msleep(25); | 1697 | msleep(25); |
1692 | 1698 | ||
1693 | #define LIVE_FLAGS ( USB_PORT_STAT_POWER \ | 1699 | #define LIVE_FLAGS ( USB_PORT_STAT_POWER \ |
@@ -1754,8 +1760,11 @@ int usb_port_resume(struct usb_device *udev) | |||
1754 | // NOTE this fails if parent is also suspended... | 1760 | // NOTE this fails if parent is also suspended... |
1755 | status = hub_port_resume(hdev_to_hub(udev->parent), | 1761 | status = hub_port_resume(hdev_to_hub(udev->parent), |
1756 | udev->portnum, udev); | 1762 | udev->portnum, udev); |
1757 | } else | 1763 | } else { |
1764 | dev_dbg(&udev->dev, "usb %sresume\n", | ||
1765 | udev->auto_pm ? "auto-" : ""); | ||
1758 | status = finish_port_resume(udev); | 1766 | status = finish_port_resume(udev); |
1767 | } | ||
1759 | if (status < 0) | 1768 | if (status < 0) |
1760 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); | 1769 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); |
1761 | return status; | 1770 | return status; |
@@ -1765,19 +1774,23 @@ static int remote_wakeup(struct usb_device *udev) | |||
1765 | { | 1774 | { |
1766 | int status = 0; | 1775 | int status = 0; |
1767 | 1776 | ||
1768 | /* don't repeat RESUME sequence if this device | 1777 | /* All this just to avoid sending a port-resume message |
1769 | * was already woken up by some other task | 1778 | * to the parent hub! */ |
1770 | */ | 1779 | |
1771 | usb_lock_device(udev); | 1780 | usb_lock_device(udev); |
1781 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
1772 | if (udev->state == USB_STATE_SUSPENDED) { | 1782 | if (udev->state == USB_STATE_SUSPENDED) { |
1773 | dev_dbg(&udev->dev, "RESUME (wakeup)\n"); | 1783 | dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); |
1774 | /* TRSMRCY = 10 msec */ | 1784 | /* TRSMRCY = 10 msec */ |
1775 | msleep(10); | 1785 | msleep(10); |
1776 | status = finish_port_resume(udev); | 1786 | status = finish_port_resume(udev); |
1787 | if (status == 0) | ||
1788 | udev->dev.power.power_state.event = PM_EVENT_ON; | ||
1777 | } | 1789 | } |
1790 | mutex_unlock(&udev->pm_mutex); | ||
1778 | 1791 | ||
1779 | if (status == 0) | 1792 | if (status == 0) |
1780 | usb_resume_both(udev); | 1793 | usb_autoresume_device(udev, 0); |
1781 | usb_unlock_device(udev); | 1794 | usb_unlock_device(udev); |
1782 | return status; | 1795 | return status; |
1783 | } | 1796 | } |
@@ -1834,7 +1847,9 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg) | |||
1834 | == PM_EVENT_ON | 1847 | == PM_EVENT_ON |
1835 | #endif | 1848 | #endif |
1836 | ) { | 1849 | ) { |
1837 | dev_dbg(&intf->dev, "port %d nyet suspended\n", port1); | 1850 | if (!hdev->auto_pm) |
1851 | dev_dbg(&intf->dev, "port %d nyet suspended\n", | ||
1852 | port1); | ||
1838 | return -EBUSY; | 1853 | return -EBUSY; |
1839 | } | 1854 | } |
1840 | } | 1855 | } |
@@ -2587,7 +2602,7 @@ static void hub_events(void) | |||
2587 | * stub "device" node was never suspended. | 2602 | * stub "device" node was never suspended. |
2588 | */ | 2603 | */ |
2589 | if (i) | 2604 | if (i) |
2590 | usb_resume_both(hdev); | 2605 | usb_autoresume_device(hdev, 0); |
2591 | 2606 | ||
2592 | /* If this is an inactive or suspended hub, do nothing */ | 2607 | /* If this is an inactive or suspended hub, do nothing */ |
2593 | if (hub->quiescing) | 2608 | if (hub->quiescing) |
@@ -2993,6 +3008,9 @@ int usb_reset_composite_device(struct usb_device *udev, | |||
2993 | return -EINVAL; | 3008 | return -EINVAL; |
2994 | } | 3009 | } |
2995 | 3010 | ||
3011 | /* Prevent autosuspend during the reset */ | ||
3012 | usb_autoresume_device(udev, 1); | ||
3013 | |||
2996 | if (iface && iface->condition != USB_INTERFACE_BINDING) | 3014 | if (iface && iface->condition != USB_INTERFACE_BINDING) |
2997 | iface = NULL; | 3015 | iface = NULL; |
2998 | 3016 | ||
@@ -3034,6 +3052,7 @@ int usb_reset_composite_device(struct usb_device *udev, | |||
3034 | } | 3052 | } |
3035 | } | 3053 | } |
3036 | 3054 | ||
3055 | usb_autosuspend_device(udev, 1); | ||
3037 | return ret; | 3056 | return ret; |
3038 | } | 3057 | } |
3039 | EXPORT_SYMBOL(usb_reset_composite_device); | 3058 | EXPORT_SYMBOL(usb_reset_composite_device); |
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c index b0c0a993338f..6b029cdb8671 100644 --- a/drivers/usb/core/usb.c +++ b/drivers/usb/core/usb.c | |||
@@ -168,6 +168,10 @@ static void usb_release_dev(struct device *dev) | |||
168 | 168 | ||
169 | udev = to_usb_device(dev); | 169 | udev = to_usb_device(dev); |
170 | 170 | ||
171 | #ifdef CONFIG_PM | ||
172 | cancel_delayed_work(&udev->autosuspend); | ||
173 | flush_scheduled_work(); | ||
174 | #endif | ||
171 | usb_destroy_configuration(udev); | 175 | usb_destroy_configuration(udev); |
172 | usb_put_hcd(bus_to_hcd(udev->bus)); | 176 | usb_put_hcd(bus_to_hcd(udev->bus)); |
173 | kfree(udev->product); | 177 | kfree(udev->product); |
@@ -176,6 +180,21 @@ static void usb_release_dev(struct device *dev) | |||
176 | kfree(udev); | 180 | kfree(udev); |
177 | } | 181 | } |
178 | 182 | ||
183 | #ifdef CONFIG_PM | ||
184 | |||
185 | /* usb_autosuspend_work - callback routine to autosuspend a USB device */ | ||
186 | static void usb_autosuspend_work(void *_udev) | ||
187 | { | ||
188 | struct usb_device *udev = _udev; | ||
189 | |||
190 | mutex_lock_nested(&udev->pm_mutex, udev->level); | ||
191 | udev->auto_pm = 1; | ||
192 | usb_suspend_both(udev, PMSG_SUSPEND); | ||
193 | mutex_unlock(&udev->pm_mutex); | ||
194 | } | ||
195 | |||
196 | #endif | ||
197 | |||
179 | /** | 198 | /** |
180 | * usb_alloc_dev - usb device constructor (usbcore-internal) | 199 | * usb_alloc_dev - usb device constructor (usbcore-internal) |
181 | * @parent: hub to which device is connected; null to allocate a root hub | 200 | * @parent: hub to which device is connected; null to allocate a root hub |
@@ -251,6 +270,10 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) | |||
251 | dev->parent = parent; | 270 | dev->parent = parent; |
252 | INIT_LIST_HEAD(&dev->filelist); | 271 | INIT_LIST_HEAD(&dev->filelist); |
253 | 272 | ||
273 | #ifdef CONFIG_PM | ||
274 | mutex_init(&dev->pm_mutex); | ||
275 | INIT_WORK(&dev->autosuspend, usb_autosuspend_work, dev); | ||
276 | #endif | ||
254 | return dev; | 277 | return dev; |
255 | } | 278 | } |
256 | 279 | ||
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h index 5162cb370215..10688ad73c6d 100644 --- a/drivers/usb/core/usb.h +++ b/drivers/usb/core/usb.h | |||
@@ -49,6 +49,20 @@ static inline int usb_resume_both(struct usb_device *udev) | |||
49 | 49 | ||
50 | #endif | 50 | #endif |
51 | 51 | ||
52 | #ifdef CONFIG_USB_SUSPEND | ||
53 | |||
54 | #define USB_AUTOSUSPEND_DELAY (HZ*2) | ||
55 | |||
56 | extern void usb_autosuspend_device(struct usb_device *udev, int dec_busy_cnt); | ||
57 | extern int usb_autoresume_device(struct usb_device *udev, int inc_busy_cnt); | ||
58 | |||
59 | #else | ||
60 | |||
61 | #define usb_autosuspend_device(udev, dec_busy_cnt) do {} while (0) | ||
62 | #define usb_autoresume_device(udev, inc_busy_cnt) 0 | ||
63 | |||
64 | #endif | ||
65 | |||
52 | extern struct bus_type usb_bus_type; | 66 | extern struct bus_type usb_bus_type; |
53 | extern struct usb_device_driver usb_generic_driver; | 67 | extern struct usb_device_driver usb_generic_driver; |
54 | 68 | ||
diff --git a/include/linux/usb.h b/include/linux/usb.h index df5c93eb3ce9..0da15b0b02be 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h | |||
@@ -19,6 +19,7 @@ | |||
19 | #include <linux/fs.h> /* for struct file_operations */ | 19 | #include <linux/fs.h> /* for struct file_operations */ |
20 | #include <linux/completion.h> /* for struct completion */ | 20 | #include <linux/completion.h> /* for struct completion */ |
21 | #include <linux/sched.h> /* for current && schedule_timeout */ | 21 | #include <linux/sched.h> /* for current && schedule_timeout */ |
22 | #include <linux/mutex.h> /* for struct mutex */ | ||
22 | 23 | ||
23 | struct usb_device; | 24 | struct usb_device; |
24 | struct usb_driver; | 25 | struct usb_driver; |
@@ -103,8 +104,12 @@ enum usb_interface_condition { | |||
103 | * @condition: binding state of the interface: not bound, binding | 104 | * @condition: binding state of the interface: not bound, binding |
104 | * (in probe()), bound to a driver, or unbinding (in disconnect()) | 105 | * (in probe()), bound to a driver, or unbinding (in disconnect()) |
105 | * @is_active: flag set when the interface is bound and not suspended. | 106 | * @is_active: flag set when the interface is bound and not suspended. |
107 | * @needs_remote_wakeup: flag set when the driver requires remote-wakeup | ||
108 | * capability during autosuspend. | ||
106 | * @dev: driver model's view of this device | 109 | * @dev: driver model's view of this device |
107 | * @class_dev: driver model's class view of this device. | 110 | * @class_dev: driver model's class view of this device. |
111 | * @pm_usage_cnt: PM usage counter for this interface; autosuspend is not | ||
112 | * allowed unless the counter is 0. | ||
108 | * | 113 | * |
109 | * USB device drivers attach to interfaces on a physical device. Each | 114 | * USB device drivers attach to interfaces on a physical device. Each |
110 | * interface encapsulates a single high level function, such as feeding | 115 | * interface encapsulates a single high level function, such as feeding |
@@ -144,9 +149,11 @@ struct usb_interface { | |||
144 | * bound to */ | 149 | * bound to */ |
145 | enum usb_interface_condition condition; /* state of binding */ | 150 | enum usb_interface_condition condition; /* state of binding */ |
146 | unsigned is_active:1; /* the interface is not suspended */ | 151 | unsigned is_active:1; /* the interface is not suspended */ |
152 | unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */ | ||
147 | 153 | ||
148 | struct device dev; /* interface specific device info */ | 154 | struct device dev; /* interface specific device info */ |
149 | struct class_device *class_dev; | 155 | struct class_device *class_dev; |
156 | int pm_usage_cnt; /* usage counter for autosuspend */ | ||
150 | }; | 157 | }; |
151 | #define to_usb_interface(d) container_of(d, struct usb_interface, dev) | 158 | #define to_usb_interface(d) container_of(d, struct usb_interface, dev) |
152 | #define interface_to_usbdev(intf) \ | 159 | #define interface_to_usbdev(intf) \ |
@@ -372,6 +379,15 @@ struct usb_device { | |||
372 | 379 | ||
373 | int maxchild; /* Number of ports if hub */ | 380 | int maxchild; /* Number of ports if hub */ |
374 | struct usb_device *children[USB_MAXCHILDREN]; | 381 | struct usb_device *children[USB_MAXCHILDREN]; |
382 | |||
383 | #ifdef CONFIG_PM | ||
384 | struct work_struct autosuspend; /* for delayed autosuspends */ | ||
385 | struct mutex pm_mutex; /* protects PM operations */ | ||
386 | int pm_usage_cnt; /* usage counter for autosuspend */ | ||
387 | |||
388 | unsigned auto_pm:1; /* autosuspend/resume in progress */ | ||
389 | unsigned do_remote_wakeup:1; /* remote wakeup should be enabled */ | ||
390 | #endif | ||
375 | }; | 391 | }; |
376 | #define to_usb_device(d) container_of(d, struct usb_device, dev) | 392 | #define to_usb_device(d) container_of(d, struct usb_device, dev) |
377 | 393 | ||
@@ -392,6 +408,17 @@ extern int usb_reset_composite_device(struct usb_device *dev, | |||
392 | 408 | ||
393 | extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id); | 409 | extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id); |
394 | 410 | ||
411 | /* USB autosuspend and autoresume */ | ||
412 | #ifdef CONFIG_USB_SUSPEND | ||
413 | extern int usb_autopm_get_interface(struct usb_interface *intf); | ||
414 | extern void usb_autopm_put_interface(struct usb_interface *intf); | ||
415 | |||
416 | #else | ||
417 | #define usb_autopm_get_interface(intf) 0 | ||
418 | #define usb_autopm_put_interface(intf) do {} while (0) | ||
419 | #endif | ||
420 | |||
421 | |||
395 | /*-------------------------------------------------------------------------*/ | 422 | /*-------------------------------------------------------------------------*/ |
396 | 423 | ||
397 | /* for drivers using iso endpoints */ | 424 | /* for drivers using iso endpoints */ |
@@ -593,6 +620,8 @@ struct usbdrv_wrap { | |||
593 | * @drvwrap: Driver-model core structure wrapper. | 620 | * @drvwrap: Driver-model core structure wrapper. |
594 | * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be | 621 | * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be |
595 | * added to this driver by preventing the sysfs file from being created. | 622 | * added to this driver by preventing the sysfs file from being created. |
623 | * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend | ||
624 | * for interfaces bound to this driver. | ||
596 | * | 625 | * |
597 | * USB interface drivers must provide a name, probe() and disconnect() | 626 | * USB interface drivers must provide a name, probe() and disconnect() |
598 | * methods, and an id_table. Other driver fields are optional. | 627 | * methods, and an id_table. Other driver fields are optional. |
@@ -631,6 +660,7 @@ struct usb_driver { | |||
631 | struct usb_dynids dynids; | 660 | struct usb_dynids dynids; |
632 | struct usbdrv_wrap drvwrap; | 661 | struct usbdrv_wrap drvwrap; |
633 | unsigned int no_dynamic_id:1; | 662 | unsigned int no_dynamic_id:1; |
663 | unsigned int supports_autosuspend:1; | ||
634 | }; | 664 | }; |
635 | #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) | 665 | #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) |
636 | 666 | ||
@@ -648,6 +678,8 @@ struct usb_driver { | |||
648 | * @suspend: Called when the device is going to be suspended by the system. | 678 | * @suspend: Called when the device is going to be suspended by the system. |
649 | * @resume: Called when the device is being resumed by the system. | 679 | * @resume: Called when the device is being resumed by the system. |
650 | * @drvwrap: Driver-model core structure wrapper. | 680 | * @drvwrap: Driver-model core structure wrapper. |
681 | * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend | ||
682 | * for devices bound to this driver. | ||
651 | * | 683 | * |
652 | * USB drivers must provide all the fields listed above except drvwrap. | 684 | * USB drivers must provide all the fields listed above except drvwrap. |
653 | */ | 685 | */ |
@@ -660,6 +692,7 @@ struct usb_device_driver { | |||
660 | int (*suspend) (struct usb_device *udev, pm_message_t message); | 692 | int (*suspend) (struct usb_device *udev, pm_message_t message); |
661 | int (*resume) (struct usb_device *udev); | 693 | int (*resume) (struct usb_device *udev); |
662 | struct usbdrv_wrap drvwrap; | 694 | struct usbdrv_wrap drvwrap; |
695 | unsigned int supports_autosuspend:1; | ||
663 | }; | 696 | }; |
664 | #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ | 697 | #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ |
665 | drvwrap.driver) | 698 | drvwrap.driver) |