nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / smbus / atmel_mxt / obp-utils.c
1 /*
2  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *------------------------------------------------------------------------------
35  * Copyright 2011 Atmel Corporation. All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions are met:
39  *
40  *    1. Redistributions of source code must retain the above copyright notice,
41  *    this list of conditions and the following disclaimer.
42  *
43  *    2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY ATMEL ''AS IS'' AND ANY EXPRESS OR IMPLIED
48  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
50  * EVENT SHALL ATMEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
53  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  *-----------------------------------------------------------------------------
58  */
59 /*
60  * This file contains a mix of code taken from the atmel-maxtouch/obp-utils
61  * github codebase and my own for testing the atmel touchscreen chipset in
62  * the Acer C720P chromebook.
63  *
64  * This code is independent and is used by both kernel AND user mode programs.
65  */
66
67 #include <sys/types.h>
68
69 #include "obp-utils.h"
70
71 /*!
72  * @brief Information block checksum return function.
73  * @return 24-bit checksum value in 32-bit integer.
74  */
75 uint32_t
76 obp_convert_crc(struct mxt_raw_crc *crc)
77 {
78         return ((crc->CRC_hi<<16u) | (crc->CRC));
79 }
80
81 /*!
82  * @brief  Information Block Checksum algorithm.
83  * @return Calculated Information Block Checksum.
84  */
85 static
86 uint32_t
87 crc24_2byte(uint32_t crc, uint8_t firstbyte, uint8_t secondbyte)
88 {
89         static const uint32_t CRCPOLY = 0x0080001B;
90         uint32_t result;
91         uint16_t data_word;
92
93         data_word = (uint16_t) ((uint16_t)(secondbyte << 8u) | firstbyte);
94         result = ((crc << 1u) ^ (uint32_t)data_word);
95
96         /* Check if 25th bit is set, and XOR the result to create
97          * 24-bit checksum */
98         if (result & 0x1000000) {
99                 result ^= CRCPOLY;
100         }
101         return result;
102 }
103
104 /*
105  * Return 24-bit crc.
106  */
107 uint32_t
108 obp_crc24(uint8_t *buf, size_t bytes)
109 {
110         uint32_t crc;
111         size_t i;
112
113         crc = 0;
114         for (i = 0; i < (bytes & ~1); i += 2) {
115                 crc = crc24_2byte(crc, buf[i], buf[i+1]);
116         }
117         if (i < bytes)
118                 crc = crc24_2byte(crc, buf[i], 0);
119         crc &= 0x00FFFFFF;
120
121         return crc;
122 }