summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/msg2233/mstar_drv_hotknot_queue.c
blob: 9dc386de5f2663a4d6e089902f495d77376d76da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2012 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (??MStar Confidential Information??) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////

/**
 *
 * @file    mstar_drv_hotknot_queue.c
 *
 * @brief   This file defines the queue structure for hotknot
 *
 *
 */

////////////////////////////////////////////////////////////
/// Included Files
////////////////////////////////////////////////////////////
#include "mstar_drv_hotknot_queue.h"


#ifdef CONFIG_ENABLE_HOTKNOT
////////////////////////////////////////////////////////////
/// Data Types
////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////
/// LOCAL VARIABLE DEFINITION
////////////////////////////////////////////////////////////
static u8 * _gQueue;
static u16  _gQFront=0;
static u16  _gQRear;
static u16  _gQSize = HOTKNOT_QUEUE_SIZE;


////////////////////////////////////////////////////////////
/// Macro
////////////////////////////////////////////////////////////
#define RESULT_OK                     0
#define RESULT_OVERPUSH              -1
#define RESULT_OVERPOP               -2


////////////////////////////////////////////////////////////
/// Function Prototypes
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
/// Function Implementation
////////////////////////////////////////////////////////////

void _DebugShowQueueArray(u8 *pBuf, u16 nLen)
{
    int i;

    for(i=0; i < nLen; i++)
    {
        DBG("%02X ", pBuf[i]);       

        if(i%16==15){  
            DBG("\n");
        }
    }
    DBG("\n");    
}


void CreateQueue()
{
    DBG("*** %s() ***\n", __func__);

	_gQueue = (u8*)kmalloc(sizeof(u8)*_gQSize, GFP_KERNEL );
    _gQFront = _gQRear = 0;
}


void ClearQueue()
{
    DBG("*** %s() ***\n", __func__);

    _gQFront = _gQRear = 0;
}


int PushQueue(u8 * pBuf, u16 nLength)
{
    u16 nPushLen = nLength;   

    DBG("*** %s() ***\n", __func__);

    //DBG("*** Show data to PushQueue() ***\n", __func__);
    //_DebugShowQueueArray(pBuf, nLength);
    //DBG("*** Show Queue data before PushQueue() ***\n", __func__);
    //_DebugShowQueueArray(_gQueue, _gQSize);
    
    DBG("*** Before PushQueue: _gQFront = %d, _gQRear = %d ***\n", _gQFront, _gQRear);

    if(_gQRear >= _gQFront)
    {
        if(nPushLen > 0 && _gQFront == 0 && _gQRear == _gQSize-1)    //full
        {
            DBG("*** PushQueue: RESULT_OVERPUSH ***\n");
            return RESULT_OVERPUSH;
        }
    
        if(nPushLen > _gQSize-1 - (_gQRear - _gQFront))    //over push
        {
            DBG("*** PushQueue: RESULT_OVERPUSH ***\n");        
            return RESULT_OVERPUSH;
        }
    
        if(_gQRear+nPushLen <= _gQSize-1)
        {
            memcpy(&_gQueue[_gQRear+1], pBuf, nPushLen);
            _gQRear = _gQRear + nPushLen;
        }
        else
        {
            u16 nQTmp = (_gQSize-1) -_gQRear;
            memcpy(&_gQueue[_gQRear+1], pBuf, nQTmp);          //push data from rear to end
            memcpy(_gQueue, &pBuf[nQTmp], nPushLen - nQTmp);    //push data lest
            _gQRear = nPushLen - nQTmp - 1;        
        }           
    }
    else    //_gQRear < _gQFront
    {
        if(nPushLen > 0 && _gQFront == _gQRear+1)    //full
        {
            DBG("*** PushQueue: RESULT_OVERPUSH ***\n");        
            return RESULT_OVERPUSH;
        }
    
        if(nPushLen > (_gQFront - _gQRear) - 1)    //over push
        {
            DBG("*** PushQueue: RESULT_OVERPUSH ***\n");        
            return RESULT_OVERPUSH;
        }
        
        memcpy(&_gQueue[_gQRear+1], pBuf, nPushLen);
        _gQRear = _gQRear + nPushLen;       
    }

    //DBG("*** Show Queue data after PushQueue() ***\n", __func__);
    //_DebugShowQueueArray(_gQueue, _gQSize);

    DBG("*** After PushQueue: _gQFront = %d, _gQRear = %d ***\n", _gQFront, _gQRear); 
    return nPushLen;     
}


int PopQueue(u8 * pBuf, u16 nLength)
{
    u16 nPopLen = nLength; 

    DBG("*** %s() ***\n", __func__);

    DBG("*** Before PopQueue: _gQFront = %d, _gQRear = %d ***\n", _gQFront, _gQRear);  

    if(_gQRear >= _gQFront)
    {
        if(nPopLen > 0 && _gQRear == _gQFront)    //empty
        {
            DBG("*** PushQueue: RESULT_OVERPOP ***\n");        
            return RESULT_OVERPOP;
        }
    
        if(nPopLen > _gQRear - _gQFront)    //over pop
        {
            DBG("*** PushQueue: RESULT_OVERPOP ***\n");        
            return RESULT_OVERPOP;
        }
        
        memcpy(pBuf, &_gQueue[_gQFront+1], nPopLen);
        _gQFront = _gQFront + nPopLen;
    }
    else    //_gQRear < _gQFront 
    {
        if(nPopLen > _gQSize - (_gQFront - _gQRear))    //over pop
        {
            DBG("*** PushQueue: RESULT_OVERPOP ***\n");        
            return RESULT_OVERPOP;
        }
    
        if(_gQFront + nPopLen <= _gQSize-1)
        {
            memcpy(pBuf, &_gQueue[_gQFront+1], nPopLen);
            _gQFront = _gQFront + nPopLen;
        }
        else
        {
            u16 nQTmp = (_gQSize-1) -_gQFront;
            memcpy(pBuf, &_gQueue[_gQFront+1], nQTmp);        //pop data from rear to end
            memcpy(&pBuf[nQTmp], _gQueue, nPopLen - nQTmp);    //pop data lest
            _gQFront = nPopLen - nQTmp - 1;        
        }
    }

    DBG("*** After PopQueue: _gQFront = %d, _gQRear = %d ***\n", _gQFront, _gQRear);   
    return nPopLen;    
}


int ShowQueue(u8 * pBuf, u16 nLength)    //just show data, not fetch data
{
    u16 nShowLen = nLength; 

    DBG("*** %s() ***\n", __func__);

    if(_gQRear >= _gQFront)
    {
        if(nShowLen > 0 && _gQRear == _gQFront)    //empty
        {
            return RESULT_OVERPOP;
        }
    
        if(nShowLen > _gQRear - _gQFront)    //over pop
        {
            return RESULT_OVERPOP;            
        }
        
        memcpy(pBuf, &_gQueue[_gQFront+1], nShowLen);
        //_gQFront = _gQFront + nPopLen;
    }
    else    //_gQRear < _gQFront 
    {
        if(nShowLen > _gQSize - (_gQFront - _gQRear))    //over pop
        {
            return RESULT_OVERPOP;            
        }
    
        if(_gQFront + nShowLen <= _gQSize-1)
        {
            memcpy(pBuf, &_gQueue[_gQFront+1], nShowLen);
            //_gQFront = _gQFront + nPopLen;
        }
        else
        {
            u16 nQTmp = (_gQSize-1) -_gQFront;
            memcpy(pBuf, &_gQueue[_gQFront+1], nQTmp);        //pop data from rear to end
            memcpy(&pBuf[nQTmp], _gQueue, nShowLen - nQTmp);    //pop data lest
            //_gQFront = nPopLen - nQTmp - 1;        
        }
    }
    
    return nShowLen;
}


void ShowAllQueue(u8 * pBuf, u16 * pFront, u16 * pRear)    //just show data, not fetch data
{
    DBG("*** %s() ***\n", __func__);

    memcpy(pBuf, _gQueue, HOTKNOT_QUEUE_SIZE);        //pop data from rear to end
    *pFront = _gQFront;
    *pRear = _gQRear;    
}


void DeleteQueue()
{
    DBG("*** %s() ***\n", __func__);

    _gQFront = _gQRear = 0;   
    kfree(_gQueue);
}

#endif //CONFIG_ENABLE_HOTKNOT