Skip to content

Commit 201c53c

Browse files
Kuen-Han Tsaigregkh
authored andcommitted
usb: gadget: Introduce free_usb_request helper
Introduce the free_usb_request() function that frees both the request's buffer and the request itself. This function serves as the cleanup callback for DEFINE_FREE() to enable automatic, scope-based cleanup for usb_request pointers. Signed-off-by: Kuen-Han Tsai <khtsai@google.com> Link: https://lore.kernel.org/r/20250916-ready-v1-2-4997bf277548@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250916-ready-v1-2-4997bf277548@google.com
1 parent bfb1d99 commit 201c53c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

include/linux/usb/gadget.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef __LINUX_USB_GADGET_H
1616
#define __LINUX_USB_GADGET_H
1717

18+
#include <linux/cleanup.h>
1819
#include <linux/configfs.h>
1920
#include <linux/device.h>
2021
#include <linux/errno.h>
@@ -293,6 +294,28 @@ static inline void usb_ep_fifo_flush(struct usb_ep *ep)
293294

294295
/*-------------------------------------------------------------------------*/
295296

297+
/**
298+
* free_usb_request - frees a usb_request object and its buffer
299+
* @req: the request being freed
300+
*
301+
* This helper function frees both the request's buffer and the request object
302+
* itself by calling usb_ep_free_request(). Its signature is designed to be used
303+
* with DEFINE_FREE() to enable automatic, scope-based cleanup for usb_request
304+
* pointers.
305+
*/
306+
static inline void free_usb_request(struct usb_request *req)
307+
{
308+
if (!req)
309+
return;
310+
311+
kfree(req->buf);
312+
usb_ep_free_request(req->ep, req);
313+
}
314+
315+
DEFINE_FREE(free_usb_request, struct usb_request *, free_usb_request(_T))
316+
317+
/*-------------------------------------------------------------------------*/
318+
296319
struct usb_dcd_config_params {
297320
__u8 bU1devExitLat; /* U1 Device exit Latency */
298321
#define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */

0 commit comments

Comments
 (0)