summaryrefstree
path: root/kernel/drivers/nfc/cold_reset.c
blob: 6ae3635a731bf1276c7816c782a6d96a59bb351c (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
/******************************************************************************
 *  Copyright (C) 2020 NXP
 *   *
 * 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.
 *
 * 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/kernel.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/fs.h>
#include <linux/version.h>
#include "cold_reset.h"
#include "pn547.h"

/*ESE_COLD_RESET MACROS */
#define MAX_BUFFER_SIZE       512 /* copied from pn553.c */
#define MSG_NFCC_CMD          0x20
#define NCI_PROP_RST_RSP_SIZE 0x04

/* Evaluates to 1 If cold reset is in progress or the guard timer is still running */
#define IS_COLD_RESET_REQ_IN_PROGRESS(flags) \
		(flags & (MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER))

#define IS_RESET_PROTECTION_ENABLED(flags) (flags & RST_PROTECTION_ENABLED)

#define IS_COLD_RESET_ALLOWED(flags, src) (!IS_COLD_RESET_REQ_IN_PROGRESS(flags)    \
		&& (!IS_RESET_PROTECTION_ENABLED(flags) || src == ESE_COLD_RESET_SOURCE_SPI))

#define IS_COLD_RESET_ALLOWED_NFC(flags, src) ((IS_RESET_PROTECTION_ENABLED(flags) \
		&& src == ESE_COLD_RESET_SOURCE_NFC))

static struct pn547_dev *pn547_dev;
struct mutex  ese_cold_reset_sync_mutex;
struct mutex  nci_send_cmd_mutex;
bool is_force_reset_allowed;

//extern ssize_t pn547_dev_read(struct file *filp, char __user *buf,
 //       size_t count, loff_t *offset);

static int8_t prop_nci_rsp[NCI_PROP_RST_RSP_SIZE];

static struct timer_list ese_cold_reset_timer;
static struct completion prop_cmd_resp_sema;
static struct completion ese_cold_reset_sema;

#if KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE
static void ese_cold_reset_gaurd_timer_callback(unsigned long data);
#else
static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused);
#endif
static long start_ese_cold_reset_guard_timer(void);

#if KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE
static void ese_cold_reset_gaurd_timer_callback(unsigned long data)
{
	(void)data;
#else
static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused)
{
#endif
	pr_info("%s: Enter\n", __func__);

	pn547_dev->state_flags &= ~MASK_ESE_COLD_RESET_GUARD_TIMER;
}

static long start_ese_cold_reset_guard_timer(void)
{
	long ret = -EINVAL;

	pr_info("starting ese_cold_reset_timer\n");

	if (timer_pending(&ese_cold_reset_timer) == 1) {
		pr_info("ese_cold_reset_guard_timer: delete pending timer\n");
		/* delete timer if already pending */
		del_timer(&ese_cold_reset_timer);
	}

	pn547_dev->state_flags |= MASK_ESE_COLD_RESET_GUARD_TIMER;

#if KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE
	init_timer(&ese_cold_reset_timer);
	setup_timer(&ese_cold_reset_timer,
		ese_cold_reset_gaurd_timer_callback, 0);
#else
	timer_setup(&ese_cold_reset_timer, ese_cold_reset_gaurd_timer_callback, 0);
#endif
	ret = mod_timer(&ese_cold_reset_timer,
		jiffies + msecs_to_jiffies(ESE_COLD_RESET_GUARD_TIME));
	if (ret)
		pr_info("%s: Error in mod_timer\n", __func__);

	return ret;
}

void set_force_reset(bool value)
{
	is_force_reset_allowed = value;
}

void ese_reset_resource_init(void)
{
	mutex_init(&ese_cold_reset_sync_mutex);
	mutex_init(&nci_send_cmd_mutex);
}

void ese_reset_resource_destroy(void)
{
	mutex_destroy(&ese_cold_reset_sync_mutex);
	mutex_destroy(&nci_send_cmd_mutex);
}

void rcv_prop_resp_status(const char * const buf)
{
	int ret = -1;
	char tmp[MAX_BUFFER_SIZE];
	size_t rcount = 0;

	memset(&prop_nci_rsp, 0, sizeof(prop_nci_rsp));
	memcpy(prop_nci_rsp, buf, 3);
	rcount = (size_t)prop_nci_rsp[2];

	/* Read data: No need to wait for the interrupt */
	ret = i2c_master_recv(pn547_dev->client, tmp, rcount);
	if (ret == rcount) {
		prop_nci_rsp[3] = tmp[0];
		pr_info("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__,
			prop_nci_rsp[0], prop_nci_rsp[1],
			prop_nci_rsp[2], prop_nci_rsp[3]);
	} else {
		pr_err("%s: Failed to receive payload of the cold_rst_cmd\n",
			__func__);
		prop_nci_rsp[3] = -EIO;
	}

	if (pn547_dev->state_flags & (PN547_STATE_NFC_ON))
		complete(&prop_cmd_resp_sema);
}

/******************************************************************************
 * Function    : send_nci_transceive
 *
 * Description : Common NCI command send utility function.
 *
 * Parameters  : prop_cmd      : Data to be sent to NFCC
 *               prop_cmd_size : Length of the data to be sent
 *
 * Returns     : 0 (SUCCESS)/ (-1)otherwise

 *****************************************************************************/
static int send_nci_transceive(uint8_t *prop_cmd, size_t prop_cmd_size)
{
	int ret = 0;
	unsigned int loop = 0x04;
	struct file filp;
	int retry = 1;

	pr_info("%s: Enter\n", __func__);

	filp.private_data = pn547_dev;
	if (pn547_dev->state_flags & PN547_STATE_FW_DNLD) {
		/* If FW DNLD, Operation is not permitted */
		pr_err("%s: Operation is not permitted during fwdnld\n",
				__func__);
		return -ECANCELED;
	}

	mutex_lock(&nci_send_cmd_mutex);

	init_completion(&prop_cmd_resp_sema);

	/* write command to I2C line*/
	do {
		ret = i2c_master_send(pn547_dev->client, prop_cmd, prop_cmd_size);
		if (ret == prop_cmd_size)
			break;

		usleep_range(5000, 6000);
	} while (--loop);

	if (!loop && (ret != prop_cmd_size)) {
		pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
		mutex_unlock(&nci_send_cmd_mutex);
		return -EREMOTEIO;
	}

	ret = 0x00;
	do {
		if (pn547_dev->state_flags & PN547_STATE_NFC_ON)/* NFC_ON */ {
			/* Read is pending from the NFC service which will
			 * complete the prop_cmd_resp_sema
			 */
			if (wait_for_completion_timeout(&prop_cmd_resp_sema,
					msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT)) == 0) {
				pr_err("%s: Timeout\n", __func__);
				ret = prop_nci_rsp[3] = -EAGAIN; // Failure case
			}
		} else { /* NFC_OFF */
			/* call the pn547_dev_read() */
			filp.f_flags &= ~O_NONBLOCK;
			ret = pn547_dev_read(&filp, NULL, 3, 0);
			usleep_range(2000, 3000);
		}
	} while ((retry-- >= 0) && (ret == -ERESTARTSYS || ret == -EFAULT));

	mutex_unlock(&nci_send_cmd_mutex);

	if (ret == 0x00 && prop_nci_rsp[3])
		ret = -1 * prop_nci_rsp[3];

	/* Return the status to the SPI/UWB Driver */
	pr_info("%s: Exit, Status:%d\n", __func__, ret);

	return ret;
}

/******************************************************************************
 * Function    : do_reset_protection
 *
 * Description : It shall be called by SPI driver to enable/disable reset
 *               protection
 *
 * Parameters  : Enable(TRUE)/Disable(FALSE)
 *
 * Returns     :
 *     0           :    OK             < Success case >
 *    -EPERM(-1)   :    REJECTED       < NFCC rejects the cold reset cmd>
 *    -3           :    FAILED         < NFCC responds to cold reset cmd>
 *    -EIO(-5)     :    SYNTAX_ERROR   < NFCC cmd framing is wrong >
 *    -6           :    SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
 *    -9           :    INVALID_PARAM  < NFCC rsp to cold reset cmd >
 *    -EAGAIN(-11) :    < 1. mod_timer(): temp error during kernel alloc >
 *                      < 2. No rsp received from NFCC for cold reset cmd >
 *    -ENOMEM(-12) :    < mod_timer(): failed to allocate memory >
 *    -EINVAL(-22) :    < 1. cold rst req is received from unknown source >
 *                      < 2. mod_timer(): invalid arg is passed>
 *    -EREMOTEIO(-121): < Reset cmd write failure over I2C >
 *    -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
 *    -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
 *****************************************************************************/
int do_reset_protection(bool type)
{
	int ret = 0;
	uint8_t prop_cmd[] = {0x2F, 0x1F, 0x01, 0x00};

	pn547_dev = get_nfcc_dev_data();

	pr_info("%s: Enter cmd type: %d", __func__, type);

	prop_cmd[RST_PROTECTION_CMD_INDEX] = (type) ? 1 : 0;

	if (type) {
		pn547_dev->state_flags |= RST_PROTECTION_ENABLED;
	} else {
		if (!(pn547_dev->state_flags & RST_PROTECTION_ENABLED))
			return ret;
	}

	pr_info("%s: NxpNciX: %d > %02X%02X%02X%02X\n", __func__,
		ret, prop_cmd[0], prop_cmd[1], prop_cmd[2], prop_cmd[3]);

	ret = send_nci_transceive(prop_cmd, sizeof(prop_cmd));
	if (ret)
		pr_err("%s: send_nci_command returned %d\n", __func__, ret);

	if (!type)
		pn547_dev->state_flags &= ~RST_PROTECTION_ENABLED;

	pr_info("%s: Exit, Status:%d state_flag : %x ", __func__, ret,
		pn547_dev->state_flags);

	return ret;
}
EXPORT_SYMBOL(do_reset_protection);

/******************************************************************************
 * Function    : ese_cold_reset
 *
 * Description : It shall be called by NFC/SPI/UWB driver to perform driver to
 *               to driver eSE cold reset.
 *
 * Parameters  : src Source of the cold reset request
 *
 * Returns     :
 *     0           :    OK             < Success case >
 *    -EPERM(-1)   :    REJECTED       < Guard timer running>
 *    -3           :    FAILED         < NFCC responds to cold reset cmd>
 *    -EIO(-5)     :    SYNTAX_ERROR   < NFCC cmd framing is wrong >
 *    -6           :    SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
 *    -9           :    INVALID_PARAM  < NFCC rsp to cold reset cmd >
 *    -EAGAIN(-11) :    < 1. mod_timer(): temp error during kernel alloc >
 *                      < 2. No rsp received from NFCC for cold reset cmd >
 *    -ENOMEM(-12) :    < mod_timer(): failed to allocate memory >
 *    -EBUSY(-16)  :    < eSE busy, in updater mode>
 *    -EINVAL(-22) :    < 1. cold rst req is received from unknown source >
 *                      < 2. mod_timer(): invalid arg is passed>
 *    -EREMOTEIO(-121): < Reset cmd write failure over I2C >
 *    -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
 *    -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
 *****************************************************************************/
int ese_cold_reset(enum ese_cold_reset_origin src)
{
	int ret = 0;
	bool status = false;
	uint8_t ese_cld_reset[] = {0x2F, 0x1E, 0x00};

	pr_info("%s: Enter origin:%d", __func__, src);

	switch (src) {
	case ESE_COLD_RESET_SOURCE_NFC:
	case ESE_COLD_RESET_SOURCE_SPI:
	case ESE_COLD_RESET_SOURCE_UWB:
		break;
	default:
		pr_info("%s: Invalid argument", __func__);
		return -EINVAL;
	}

	pn547_dev = get_nfcc_dev_data();

	mutex_lock(&ese_cold_reset_sync_mutex);

	if (is_force_reset_allowed)
		status = IS_COLD_RESET_ALLOWED_NFC(pn547_dev->state_flags, src);
	else
		status = IS_COLD_RESET_ALLOWED(pn547_dev->state_flags, src);

	if (status) {
		ret = start_ese_cold_reset_guard_timer();

		if (ret) {
			mutex_unlock(&ese_cold_reset_sync_mutex);
			return ret; /* EAGAIN/EINVAL/ENOMEM*/
		}

		pn547_dev->state_flags |= src << ESE_COLD_RESET_ORIGIN_FLAGS_POS;

		init_completion(&ese_cold_reset_sema);

		pr_info("%s: NxpNciX: %d > %02X%02X%02X\n", __func__, ret, ese_cld_reset[0],
			ese_cld_reset[1], ese_cld_reset[2]);

		ret = send_nci_transceive(ese_cld_reset, sizeof(ese_cld_reset));
		if (ret) {
			pn547_dev->state_flags &= ~(MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER);
			mutex_unlock(&ese_cold_reset_sync_mutex);
			return ret;
		}

		/* wait for reboot guard timer*/
		if (!ret && wait_for_completion_timeout(&ese_cold_reset_sema,
			msecs_to_jiffies(ESE_COLD_RESET_REBOOT_GUARD_TIME)) == 0) {
			pr_info("%s: guard Timeout", __func__);
		}
	} else {
		if (IS_RESET_PROTECTION_ENABLED(pn547_dev->state_flags)) {
			pr_err("%s : Not allowed resource busy\n", __func__);
			ret = -EBUSY;
		} else if (IS_COLD_RESET_REQ_IN_PROGRESS(pn547_dev->state_flags)) {
			pr_err("%s : Operation not permitted\n", __func__);
			ret = -EPERM;
		} else {
			/*No Action required*/
		}
	}

	pn547_dev->state_flags &= ~(src << ESE_COLD_RESET_ORIGIN_FLAGS_POS);

	mutex_unlock(&ese_cold_reset_sync_mutex);

	/* Return the status to the SPI/UWB Driver */
	pr_info("%s:%d exit, Status:%d", __func__, src, ret);

	return ret;
}
EXPORT_SYMBOL(ese_cold_reset);