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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// SPDX-License-Identifier: GPL-2.0
/*
* offload.c - USB offload related functions
*
* Copyright (c) 2025, Google LLC.
*
* Author: Guan-Yu Lin
*/
#include <linux/usb.h>
#include "usb.h"
/**
* usb_offload_get - increment the offload_usage of a USB device
* @udev: the USB device to increment its offload_usage
*
* Incrementing the offload_usage of a usb_device indicates that offload is
* enabled on this usb_device; that is, another entity is actively handling USB
* transfers. This information allows the USB driver to adjust its power
* management policy based on offload activity.
*
* Return: 0 on success. A negative error code otherwise.
*/
int usb_offload_get(struct usb_device *udev)
{
int ret;
usb_lock_device(udev);
if (udev->state == USB_STATE_NOTATTACHED) {
usb_unlock_device(udev);
return -ENODEV;
}
if (udev->state == USB_STATE_SUSPENDED ||
udev->offload_at_suspend) {
usb_unlock_device(udev);
return -EBUSY;
}
/*
* offload_usage could only be modified when the device is active, since
* it will alter the suspend flow of the device.
*/
ret = usb_autoresume_device(udev);
if (ret < 0) {
usb_unlock_device(udev);
return ret;
}
udev->offload_usage++;
usb_autosuspend_device(udev);
usb_unlock_device(udev);
return ret;
}
EXPORT_SYMBOL_GPL(usb_offload_get);
/**
* usb_offload_put - drop the offload_usage of a USB device
* @udev: the USB device to drop its offload_usage
*
* The inverse operation of usb_offload_get, which drops the offload_usage of
* a USB device. This information allows the USB driver to adjust its power
* management policy based on offload activity.
*
* Return: 0 on success. A negative error code otherwise.
*/
int usb_offload_put(struct usb_device *udev)
{
int ret;
usb_lock_device(udev);
if (udev->state == USB_STATE_NOTATTACHED) {
usb_unlock_device(udev);
return -ENODEV;
}
if (udev->state == USB_STATE_SUSPENDED ||
udev->offload_at_suspend) {
usb_unlock_device(udev);
return -EBUSY;
}
/*
* offload_usage could only be modified when the device is active, since
* it will alter the suspend flow of the device.
*/
ret = usb_autoresume_device(udev);
if (ret < 0) {
usb_unlock_device(udev);
return ret;
}
/* Drop the count when it wasn't 0, ignore the operation otherwise. */
if (udev->offload_usage)
udev->offload_usage--;
usb_autosuspend_device(udev);
usb_unlock_device(udev);
return ret;
}
EXPORT_SYMBOL_GPL(usb_offload_put);
/**
* usb_offload_check - check offload activities on a USB device
* @udev: the USB device to check its offload activity.
*
* Check if there are any offload activity on the USB device right now. This
* information could be used for power management or other forms of resource
* management.
*
* The caller must hold @udev's device lock. In addition, the caller should
* ensure downstream usb devices are all either suspended or marked as
* "offload_at_suspend" to ensure the correctness of the return value.
*
* Returns true on any offload activity, false otherwise.
*/
bool usb_offload_check(struct usb_device *udev) __must_hold(&udev->dev->mutex)
{
struct usb_device *child;
bool active;
int port1;
usb_hub_for_each_child(udev, port1, child) {
usb_lock_device(child);
active = usb_offload_check(child);
usb_unlock_device(child);
if (active)
return true;
}
return !!udev->offload_usage;
}
EXPORT_SYMBOL_GPL(usb_offload_check);
|