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