blob: 947b7ddff6916fc89917d382326e87f64a733335 [file] [log] [blame]
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001/*
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
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020038static inline u8 *__usb_ext_prop_ptr(u8 *buf, size_t offset)
39{
40 return buf + offset;
41}
42
43static inline u8 *usb_ext_prop_size_ptr(u8 *buf)
44{
45 return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_SIZE);
46}
47
48static inline u8 *usb_ext_prop_type_ptr(u8 *buf)
49{
50 return __usb_ext_prop_ptr(buf, USB_EXT_PROP_DW_PROPERTY_DATA_TYPE);
51}
52
53static inline u8 *usb_ext_prop_name_len_ptr(u8 *buf)
54{
55 return __usb_ext_prop_ptr(buf, USB_EXT_PROP_W_PROPERTY_NAME_LENGTH);
56}
57
58static inline u8 *usb_ext_prop_name_ptr(u8 *buf)
59{
60 return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_NAME);
61}
62
63static inline u8 *usb_ext_prop_data_len_ptr(u8 *buf, size_t off)
64{
65 return __usb_ext_prop_ptr(buf,
66 USB_EXT_PROP_DW_PROPERTY_DATA_LENGTH + off);
67}
68
69static inline u8 *usb_ext_prop_data_ptr(u8 *buf, size_t off)
70{
71 return __usb_ext_prop_ptr(buf, USB_EXT_PROP_B_PROPERTY_DATA + off);
72}
73
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020074static inline void usb_ext_prop_put_size(u8 *buf, int dw_size)
75{
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020076 put_unaligned_le32(dw_size, usb_ext_prop_size_ptr(buf));
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020077}
78
79static inline void usb_ext_prop_put_type(u8 *buf, int type)
80{
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020081 put_unaligned_le32(type, usb_ext_prop_type_ptr(buf));
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020082}
83
84static inline int usb_ext_prop_put_name(u8 *buf, const char *name, int pnl)
85{
86 int result;
87
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020088 put_unaligned_le16(pnl, usb_ext_prop_name_len_ptr(buf));
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020089 result = utf8s_to_utf16s(name, strlen(name), UTF16_LITTLE_ENDIAN,
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020090 (wchar_t *) usb_ext_prop_name_ptr(buf), pnl - 2);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020091 if (result < 0)
92 return result;
93
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +020094 put_unaligned_le16(0, &buf[USB_EXT_PROP_B_PROPERTY_NAME + pnl - 2]);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020095
96 return pnl;
97}
98
99static inline void usb_ext_prop_put_binary(u8 *buf, int pnl, const u8 *data,
100 int data_len)
101{
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +0200102 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);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200104}
105
106static inline int usb_ext_prop_put_unicode(u8 *buf, int pnl, const char *string,
107 int data_len)
108{
109 int result;
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +0200110 put_unaligned_le32(data_len, usb_ext_prop_data_len_ptr(buf, pnl));
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200111 result = utf8s_to_utf16s(string, data_len >> 1, UTF16_LITTLE_ENDIAN,
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +0200112 (wchar_t *) usb_ext_prop_data_ptr(buf, pnl),
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200113 data_len - 2);
114 if (result < 0)
115 return result;
116
117 put_unaligned_le16(0,
Andrzej Pietrasiewicz7ea4f082014-07-09 12:20:07 +0200118 &buf[USB_EXT_PROP_B_PROPERTY_DATA + pnl + data_len - 2]);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200119
120 return data_len;
121}
122
123#endif /* __U_OS_DESC_H__ */