History

usb: gadget: function: move u_f.h to include/linux/usb/func_utils.h

We move the func_utils.h header to include/linux/usb to be able to compile function drivers outside of the drivers/usb/gadget/function directory.

Michael Grzeschik committed 2 years ago

Commit d9c61bb1 parent f20998f9 files · +12 −12Browse files

Showing the changes to include/linux/usb (1 of 9 files). Show all

Unified · Split

include/linux/usb/func_utils.h+86−0
@@ -0,0 +1,86 @@
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_ */