~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/usb/func_utils.h

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * func_utils.h
  4  *
  5  * Utility definitions for USB functions
  6  *
  7  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  8  *              http://www.samsung.com
  9  *
 10  * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
 11  */
 12 
 13 #ifndef _FUNC_UTILS_H_
 14 #define _FUNC_UTILS_H_
 15 
 16 #include <linux/usb/gadget.h>
 17 #include <linux/overflow.h>
 18 
 19 /* Variable Length Array Macros **********************************************/
 20 #define vla_group(groupname) size_t groupname##__next = 0
 21 #define vla_group_size(groupname) groupname##__next
 22 
 23 #define vla_item(groupname, type, name, n) \
 24         size_t groupname##_##name##__offset = ({                               \
 25                 size_t offset = 0;                                             \
 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) &            \
 30                                   ~align_mask;                                 \
 31                         if (check_add_overflow(offset, size,                   \
 32                                                &groupname##__next)) {          \
 33                                 groupname##__next = SIZE_MAX;                  \
 34                                 offset = 0;                                    \
 35                         }                                                      \
 36                 }                                                              \
 37                 offset;                                                        \
 38         })
 39 
 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 = ({                                \
 43                 size_t offset = 0;                                              \
 44                 if (groupname##__next != SIZE_MAX) {                            \
 45                         size_t align_mask = __alignof__(type) - 1;              \
 46                         offset = (groupname##__next + align_mask) &             \
 47                                   ~align_mask;                                  \
 48                         if (check_add_overflow(offset, groupname##_##name##__sz,\
 49                                                         &groupname##__next)) {  \
 50                                 groupname##__next = SIZE_MAX;                   \
 51                                 offset = 0;                                     \
 52                         }                                                       \
 53                 }                                                               \
 54                 offset;                                                         \
 55         })
 56 
 57 #define vla_ptr(ptr, groupname, name) \
 58         ((void *) ((char *)ptr + groupname##_##name##__offset))
 59 
 60 struct usb_ep;
 61 struct usb_request;
 62 
 63 /**
 64  * alloc_ep_req - returns a usb_request allocated by the gadget driver and
 65  * allocates the request's buffer.
 66  *
 67  * @ep: the endpoint to allocate a usb_request
 68  * @len: usb_requests's buffer suggested size
 69  *
 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().
 74  */
 75 struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
 76 
 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)
 79 {
 80         WARN_ON(req->buf == NULL);
 81         kfree(req->buf);
 82         req->buf = NULL;
 83         usb_ep_free_request(ep, req);
 84 }
 85 
 86 #endif /* _FUNC_UTILS_H_ */
 87 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php