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