| 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
 | /*
 * Copyright (C) 2012-2014 Motorola, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/m4sensorhub.h>
#include <linux/slab.h>
/* --------------- Global Declarations -------------- */
#define PANIC_BANK              0xFF /* Reserved for Panic bank */
#define PANIC_CMD_CHECK         0xCD /* Panic Handoff command */
#define PANIC_RESP_CHECK        0xDeadBeef /* Panic Handoff Magic code */
/* ------------ Local Function Prototypes ----------- */
/* --------------- Local Declarations -------------- */
static const char *callback_name[PANICHDL_MAX] = {
	[PANICHDL_DISPLAY_RESTORE] = "display_restore",
	[PANICHDL_IRQ_RESTORE] = "irq_restore",
	[PANICHDL_HEARTRATE_RESTORE] = "heartrate_restore",
	[PANICHDL_PASSIVE_RESTORE] = "passive_restore",
	[PANICHDL_ALS_RESTORE] = "als_restore",
	[PANICHDL_FUSION_RESTORE] = "fusion_restore",
	[PANICHDL_MPU9150_RESTORE] = "mpu9150_restore",
	[PANICHDL_PEDOMETER_RESTORE] = "pedometer_restore",
};
struct m4sensorhub_panic_callback {
	void (*callback)(struct m4sensorhub_data *, void *);
	void *data;
};
struct m4sensorhub_panicdata {
	struct mutex lock;  /* lock callback and data */
	struct m4sensorhub_panic_callback funcs[PANICHDL_MAX];
};
union panic_buf {
	struct _in {
		unsigned char bank;
		unsigned char cmd;
	} in;
	unsigned int data;
};
/* -------------- Local Data Structures ------------- */
/* -------------- Global Functions ----------------- */
/* m4sensorhub_panic_init()
   Initialized panic private data structures.
   Returns 0 on success or negative error code on failure
     m4sensorhub - pointer to the main m4sensorhub data struct
*/
int m4sensorhub_panic_init(struct m4sensorhub_data *m4sensorhub)
{
	int retval = 0;
	struct m4sensorhub_panicdata *data;
	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		retval = -ENODATA;
		goto m4sensorhub_panic_init_fail;
	}
	if (m4sensorhub->panicdata != NULL) {
		KDEBUG(M4SH_ERROR,
			"%s: Trying to overwrite previous panic data\n",
			__func__);
		retval = -EPERM;
		goto m4sensorhub_panic_init_fail;
	}
	data = kzalloc(sizeof(struct m4sensorhub_panicdata), GFP_KERNEL);
	if (data == NULL) {
		KDEBUG(M4SH_ERROR, "%s: Memory error in panic_init\n",
			__func__);
		retval = -ENOMEM;
		goto m4sensorhub_panic_init_fail;
	}
	mutex_init(&data->lock);
	m4sensorhub->panicdata = data;
m4sensorhub_panic_init_fail:
	return retval;
}
EXPORT_SYMBOL_GPL(m4sensorhub_panic_init);
/* m4sensorhub_panic_shutdown()
   Shutdown the M4 sensor hub Panic subsystem
     m4sensorhub - pointer to the main m4sensorhub data struct
*/
void m4sensorhub_panic_shutdown(struct m4sensorhub_data *m4sensorhub)
{
	struct m4sensorhub_panicdata *data = NULL;
	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		return;
	}
	data = m4sensorhub->panicdata;
	if (data != NULL) {
		m4sensorhub->panicdata = NULL;
		if (mutex_is_locked(&data->lock))
			mutex_unlock(&data->lock);
		mutex_destroy(&data->lock);
		/*
		 * Callback and data pointers were passed to us, so
		 * we leave it to the registering functions to
		 * clean up their own memory.
		 */
		kfree(data);
	}
	return;
}
EXPORT_SYMBOL_GPL(m4sensorhub_panic_shutdown);
/* m4sensorhub_panic_register()
   Register a panic handler to monitor M4 panic reset
   Returns 0 on success or negative error code on failure
     m4sensorhub - pointer to the main m4sensorhub data struct
     index - M4 Sensor Hub panic handler to register for
     cb_func - panic handler function to execute after M4 reset
     data - pointer to data for panic handler function
*/
int m4sensorhub_panic_register(struct m4sensorhub_data *m4sensorhub,
	enum m4sensorhub_panichdl_index index,
	void (*cb_func) (struct m4sensorhub_data *, void *),
	void *data)
{
	struct m4sensorhub_panicdata *panicdata = NULL;
	int retval = 0;
	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		retval = -ENODATA;
		goto m4sensorhub_panic_register_exit;
	} else if (cb_func == NULL) {
		KDEBUG(M4SH_ERROR, "%s: Callback is NULL\n", __func__);
		retval = -ENOSYS;
		goto m4sensorhub_panic_register_exit;
	} else if (index >= PANICHDL_MAX) {
		KDEBUG(M4SH_ERROR, "%s: Number of panic handlers exceeded\n",
			__func__);
		retval = -EOVERFLOW;
		goto m4sensorhub_panic_register_exit;
	} else if (m4sensorhub->panicdata == NULL) {
		KDEBUG(M4SH_ERROR, "%s: Panic data is missing\n", __func__);
		retval = -ENODATA;
		goto m4sensorhub_panic_register_exit;
	}
	panicdata = (struct m4sensorhub_panicdata *)m4sensorhub->panicdata;
	mutex_lock(&panicdata->lock);
	if (panicdata->funcs[index].callback == NULL) {
		panicdata->funcs[index].callback = cb_func;
		panicdata->funcs[index].data = data;
		KDEBUG(M4SH_NOTICE, "%s: %s callback registered\n",
			__func__, callback_name[index]);
	} else {
		KDEBUG(M4SH_ERROR, "%s: %s %s", __func__,
			callback_name[index], "callback registration failed\n");
		retval = -EPERM;
		goto m4sensorhub_panic_register_fail;
	}
m4sensorhub_panic_register_fail:
	mutex_unlock(&panicdata->lock);
m4sensorhub_panic_register_exit:
	return retval;
}
EXPORT_SYMBOL_GPL(m4sensorhub_panic_register);
/* m4sensorhub_panic_unregister()
   Unregister an panic handler to monitor M4 panic reset
   Returns 0 on success or negative error code on failure
     m4sensorhub - pointer to the main m4sensorhub data struct
     index - M4 Sensor Hub panic handler to unresiter for
*/
int m4sensorhub_panic_unregister(struct m4sensorhub_data *m4sensorhub,
	enum m4sensorhub_panichdl_index index)
{
	int retval = 0;
	struct m4sensorhub_panicdata *panicdata = NULL;
	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		retval = -ENODATA;
		goto m4sensorhub_panic_unregister_fail;
	} else if (index >= PANICHDL_MAX) {
		KDEBUG(M4SH_ERROR, "%s: Number of panic handlers exceeded\n",
			__func__);
		retval = -EOVERFLOW;
		goto m4sensorhub_panic_unregister_fail;
	} else if (m4sensorhub->panicdata == NULL) {
		KDEBUG(M4SH_ERROR, "%s: Panic data is missing\n", __func__);
		retval = -ENODATA;
		goto m4sensorhub_panic_unregister_fail;
	}
	panicdata = (struct m4sensorhub_panicdata *)m4sensorhub->panicdata;
	mutex_lock(&panicdata->lock);
	panicdata->funcs[index].callback = NULL;
	panicdata->funcs[index].data = NULL;
	mutex_unlock(&panicdata->lock);
	KDEBUG(M4SH_NOTICE, "%s: %s callback un-registered\n",
			__func__, callback_name[index]);
m4sensorhub_panic_unregister_fail:
	return retval;
}
EXPORT_SYMBOL_GPL(m4sensorhub_panic_unregister);
/* m4sensorhub_panic_process()
   Check M4 if it's panicked, use I2C to communicate with M4 panic handler
   OMAP use the same i2c sequences to send command via i2c master, then M4
   i2c slave program will handle these commands, it may have 2 slave programs
   1. Normal i2c slave program handles all vaild banks'(limit on
      M4SH_TYPE__NUM) command, for invalid bank, it always responses 0xFF
   2. Panic i2c slave program handles panic bank(reserved 0xFF for it) command,
      for others, it always responses 0x00
   To detect whether M4 is panicked, the process should be
   i. When OMAP got interrupt from M4, OMAP will check which irq is raised, it
      send normal banks' command to M4, for panic case, it always returns 0x00,
      so OMAP has a checkpoint as there's interrupt request from M4 without
      active IRQ
   ii.Then OMAP will confirm if M4 is panic via send panic bank command, if M4
      is panicked, it will handle this bank and response panic magic code;
      Otherwise, if no panic magic code returned from M4, it always means M4
      isn't panicked.
     m4sensorhub - pointer to the main m4sensorhub data struct
	*/
void m4sensorhub_panic_process(struct m4sensorhub_data *m4sensorhub)
{
	int i, ret;
	union panic_buf buf;
	struct m4sensorhub_panic_callback handler;
	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		return;
	} else if (m4sensorhub->panicdata == NULL) {
		KDEBUG(M4SH_ERROR, "%s: Panic data is missing\n", __func__);
		return;
	}
	m4sensorhub_reg_access_lock();
	buf.in.bank = PANIC_BANK;
	buf.in.cmd = PANIC_CMD_CHECK;
	ret = m4sensorhub_i2c_write_read(m4sensorhub,
		(u8 *)&buf, sizeof(buf.in), sizeof(buf.data));
	if ((ret != sizeof(buf.data)) || (buf.data != PANIC_RESP_CHECK)) {
		/* TODO maybe we shall check if M4/OMAP i2c broken */
		KDEBUG(M4SH_ERROR, "m4sensorhub: %s ret=%d, data=0x%x\n",
			"Unknown IRQ status! M4 panic handoff", ret, buf.data);
		m4sensorhub_reg_access_unlock();
		return;
	}
	KDEBUG(M4SH_ERROR, "%s: Detected M4 panic, reset M4!\n", __func__);
	/* Passing "true" will reset M4 before trying to communicate */
	ret = m4sensorhub_test_m4_reboot(m4sensorhub, true);
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "%s: Failed to restart M4, ret = %d\n",
			__func__, ret);
		BUG();
	}
	m4sensorhub_reg_access_unlock();
	for (i = 0; i < PANICHDL_MAX; i++) {
		handler = ((struct m4sensorhub_panicdata *)
			(m4sensorhub->panicdata))->funcs[i];
		if (handler.callback) {
			KDEBUG(M4SH_NOTICE, "%s: Calling %s as M4 restarted!\n",
				__func__, callback_name[i]);
			handler.callback(m4sensorhub, handler.data);
		}
	}
}
EXPORT_SYMBOL_GPL(m4sensorhub_panic_process);
 |