1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * u_f.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 __U_F_H__
14 #define __U_F_H__
15
16 #include <linux/usb/gadget.h>
17
18 /* Variable Length Array Macros **********************************************/
19 #define vla_group(groupname) size_t groupname##__next = 0
20 #define vla_group_size(groupname) groupname##__next
21
22 #define vla_item(groupname, type, name, n) \
23 size_t groupname##_##name##__offset = ({ \
24 size_t align_mask = __alignof__(type) - 1; \
25 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
26 size_t size = (n) * sizeof(type); \
27 groupname##__next = offset + size; \
28 offset; \
29 })
30
31 #define vla_item_with_sz(groupname, type, name, n) \
32 size_t groupname##_##name##__sz = (n) * sizeof(type); \
33 size_t groupname##_##name##__offset = ({ \
34 size_t align_mask = __alignof__(type) - 1; \
35 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
36 size_t size = groupname##_##name##__sz; \
37 groupname##__next = offset + size; \
38 offset; \
39 })
40
41 #define vla_ptr(ptr, groupname, name) \
42 ((void *) ((char *)ptr + groupname##_##name##__offset))
43
44 struct usb_ep;
45 struct usb_request;
46
47 /**
48 * alloc_ep_req - returns a usb_request allocated by the gadget driver and
49 * allocates the request's buffer.
50 *
51 * @ep: the endpoint to allocate a usb_request
52 * @len: usb_requests's buffer suggested size
53 *
54 * In case @ep direction is OUT, the @len will be aligned to ep's
55 * wMaxPacketSize. In order to avoid memory leaks or drops, *always* use
56 * usb_requests's length (req->length) to refer to the allocated buffer size.
57 * Requests allocated via alloc_ep_req() *must* be freed by free_ep_req().
58 */
59 struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
60
61 /* Frees a usb_request previously allocated by alloc_ep_req() */
free_ep_req(struct usb_ep * ep,struct usb_request * req)62 static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
63 {
64 WARN_ON(req->buf == NULL);
65 kfree(req->buf);
66 req->buf = NULL;
67 usb_ep_free_request(ep, req);
68 }
69
70 #endif /* __U_F_H__ */
71