treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / gadget / u_f.h
blobeaa13fd3dc7f36bd4f3953817037ff3cb818f463
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * u_f.h
5 * Utility definitions for USB functions
7 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
8 * http://www.samsung.com
10 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
13 #ifndef __U_F_H__
14 #define __U_F_H__
16 #include <linux/usb/gadget.h>
18 /* Variable Length Array Macros **********************************************/
19 #define vla_group(groupname) size_t groupname##__next = 0
20 #define vla_group_size(groupname) groupname##__next
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; \
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; \
41 #define vla_ptr(ptr, groupname, name) \
42 ((void *) ((char *)ptr + groupname##_##name##__offset))
44 struct usb_ep;
45 struct usb_request;
47 /**
48 * alloc_ep_req - returns a usb_request allocated by the gadget driver and
49 * allocates the request's buffer.
51 * @ep: the endpoint to allocate a usb_request
52 * @len: usb_requests's buffer suggested size
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().
59 struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
61 /* Frees a usb_request previously allocated by alloc_ep_req() */
62 static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
64 WARN_ON(req->buf == NULL);
65 kfree(req->buf);
66 req->buf = NULL;
67 usb_ep_free_request(ep, req);
70 #endif /* __U_F_H__ */