^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * u_f.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Utility definitions for USB functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2013 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * http://www.samsung.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #ifndef __U_F_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define __U_F_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/overflow.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Variable Length Array Macros **********************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define vla_group(groupname) size_t groupname##__next = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define vla_group_size(groupname) groupname##__next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define vla_item(groupname, type, name, n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) size_t groupname##_##name##__offset = ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) size_t offset = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (groupname##__next != SIZE_MAX) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) size_t align_mask = __alignof__(type) - 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) size_t size = array_size(n, sizeof(type)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) offset = (groupname##__next + align_mask) & \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ~align_mask; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (check_add_overflow(offset, size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) &groupname##__next)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) groupname##__next = SIZE_MAX; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) offset = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) offset; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define vla_item_with_sz(groupname, type, name, n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) size_t groupname##_##name##__sz = array_size(n, sizeof(type)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) size_t groupname##_##name##__offset = ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) size_t offset = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (groupname##__next != SIZE_MAX) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) size_t align_mask = __alignof__(type) - 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) offset = (groupname##__next + align_mask) & \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ~align_mask; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (check_add_overflow(offset, groupname##_##name##__sz,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) &groupname##__next)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) groupname##__next = SIZE_MAX; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) offset = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) offset; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define vla_ptr(ptr, groupname, name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ((void *) ((char *)ptr + groupname##_##name##__offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct usb_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct usb_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * alloc_ep_req - returns a usb_request allocated by the gadget driver and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * allocates the request's buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @ep: the endpoint to allocate a usb_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @len: usb_requests's buffer suggested size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * In case @ep direction is OUT, the @len will be aligned to ep's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * wMaxPacketSize. In order to avoid memory leaks or drops, *always* use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * usb_requests's length (req->length) to refer to the allocated buffer size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Requests allocated via alloc_ep_req() *must* be freed by free_ep_req().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Frees a usb_request previously allocated by alloc_ep_req() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) WARN_ON(req->buf == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree(req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) req->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) usb_ep_free_request(ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #endif /* __U_F_H__ */