aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/tspdrv/VibeOSKernelLinuxTime.c
blob: bb6a37d5ddd13ca4ce4d2dbce1be96f883be55ec (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
/*
** =========================================================================
** File:
**	 VibeOSKernelLinuxTime.c
**
** Description:
**	 High Resolution Time helper functions for Linux.
**
** Portions Copyright (c) 2010 Immersion Corporation. All Rights Reserved.
**
** P1 Compat Author: Humberto Borba <kernel@humberos.com.br>
**
** This file contains Original Code and/or Modifications of Original Code
** as defined in and that are subject to the GNU Public License v2 -
** (the 'License'). You may not use this file except in compliance with the
** License. You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or contact
** TouchSenseSales@immersion.com.
**
** The Original Code and all software distributed under the License are
** distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
** EXPRESS OR IMPLIED, AND IMMERSION HEREBY DISCLAIMS ALL SUCH WARRANTIES,
** INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
** FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
** the License for the specific language governing rights and limitations
** under the License.
** =========================================================================
*/

/*
** Kernel high-resolution software timer is used as an example but another type
** of timer (such as HW timer or standard software timer) might be used to achieve
** the 5ms required rate.
*/

#include <linux/hrtimer.h>
#include <linux/mutex.h>
#include <linux/kthread.h>

#define WATCHDOG_TIMEOUT 10 /* 10 timer cycles = 50ms */

/* Global variables */
static bool g_bTimerThreadStarted = false;
static bool g_bTimerStarted = false;
static struct hrtimer g_tspTimer;
static ktime_t g_ktFiveMs;
struct task_struct *g_pTspThread;
static int g_nWatchdogCounter = 0;

DECLARE_COMPLETION(g_tspCompletion);
DEFINE_SEMAPHORE(g_hMutex);

/* Forward declarations */
static void VibeOSKernelLinuxStartTimer(void);
static void VibeOSKernelLinuxStopTimer(void);

/**
 * VibeSemIsLocked - is the semaphore locked
 * @lock: the semaphore to be queried
 *
 * Returns 1 if the semaphore is locked, 0 if unlocked.
 */
static inline int VibeSemIsLocked(struct semaphore *lock)
{
#if ((LINUX_VERSION_CODE & 0xFFFFFF) < KERNEL_VERSION(2,6,27))
	return atomic_read(&lock->count) != 1;
#else
	return (lock->count) != 1;
#endif
}


static enum hrtimer_restart tsp_timer_interrupt(struct hrtimer *timer)
{
	/* Scheduling next timeout value right away */
	hrtimer_forward_now(timer, g_ktFiveMs);

	if (g_bTimerStarted) {
		/* Notifying the handler of this timer of the tick */
		/* This allows us to perform operations that are usually */
		/* not allowed inside interrupt context, such as locking */
		/* a mutex, etc. */
		complete(&g_tspCompletion);
	}

	return HRTIMER_RESTART;
}


static int VibeOSKernelTimerProc(void* data)
{
	int nActuatorNotPlaying;
	int i;
	int bReachEndBuffer = 0;

	while (!kthread_should_stop()) {
		if (g_bTimerThreadStarted) {
			/* Block until we get woken up by timer tick */
			/* . only do this if we're not exiting entirely */
			wait_for_completion(&g_tspCompletion);

			/* Reinitialized completion so it isn't free by default */
			init_completion(&g_tspCompletion);
		}

		nActuatorNotPlaying = 0;

		/* Return right away if timer is not supposed to run */
		if (g_bTimerStarted) {
			for (i = 0; i < NUM_ACTUATORS; i++) {
				actuator_samples_buffer *pCurrentActuatorSample = &(g_SamplesBuffer[i]);

				if (-1 == pCurrentActuatorSample->nIndexPlayingBuffer) {
					nActuatorNotPlaying++;
					if ((NUM_ACTUATORS == nActuatorNotPlaying) && ((++g_nWatchdogCounter) > WATCHDOG_TIMEOUT)) {
						/*
						Nothing to play for all actuators,
						turn off the timer when we reach the watchdog tick count limit
						*/
						ImmVibeSPI_ForceOut_Set(i, 0);
						ImmVibeSPI_ForceOut_AmpDisable(i);
						VibeOSKernelLinuxStopTimer();
						/* Reset watchdog counter */
						g_nWatchdogCounter = 0;
					}
				} else {
					/* Play the current buffer */
					ImmVibeSPI_ForceOut_Set(i, pCurrentActuatorSample->actuatorSamples[(int)pCurrentActuatorSample->nIndexPlayingBuffer].dataBuffer[(int)(pCurrentActuatorSample->nIndexOutputValue++)]);

					if (pCurrentActuatorSample->nIndexOutputValue >= pCurrentActuatorSample->actuatorSamples[(int)pCurrentActuatorSample->nIndexPlayingBuffer].nBufferSize) {
						/* We were playing in the last tick */
						/* Reach the end of the current buffer */
						pCurrentActuatorSample->actuatorSamples[(int)pCurrentActuatorSample->nIndexPlayingBuffer].nBufferSize = 0;

						bReachEndBuffer = 1;

						/* Check stop request and empty buffer */
						if ((g_bStopRequested) || (0 == (pCurrentActuatorSample->actuatorSamples[(int)((pCurrentActuatorSample->nIndexPlayingBuffer) ^ 1)].nBufferSize))) {
							pCurrentActuatorSample->nIndexPlayingBuffer = -1;

							if (g_bStopRequested) {
								/* g_bStopReqested is set, so turn off all actuators */
								ImmVibeSPI_ForceOut_Set(i, 0);
								ImmVibeSPI_ForceOut_AmpDisable(i);

								/* If it's the last actuator, stop the timer */
								if (i == (NUM_ACTUATORS-1)) {
									VibeOSKernelLinuxStopTimer();

									/* Reset watchdog counter */
									g_nWatchdogCounter = 0;
								}
							}
						} else { /* The other buffer has data in it */
							/* Switch buffer */
							(pCurrentActuatorSample->nIndexPlayingBuffer) ^= 1;
							pCurrentActuatorSample->nIndexOutputValue = 0;
						}
					}
				}
			}
			/* Release the mutex if locked */
			if (bReachEndBuffer && VibeSemIsLocked(&g_hMutex)) {
				up(&g_hMutex);
			}
		}
	}
	return 0;
}

static void VibeOSKernelLinuxInitTimer(void)
{
	/* Get a 5,000,000ns = 5ms time value */
	g_ktFiveMs = ktime_set(0, 5000000);

	/* Start the companion thread. It's controlled by the timer, so when that is stopped, we're OK. */
	g_bTimerThreadStarted = true;
	g_pTspThread = kthread_run(VibeOSKernelTimerProc, NULL, "TouchSense Player Thread");

	hrtimer_init(&g_tspTimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);

	/* Initialize a 5ms-timer with tsp_timer_interrupt as timer callback (interrupt driven)*/
	g_tspTimer.function = tsp_timer_interrupt;
}

static void VibeOSKernelLinuxStartTimer(void)
{
	int i;
	int res;

	/* Reset watchdog counter */
	g_nWatchdogCounter = 0;

	if (!g_bTimerStarted) {
		if (!VibeSemIsLocked(&g_hMutex))
			res = down_interruptible(&g_hMutex); /* start locked */

		g_bTimerStarted = true;

		/* Start the timer */
		hrtimer_start(&g_tspTimer, g_ktFiveMs, HRTIMER_MODE_REL);

		/* Don't block the write() function after the first sample to allow the host sending the next samples with no delay */
		for (i = 0; i < NUM_ACTUATORS; i++) {
			if ((g_SamplesBuffer[i].actuatorSamples[0].nBufferSize) || (g_SamplesBuffer[i].actuatorSamples[1].nBufferSize)) {
				g_SamplesBuffer[i].nIndexOutputValue = 0;
				return;
			}
		}
	}
	/*
	** Use interruptible version of down to be safe
	** (try to not being stuck here if the mutex is not freed for any reason)
	*/
	res = down_interruptible(&g_hMutex); /* wait for the mutex to be freed by the timer */
	if (res != 0)
		DbgOut((KERN_INFO "VibeOSKernelLinuxStartTimer: down_interruptible interrupted by a signal.\n"));
}

static void VibeOSKernelLinuxStopTimer(void)
{
	int i;

	if (g_bTimerStarted) {
		g_bTimerStarted = false;
		hrtimer_cancel(&g_tspTimer);
	}
	/* Reset samples buffers */
	for (i = 0; i < NUM_ACTUATORS; i++) {
		g_SamplesBuffer[i].nIndexPlayingBuffer = -1;
		g_SamplesBuffer[i].actuatorSamples[0].nBufferSize = 0;
		g_SamplesBuffer[i].actuatorSamples[1].nBufferSize = 0;
	}
	g_bStopRequested = false;
	g_bIsPlaying = false;
}

static void VibeOSKernelLinuxTerminateTimer(void)
{
	VibeOSKernelLinuxStopTimer();
	g_bTimerThreadStarted = false;
	complete_all(&g_tspCompletion);
	kthread_stop(g_pTspThread);
	if (VibeSemIsLocked(&g_hMutex))
		up(&g_hMutex);
}