common_ese.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/kernel.h>
  20. #include <linux/i2c.h>
  21. #include <linux/irq.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/delay.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/version.h>
  26. #include <linux/fs.h>
  27. #include "common.h"
  28. #include "common_ese.h"
  29. static void cold_reset_gaurd_timer_callback(struct timer_list *t)
  30. {
  31. struct cold_reset *cold_reset = from_timer(cold_reset, t, timer);
  32. pr_debug("%s: Enter\n", __func__);
  33. cold_reset->in_progress = false;
  34. }
  35. static long start_cold_reset_guard_timer(struct cold_reset *cold_reset)
  36. {
  37. long ret = -EINVAL;
  38. if (timer_pending(&cold_reset->timer) == 1) {
  39. pr_debug("ese_cold_reset_guard_timer: delete pending timer\n");
  40. /* delete timer if already pending */
  41. del_timer(&cold_reset->timer);
  42. }
  43. cold_reset->in_progress = true;
  44. timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
  45. ret = mod_timer(&cold_reset->timer,
  46. jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME));
  47. return ret;
  48. }
  49. static int send_cold_reset_protection_cmd(struct nfc_dev *nfc_dev, bool requestType)
  50. {
  51. int ret = 0;
  52. int cmd_length = 0;
  53. uint8_t *cmd = nfc_dev->write_kbuf;
  54. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  55. mutex_lock(&nfc_dev->write_mutex);
  56. *cmd++ = NCI_PROP_MSG_CMD;
  57. if (requestType) { /*reset protection*/
  58. *cmd++ = RST_PROT_OID;
  59. *cmd++ = RST_PROT_PAYLOAD_SIZE;
  60. *cmd++ = (!cold_reset->reset_protection) ? 1 : 0;
  61. } else { /*cold reset*/
  62. *cmd++ = CLD_RST_OID;
  63. *cmd++ = CLD_RST_PAYLOAD_SIZE;
  64. }
  65. cmd_length = cmd - nfc_dev->write_kbuf;
  66. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, cmd_length, MAX_RETRY_COUNT);
  67. if (ret != cmd_length) {
  68. ret = -EIO;
  69. pr_err("%s : nfc_write returned %d\n", __func__, ret);
  70. goto exit;
  71. }
  72. cmd = nfc_dev->write_kbuf;
  73. if (requestType) {
  74. pr_debug("%s: NxpNciX: %d > %02X%02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
  75. cmd[2], cmd[3]);
  76. } else {
  77. pr_debug("%s: NxpNciX: %d > %02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
  78. cmd[2]);
  79. }
  80. exit:
  81. mutex_unlock(&nfc_dev->write_mutex);
  82. return ret;
  83. }
  84. void wakeup_on_prop_rsp(struct nfc_dev *nfc_dev, uint8_t *buf)
  85. {
  86. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  87. cold_reset->status = -EIO;
  88. if ((NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX]) != NCI_PROP_MSG_RSP_LEN)
  89. pr_err("%s: - invalid response for cold_reset/protection\n", __func__);
  90. else
  91. cold_reset->status = buf[NCI_PAYLOAD_IDX];
  92. pr_debug("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__, buf[0], buf[1],
  93. buf[2], buf[3]);
  94. cold_reset->rsp_pending = false;
  95. wake_up_interruptible(&cold_reset->read_wq);
  96. }
  97. static int validate_cold_reset_protection_request(struct cold_reset *cold_reset,
  98. unsigned long arg)
  99. {
  100. int ret = 0;
  101. if (!cold_reset->reset_protection) {
  102. if (IS_RST_PROT_EN_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
  103. pr_debug("%s:req - reset protection enable\n", __func__);
  104. } else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
  105. pr_debug("%s:req - cold reset\n", __func__);
  106. } else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
  107. pr_debug("%s:req - reset protection already disable\n", __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:req - disable reset protection from same src\n", __func__);
  117. } else if (IS_CLD_RST_REQ(arg)
  118. && IS_SRC(arg, cold_reset->rst_prot_src)) {
  119. pr_debug("%s:req - cold reset from same source\n", __func__);
  120. } else if (IS_RST_PROT_EN_REQ(arg)
  121. && IS_SRC(arg, cold_reset->rst_prot_src)) {
  122. pr_debug("%s:req - enable reset protection from same src\n", __func__);
  123. } else {
  124. pr_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, unsigned long arg)
  131. {
  132. int ret = 0;
  133. struct file filp;
  134. char *rsp = nfc_dev->read_kbuf;
  135. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  136. bool nfc_dev_opened = false;
  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. pr_err("%s: invalid cmd", __func__);
  141. return ret;
  142. }
  143. /* check if NFC is enabled */
  144. mutex_lock(&nfc_dev->dev_ref_mutex);
  145. nfc_dev_opened = (nfc_dev->dev_ref_count > 0) ? true : false;
  146. /*check if NFCC not in the FW download or hard reset state */
  147. ret = validate_cold_reset_protection_request(cold_reset, arg);
  148. if (ret < 0) {
  149. pr_err("%s: invalid cmd", __func__);
  150. goto err;
  151. }
  152. /*check if cold reset already in progress */
  153. if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
  154. pr_err("%s: cold reset already in progress", __func__);
  155. ret = -EBUSY;
  156. goto err;
  157. }
  158. /* set default value for status as failure */
  159. cold_reset->status = -EIO;
  160. cold_reset->rsp_pending = true;
  161. /*enable interrupt before sending cmd, when devnode not opened by HAL */
  162. if (!nfc_dev_opened)
  163. nfc_dev->nfc_enable_intr(nfc_dev);
  164. ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
  165. if (ret < 0) {
  166. pr_err("failed to send cold reset/protection command\n");
  167. cold_reset->rsp_pending = false;
  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. pr_err("%s: Error in mod_timer\n", __func__);
  178. goto err;
  179. }
  180. }
  181. }
  182. do {
  183. /* Read is pending from the HAL service which will complete the response */
  184. if (nfc_dev_opened) {
  185. if (!wait_event_interruptible_timeout
  186. (cold_reset->read_wq,
  187. cold_reset->rsp_pending == false,
  188. msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT))) {
  189. pr_err("%s:cold reset/protection response timeout\n", __func__);
  190. ret = -EAGAIN;
  191. }
  192. } else {
  193. /* Read data as NFC thread is not active */
  194. filp.private_data = nfc_dev;
  195. if (nfc_dev->interface == PLATFORM_IF_I2C) {
  196. filp.f_flags &= ~O_NONBLOCK;
  197. ret = nfc_dev->nfc_read(nfc_dev, rsp, 3, NCI_CMD_RSP_TIMEOUT);
  198. if (!ret)
  199. break;
  200. usleep_range(READ_RETRY_WAIT_TIME_USEC,
  201. READ_RETRY_WAIT_TIME_USEC + 500);
  202. }
  203. }
  204. } while (ret == -ERESTARTSYS || ret == -EFAULT);
  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 =
  210. IS_RST_PROT_EN_REQ(arg) ? GET_SRC(arg) : SRC_NONE;
  211. /* wait for reboot guard timer */
  212. } else if (wait_event_interruptible_timeout
  213. (cold_reset->read_wq, true,
  214. msecs_to_jiffies(ESE_CLD_RST_REBOOT_GUARD_TIME)) ==
  215. 0) {
  216. pr_info("%s: reboot guard timer timeout", __func__);
  217. }
  218. }
  219. err:
  220. mutex_unlock(&nfc_dev->dev_ref_mutex);
  221. return ret;
  222. }
  223. /*
  224. * Power management of the eSE
  225. * eSE and NFCC both are powered using VEN gpio,
  226. * VEN HIGH - eSE and NFCC both are powered on
  227. * VEN LOW - eSE and NFCC both are power down
  228. */
  229. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
  230. {
  231. int ret = 0;
  232. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  233. if (arg == ESE_POWER_ON) {
  234. /**
  235. * Let's store the NFC VEN pin state
  236. * will check stored value in case of eSE power off request,
  237. * to find out if NFC MW also sent request to set VEN HIGH
  238. * VEN state will remain HIGH if NFC is enabled otherwise
  239. * it will be set as LOW
  240. */
  241. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_gpio->ven);
  242. if (!nfc_dev->nfc_ven_enabled) {
  243. pr_debug("eSE HAL service setting ven HIGH\n");
  244. gpio_set_ven(nfc_dev, 1);
  245. } else {
  246. pr_debug("ven already HIGH\n");
  247. }
  248. } else if (arg == ESE_POWER_OFF) {
  249. if (!nfc_dev->nfc_ven_enabled) {
  250. pr_debug("NFC not enabled, disabling ven\n");
  251. gpio_set_ven(nfc_dev, 0);
  252. } else {
  253. pr_debug("keep ven high as NFC is enabled\n");
  254. }
  255. } else if (arg == ESE_POWER_STATE) {
  256. // eSE get power state
  257. ret = gpio_get_value(nfc_gpio->ven);
  258. } else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
  259. ret = perform_cold_reset_protection(nfc_dev, arg);
  260. } else {
  261. pr_err("%s bad arg %lu\n", __func__, arg);
  262. ret = -ENOIOCTLCMD;
  263. }
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(nfc_ese_pwr);
  267. #define ESE_LEGACY_INTERFACE
  268. #ifdef ESE_LEGACY_INTERFACE
  269. static struct nfc_dev *nfc_dev_legacy;
  270. /******************************************************************************
  271. * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
  272. * to perform cold reset only
  273. * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
  274. *
  275. * Returns:- 0 in case of success and negative values in case of failure
  276. *****************************************************************************/
  277. int perform_ese_cold_reset(unsigned long arg)
  278. {
  279. int ret = 0;
  280. if (nfc_dev_legacy) {
  281. if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
  282. ret = nfc_ese_pwr(nfc_dev_legacy, arg);
  283. } else {
  284. pr_err("%s : Operation not permitted\n", __func__);
  285. return -EPERM;
  286. }
  287. }
  288. pr_debug("%s:%d exit, status:%lu", __func__, arg, ret);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(perform_ese_cold_reset);
  292. #endif //ESE_LEGACY_INTERFACE
  293. void ese_cold_reset_release(struct nfc_dev *nfc_dev)
  294. {
  295. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  296. cold_reset->rsp_pending = false;
  297. cold_reset->in_progress = false;
  298. if (timer_pending(&cold_reset->timer) == 1)
  299. del_timer(&cold_reset->timer);
  300. }
  301. void common_ese_init(struct nfc_dev *nfc_dev)
  302. {
  303. struct cold_reset *cold_reset = &nfc_dev->cold_reset;
  304. cold_reset->reset_protection = false;
  305. cold_reset->rst_prot_src = SRC_NONE;
  306. init_waitqueue_head(&cold_reset->read_wq);
  307. ese_cold_reset_release(nfc_dev);
  308. #ifdef ESE_LEGACY_INTERFACE
  309. nfc_dev_legacy = nfc_dev;
  310. #endif //ESE_LEGACY_INTERFACE
  311. }
  312. void common_ese_exit(struct nfc_dev *nfc_dev)
  313. {
  314. #ifdef ESE_LEGACY_INTERFACE
  315. nfc_dev_legacy = NULL;
  316. #endif //ESE_LEGACY_INTERFACE
  317. }