summaryrefstree
path: root/kernel/drivers/sensorhub/stm32/ssp_data.c
blob: 8b584879a7deb287296b43c5fc66cfdd6360c1c2 (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
/*
 *  Copyright (C) 2015, Samsung Electronics Co. Ltd. All Rights Reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 */

#include "ssp_data.h"

#define U64_US2NS 1000ULL

/*************************************************************************/
/* SSP parsing the dataframe                                             */
/*************************************************************************/
static void get_timestamp(struct ssp_data *data, char *dataframe,
		int *ptr_data, struct sensor_value *event,
		u16 mode, int type)
{
	u32 ts_delta_us = 0;
	u64 ts_delta_ns = 0;

	memset(&ts_delta_us, 0, 4);
	memcpy(&ts_delta_us, dataframe + *ptr_data, 4);

	ts_delta_ns = (((u64) ts_delta_us) * U64_US2NS);

	if (mode == BATCH_MODE_RUN)
		data->latest_timestamp[type] += ts_delta_ns;
	else
		data->latest_timestamp[type] = data->timestamp;
	/* Ignore MCU's timestamp
	event->timestamp = data->latest_timestamp[sensor];
	*/
	data->latest_timestamp[type] = get_current_timestamp();
	event->timestamp = get_current_timestamp();
	*ptr_data += 4;
}

void get_sensordata(struct ssp_data *data, char *dataframe,
		int *ptr_data, int type, struct sensor_value *event)
{
	memcpy(event, dataframe + *ptr_data, data->info[type].get_data_len);
	*ptr_data += data->info[type].get_data_len;
}

int handle_big_data(struct ssp_data *data, char *dataframe, int *ptr_data)
{
	u8 big_type = 0;
	struct ssp_big *big = kzalloc(sizeof(*big), GFP_KERNEL);

	big->data = data;
	big_type = dataframe[(*ptr_data)++];
	memcpy(&big->length, dataframe + *ptr_data, 4);
	*ptr_data += 4;
	memcpy(&big->addr, dataframe + *ptr_data, 4);
	*ptr_data += 4;

	if (big_type >= BIG_TYPE_MAX) {
		kfree(big);
		return FAIL;
	}

	INIT_WORK(&big->work, data->ssp_big_task[big_type]);
	queue_work(data->debug_wq, &big->work);

	return SUCCESS;
}

void refresh_task(struct work_struct *work)
{
	struct ssp_data *data = container_of((struct delayed_work *)work,
			struct ssp_data, work_refresh);

	ssp_dbgf("REFESH TASK");
	
	if (data->is_ssp_shutdown == true) {
		ssp_errf("ssp already shutdown");
		return;
	}

	wake_lock(&data->ssp_wake_lock);
	ssp_errf();
	data->cnt_reset++;
	if (initialize_mcu(data) > 0) {
		sync_sensor_state(data);
		ssp_sensorhub_report_notice(data, MSG2SSP_AP_STATUS_RESET);
		if (data->uLastAPState != 0)
			ssp_send_cmd(data, data->uLastAPState, 0);
		if (data->uLastResumeState != 0)
			ssp_send_cmd(data, data->uLastResumeState, 0);
		data->cnt_timeout = 0;
	} else
		data->uSensorState = 0;

	wake_unlock(&data->ssp_wake_lock);
}

int queue_refresh_task(struct ssp_data *data, int delay)
{
	cancel_delayed_work_sync(&data->work_refresh);

	ssp_dbgf();

	INIT_DELAYED_WORK(&data->work_refresh, refresh_task);
	queue_delayed_work(data->debug_wq, &data->work_refresh,
			msecs_to_jiffies(delay));
	return SUCCESS;
}

int parse_dataframe(struct ssp_data *data, char *dataframe, int frame_len)
{
	struct sensor_value event;
	u16 batch_event_count;
	u16 mode;

	int type, index;
	u16 length = 0;

	if (data->is_ssp_shutdown) {
		ssp_infof("ssp shutdown, do not parse");
		return SUCCESS;
	}

	if (data->debug_enable)
		print_dataframe(data, dataframe, frame_len);

	memset(&event, 0, sizeof(event));

	for (index = 0; index < frame_len;) {
		switch (dataframe[index++]) {
		case MSG2AP_INST_BYPASS_DATA:
			type = dataframe[index++];
			if ((type < 0) || (type >= SENSOR_TYPE_MAX)) {
				ssp_errf("Mcu bypass dataframe err %d", type);
				return ERROR;
			}

			memcpy(&length, dataframe + index, 2);
			index += 2;
			batch_event_count = length;
			mode = length > 1 ? BATCH_MODE_RUN : BATCH_MODE_NONE;

			do {
				get_sensordata(data, dataframe, &index, type, &event);
				get_timestamp(data, dataframe, &index, &event, mode, type);
				report_sensor_data(data, type, &event);

				batch_event_count--;
			} while ((batch_event_count > 0) && (index < frame_len));

			if (batch_event_count > 0)
				ssp_errf("batch count error (%d)", batch_event_count);

			data->is_data_reported[type] = true;
			break;
		case MSG2AP_INST_DEBUG_DATA:
			type = print_mcu_debug(dataframe, &index, frame_len);
			if (type) {
				ssp_errf("Mcu debug dataframe err %d", type);
				return ERROR;
			}
			break;
		case MSG2AP_INST_LIBRARY_DATA:
			memcpy(&length, dataframe + index, 2);
			index += 2;
			ssp_sensorhub_handle_data(data, dataframe, index,
					index + length);
			index += length;
			break;
		case MSG2AP_INST_BIG_DATA:
			handle_big_data(data, dataframe, &index);
			break;
		case MSG2AP_INST_META_DATA:
			event.meta_data.what = dataframe[index++];
			event.meta_data.sensor = dataframe[index++];
			report_meta_data(data, SENSOR_TYPE_META, &event);
			break;
		case MSG2AP_INST_TIME_SYNC:
			data->is_time_syncing = true;
			break;
		case MSG2AP_INST_RESET:
			ssp_infof("Reset MSG received from MCU");
			if(data->is_probe_done == true)
				queue_refresh_task(data, 0);
			else
				ssp_infof("ssp_data is NULL");
			break;
		case MSG2AP_INST_DUMP_DATA:
			debug_crash_dump(data, dataframe, frame_len);
			break;
		}
	}

	return SUCCESS;
}

void initialize_function_pointer(struct ssp_data *data)
{
	data->ssp_big_task[BIG_TYPE_DUMP] = ssp_dump_task;
	data->ssp_big_task[BIG_TYPE_READ_LIB] = ssp_read_big_library_task;
}