Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * u_f.h |
| 3 | * |
| 4 | * Utility definitions for USB functions |
| 5 | * |
| 6 | * Copyright (c) 2013 Samsung Electronics Co., Ltd. |
| 7 | * http://www.samsung.com |
| 8 | * |
| 9 | * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __U_F_H__ |
| 17 | #define __U_F_H__ |
| 18 | |
Felipe F. Tonello | 079fe5a | 2015-11-10 17:52:05 +0000 | [diff] [blame] | 19 | #include <linux/usb/gadget.h> |
| 20 | |
Andrzej Pietrasiewicz | 74d4846 | 2014-05-08 14:06:21 +0200 | [diff] [blame] | 21 | /* Variable Length Array Macros **********************************************/ |
| 22 | #define vla_group(groupname) size_t groupname##__next = 0 |
| 23 | #define vla_group_size(groupname) groupname##__next |
| 24 | |
| 25 | #define vla_item(groupname, type, name, n) \ |
| 26 | size_t groupname##_##name##__offset = ({ \ |
| 27 | size_t align_mask = __alignof__(type) - 1; \ |
| 28 | size_t offset = (groupname##__next + align_mask) & ~align_mask;\ |
| 29 | size_t size = (n) * sizeof(type); \ |
| 30 | groupname##__next = offset + size; \ |
| 31 | offset; \ |
| 32 | }) |
| 33 | |
| 34 | #define vla_item_with_sz(groupname, type, name, n) \ |
| 35 | size_t groupname##_##name##__sz = (n) * sizeof(type); \ |
| 36 | size_t groupname##_##name##__offset = ({ \ |
| 37 | size_t align_mask = __alignof__(type) - 1; \ |
| 38 | size_t offset = (groupname##__next + align_mask) & ~align_mask;\ |
| 39 | size_t size = groupname##_##name##__sz; \ |
| 40 | groupname##__next = offset + size; \ |
| 41 | offset; \ |
| 42 | }) |
| 43 | |
| 44 | #define vla_ptr(ptr, groupname, name) \ |
| 45 | ((void *) ((char *)ptr + groupname##_##name##__offset)) |
| 46 | |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 47 | struct usb_ep; |
| 48 | struct usb_request; |
| 49 | |
Felipe F. Tonello | e046615 | 2016-08-08 21:30:06 +0100 | [diff] [blame] | 50 | /** |
| 51 | * alloc_ep_req - returns a usb_request allocated by the gadget driver and |
| 52 | * allocates the request's buffer. |
| 53 | * |
| 54 | * @ep: the endpoint to allocate a usb_request |
| 55 | * @len: usb_requests's buffer suggested size |
Felipe F. Tonello | e046615 | 2016-08-08 21:30:06 +0100 | [diff] [blame] | 56 | * |
| 57 | * In case @ep direction is OUT, the @len will be aligned to ep's |
| 58 | * wMaxPacketSize. In order to avoid memory leaks or drops, *always* use |
| 59 | * usb_requests's length (req->length) to refer to the allocated buffer size. |
| 60 | * Requests allocated via alloc_ep_req() *must* be freed by free_ep_req(). |
| 61 | */ |
Felipe F. Tonello | aadbe81 | 2016-08-23 18:24:49 +0100 | [diff] [blame] | 62 | struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len); |
Felipe F. Tonello | e046615 | 2016-08-08 21:30:06 +0100 | [diff] [blame] | 63 | |
| 64 | /* Frees a usb_request previously allocated by alloc_ep_req() */ |
Felipe F. Tonello | 079fe5a | 2015-11-10 17:52:05 +0000 | [diff] [blame] | 65 | static inline void free_ep_req(struct usb_ep *ep, struct usb_request *req) |
| 66 | { |
Yavuz, Tuba | b3b0809 | 2018-03-23 17:00:38 +0000 | [diff] [blame] | 67 | WARN_ON(req->buf == NULL); |
Felipe F. Tonello | 079fe5a | 2015-11-10 17:52:05 +0000 | [diff] [blame] | 68 | kfree(req->buf); |
Yavuz, Tuba | b3b0809 | 2018-03-23 17:00:38 +0000 | [diff] [blame] | 69 | req->buf = NULL; |
Felipe F. Tonello | 079fe5a | 2015-11-10 17:52:05 +0000 | [diff] [blame] | 70 | usb_ep_free_request(ep, req); |
| 71 | } |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 72 | |
| 73 | #endif /* __U_F_H__ */ |