Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * u_os_desc.h |
| 3 | * |
| 4 | * Utility definitions for "OS Descriptors" support |
| 5 | * |
| 6 | * Copyright (c) 2014 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_OS_DESC_H__ |
| 17 | #define __U_OS_DESC_H__ |
| 18 | |
| 19 | #include <linux/utf.h> |
| 20 | |
| 21 | #define USB_EXT_PROP_DW_SIZE 0 |
| 22 | #define USB_EXT_PROP_DW_PROPERTY_DATA_TYPE 4 |
| 23 | #define USB_EXT_PROP_W_PROPERTY_NAME_LENGTH 8 |
| 24 | #define USB_EXT_PROP_B_PROPERTY_NAME 10 |
| 25 | #define USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH 10 |
| 26 | #define USB_EXT_PROP_B_PROPERTY_DATA 14 |
| 27 | |
| 28 | #define USB_EXT_PROP_RESERVED 0 |
| 29 | #define USB_EXT_PROP_UNICODE 1 |
| 30 | #define USB_EXT_PROP_UNICODE_ENV 2 |
| 31 | #define USB_EXT_PROP_BINARY 3 |
| 32 | #define USB_EXT_PROP_LE32 4 |
| 33 | #define USB_EXT_PROP_BE32 5 |
| 34 | #define USB_EXT_PROP_UNICODE_LINK 6 |
| 35 | #define USB_EXT_PROP_UNICODE_MULTI 7 |
| 36 | |
| 37 | static inline u8 *__usb_ext_prop_ptr(u8 *buf, size_t offset) |
| 38 | { |
| 39 | return buf + offset; |
| 40 | } |
| 41 | |
| 42 | static inline u8 *usb_ext_prop_size_ptr(u8 *buf) |
| 43 | { |
| 44 | return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_SIZE); |
| 45 | } |
| 46 | |
| 47 | static inline u8 *usb_ext_prop_type_ptr(u8 *buf) |
| 48 | { |
| 49 | return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_PROPERTY_DATA_TYPE); |
| 50 | } |
| 51 | |
| 52 | static inline u8 *usb_ext_prop_name_len_ptr(u8 *buf) |
| 53 | { |
| 54 | return __usb_ext_prop_ptr(buf, USB_EXT_PROP_W_PROPERTY_NAME_LENGTH); |
| 55 | } |
| 56 | |
| 57 | static inline u8 *usb_ext_prop_name_ptr(u8 *buf) |
| 58 | { |
| 59 | return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_NAME); |
| 60 | } |
| 61 | |
| 62 | static inline u8 *usb_ext_prop_data_len_ptr(u8 *buf, size_t off) |
| 63 | { |
| 64 | return __usb_ext_prop_ptr(buf, |
| 65 | USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH + off); |
| 66 | } |
| 67 | |
| 68 | static inline u8 *usb_ext_prop_data_ptr(u8 *buf, size_t off) |
| 69 | { |
| 70 | return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_DATA + off); |
| 71 | } |
| 72 | |
| 73 | static inline void usb_ext_prop_put_size(u8 *buf, int dw_size) |
| 74 | { |
| 75 | put_unaligned_le32(dw_size, usb_ext_prop_size_ptr(buf)); |
| 76 | } |
| 77 | |
| 78 | static inline void usb_ext_prop_put_type(u8 *buf, int type) |
| 79 | { |
| 80 | put_unaligned_le32(type, usb_ext_prop_type_ptr(buf)); |
| 81 | } |
| 82 | |
| 83 | static inline int usb_ext_prop_put_name(u8 *buf, const char *name, int pnl) |
| 84 | { |
| 85 | int result; |
| 86 | |
| 87 | put_unaligned_le16(pnl, usb_ext_prop_name_len_ptr(buf)); |
| 88 | memset(usb_ext_prop_name_ptr(buf), 0, 2 * strlen(name)); |
| 89 | result = utf8_to_utf16le(name, (__le16 *)usb_ext_prop_name_ptr(buf), |
| 90 | strlen(name)); |
| 91 | if (result < 0) |
| 92 | return result; |
| 93 | |
| 94 | put_unaligned_le16(0, &buf[USB_EXT_PROP_B_PROPERTY_NAME + pnl - 2]); |
| 95 | |
| 96 | return pnl; |
| 97 | } |
| 98 | |
| 99 | static inline void usb_ext_prop_put_binary(u8 *buf, int pnl, const char *data, |
| 100 | int data_len) |
| 101 | { |
| 102 | put_unaligned_le32(data_len, usb_ext_prop_data_len_ptr(buf, pnl)); |
| 103 | memcpy(usb_ext_prop_data_ptr(buf, pnl), data, data_len); |
| 104 | } |
| 105 | |
| 106 | static inline int usb_ext_prop_put_unicode(u8 *buf, int pnl, const char *string, |
| 107 | int data_len) |
| 108 | { |
| 109 | int result; |
| 110 | put_unaligned_le32(data_len, usb_ext_prop_data_len_ptr(buf, pnl)); |
| 111 | memset(usb_ext_prop_data_ptr(buf, pnl), 0, 2 * (data_len >> 1)); |
| 112 | result = utf8_to_utf16le(string, (__le16 *) usb_ext_prop_data_ptr(buf, pnl), |
| 113 | data_len >> 1); |
| 114 | if (result < 0) |
| 115 | return result; |
| 116 | |
| 117 | put_unaligned_le16(0, |
| 118 | &buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl + data_len - 2]); |
| 119 | |
| 120 | return data_len; |
| 121 | } |
| 122 | |
| 123 | #endif /* __U_OS_DESC_H__ */ |