u_os_desc.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * u_os_desc.h
  4. *
  5. * Utility definitions for "OS Descriptors" support
  6. *
  7. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  8. * http://www.samsung.com
  9. *
  10. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  11. */
  12. #ifndef __U_OS_DESC_H__
  13. #define __U_OS_DESC_H__
  14. #include <asm/unaligned.h>
  15. #include <linux/nls.h>
  16. #define USB_EXT_PROP_DW_SIZE 0
  17. #define USB_EXT_PROP_DW_PROPERTY_DATA_TYPE 4
  18. #define USB_EXT_PROP_W_PROPERTY_NAME_LENGTH 8
  19. #define USB_EXT_PROP_B_PROPERTY_NAME 10
  20. #define USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH 10
  21. #define USB_EXT_PROP_B_PROPERTY_DATA 14
  22. #define USB_EXT_PROP_RESERVED 0
  23. #define USB_EXT_PROP_UNICODE 1
  24. #define USB_EXT_PROP_UNICODE_ENV 2
  25. #define USB_EXT_PROP_BINARY 3
  26. #define USB_EXT_PROP_LE32 4
  27. #define USB_EXT_PROP_BE32 5
  28. #define USB_EXT_PROP_UNICODE_LINK 6
  29. #define USB_EXT_PROP_UNICODE_MULTI 7
  30. static inline u8 *__usb_ext_prop_ptr(u8 *buf, size_t offset)
  31. {
  32. return buf + offset;
  33. }
  34. static inline u8 *usb_ext_prop_size_ptr(u8 *buf)
  35. {
  36. return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_SIZE);
  37. }
  38. static inline u8 *usb_ext_prop_type_ptr(u8 *buf)
  39. {
  40. return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_PROPERTY_DATA_TYPE);
  41. }
  42. static inline u8 *usb_ext_prop_name_len_ptr(u8 *buf)
  43. {
  44. return __usb_ext_prop_ptr(buf, USB_EXT_PROP_W_PROPERTY_NAME_LENGTH);
  45. }
  46. static inline u8 *usb_ext_prop_name_ptr(u8 *buf)
  47. {
  48. return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_NAME);
  49. }
  50. static inline u8 *usb_ext_prop_data_len_ptr(u8 *buf, size_t off)
  51. {
  52. return __usb_ext_prop_ptr(buf,
  53. USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH + off);
  54. }
  55. static inline u8 *usb_ext_prop_data_ptr(u8 *buf, size_t off)
  56. {
  57. return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_DATA + off);
  58. }
  59. static inline void usb_ext_prop_put_size(u8 *buf, int dw_size)
  60. {
  61. put_unaligned_le32(dw_size, usb_ext_prop_size_ptr(buf));
  62. }
  63. static inline void usb_ext_prop_put_type(u8 *buf, int type)
  64. {
  65. put_unaligned_le32(type, usb_ext_prop_type_ptr(buf));
  66. }
  67. static inline int usb_ext_prop_put_name(u8 *buf, const char *name, int pnl)
  68. {
  69. int result;
  70. put_unaligned_le16(pnl, usb_ext_prop_name_len_ptr(buf));
  71. result = utf8s_to_utf16s(name, strlen(name), UTF16_LITTLE_ENDIAN,
  72. (wchar_t *) usb_ext_prop_name_ptr(buf), pnl - 2);
  73. if (result < 0)
  74. return result;
  75. put_unaligned_le16(0, &buf[USB_EXT_PROP_B_PROPERTY_NAME + pnl - 2]);
  76. return pnl;
  77. }
  78. static inline void usb_ext_prop_put_binary(u8 *buf, int pnl, const u8 *data,
  79. int data_len)
  80. {
  81. put_unaligned_le32(data_len, usb_ext_prop_data_len_ptr(buf, pnl));
  82. memcpy(usb_ext_prop_data_ptr(buf, pnl), data, data_len);
  83. }
  84. static inline int usb_ext_prop_put_unicode(u8 *buf, int pnl, const char *string,
  85. int data_len)
  86. {
  87. int result;
  88. put_unaligned_le32(data_len, usb_ext_prop_data_len_ptr(buf, pnl));
  89. result = utf8s_to_utf16s(string, data_len >> 1, UTF16_LITTLE_ENDIAN,
  90. (wchar_t *) usb_ext_prop_data_ptr(buf, pnl),
  91. data_len - 2);
  92. if (result < 0)
  93. return result;
  94. put_unaligned_le16(0,
  95. &buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl + data_len - 2]);
  96. return data_len;
  97. }
  98. #endif /* __U_OS_DESC_H__ */