Merge branch 'next' into for-linus
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / u_os_desc.h
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 <asm/unaligned.h>
20 #include <linux/nls.h>
21
22 #define USB_EXT_PROP_DW_SIZE                    0
23 #define USB_EXT_PROP_DW_PROPERTY_DATA_TYPE      4
24 #define USB_EXT_PROP_W_PROPERTY_NAME_LENGTH     8
25 #define USB_EXT_PROP_B_PROPERTY_NAME            10
26 #define USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH    10
27 #define USB_EXT_PROP_B_PROPERTY_DATA            14
28
29 #define USB_EXT_PROP_RESERVED                   0
30 #define USB_EXT_PROP_UNICODE                    1
31 #define USB_EXT_PROP_UNICODE_ENV                2
32 #define USB_EXT_PROP_BINARY                     3
33 #define USB_EXT_PROP_LE32                       4
34 #define USB_EXT_PROP_BE32                       5
35 #define USB_EXT_PROP_UNICODE_LINK               6
36 #define USB_EXT_PROP_UNICODE_MULTI              7
37
38 static inline void usb_ext_prop_put_size(u8 *buf, int dw_size)
39 {
40         put_unaligned_le32(dw_size, &buf[USB_EXT_PROP_DW_SIZE]);
41 }
42
43 static inline void usb_ext_prop_put_type(u8 *buf, int type)
44 {
45         put_unaligned_le32(type, &buf[USB_EXT_PROP_DW_PROPERTY_DATA_TYPE]);
46 }
47
48 static inline int usb_ext_prop_put_name(u8 *buf, const char *name, int pnl)
49 {
50         int result;
51
52         put_unaligned_le16(pnl, &buf[USB_EXT_PROP_W_PROPERTY_NAME_LENGTH]);
53         result = utf8s_to_utf16s(name, strlen(name), UTF16_LITTLE_ENDIAN,
54                 (wchar_t *) &buf[USB_EXT_PROP_B_PROPERTY_NAME], pnl - 2);
55         if (result < 0)
56                 return result;
57
58         put_unaligned_le16(0, &buf[USB_EXT_PROP_B_PROPERTY_NAME + pnl]);
59
60         return pnl;
61 }
62
63 static inline void usb_ext_prop_put_binary(u8 *buf, int pnl, const u8 *data,
64                                            int data_len)
65 {
66         put_unaligned_le32(data_len,
67                            &buf[USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH + pnl]);
68         memcpy(&buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl], data, data_len);
69 }
70
71 static inline int usb_ext_prop_put_unicode(u8 *buf, int pnl, const char *string,
72                                            int data_len)
73 {
74         int result;
75         put_unaligned_le32(data_len,
76                         &buf[USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH + pnl]);
77
78         result = utf8s_to_utf16s(string, data_len >> 1, UTF16_LITTLE_ENDIAN,
79                         (wchar_t *) &buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl],
80                         data_len - 2);
81         if (result < 0)
82                 return result;
83
84         put_unaligned_le16(0,
85                         &buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl + data_len]);
86
87         return data_len;
88 }
89
90 #endif /* __U_OS_DESC_H__ */