blob: eaa13fd3dc7f36bd4f3953817037ff3cb818f463 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +01002/*
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 *
Andrzej Pietrasiewicz1b4a3b52018-12-13 14:24:57 +010010 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010011 */
12
13#ifndef __U_F_H__
14#define __U_F_H__
15
Felipe F. Tonello079fe5a2015-11-10 17:52:05 +000016#include <linux/usb/gadget.h>
17
Andrzej Pietrasiewicz74d48462014-05-08 14:06:21 +020018/* 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
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010044struct usb_ep;
45struct usb_request;
46
Felipe F. Tonelloe0466152016-08-08 21:30:06 +010047/**
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
Felipe F. Tonelloe0466152016-08-08 21:30:06 +010053 *
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 */
Felipe F. Tonelloaadbe812016-08-23 18:24:49 +010059struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len);
Felipe F. Tonelloe0466152016-08-08 21:30:06 +010060
61/* Frees a usb_request previously allocated by alloc_ep_req() */
Felipe F. Tonello079fe5a2015-11-10 17:52:05 +000062static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req)
63{
Yavuz, Tuba7fafcfd2018-03-23 17:00:38 +000064 WARN_ON(req->buf == NULL);
Felipe F. Tonello079fe5a2015-11-10 17:52:05 +000065 kfree(req->buf);
Yavuz, Tuba7fafcfd2018-03-23 17:00:38 +000066 req->buf = NULL;
Felipe F. Tonello079fe5a2015-11-10 17:52:05 +000067 usb_ep_free_request(ep, req);
68}
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010069
70#endif /* __U_F_H__ */