common_ese.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /******************************************************************************
  2. * Copyright (C) 2020 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. cold_reset_t *cold_reset = from_timer(cold_reset, t, timer);
  32. pr_debug("%s: Enter\n", __func__);
  33. cold_reset->in_progress = false;
  34. return;
  35. }
  36. static long start_cold_reset_guard_timer(cold_reset_t *cold_reset)
  37. {
  38. long ret = -EINVAL;
  39. if (timer_pending(&cold_reset->timer) == 1) {
  40. pr_debug("ese_cold_reset_guard_timer: delete pending timer \n");
  41. /* delete timer if already pending */
  42. del_timer(&cold_reset->timer);
  43. }
  44. cold_reset->in_progress = true;
  45. timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
  46. ret = mod_timer(&cold_reset->timer,
  47. jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME));
  48. return ret;
  49. }
  50. static int send_cold_reset_protection_cmd(nfc_dev_t *nfc_dev, bool requestType)
  51. {
  52. int ret = 0;
  53. int length = 0;
  54. uint8_t *cmd = NULL;
  55. uint8_t cld_rst_cmd[] = { NCI_PROP_MSG_CMD, CLD_RST_OID,
  56. CLD_RST_PAYLOAD_SIZE
  57. };
  58. uint8_t rst_prot_cmd[] = { NCI_PROP_MSG_CMD, RST_PROT_OID,
  59. RST_PROT_PAYLOAD_SIZE, 0x00
  60. };
  61. cold_reset_t *cold_reset = &nfc_dev->cold_reset;
  62. if (requestType) {
  63. length = sizeof(rst_prot_cmd);
  64. rst_prot_cmd[NCI_PAYLOAD_IDX] = (!cold_reset->reset_protection) ? 1 : 0;
  65. cmd = rst_prot_cmd;
  66. } else {
  67. length = sizeof(cld_rst_cmd);
  68. cmd = cld_rst_cmd;
  69. }
  70. ret = nfc_dev->nfc_write(nfc_dev, cmd, length, MAX_RETRY_COUNT);
  71. if (ret != length) {
  72. pr_err("%s : nfc_write returned %d\n", __func__, ret);
  73. return -EIO;
  74. }
  75. if (requestType) {
  76. pr_debug("%s: NxpNciX: %d > %02X%02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
  77. cmd[2], cmd[3]);
  78. } else {
  79. pr_debug("%s: NxpNciX: %d > %02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
  80. cmd[2]);
  81. }
  82. return ret;
  83. }
  84. void wakeup_on_prop_rsp(nfc_dev_t *nfc_dev, uint8_t *buf)
  85. {
  86. cold_reset_t *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. }
  93. pr_debug("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__, buf[0], buf[1],
  94. buf[2], buf[3]);
  95. cold_reset->rsp_pending = false;
  96. wake_up_interruptible(&cold_reset->read_wq);
  97. }
  98. static int validate_cold_reset_protection_request(cold_reset_t *cold_reset,
  99. unsigned long arg)
  100. {
  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. return -EINVAL;
  109. } else {
  110. pr_err("%s:Operation not permitted \n", __func__);
  111. return -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:request - enable reset protection from same source\n", __func__);
  123. } else {
  124. pr_err("%s: Operation not permitted \n", __func__);
  125. return -EPERM;
  126. }
  127. }
  128. return 0;
  129. }
  130. static int perform_cold_reset_protection(nfc_dev_t *nfc_dev, unsigned long arg)
  131. {
  132. int ret = 0;
  133. struct file filp;
  134. cold_reset_t *cold_reset = &nfc_dev->cold_reset;
  135. bool nfc_dev_opened = false;
  136. /*check if NFCC not in the FW download or hard reset state */
  137. ret = validate_nfc_state_nci(nfc_dev);
  138. if (ret < 0) {
  139. pr_err("%s: invalid cmd", __func__);
  140. return ret;
  141. }
  142. /* check if NFC is enabled */
  143. mutex_lock(&nfc_dev->dev_ref_mutex);
  144. nfc_dev_opened = (nfc_dev->dev_ref_count > 0) ? true : false;
  145. mutex_unlock(&nfc_dev->dev_ref_mutex);
  146. mutex_lock(&cold_reset->sync_mutex);
  147. /*check if NFCC not in the FW download or hard reset state */
  148. ret = validate_cold_reset_protection_request(cold_reset, arg);
  149. if (ret < 0) {
  150. pr_err("%s: invalid cmd", __func__);
  151. goto err;
  152. }
  153. /*check if cold reset already in progress */
  154. if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
  155. pr_err("%s: cold reset already in progress", __func__);
  156. ret = -EBUSY;
  157. goto err;
  158. }
  159. /* set default value for status as failure */
  160. cold_reset->status = -EIO;
  161. cold_reset->rsp_pending = true;
  162. /*enable interrupt before sending cmd, when devnode not opened by HAL */
  163. if (!nfc_dev_opened)
  164. nfc_dev->nfc_enable_intr(nfc_dev);
  165. ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
  166. if (ret < 0) {
  167. pr_err("failed to send cold reset/protection command\n");
  168. cold_reset->rsp_pending = false;
  169. goto err;
  170. }
  171. ret = 0;
  172. /*start the cold reset guard timer */
  173. if (IS_CLD_RST_REQ(arg)) {
  174. /*Guard timer not needed when OSU over NFC*/
  175. if(!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
  176. ret = start_cold_reset_guard_timer(cold_reset);
  177. if (ret) {
  178. pr_err("%s: Error in mod_timer\n", __func__);
  179. goto err;
  180. }
  181. }
  182. }
  183. do {
  184. /* Read is pending from the HAL service which will complete the response */
  185. if (nfc_dev_opened) {
  186. if (!wait_event_interruptible_timeout
  187. (cold_reset->read_wq,
  188. cold_reset->rsp_pending == false,
  189. msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT))) {
  190. pr_err("%s:cold reset/protection response timeout\n", __func__);
  191. ret = -EAGAIN;
  192. }
  193. } else {
  194. /* Read data as NFC thread is not active */
  195. filp.private_data = nfc_dev;
  196. #if IS_ENABLED(CONFIG_NXP_NFC_I2C)
  197. if (nfc_dev->interface == PLATFORM_IF_I2C) {
  198. filp.f_flags &= ~O_NONBLOCK;
  199. ret = nfc_i2c_dev_read(&filp, NULL, 3, 0);
  200. usleep_range(3500, 4000);
  201. }
  202. #endif //IS_ENABLED(CONFIG_NXP_NFC_I2C)
  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(&cold_reset->sync_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(nfc_dev_t *nfc_dev, unsigned long arg)
  230. {
  231. int ret = 0;
  232. if (arg == ESE_POWER_ON) {
  233. /**
  234. * Let's store the NFC VEN pin state
  235. * will check stored value in case of eSE power off request,
  236. * to find out if NFC MW also sent request to set VEN HIGH
  237. * VEN state will remain HIGH if NFC is enabled otherwise
  238. * it will be set as LOW
  239. */
  240. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_dev->gpio.ven);
  241. if (!nfc_dev->nfc_ven_enabled) {
  242. pr_debug("eSE HAL service setting ven HIGH\n");
  243. gpio_set_ven(nfc_dev, 1);
  244. } else {
  245. pr_debug("ven already HIGH\n");
  246. }
  247. } else if (arg == ESE_POWER_OFF) {
  248. if (!nfc_dev->nfc_ven_enabled) {
  249. pr_debug("NFC not enabled, disabling ven\n");
  250. gpio_set_ven(nfc_dev, 0);
  251. } else {
  252. pr_debug("keep ven high as NFC is enabled\n");
  253. }
  254. } else if (arg == ESE_POWER_STATE) {
  255. // eSE get power state
  256. ret = gpio_get_value(nfc_dev->gpio.ven);
  257. } else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
  258. ret = perform_cold_reset_protection(nfc_dev, arg);
  259. } else {
  260. pr_err("%s bad arg %lu\n", __func__, arg);
  261. ret = -ENOIOCTLCMD;
  262. }
  263. return ret;
  264. }
  265. EXPORT_SYMBOL(nfc_ese_pwr);
  266. #define ESE_LEGACY_INTERFACE
  267. #ifdef ESE_LEGACY_INTERFACE
  268. static nfc_dev_t *nfc_dev_legacy = NULL;
  269. /******************************************************************************
  270. * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
  271. * to perform cold reset only
  272. * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
  273. *
  274. * Returns:- 0 in case of sucess and negative values in case of failure
  275. *****************************************************************************/
  276. int perform_ese_cold_reset(unsigned long arg)
  277. {
  278. int ret = 0;
  279. if (nfc_dev_legacy) {
  280. if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
  281. ret = nfc_ese_pwr(nfc_dev_legacy, arg);
  282. } else {
  283. pr_err("%s : Operation not permitted \n", __func__);
  284. return -EPERM;
  285. }
  286. }
  287. pr_debug("%s:%d exit, status:%lu", __func__, arg, ret);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(perform_ese_cold_reset);
  291. #endif //ESE_LEGACY_INTERFACE
  292. void common_ese_on_hard_reset(nfc_dev_t *nfc_dev)
  293. {
  294. cold_reset_t *cold_reset = &nfc_dev->cold_reset;
  295. cold_reset->rsp_pending = false;
  296. cold_reset->in_progress = false;
  297. if (timer_pending(&cold_reset->timer) == 1) {
  298. del_timer(&cold_reset->timer);
  299. }
  300. }
  301. void common_ese_init(nfc_dev_t *nfc_dev)
  302. {
  303. cold_reset_t *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. mutex_init(&cold_reset->sync_mutex);
  308. common_ese_on_hard_reset(nfc_dev);
  309. #ifdef ESE_LEGACY_INTERFACE
  310. nfc_dev_legacy = nfc_dev;
  311. #endif //ESE_LEGACY_INTERFACE
  312. }
  313. void common_ese_exit(nfc_dev_t *nfc_dev)
  314. {
  315. mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
  316. #ifdef ESE_LEGACY_INTERFACE
  317. nfc_dev_legacy = NULL;
  318. #endif //ESE_LEGACY_INTERFACE
  319. }