common_ese.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /******************************************************************************
  2. * Copyright (C) 2020-2021 NXP
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. ******************************************************************************/
  19. #include <linux/jiffies.h>
  20. #include <linux/delay.h>
  21. #include <linux/gpio.h>
  22. #include "common_ese.h"
  23. static void cold_reset_gaurd_timer_callback(struct timer_list *t)
  24. {
  25. struct cold_reset *cold_reset = from_timer(cold_reset, t, timer);
  26. NFC_LOG_REC("%s: entry\n", __func__);
  27. cold_reset->in_progress = false;
  28. }
  29. static long start_cold_reset_guard_timer(struct cold_reset *cold_reset)
  30. {
  31. long ret = -EINVAL;
  32. if (timer_pending(&cold_reset->timer) == 1) {
  33. NFC_LOG_REC("%s: delete pending timer\n", __func__);
  34. /* delete timer if already pending */
  35. del_timer(&cold_reset->timer);
  36. }
  37. cold_reset->in_progress = true;
  38. timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
  39. ret = mod_timer(&cold_reset->timer,
  40. jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME_MS));
  41. return ret;
  42. }
  43. static int send_cold_reset_protection_cmd(struct nfc_dev *nfc_dev,
  44. bool requestType)
  45. {
  46. int ret = 0;
  47. int cmd_length = 0;
  48. uint8_t *cmd = nfc_dev->write_kbuf;
  49. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  50. *cmd++ = NCI_PROP_MSG_CMD;
  51. if (requestType) { /* reset protection */
  52. *cmd++ = RST_PROT_OID;
  53. *cmd++ = RST_PROT_PAYLOAD_SIZE;
  54. *cmd++ = (!cold_reset->reset_protection) ? 1 : 0;
  55. } else { /* cold reset */
  56. *cmd++ = CLD_RST_OID;
  57. *cmd++ = CLD_RST_PAYLOAD_SIZE;
  58. }
  59. cmd_length = cmd - nfc_dev->write_kbuf;
  60. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, cmd_length,
  61. MAX_RETRY_COUNT);
  62. if (ret != cmd_length) {
  63. ret = -EIO;
  64. NFC_LOG_ERR("%s: nfc_write returned %d\n", __func__, ret);
  65. goto exit;
  66. }
  67. cmd = nfc_dev->write_kbuf;
  68. if (requestType)
  69. NFC_LOG_DBG(" %s: NxpNciX: %d > 0x%02x%02x%02x%02x\n", __func__,
  70. ret, cmd[NCI_HDR_IDX], cmd[NCI_HDR_OID_IDX],
  71. cmd[NCI_PAYLOAD_LEN_IDX], cmd[NCI_PAYLOAD_IDX]);
  72. else
  73. NFC_LOG_DBG(" %s: NxpNciX: %d > 0x%02x%02x%02x\n", __func__, ret,
  74. cmd[NCI_HDR_IDX], cmd[NCI_HDR_OID_IDX],
  75. cmd[NCI_PAYLOAD_LEN_IDX]);
  76. exit:
  77. return ret;
  78. }
  79. void wakeup_on_prop_rsp(struct nfc_dev *nfc_dev, uint8_t *buf)
  80. {
  81. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  82. cold_reset->status = -EIO;
  83. if ((NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX]) != NCI_PROP_MSG_RSP_LEN)
  84. NFC_LOG_ERR("%s: invalid response for cold_reset/protection\n",
  85. __func__);
  86. else
  87. cold_reset->status = buf[NCI_PAYLOAD_IDX];
  88. NFC_LOG_DBG(" %s: NxpNciR 0x%02x%02x%02x%02x\n", __func__,
  89. buf[NCI_HDR_IDX], buf[NCI_HDR_OID_IDX],
  90. buf[NCI_PAYLOAD_LEN_IDX], buf[NCI_PAYLOAD_IDX]);
  91. cold_reset->rsp_pending = false;
  92. wake_up_interruptible(&cold_reset->read_wq);
  93. }
  94. static int validate_cold_reset_protection_request(struct cold_reset *cold_reset,
  95. unsigned long arg)
  96. {
  97. int ret = 0;
  98. if (!cold_reset->reset_protection) {
  99. if (IS_RST_PROT_EN_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
  100. NFC_LOG_REC("%s: reset protection enable\n", __func__);
  101. } else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
  102. NFC_LOG_REC("%s: cold reset\n", __func__);
  103. } else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
  104. NFC_LOG_REC("%s: reset protection already disable\n",
  105. __func__);
  106. ret = -EINVAL;
  107. } else {
  108. NFC_LOG_ERR("%s: operation not permitted\n", __func__);
  109. ret = -EPERM;
  110. }
  111. } else {
  112. if (IS_RST_PROT_DIS_REQ(arg) &&
  113. IS_SRC(arg, cold_reset->rst_prot_src)) {
  114. NFC_LOG_REC("%s: disable reset protection from same src\n",
  115. __func__);
  116. } else if (IS_CLD_RST_REQ(arg) &&
  117. IS_SRC(arg, cold_reset->rst_prot_src)) {
  118. NFC_LOG_REC("%s: cold reset from same source\n", __func__);
  119. } else if (IS_RST_PROT_EN_REQ(arg) &&
  120. IS_SRC(arg, cold_reset->rst_prot_src)) {
  121. NFC_LOG_REC("%s: enable reset protection from same src\n",
  122. __func__);
  123. } else {
  124. NFC_LOG_ERR("%s: operation not permitted\n", __func__);
  125. ret = -EPERM;
  126. }
  127. }
  128. return ret;
  129. }
  130. static int perform_cold_reset_protection(struct nfc_dev *nfc_dev,
  131. unsigned long arg)
  132. {
  133. int ret = 0;
  134. int timeout = 0;
  135. char *rsp = nfc_dev->read_kbuf;
  136. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  137. /* check if NFCC not in the FW download or hard reset state */
  138. ret = validate_nfc_state_nci(nfc_dev);
  139. if (ret < 0) {
  140. NFC_LOG_ERR("%s: invalid state\n", __func__);
  141. return ret;
  142. }
  143. /* check if NFCC not in the FW download or hard reset state */
  144. ret = validate_cold_reset_protection_request(cold_reset, arg);
  145. if (ret < 0) {
  146. NFC_LOG_ERR("%s: invalid cmd\n", __func__);
  147. goto err;
  148. }
  149. /* check if cold reset already in progress */
  150. if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
  151. NFC_LOG_ERR("%s: cold reset already in progress\n", __func__);
  152. ret = -EBUSY;
  153. goto err;
  154. }
  155. /* enable interrupt if not enabled incase when devnode not opened by HAL */
  156. nfc_dev->nfc_enable_intr(nfc_dev);
  157. mutex_lock(&nfc_dev->write_mutex);
  158. /* write api has 15ms maximum wait to clear any pending read before */
  159. cold_reset->status = -EIO;
  160. cold_reset->rsp_pending = true;
  161. ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
  162. if (ret < 0) {
  163. mutex_unlock(&nfc_dev->write_mutex);
  164. cold_reset->rsp_pending = false;
  165. NFC_LOG_ERR("%s: failed to send cold reset/protection cmd\n",
  166. __func__);
  167. goto err;
  168. }
  169. ret = 0;
  170. /* start the cold reset guard timer */
  171. if (IS_CLD_RST_REQ(arg)) {
  172. /* Guard timer not needed when OSU over NFC */
  173. if (!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
  174. ret = start_cold_reset_guard_timer(cold_reset);
  175. if (ret) {
  176. mutex_unlock(&nfc_dev->write_mutex);
  177. NFC_LOG_ERR("%s: error in mod_timer\n", __func__);
  178. goto err;
  179. }
  180. }
  181. }
  182. timeout = NCI_CMD_RSP_TIMEOUT_MS;
  183. do {
  184. /* call read api directly if reader thread is not blocked */
  185. if (mutex_trylock(&nfc_dev->read_mutex)) {
  186. pr_debug("%s: reader thread not pending\n", __func__);
  187. ret = nfc_dev->nfc_read(nfc_dev, rsp, 3,
  188. timeout);
  189. mutex_unlock(&nfc_dev->read_mutex);
  190. if (!ret)
  191. break;
  192. usleep_range(READ_RETRY_WAIT_TIME_US,
  193. READ_RETRY_WAIT_TIME_US + 500);
  194. /* Read pending response form the HAL service */
  195. } else if (!wait_event_interruptible_timeout(
  196. cold_reset->read_wq,
  197. cold_reset->rsp_pending == false,
  198. msecs_to_jiffies(timeout))) {
  199. pr_err("%s: cold reset/prot response timeout\n", __func__);
  200. ret = -EAGAIN;
  201. }
  202. } while (ret == -ERESTARTSYS || ret == -EFAULT);
  203. mutex_unlock(&nfc_dev->write_mutex);
  204. timeout = ESE_CLD_RST_REBOOT_GUARD_TIME_MS;
  205. if (ret == 0) { /* success case */
  206. ret = cold_reset->status;
  207. if (IS_RST_PROT_REQ(arg)) {
  208. cold_reset->reset_protection = IS_RST_PROT_EN_REQ(arg);
  209. cold_reset->rst_prot_src = IS_RST_PROT_EN_REQ(arg) ?
  210. GET_SRC(arg) :
  211. SRC_NONE;
  212. /* wait for reboot guard timer */
  213. } else if (wait_event_interruptible_timeout(
  214. cold_reset->read_wq, true,
  215. msecs_to_jiffies(timeout)) == 0) {
  216. NFC_LOG_INFO("%s: reboot guard timer timeout\n", __func__);
  217. }
  218. }
  219. err:
  220. return ret;
  221. }
  222. /**
  223. * nfc_ese_pwr() - power control for ese
  224. * @nfc_dev: nfc device data structure
  225. * @arg: mode that we want to move to
  226. *
  227. * Device power control. Depending on the arg value, device moves to
  228. * different states, refer common_ese.h for args
  229. *
  230. * Return: -ENOIOCTLCMD if arg is not supported
  231. * 0 if Success(or no issue)
  232. * 0 or 1 in case of arg is ESE_POWER_STATE
  233. * and error ret code otherwise
  234. */
  235. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
  236. {
  237. int ret = 0;
  238. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  239. if (arg == ESE_POWER_ON) {
  240. /*
  241. * Let's store the NFC VEN pin state
  242. * will check stored value in case of eSE power off request,
  243. * to find out if NFC MW also sent request to set VEN HIGH
  244. * VEN state will remain HIGH if NFC is enabled otherwise
  245. * it will be set as LOW
  246. */
  247. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_gpio->ven);
  248. if (!nfc_dev->nfc_ven_enabled) {
  249. NFC_LOG_REC("%s: ese hal service setting ven high\n",
  250. __func__);
  251. gpio_set_ven(nfc_dev, 1);
  252. } else {
  253. NFC_LOG_REC("%s: ven already high\n", __func__);
  254. }
  255. } else if (arg == ESE_POWER_OFF) {
  256. if (!nfc_dev->nfc_ven_enabled) {
  257. NFC_LOG_REC("%s: nfc not enabled, disabling ven\n",
  258. __func__);
  259. gpio_set_ven(nfc_dev, 0);
  260. } else {
  261. NFC_LOG_REC("%s: keep ven high as nfc is enabled\n",
  262. __func__);
  263. }
  264. } else if (arg == ESE_POWER_STATE) {
  265. /* eSE get power state */
  266. ret = gpio_get_value(nfc_gpio->ven);
  267. } else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
  268. ret = perform_cold_reset_protection(nfc_dev, arg);
  269. } else {
  270. NFC_LOG_ERR("%s: bad arg %lu\n", __func__, arg);
  271. ret = -ENOIOCTLCMD;
  272. }
  273. return ret;
  274. }
  275. EXPORT_SYMBOL(nfc_ese_pwr);
  276. #define ESE_LEGACY_INTERFACE
  277. #ifdef ESE_LEGACY_INTERFACE
  278. static struct nfc_dev *nfc_dev_legacy;
  279. /******************************************************************************
  280. * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
  281. * to perform cold reset only
  282. * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
  283. *
  284. * Returns:- 0 in case of success and negative values in case of failure
  285. *****************************************************************************/
  286. int perform_ese_cold_reset(unsigned long arg)
  287. {
  288. int ret = 0;
  289. if (nfc_dev_legacy) {
  290. if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
  291. ret = nfc_ese_pwr(nfc_dev_legacy, arg);
  292. } else {
  293. NFC_LOG_ERR("%s: operation not permitted\n", __func__);
  294. return -EPERM;
  295. }
  296. }
  297. NFC_LOG_DBG("%s: arg = %lu ret = %d\n", __func__, arg, ret);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL(perform_ese_cold_reset);
  301. #endif /* ESE_LEGACY_INTERFACE */
  302. void ese_cold_reset_release(struct nfc_dev *nfc_dev)
  303. {
  304. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  305. cold_reset->rsp_pending = false;
  306. cold_reset->in_progress = false;
  307. if (timer_pending(&cold_reset->timer) == 1)
  308. del_timer(&cold_reset->timer);
  309. }
  310. void common_ese_init(struct nfc_dev *nfc_dev)
  311. {
  312. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  313. cold_reset->reset_protection = false;
  314. cold_reset->rst_prot_src = SRC_NONE;
  315. init_waitqueue_head(&cold_reset->read_wq);
  316. ese_cold_reset_release(nfc_dev);
  317. #ifdef ESE_LEGACY_INTERFACE
  318. nfc_dev_legacy = nfc_dev;
  319. #endif /* ESE_LEGACY_INTERFACE */
  320. }
  321. void common_ese_exit(struct nfc_dev *nfc_dev)
  322. {
  323. #ifdef ESE_LEGACY_INTERFACE
  324. nfc_dev_legacy = NULL;
  325. #endif /* ESE_LEGACY_INTERFACE */
  326. }