ese_cold_reset.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. *
  5. ***************************************************************************/
  6. /*
  7. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  8. *
  9. ***************************************************************************/
  10. #include <linux/slab.h>
  11. #include <linux/gpio.h>
  12. #include <linux/uaccess.h>
  13. #include "common.h"
  14. /*
  15. * Power management of the eSE
  16. * eSE and NFCC both are powered using VEN gpio,
  17. * VEN HIGH - eSE and NFCC both are powered on
  18. * VEN LOW - eSE and NFCC both are power down
  19. */
  20. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
  21. {
  22. int ret = 0;
  23. if (arg == ESE_POWER_ON) {
  24. /*
  25. * Let's store the NFC VEN pin state
  26. * will check stored value in case of eSE power off request,
  27. * to find out if NFC MW also sent request to set VEN HIGH
  28. * VEN state will remain HIGH if NFC is enabled otherwise
  29. * it will be set as LOW
  30. */
  31. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_dev->configs.gpio.ven);
  32. if (!nfc_dev->nfc_ven_enabled) {
  33. pr_debug("eSE HAL service setting ven HIGH\n");
  34. gpio_set_ven(nfc_dev, 1);
  35. } else {
  36. pr_debug("ven already HIGH\n");
  37. }
  38. nfc_dev->is_ese_session_active = true;
  39. } else if (arg == ESE_POWER_OFF) {
  40. if (!nfc_dev->nfc_ven_enabled) {
  41. pr_debug("NFC not enabled, disabling ven\n");
  42. gpio_set_ven(nfc_dev, 0);
  43. } else {
  44. pr_debug("keep ven high as NFC is enabled\n");
  45. }
  46. nfc_dev->is_ese_session_active = false;
  47. if (nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  48. if(chk_eSE_pwr_off)
  49. up(&sem_eSE_pwr_off);
  50. }
  51. } else if (arg == ESE_POWER_STATE) {
  52. /* get VEN gpio state for eSE, as eSE also enabled through same GPIO */
  53. ret = gpio_get_value(nfc_dev->configs.gpio.ven);
  54. } else {
  55. pr_err("%s bad arg %lu\n", __func__, arg);
  56. ret = -ENOIOCTLCMD;
  57. }
  58. return ret;
  59. }
  60. /**
  61. * send_ese_cmd() - Send eSE command to NFC controller.
  62. * @nfc_dev: NFC device handle.
  63. *
  64. * Return: 0 on pass and negative value on failure.
  65. */
  66. static int send_ese_cmd(struct nfc_dev *nfc_dev)
  67. {
  68. int ret;
  69. if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
  70. dev_err(nfc_dev->nfc_device,
  71. "cannot send ese cmd as FW download is in-progress\n");
  72. return -EBUSY;
  73. }
  74. if (!gpio_get_value(nfc_dev->configs.gpio.ven)) {
  75. dev_err(nfc_dev->nfc_device,
  76. "cannot send ese cmd as NFCC powered off\n");
  77. return -ENODEV;
  78. }
  79. if (nfc_dev->cold_reset.cmd_buf == NULL)
  80. return -EFAULT;
  81. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->cold_reset.cmd_buf,
  82. nfc_dev->cold_reset.cmd_len,
  83. MAX_RETRY_COUNT);
  84. if (ret <= 0)
  85. dev_err(nfc_dev->nfc_device,
  86. "%s: write failed after max retry, ret %d\n",
  87. __func__, ret);
  88. return ret;
  89. }
  90. /**
  91. * read_cold_reset_rsp() - Read response of the cold reset command.
  92. * @nfc_dev: NFC device handle.
  93. * @header: Pointer to NCI header if it is already read.
  94. *
  95. * Return: 0 on pass and negative value on failure.
  96. */
  97. int read_cold_reset_rsp(struct nfc_dev *nfc_dev, char *header)
  98. {
  99. int ret = -EPERM;
  100. struct cold_reset *cold_rst = &nfc_dev->cold_reset;
  101. char *rsp_buf = NULL;
  102. if (cold_rst->rsp_len < COLD_RESET_RSP_LEN) {
  103. dev_err(nfc_dev->nfc_device,
  104. "%s: received cold reset rsp buffer length is invalid \n",
  105. __func__);
  106. return -EINVAL;
  107. }
  108. rsp_buf = kzalloc(cold_rst->rsp_len, GFP_DMA | GFP_KERNEL);
  109. if (!rsp_buf)
  110. return -ENOMEM;
  111. /*
  112. * read header if NFC is disabled
  113. * for enable case, header is read by nfc read thread(for i2c)
  114. */
  115. if ((!cold_rst->is_nfc_enabled) &&
  116. (nfc_dev->interface == PLATFORM_IF_I2C)) {
  117. ret = i2c_master_recv(nfc_dev->i2c_dev.client, rsp_buf, NCI_HDR_LEN);
  118. if (ret <= 0) {
  119. dev_err(nfc_dev->nfc_device,
  120. "%s: failure to read cold reset rsp header\n",
  121. __func__);
  122. ret = -EIO;
  123. goto error;
  124. }
  125. /*
  126. * return failure, if packet is not a response packet or
  127. * if response's OID doesn't match with the CMD's OID
  128. */
  129. if (!(rsp_buf[0] & NCI_RSP_PKT_TYPE) ||
  130. (!cold_rst->cmd_buf) ||
  131. (rsp_buf[1] != cold_rst->cmd_buf[1])) {
  132. dev_err(nfc_dev->nfc_device,
  133. "%s: - invalid cold reset response 0x%x 0x%x\n",
  134. __func__, rsp_buf[0], rsp_buf[1]);
  135. ret = -EINVAL;
  136. goto error;
  137. }
  138. } else if (header) {
  139. memcpy(rsp_buf, header, NCI_HDR_LEN);
  140. } else {
  141. dev_err(nfc_dev->nfc_device,
  142. "%s: - invalid or NULL header\n", __func__);
  143. ret = -EINVAL;
  144. goto error;
  145. }
  146. if ((NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX]) >
  147. cold_rst->rsp_len) {
  148. dev_err(nfc_dev->nfc_device,
  149. "%s: - no space for cold_reset resp\n", __func__);
  150. ret = -ENOMEM;
  151. goto error;
  152. }
  153. if (nfc_dev->interface == PLATFORM_IF_I2C) {
  154. ret = nfc_dev->nfc_read(nfc_dev,
  155. &rsp_buf[NCI_PAYLOAD_IDX],
  156. rsp_buf[NCI_PAYLOAD_LEN_IDX],
  157. NCI_CMD_RSP_TIMEOUT_MS);
  158. if (ret <= 0) {
  159. dev_err(nfc_dev->nfc_device,
  160. "%s: failure to read cold reset rsp payload\n",
  161. __func__);
  162. ret = -EIO;
  163. goto error;
  164. }
  165. ret = cold_rst->status = rsp_buf[NCI_PAYLOAD_IDX];
  166. pr_debug("nfc ese rsp hdr 0x%x 0x%x 0x%x, payload byte0 0x%x\n",
  167. rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3]);
  168. }
  169. error:
  170. kfree(rsp_buf);
  171. return ret;
  172. }
  173. /**
  174. * ese_cold_reset_ioctl() - This function handles the eSE cold reset ioctls.
  175. * @nfc_dev: NFC device handle.
  176. * @arg: ioctl argument.
  177. *
  178. * Return: 0 on pass and negative value on failure.
  179. */
  180. int ese_cold_reset_ioctl(struct nfc_dev *nfc_dev, unsigned long arg)
  181. {
  182. int ret;
  183. struct ese_ioctl_arg ioctl_arg;
  184. struct ese_cold_reset_arg *cold_reset_arg = NULL;
  185. if (!arg) {
  186. dev_err(nfc_dev->nfc_device, "arg is invalid\n");
  187. return -EINVAL;
  188. }
  189. ret = copy_from_user((void *)&ioctl_arg, (const void *)arg,
  190. sizeof(ioctl_arg));
  191. if (ret) {
  192. dev_err(nfc_dev->nfc_device,
  193. "ese ioctl arg copy from user failed\n");
  194. return -EFAULT;
  195. }
  196. cold_reset_arg = kzalloc(sizeof(struct ese_cold_reset_arg), GFP_KERNEL);
  197. if (!cold_reset_arg)
  198. return -ENOMEM;
  199. mutex_lock(&nfc_dev->write_mutex);
  200. ret = copy_struct_from_user(cold_reset_arg,
  201. sizeof(struct ese_cold_reset_arg),
  202. u64_to_user_ptr(ioctl_arg.buf),
  203. sizeof(struct ese_cold_reset_arg));
  204. if (ret) {
  205. dev_err(nfc_dev->nfc_device,
  206. "ese ioctl arg buffer copy from user failed\n");
  207. ret = -EFAULT;
  208. goto err;
  209. }
  210. switch (cold_reset_arg->sub_cmd) {
  211. case ESE_COLD_RESET_DO:
  212. /*
  213. * cold reset allowed during protection enable, only if the
  214. * source is same as the one which enabled protection.
  215. */
  216. if (nfc_dev->cold_reset.is_crp_en &&
  217. (cold_reset_arg->src !=
  218. nfc_dev->cold_reset.last_src_ese_prot)) {
  219. dev_err(nfc_dev->nfc_device,
  220. "cold reset from %d denied, protection is on\n",
  221. cold_reset_arg->src);
  222. ret = -EACCES;
  223. goto err;
  224. }
  225. nfc_dev->cold_reset.cmd_buf = kzalloc(COLD_RESET_CMD_LEN,
  226. GFP_DMA | GFP_KERNEL);
  227. if (!nfc_dev->cold_reset.cmd_buf) {
  228. ret = -ENOMEM;
  229. goto err;
  230. }
  231. nfc_dev->cold_reset.cmd_buf[0] = PROP_NCI_CMD_GID;
  232. nfc_dev->cold_reset.cmd_buf[1] = COLD_RESET_OID;
  233. nfc_dev->cold_reset.cmd_buf[2] = COLD_RESET_CMD_PL_LEN;
  234. nfc_dev->cold_reset.cmd_len = NCI_HDR_LEN +
  235. COLD_RESET_CMD_PL_LEN;
  236. nfc_dev->cold_reset.rsp_len = COLD_RESET_RSP_LEN;
  237. break;
  238. case ESE_COLD_RESET_PROTECT_EN:
  239. if (nfc_dev->cold_reset.is_crp_en) {
  240. if (cold_reset_arg->src !=
  241. nfc_dev->cold_reset.last_src_ese_prot) {
  242. dev_err(nfc_dev->nfc_device,
  243. "ese protection enable denied\n");
  244. ret = -EACCES;
  245. goto err;
  246. }
  247. pr_warn("ese protection already enabled\n");
  248. ret = 0;
  249. /* free buffers and exit with pass */
  250. goto err;
  251. }
  252. fallthrough;
  253. case ESE_COLD_RESET_PROTECT_DIS:
  254. if (nfc_dev->cold_reset.is_crp_en &&
  255. cold_reset_arg->src !=
  256. nfc_dev->cold_reset.last_src_ese_prot) {
  257. pr_err("ese cold reset protection disable denied\n");
  258. ret = -EACCES;
  259. goto err;
  260. }
  261. nfc_dev->cold_reset.cmd_buf = kzalloc(COLD_RESET_PROT_CMD_LEN,
  262. GFP_DMA | GFP_KERNEL);
  263. if (!nfc_dev->cold_reset.cmd_buf) {
  264. ret = -ENOMEM;
  265. goto err;
  266. }
  267. nfc_dev->cold_reset.cmd_buf[0] = PROP_NCI_CMD_GID;
  268. nfc_dev->cold_reset.cmd_buf[1] = COLD_RESET_PROT_OID;
  269. nfc_dev->cold_reset.cmd_buf[2] = COLD_RESET_PROT_CMD_PL_LEN;
  270. nfc_dev->cold_reset.cmd_len = NCI_HDR_LEN +
  271. COLD_RESET_PROT_CMD_PL_LEN;
  272. nfc_dev->cold_reset.rsp_len = COLD_RESET_PROT_RSP_LEN;
  273. if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_EN)
  274. nfc_dev->cold_reset.cmd_buf[3] = 0x1;
  275. else
  276. nfc_dev->cold_reset.cmd_buf[3] = 0x0;
  277. break;
  278. default:
  279. pr_err("%s invalid ese ioctl sub cmd %d\n", __func__,
  280. cold_reset_arg->sub_cmd);
  281. ret = -ENOIOCTLCMD;
  282. goto err;
  283. }
  284. pr_debug("nfc ese cmd hdr 0x%x 0x%x 0x%x\n",
  285. nfc_dev->cold_reset.cmd_buf[0],
  286. nfc_dev->cold_reset.cmd_buf[1],
  287. nfc_dev->cold_reset.cmd_buf[2]);
  288. ret = send_ese_cmd(nfc_dev);
  289. if (ret <= 0) {
  290. pr_err("failed to send ese command\n");
  291. goto err;
  292. }
  293. nfc_dev->cold_reset.rsp_pending = true;
  294. /* check if NFC is enabled */
  295. if (nfc_dev->cold_reset.is_nfc_enabled) {
  296. /*
  297. * nfc_read thread will initiate cold reset response
  298. * and it will signal for data available
  299. */
  300. wait_event_interruptible(nfc_dev->cold_reset.read_wq,
  301. !nfc_dev->cold_reset.rsp_pending);
  302. } else {
  303. /*
  304. * Read data as NFC read thread is not active
  305. */
  306. if (nfc_dev->interface == PLATFORM_IF_I2C) {
  307. ret = is_nfc_data_available_for_read(nfc_dev);
  308. if (ret <= 0) {
  309. nfc_dev->nfc_disable_intr(nfc_dev);
  310. nfc_dev->cold_reset.rsp_pending = false;
  311. goto err;
  312. }
  313. ret = read_cold_reset_rsp(nfc_dev, NULL);
  314. nfc_dev->cold_reset.rsp_pending = false;
  315. if (ret < 0) {
  316. pr_err("%s rsp read err\n", __func__);
  317. goto err;
  318. }
  319. } else {
  320. /*
  321. * Enable intr as it is disabled when NFC is in disable
  322. * state
  323. */
  324. nfc_dev->nfc_enable_intr(nfc_dev);
  325. wait_event_interruptible(
  326. nfc_dev->cold_reset.read_wq,
  327. !nfc_dev->cold_reset.rsp_pending);
  328. }
  329. nfc_dev->nfc_disable_intr(nfc_dev);
  330. }
  331. if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_EN) {
  332. nfc_dev->cold_reset.is_crp_en = true;
  333. nfc_dev->cold_reset.last_src_ese_prot = cold_reset_arg->src;
  334. } else if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_DIS) {
  335. nfc_dev->cold_reset.is_crp_en = false;
  336. nfc_dev->cold_reset.last_src_ese_prot =
  337. ESE_COLD_RESET_ORIGIN_NONE;
  338. } else
  339. pr_debug("ese cmd is %d\n", cold_reset_arg->sub_cmd);
  340. ret = nfc_dev->cold_reset.status;
  341. err:
  342. kfree(nfc_dev->cold_reset.cmd_buf);
  343. nfc_dev->cold_reset.cmd_buf = NULL;
  344. kfree(cold_reset_arg);
  345. cold_reset_arg = NULL;
  346. mutex_unlock(&nfc_dev->write_mutex);
  347. return ret;
  348. }