common_ese.c 11 KB

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