drivers/usb/core/offload.c

Source file repositories/reference/linux-study-clean/drivers/usb/core/offload.c

File Facts

System
Linux kernel
Corpus path
drivers/usb/core/offload.c
Extension
.c
Size
3726 bytes
Lines
153
Domain
Driver Families
Bucket
drivers/usb
Inferred role
Driver Families: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// 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 = 0;

	if (!usb_get_dev(udev))
		return -ENODEV;

	if (pm_runtime_get_if_active(&udev->dev) != 1) {
		ret = -EBUSY;
		goto err_rpm;
	}

	spin_lock(&udev->offload_lock);

	if (udev->offload_pm_locked) {
		ret = -EAGAIN;
		goto err;
	}

	udev->offload_usage++;

err:
	spin_unlock(&udev->offload_lock);
	pm_runtime_put_autosuspend(&udev->dev);
err_rpm:
	usb_put_dev(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 = 0;

	if (!usb_get_dev(udev))
		return -ENODEV;

	if (pm_runtime_get_if_active(&udev->dev) != 1) {
		ret = -EBUSY;
		goto err_rpm;
	}

	spin_lock(&udev->offload_lock);

	if (udev->offload_pm_locked) {
		ret = -EAGAIN;
		goto err;
	}

	/* Drop the count when it wasn't 0, ignore the operation otherwise. */
	if (udev->offload_usage)
		udev->offload_usage--;

err:

Annotation

Implementation Notes