u_f.h 2.2 KB

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