1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/slab.h>
15 #include "gdm_endian.h"
16 
gdm_set_endian(struct gdm_endian * ed,u8 dev_endian)17 void gdm_set_endian(struct gdm_endian *ed, u8 dev_endian)
18 {
19 	u8 a[2] = {0x12, 0x34};
20 	u8 b[2] = {0, };
21 	u16 c = 0x1234;
22 
23 	if (dev_endian == ENDIANNESS_BIG)
24 		ed->dev_ed = ENDIANNESS_BIG;
25 	else
26 		ed->dev_ed = ENDIANNESS_LITTLE;
27 
28 	memcpy(b, &c, 2);
29 
30 	if (a[0] != b[0])
31 		ed->host_ed = ENDIANNESS_LITTLE;
32 	else
33 		ed->host_ed = ENDIANNESS_BIG;
34 
35 }
36 
gdm_cpu_to_dev16(struct gdm_endian * ed,u16 x)37 u16 gdm_cpu_to_dev16(struct gdm_endian *ed, u16 x)
38 {
39 	if (ed->dev_ed == ed->host_ed)
40 		return x;
41 
42 	return Endian16_Swap(x);
43 }
44 
gdm_dev16_to_cpu(struct gdm_endian * ed,u16 x)45 u16 gdm_dev16_to_cpu(struct gdm_endian *ed, u16 x)
46 {
47 	if (ed->dev_ed == ed->host_ed)
48 		return x;
49 
50 	return Endian16_Swap(x);
51 }
52 
gdm_cpu_to_dev32(struct gdm_endian * ed,u32 x)53 u32 gdm_cpu_to_dev32(struct gdm_endian *ed, u32 x)
54 {
55 	if (ed->dev_ed == ed->host_ed)
56 		return x;
57 
58 	return Endian32_Swap(x);
59 }
60 
gdm_dev32_to_cpu(struct gdm_endian * ed,u32 x)61 u32 gdm_dev32_to_cpu(struct gdm_endian *ed, u32 x)
62 {
63 	if (ed->dev_ed == ed->host_ed)
64 		return x;
65 
66 	return Endian32_Swap(x);
67 }
68