common_ese.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /******************************************************************************
  2. * Copyright (C) 2020-2022 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. pr_debug("%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. pr_debug("%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. pr_err("%s: nfc_write returned %d\n", __func__, ret);
  65. goto exit;
  66. }
  67. cmd = nfc_dev->write_kbuf;
  68. if (requestType)
  69. pr_debug(" %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. pr_debug(" %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. pr_err("%s: invalid response for cold_reset/protection\n",
  85. __func__);
  86. else
  87. cold_reset->status = buf[NCI_PAYLOAD_IDX];
  88. pr_debug(" %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. pr_debug("%s: reset protection enable\n", __func__);
  101. } else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
  102. pr_debug("%s: cold reset\n", __func__);
  103. } else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
  104. pr_debug("%s: reset protection already disable\n",
  105. __func__);
  106. ret = -EINVAL;
  107. } else {
  108. pr_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. pr_debug("%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. pr_debug("%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. pr_debug("%s: enable reset protection from same src\n",
  122. __func__);
  123. ret = -EINVAL;
  124. } else {
  125. pr_err("%s: operation not permitted\n", __func__);
  126. ret = -EPERM;
  127. }
  128. }
  129. return ret;
  130. }
  131. static int perform_cold_reset_protection(struct nfc_dev *nfc_dev,
  132. unsigned long arg)
  133. {
  134. int ret = 0;
  135. int timeout = 0;
  136. int retry_cnt = 0;
  137. char *rsp = nfc_dev->read_kbuf;
  138. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  139. /* check if NFCC not in the FW download or hard reset state */
  140. ret = validate_nfc_state_nci(nfc_dev);
  141. if (ret < 0) {
  142. pr_err("%s: invalid cmd\n", __func__);
  143. return ret;
  144. }
  145. /* check if NFCC not in the FW download or hard reset state */
  146. ret = validate_cold_reset_protection_request(cold_reset, arg);
  147. if (ret < 0) {
  148. goto err;
  149. }
  150. /* check if cold reset already in progress */
  151. if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
  152. pr_err("%s: cold reset already in progress\n", __func__);
  153. ret = -EBUSY;
  154. goto err;
  155. }
  156. /* enable interrupt if not enabled incase when devnode not opened by HAL */
  157. nfc_dev->nfc_enable_intr(nfc_dev);
  158. mutex_lock(&nfc_dev->write_mutex);
  159. /* write api has 15ms maximum wait to clear any pending read before */
  160. cold_reset->status = -EIO;
  161. cold_reset->rsp_pending = true;
  162. ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
  163. if (ret < 0) {
  164. mutex_unlock(&nfc_dev->write_mutex);
  165. cold_reset->rsp_pending = false;
  166. pr_err("%s: failed to send cold reset/protection cmd\n",
  167. __func__);
  168. goto err;
  169. }
  170. ret = 0;
  171. /* start the cold reset guard timer */
  172. if (IS_CLD_RST_REQ(arg)) {
  173. /* Guard timer not needed when OSU over NFC */
  174. if (!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
  175. ret = start_cold_reset_guard_timer(cold_reset);
  176. if (ret) {
  177. mutex_unlock(&nfc_dev->write_mutex);
  178. pr_err("%s: error in mod_timer\n", __func__);
  179. goto err;
  180. }
  181. }
  182. }
  183. timeout = NCI_CMD_RSP_TIMEOUT_MS;
  184. mutex_lock(&nfc_dev->dev_ref_mutex);
  185. do {
  186. if (nfc_dev->cold_reset.is_nfc_read_pending) {
  187. if (!wait_event_interruptible_timeout
  188. (cold_reset->read_wq,
  189. cold_reset->rsp_pending == false,
  190. msecs_to_jiffies(timeout))) {
  191. pr_err("%s: cold reset/prot response timeout\n",
  192. __func__);
  193. if (retry_cnt <= 1) {
  194. retry_cnt = retry_cnt + 1;
  195. ret = -EAGAIN;
  196. } else {
  197. pr_debug("%s: Maximum retry reached",
  198. __func__);
  199. ret = -ETIMEDOUT;
  200. }
  201. }
  202. } else {
  203. ret = nfc_dev->nfc_read(nfc_dev, rsp, 3, timeout);
  204. if (!ret)
  205. break;
  206. usleep_range(READ_RETRY_WAIT_TIME_US,
  207. READ_RETRY_WAIT_TIME_US + 500);
  208. }
  209. } while (ret == -ERESTARTSYS || ret == -EFAULT || ret == -EAGAIN);
  210. mutex_unlock(&nfc_dev->dev_ref_mutex);
  211. mutex_unlock(&nfc_dev->write_mutex);
  212. timeout = ESE_CLD_RST_REBOOT_GUARD_TIME_MS;
  213. if (ret == 0) { /* success case */
  214. ret = cold_reset->status;
  215. if (IS_RST_PROT_REQ(arg)) {
  216. cold_reset->reset_protection = IS_RST_PROT_EN_REQ(arg);
  217. cold_reset->rst_prot_src = IS_RST_PROT_EN_REQ(arg) ?
  218. GET_SRC(arg) : SRC_NONE;
  219. /* wait for reboot guard timer */
  220. } else {
  221. if (wait_event_interruptible_timeout
  222. (cold_reset->read_wq, true,
  223. msecs_to_jiffies(timeout)) == 0) {
  224. pr_info("%s: reboot guard timer timeout\n",
  225. __func__);
  226. }
  227. }
  228. }
  229. err:
  230. mutex_unlock(&nfc_dev->dev_ref_mutex);
  231. return ret;
  232. }
  233. /**
  234. * nfc_ese_pwr() - power control for ese
  235. * @nfc_dev: nfc device data structure
  236. * @arg: mode that we want to move to
  237. *
  238. * Device power control. Depending on the arg value, device moves to
  239. * different states, refer common_ese.h for args
  240. *
  241. * Return: -ENOIOCTLCMD if arg is not supported
  242. * 0 if Success(or no issue)
  243. * 0 or 1 in case of arg is ESE_POWER_STATE
  244. * and error ret code otherwise
  245. */
  246. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
  247. {
  248. int ret = 0;
  249. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  250. if (arg == ESE_POWER_ON) {
  251. /*
  252. * Let's store the NFC VEN pin state
  253. * will check stored value in case of eSE power off request,
  254. * to find out if NFC MW also sent request to set VEN HIGH
  255. * VEN state will remain HIGH if NFC is enabled otherwise
  256. * it will be set as LOW
  257. */
  258. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_gpio->ven);
  259. if (!nfc_dev->nfc_ven_enabled) {
  260. pr_debug("%s: ese hal service setting ven high\n",
  261. __func__);
  262. gpio_set_ven(nfc_dev, 1);
  263. } else {
  264. pr_debug("%s: ven already high\n", __func__);
  265. }
  266. } else if (arg == ESE_POWER_OFF) {
  267. if (!nfc_dev->nfc_ven_enabled) {
  268. pr_debug("%s: nfc not enabled, disabling ven\n",
  269. __func__);
  270. gpio_set_ven(nfc_dev, 0);
  271. } else {
  272. pr_debug("%s: keep ven high as nfc is enabled\n",
  273. __func__);
  274. }
  275. } else if (arg == ESE_POWER_STATE) {
  276. /* eSE get power state */
  277. ret = gpio_get_value(nfc_gpio->ven);
  278. } else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
  279. ret = perform_cold_reset_protection(nfc_dev, arg);
  280. } else {
  281. pr_err("%s: bad arg %lu\n", __func__, arg);
  282. ret = -ENOIOCTLCMD;
  283. }
  284. return ret;
  285. }
  286. EXPORT_SYMBOL(nfc_ese_pwr);
  287. #define ESE_LEGACY_INTERFACE
  288. #ifdef ESE_LEGACY_INTERFACE
  289. static struct nfc_dev *nfc_dev_legacy;
  290. /******************************************************************************
  291. * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
  292. * to perform cold reset only
  293. * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
  294. *
  295. * Returns:- 0 in case of success and negative values in case of failure
  296. *****************************************************************************/
  297. int perform_ese_cold_reset(unsigned long arg)
  298. {
  299. int ret = 0;
  300. if (nfc_dev_legacy) {
  301. if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
  302. ret = nfc_ese_pwr(nfc_dev_legacy, arg);
  303. } else {
  304. pr_err("%s: operation not permitted\n", __func__);
  305. return -EPERM;
  306. }
  307. }
  308. pr_debug("%s: arg = %d ret = %lu\n", __func__, arg, ret);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL(perform_ese_cold_reset);
  312. #endif /* ESE_LEGACY_INTERFACE */
  313. void ese_cold_reset_release(struct nfc_dev *nfc_dev)
  314. {
  315. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  316. cold_reset->rsp_pending = false;
  317. cold_reset->in_progress = false;
  318. if (timer_pending(&cold_reset->timer) == 1)
  319. del_timer(&cold_reset->timer);
  320. }
  321. void common_ese_init(struct nfc_dev *nfc_dev)
  322. {
  323. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  324. cold_reset->reset_protection = false;
  325. cold_reset->rst_prot_src = SRC_NONE;
  326. init_waitqueue_head(&cold_reset->read_wq);
  327. ese_cold_reset_release(nfc_dev);
  328. #ifdef ESE_LEGACY_INTERFACE
  329. nfc_dev_legacy = nfc_dev;
  330. #endif /* ESE_LEGACY_INTERFACE */
  331. }
  332. void common_ese_exit(struct nfc_dev *nfc_dev)
  333. {
  334. #ifdef ESE_LEGACY_INTERFACE
  335. nfc_dev_legacy = NULL;
  336. #endif /* ESE_LEGACY_INTERFACE */
  337. }