1 // SPDX-License-Identifier: GPL-2.0
5 * Utility definitions for USB functions
7 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
8 * http://www.samsung.com
10 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
16 #include <linux/usb/gadget.h>
17 #include <linux/overflow.h>
19 /* Variable Length Array Macros **********************************************/
20 #define vla_group(groupname) size_t groupname##__next = 0
21 #define vla_group_size(groupname) groupname##__next
23 #define vla_item(groupname, type, name, n) \
24 size_t groupname##_##name##__offset = ({ \
26 if (groupname##__next != SIZE_MAX) { \
27 size_t align_mask = __alignof__(type) - 1; \
28 size_t size = array_size(n, sizeof(type)); \
29 offset = (groupname##__next + align_mask) & \
31 if (check_add_overflow(offset, size, \
32 &groupname##__next)) { \
33 groupname##__next = SIZE_MAX; \
40 #define vla_item_with_sz(groupname, type, name, n) \
41 size_t groupname##_##name##__sz = array_size(n, sizeof(type)); \
42 size_t groupname##_##name##__offset = ({ \
44 if (groupname##__next != SIZE_MAX) { \
45 size_t align_mask = __alignof__(type) - 1; \
46 offset = (groupname##__next + align_mask) & \
48 if (check_add_overflow(offset, groupname##_##name##__sz,\
49 &groupname##__next)) { \
50 groupname##__next = SIZE_MAX; \
57 #define vla_ptr(ptr, groupname, name) \
58 ((void *) ((char *)ptr + groupname##_##name##__offset))
64 * alloc_ep_req - returns a usb_request allocated by the gadget driver and
65 * allocates the request's buffer.
67 * @ep: the endpoint to allocate a usb_request
68 * @len: usb_requests's buffer suggested size
70 * In case @ep direction is OUT, the @len will be aligned to ep's
71 * wMaxPacketSize. In order to avoid memory leaks or drops, *always* use
72 * usb_requests's length (req->length) to refer to the allocated buffer size.
73 * Requests allocated via alloc_ep_req() *must* be freed by free_ep_req().
75 struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
77 /* Frees a usb_request previously allocated by alloc_ep_req() */
78 static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
80 WARN_ON(req->buf == NULL);
83 usb_ep_free_request(ep, req);
86 #endif /* __U_F_H__ */