ese_cold_reset.c 9.8 KB

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