summaryrefstree
path: root/kernel/drivers/sensorhub/brcm/ssp_bbd.c
blob: 22eff2485d0de410af54d879f884754941909917 (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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 *  Copyright (C) 2012, 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.h"


bool ssp_dbg;
bool ssp_pkt_dbg;

#define dprint(fmt, args...) \
		if (unlikely(ssp_dbg))	 printk(KERN_INFO "[SSPBBD]:(%s:%d): " fmt, __func__,__LINE__, ##args)

#define DEBUG_SHOW_HEX_SEND(msg, len) \
		if (unlikely(ssp_pkt_dbg))	print_hex_dump(KERN_INFO, "SSP->MCU: ", DUMP_PREFIX_NONE, 16, 1, (msg), (len), true);
#define DEBUG_SHOW_HEX_RECV(msg, len) \
		if (unlikely(ssp_pkt_dbg))	print_hex_dump(KERN_INFO, "SSP<-MCU: ", DUMP_PREFIX_NONE, 16, 1, (msg), (len), true);

extern void clean_msg(struct ssp_msg *msg);

/**
 * transfer ssp data to mcu thru bbd driver.
 *
 * @data:	ssp data pointer
 * @msg:	ssp message
 * @done:	completion
 * @timeout:	wait response time (ms)
 *
 * @return:	 1 = success, -1 = failed to send data to bbd
 *		-2 = failed to get response from mcu
 */

extern void exynos_show_eint_mask_registers(void);
extern void bcm4773_debug_info(void);

int bbd_do_transfer(struct ssp_data *data, struct ssp_msg *msg,
		struct completion *done, int timeout) {
	int status = 0;
	bool msg_dead = false, ssp_down = false;
	bool use_no_irq = false;

	if(data == NULL || msg == NULL) {
		pr_err("%s():[SSPBBD] data or msg is NULL\n", __func__);
		return -1;
	}

	mutex_lock(&data->comm_mutex);

	ssp_down = data->bSspShutdown;

	if (ssp_down) {
		pr_err("[SSPBBD]: ssp_down == true. returning\n");
		clean_msg(msg);
		mdelay(5);
		mutex_unlock(&data->comm_mutex);
		return -1;
	}

	msg->dead_hook = &msg_dead;
	msg->dead = false;
	msg->done = done;
	use_no_irq = (msg->length == 0);

	mutex_lock(&data->pending_mutex);

	if (bbd_send_packet((unsigned char *)msg, 9) > 0) {
		status = 1;
		DEBUG_SHOW_HEX_SEND(msg, 9)
	} else {
		pr_err("[SSP]: %s bbd_send_packet fail!!\n", __func__);
		data->uTimeOutCnt++;
		clean_msg(msg);
		mutex_unlock(&data->pending_mutex);
		mutex_unlock(&data->comm_mutex);
		return -1;
	}

	if (!use_no_irq) {
//		mutex_lock(&data->pending_mutex);	//moved UP
		list_add_tail(&msg->list, &data->pending_list);
//		mutex_unlock(&data->pending_mutex);	//moved down
	}

	mutex_unlock(&data->pending_mutex);

	if (status == 1 && done != NULL) {
		dprint("waiting completion ...\n");
		if (wait_for_completion_timeout(done, msecs_to_jiffies(timeout)) == 0) {
			pr_err("[SSPBBD] : %s() : completion is timeout !\n", __func__);

			bcm4773_debug_info();
			exynos_show_eint_mask_registers();

			status = -2;
			mutex_lock(&data->pending_mutex);
			if (!use_no_irq && !msg_dead) {
				if ((msg->list.next != NULL) &&
					(msg->list.next != LIST_POISON1))
					list_del(&msg->list);
			}
			mutex_unlock(&data->pending_mutex);
		}else{
			dprint("completion is cleared !\n");
		}
	}

	mutex_lock(&data->pending_mutex);
	if (msg != NULL && !msg_dead) {
		msg->done = NULL;
		msg->dead_hook = NULL;

		if (status != 1)
			msg->dead = true;
		if (status == -2)
			data->uTimeOutCnt++;
	}
	mutex_unlock(&data->pending_mutex);

	if (use_no_irq)
		clean_msg(msg);

	mutex_unlock(&data->comm_mutex);

	return status;
}

/****************************************************************
 *
 * Callback fucntios
 *
 ****************************************************************/

/**
 * callback function:
 *	called this function by bbdpl when packet comes from MCU
 *
 * @ssh_data:	ssh data pointer
 *
 * @return:	0 = success, -1 = failure
 *
 */
int callback_bbd_on_packet_alarm(void *ssh_data)
{
	struct ssp_data *data = (struct ssp_data *)ssh_data;

	if(ssh_data == NULL)
		return -1;
#if 0
	/* check if already processing work queue */
	if(mutex_is_locked(&data->pending_mutex)) {
		dprint("already processing work queue\n");
		return 0;
	}
#endif

	if(queue_work(data->bbd_on_packet_wq, &data->work_bbd_on_packet)){
		/* in case of adding the work to queue */
		dprint("queue_work ok!!\n");
		return 0;
	}

	return 0;
}

/**
 * callback function
 *	called this function by bbdpl whenever mcu is(not) ready
 *
 * @ssh_data: 	ssh data pointer
 * @ready:	true = ready, false = not ready
 *
 * @return:	0 = success, -1 = failure
 */
int callback_bbd_on_mcu_ready(void *ssh_data, bool ready)
{
	struct ssp_data *data = (struct ssp_data *)ssh_data;

	if(data == NULL)
		return -1;

	if(ready == true) {
		/* Start queue work for initializing MCU */
		queue_work(data->bbd_mcu_ready_wq, &data->work_bbd_mcu_ready);
	}else{
		/* Disable SSP */
		ssp_enable(data, false);	
		dprint("MCU is not ready and disabled SSP\n");
	}

	return 0;
}

/**
 * callback function
 *	called this function by bbdpl when received control command from LHD
 *
 * @ssh_data:	ssh data pointer
 * @str_ctrl:	string type control command
 *
 * @return:	0 = success, -1 = failure
 */
int callback_bbd_on_control(void *ssh_data, char *str_ctrl)
{
	if(!ssh_data || !str_ctrl)
		return -1;

	dprint("Received string command from LHD(=%s)\n", str_ctrl);

	return 0;
}

/****************************************************************
 *
 * Work queue fucntios
 *
 ****************************************************************/

/**
 * Work queue function for MCU ready 
 *	This is called by bbdpl when MCU is ready and then
 *	initialize MCU

 * @work:	work structure
 *
 * @return:	none
 */
void bbd_mcu_ready_work_func(struct work_struct *work)
{
	struct ssp_data *data = container_of(work, struct ssp_data, work_bbd_mcu_ready);
	int ret = 0;
	int retries = 0;

	msleep(1000);
	dprint("MCU is ready.(work_queue)\n");

	mutex_lock(&data->ssp_enable_mutex);
	clean_pending_list(data);

	ssp_enable(data, true);
retries:
	ret = initialize_mcu(data);
	if (ret != SUCCESS) {
		data->uResetCnt++;
		mdelay(100);
		if(++retries > 3) {
			pr_err("[SSPBBD] fail to initialize mcu\n");
			ssp_enable(data, false);
			mutex_unlock(&data->ssp_enable_mutex);
			return;
		}
		goto retries;
	}
	mutex_unlock(&data->ssp_enable_mutex);

	dprint("mcu is initiialized (retries=%d)\n", retries);

	/* recover previous state */
	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);
}

/**
 * This work queue fucntion starts from alarm callback function
 *
 * @work:	work structure
 *
 * @return:	none
 */
#define BBD_PULL_TIMEOUT	1000	/* the timeout for getting data from bbdpl */

#define MAX_SSP_DATA_SIZE	(4*BBD_MAX_DATA_SIZE)
unsigned char rBuff[MAX_SSP_DATA_SIZE] = {-1};

void bbd_on_packet_work_func(struct work_struct *work)
{
	struct ssp_data *data = container_of(work, struct ssp_data, work_bbd_on_packet);
	unsigned short chLength = 0, msg_options = 0;
	unsigned char msg_type = 0;
	int iRet = 0;
	unsigned char *pData = NULL, *p, *q;
	int nDataLen = 0;

	u64 timestamp;
	//struct timespec ts;

	iRet = bbd_pull_packet(rBuff, sizeof(rBuff), BBD_PULL_TIMEOUT);
	if (iRet <= 0) {
		// This is not error condition because previous run of this function may have read all packets from BBD
		// This happens if this work function is scheduled while it starts running.
		return;
	}

	p = rBuff;
	q = rBuff + iRet;	// q points end of currently received data bytes

process_one:
	DEBUG_SHOW_HEX_RECV(p, 4)

	memcpy(&msg_options, p, 2); p+=2;
	msg_type = msg_options & SSP_SPI_MASK;
	memcpy(&chLength, p, 2); p+=2;
	pData = p;	// pData points current frame's data
	if (msg_type == AP2HUB_READ || msg_type == HUB2AP_WRITE)
	p += chLength;	// now p points next frame

	nDataLen = q - pData;

	// wait until we receive full frame
	while (q<p && q < rBuff+sizeof(rBuff)) {
		iRet = bbd_pull_packet(q, rBuff+sizeof(rBuff)-q, BBD_PULL_TIMEOUT);
		if (iRet<=0) {
			pr_err("[SSP]: %s bbd_pull_packet fail 2!! (iRet=%d)\n", __func__, iRet);
			return;
		}
		q += iRet;
		nDataLen += iRet;
	}

	switch (msg_type) {
	case AP2HUB_READ:
	case AP2HUB_WRITE:
		mutex_lock(&data->pending_mutex);
		if (!list_empty(&data->pending_list)) {
			struct ssp_msg *msg, *n;
			bool found = false;

			list_for_each_entry_safe(msg, n, &data->pending_list, list)
			{
				if (msg->options == msg_options) {
					list_del(&msg->list);
					found = true;
					break;
				}
			}

			if (!found) {
				pr_err("[SSP]: %s %d - Not match error\n", __func__, msg_options);
				goto exit;
			}

			if (msg->dead && !msg->free_buffer) {
				msg->buffer = (char*) kzalloc(msg->length, GFP_KERNEL);
				msg->free_buffer = 1;
			} // For dead msg, make a temporary buffer to read.

			if(msg->buffer == NULL) {
				pr_err("[SSPBBD]: %s() : msg->buffer is NULL\n", __func__);
				goto exit;
			}
			if (msg_type == AP2HUB_READ) {
				if(nDataLen <= 0) {
					dprint("Waiting 2nd message...(msg=%p, length=%d)\n",
						msg, msg->length);
					iRet = bbd_pull_packet(msg->buffer, msg->length, 
								BBD_PULL_TIMEOUT);
					dprint("Received 2nd message. (iRet=%d)\n",  iRet);
				}else{
					memcpy(msg->buffer, pData, msg->length);
					nDataLen -= msg->length;
				}
				DEBUG_SHOW_HEX_RECV(msg->buffer, msg->length)
			}
			if (msg_type == AP2HUB_WRITE) {
				iRet = bbd_send_packet(msg->buffer, msg->length);
				if(iRet <= 0) {
					pr_err("[SSP]: %s bbd_send_packet fail!!(AP2HUB_WRITE)\n",
						 __func__);
					goto exit;
				}

				DEBUG_SHOW_HEX_SEND(msg->buffer, msg->length)

				if (msg_options & AP2HUB_RETURN) {
					msg->options = AP2HUB_READ | AP2HUB_RETURN;
					msg->length = 1;
					list_add_tail(&msg->list, &data->pending_list);
					goto exit;
				}
			}
			if (msg->done != NULL && !completion_done(msg->done)){
				dprint("complete(mg->done)\n");
				complete(msg->done);
			}
			if (msg->dead_hook != NULL)
				*(msg->dead_hook) = true;

			clean_msg(msg);
		} else
			pr_err("[SSP]List empty error(%d)\n", msg_type);
exit:
		mutex_unlock(&data->pending_mutex);
		break;
	case HUB2AP_WRITE:
		{
		char* buffer;
		if(chLength == 0)
		{
			pr_err("[SSP] %s chlength is zero\n", __func__);
			iRet = -EINVAL;
			break;
		}
		buffer = (char*) kzalloc(chLength, GFP_KERNEL);
		if (buffer == NULL) {
			pr_err("[SSP] %s, failed to alloc memory for buffer\n", __func__);
			iRet = -ENOMEM;
			break;
		}
		if(nDataLen <= 0) {
			dprint("Waiting 2nd message...(chLength=%d)\n", chLength);
			iRet = bbd_pull_packet(buffer, chLength, BBD_PULL_TIMEOUT);
			dprint("Received 2nd message. (iRet=%d)\n",  iRet);
		}else{
			memcpy(buffer, pData, chLength);
			iRet = chLength;
		}
		DEBUG_SHOW_HEX_RECV(buffer, chLength)
		if (iRet < 0)
			pr_err("[SSP] %s bbd_pull_packet fail.(iRet=%d)\n", __func__,iRet);
		else {
			//ts = ktime_to_timespec(ktime_get_boottime());
			//timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
			timestamp = get_kernel_timestamp();
			data->timestamp = timestamp;
			//pr_err("[SSP] event %lld\n", timestamp);

			parse_dataframe(data, buffer, chLength);
		}
		//iRet = spi_read(data->spi, buffer, chLength);
		kfree(buffer);
		break;
		}
	default:
		pr_err("[SSP]No type error(%d)\n", msg_type);
		break;
	}

	if (iRet < 0) {
		pr_err("[SSP]: %s - MSG2SSP_SSD error %d\n", __func__, iRet);
	}

	if (p<q)
		goto process_one;
}