common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2021 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. /*
  21. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  22. *
  23. *****************************************************************************/
  24. #include <linux/gpio.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/delay.h>
  27. #include <linux/pinctrl/qcom-pinctrl.h>
  28. #include "common.h"
  29. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  30. uint8_t interface)
  31. {
  32. int ret;
  33. struct device_node *np = dev->of_node;
  34. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  35. struct platform_ldo *ldo = &nfc_configs->ldo;
  36. if (!np) {
  37. pr_err("%s: nfc of_node NULL\n", __func__);
  38. return -EINVAL;
  39. }
  40. nfc_gpio->irq = -EINVAL;
  41. nfc_gpio->dwl_req = -EINVAL;
  42. nfc_gpio->ven = -EINVAL;
  43. nfc_gpio->clkreq = -EINVAL;
  44. /* irq required for i2c based chips only */
  45. if (interface == PLATFORM_IF_I2C) {
  46. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  47. if ((!gpio_is_valid(nfc_gpio->irq))) {
  48. pr_err("%s: nfc irq gpio invalid %d\n", __func__,
  49. nfc_gpio->irq);
  50. return -EINVAL;
  51. }
  52. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  53. }
  54. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  55. if ((!gpio_is_valid(nfc_gpio->ven))) {
  56. pr_err("%s: nfc ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  57. return -EINVAL;
  58. }
  59. /* some products like sn220 does not required fw dwl pin */
  60. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  61. /* not returning failure for dwl gpio as it is optional for sn220 */
  62. if ((!gpio_is_valid(nfc_gpio->dwl_req))){
  63. pr_warn("%s: nfc dwl_req gpio invalid %d\n", __func__,
  64. nfc_gpio->dwl_req);
  65. }
  66. /* Read clkreq GPIO pin number from DTSI */
  67. nfc_gpio->clkreq = of_get_named_gpio(np, DTS_CLKREQ_GPIO_STR, 0);
  68. if (!gpio_is_valid(nfc_gpio->clkreq)) {
  69. dev_err(dev, "clkreq gpio invalid %d\n", nfc_gpio->clkreq);
  70. return -EINVAL;
  71. }
  72. /* Read DTS_SZONE_STR to check secure zone support */
  73. if (of_property_read_string(np, DTS_SZONE_STR, &nfc_configs->szone)) {
  74. nfc_configs->CNSS_NFC_HW_SECURE_ENABLE = false;
  75. }else
  76. nfc_configs->CNSS_NFC_HW_SECURE_ENABLE = true;
  77. pr_info("%s: irq %d, ven %d, dwl %d, clkreq %d \n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  78. nfc_gpio->dwl_req, nfc_gpio->clkreq);
  79. /* optional property */
  80. ret = of_property_read_u32_array(np, NFC_LDO_VOL_DT_NAME,
  81. (u32 *) ldo->vdd_levels,
  82. ARRAY_SIZE(ldo->vdd_levels));
  83. if (ret) {
  84. dev_err(dev, "error reading NFC VDDIO min and max value\n");
  85. // set default as per datasheet
  86. ldo->vdd_levels[0] = NFC_VDDIO_MIN;
  87. ldo->vdd_levels[1] = NFC_VDDIO_MAX;
  88. }
  89. /* optional property */
  90. ret = of_property_read_u32(np, NFC_LDO_CUR_DT_NAME, &ldo->max_current);
  91. if (ret) {
  92. dev_err(dev, "error reading NFC current value\n");
  93. // set default as per datasheet
  94. ldo->max_current = NFC_CURRENT_MAX;
  95. }
  96. return 0;
  97. }
  98. void set_valid_gpio(int gpio, int value)
  99. {
  100. if (gpio_is_valid(gpio)) {
  101. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  102. gpio_set_value(gpio, value);
  103. /* hardware dependent delay */
  104. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  105. NFC_GPIO_SET_WAIT_TIME_US + 100);
  106. }
  107. }
  108. int get_valid_gpio(int gpio)
  109. {
  110. int value = -EINVAL;
  111. if (gpio_is_valid(gpio)) {
  112. value = gpio_get_value(gpio);
  113. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  114. }
  115. return value;
  116. }
  117. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  118. {
  119. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  120. if(!nfc_dev->secure_zone) {
  121. if (gpio_get_value(nfc_gpio->ven) != value) {
  122. pr_debug("%s: value %d\n", __func__, value);
  123. gpio_set_value(nfc_gpio->ven, value);
  124. /* hardware dependent delay */
  125. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  126. NFC_GPIO_SET_WAIT_TIME_US + 100);
  127. }
  128. }
  129. }
  130. int configure_gpio(unsigned int gpio, int flag)
  131. {
  132. int ret;
  133. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  134. if (gpio_is_valid(gpio)) {
  135. ret = gpio_request(gpio, "nfc_gpio");
  136. if (ret) {
  137. pr_err("%s: unable to request nfc gpio [%d]\n",
  138. __func__, gpio);
  139. return ret;
  140. }
  141. /* set direction and value for output pin */
  142. if (flag & GPIO_OUTPUT) {
  143. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  144. pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
  145. gpio, gpio_get_value(gpio));
  146. } else {
  147. ret = gpio_direction_input(gpio);
  148. pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
  149. }
  150. if (ret) {
  151. pr_err("%s: unable to set direction for nfc gpio [%d]\n",
  152. __func__, gpio);
  153. gpio_free(gpio);
  154. return ret;
  155. }
  156. /* Consider value as control for input IRQ pin */
  157. if (flag & GPIO_IRQ) {
  158. ret = gpio_to_irq(gpio);
  159. if (ret < 0) {
  160. pr_err("%s: unable to set irq [%d]\n", __func__,
  161. gpio);
  162. gpio_free(gpio);
  163. return ret;
  164. }
  165. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
  166. gpio);
  167. return ret;
  168. }
  169. } else {
  170. pr_err("%s: invalid gpio\n", __func__);
  171. ret = -EINVAL;
  172. }
  173. return ret;
  174. }
  175. void gpio_free_all(struct nfc_dev *nfc_dev)
  176. {
  177. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  178. if (gpio_is_valid(nfc_gpio->clkreq))
  179. gpio_free(nfc_gpio->clkreq);
  180. if (gpio_is_valid(nfc_gpio->dwl_req))
  181. gpio_free(nfc_gpio->dwl_req);
  182. if (gpio_is_valid(nfc_gpio->irq))
  183. gpio_free(nfc_gpio->irq);
  184. if (gpio_is_valid(nfc_gpio->ven))
  185. gpio_free(nfc_gpio->ven);
  186. }
  187. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  188. {
  189. pr_debug("%s: entry\n", __func__);
  190. kfree(nfc_dev->kbuf);
  191. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  192. cdev_del(&nfc_dev->c_dev);
  193. class_destroy(nfc_dev->nfc_class);
  194. unregister_chrdev_region(nfc_dev->devno, count);
  195. if (nfc_dev->ipcl)
  196. ipc_log_context_destroy(nfc_dev->ipcl);
  197. }
  198. int nfc_misc_register(struct nfc_dev *nfc_dev,
  199. const struct file_operations *nfc_fops, int count,
  200. char *devname, char *classname)
  201. {
  202. int ret = 0;
  203. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  204. if (ret < 0) {
  205. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
  206. ret);
  207. return ret;
  208. }
  209. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  210. if (IS_ERR(nfc_dev->nfc_class)) {
  211. ret = PTR_ERR(nfc_dev->nfc_class);
  212. pr_err("%s: failed to register device class ret %d\n", __func__,
  213. ret);
  214. unregister_chrdev_region(nfc_dev->devno, count);
  215. return ret;
  216. }
  217. cdev_init(&nfc_dev->c_dev, nfc_fops);
  218. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  219. if (ret < 0) {
  220. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  221. class_destroy(nfc_dev->nfc_class);
  222. unregister_chrdev_region(nfc_dev->devno, count);
  223. return ret;
  224. }
  225. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  226. nfc_dev->devno, nfc_dev, devname);
  227. if (IS_ERR(nfc_dev->nfc_device)) {
  228. ret = PTR_ERR(nfc_dev->nfc_device);
  229. pr_err("%s: failed to create the device ret %d\n", __func__,
  230. ret);
  231. cdev_del(&nfc_dev->c_dev);
  232. class_destroy(nfc_dev->nfc_class);
  233. unregister_chrdev_region(nfc_dev->devno, count);
  234. return ret;
  235. }
  236. nfc_dev->ipcl = ipc_log_context_create(NUM_OF_IPC_LOG_PAGES,
  237. dev_name(nfc_dev->nfc_device), 0);
  238. nfc_dev->kbuflen = MAX_NCI_BUFFER_SIZE;
  239. nfc_dev->kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  240. if (!nfc_dev->kbuf) {
  241. nfc_misc_unregister(nfc_dev, count);
  242. return -ENOMEM;
  243. }
  244. nfc_dev->cold_reset.rsp_pending = false;
  245. nfc_dev->cold_reset.is_nfc_enabled = false;
  246. nfc_dev->cold_reset.is_crp_en = false;
  247. nfc_dev->cold_reset.last_src_ese_prot = ESE_COLD_RESET_ORIGIN_NONE;
  248. init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
  249. return 0;
  250. }
  251. /**
  252. * nfc_ioctl_power_states() - power control
  253. * @nfc_dev: nfc device data structure
  254. * @arg: mode that we want to move to
  255. *
  256. * Device power control. Depending on the arg value, device moves to
  257. * different states, refer common.h for args
  258. *
  259. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  260. * and error ret code otherwise
  261. */
  262. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  263. {
  264. int ret = 0;
  265. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  266. if (arg == NFC_POWER_OFF) {
  267. /*
  268. * We are attempting a hardware reset so let us disable
  269. * interrupts to avoid spurious notifications to upper
  270. * layers.
  271. */
  272. nfc_dev->nfc_disable_intr(nfc_dev);
  273. set_valid_gpio(nfc_gpio->dwl_req, 0);
  274. gpio_set_ven(nfc_dev, 0);
  275. nfc_dev->nfc_ven_enabled = false;
  276. } else if (arg == NFC_POWER_ON) {
  277. nfc_dev->nfc_enable_intr(nfc_dev);
  278. set_valid_gpio(nfc_gpio->dwl_req, 0);
  279. gpio_set_ven(nfc_dev, 1);
  280. nfc_dev->nfc_ven_enabled = true;
  281. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  282. /*
  283. * We are switching to download Mode, toggle the enable pin
  284. * in order to set the NFCC in the new mode
  285. */
  286. nfc_dev->nfc_disable_intr(nfc_dev);
  287. set_valid_gpio(nfc_gpio->dwl_req, 1);
  288. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  289. gpio_set_ven(nfc_dev, 0);
  290. gpio_set_ven(nfc_dev, 1);
  291. nfc_dev->nfc_enable_intr(nfc_dev);
  292. } else if (arg == NFC_FW_DWL_HIGH) {
  293. /*
  294. * Setting firmware download gpio to HIGH
  295. * before FW download start
  296. */
  297. pr_debug("set fw gpio high\n");
  298. set_valid_gpio(nfc_gpio->dwl_req, 1);
  299. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  300. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  301. nfc_dev->nfc_disable_intr(nfc_dev);
  302. gpio_set_ven(nfc_dev, 0);
  303. gpio_set_ven(nfc_dev, 1);
  304. nfc_dev->nfc_enable_intr(nfc_dev);
  305. pr_info("%s VEN forced reset done\n", __func__);
  306. } else if (arg == NFC_FW_DWL_LOW) {
  307. /*
  308. * Setting firmware download gpio to LOW
  309. * FW download finished
  310. */
  311. pr_debug("set fw gpio LOW\n");
  312. set_valid_gpio(nfc_gpio->dwl_req, 0);
  313. nfc_dev->nfc_state = NFC_STATE_NCI;
  314. } else if (arg == NFC_ENABLE) {
  315. /* Setting flag true when NFC is enabled */
  316. nfc_dev->cold_reset.is_nfc_enabled = true;
  317. } else if (arg == NFC_DISABLE) {
  318. /* Setting flag true when NFC is disabled */
  319. nfc_dev->cold_reset.is_nfc_enabled = false;
  320. } else {
  321. pr_err("%s bad arg %lu\n", __func__, arg);
  322. ret = -ENOIOCTLCMD;
  323. }
  324. return ret;
  325. }
  326. #ifdef CONFIG_COMPAT
  327. /**
  328. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  329. * @pfile file node for opened device.
  330. * @cmd ioctl type from upper layer.
  331. * @arg ioctl arg from upper layer.
  332. *
  333. * NFC and ESE Device power control, based on the argument value
  334. *
  335. * Return: -ENOIOCTLCMD if arg is not supported
  336. * 0 if Success(or no issue)
  337. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  338. * and error ret code otherwise
  339. */
  340. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  341. unsigned long arg)
  342. {
  343. int ret = 0;
  344. arg = (compat_u64)arg;
  345. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  346. ret = nfc_dev_ioctl(pfile, cmd, arg);
  347. return ret;
  348. }
  349. #endif
  350. /**
  351. * nfc_post_init() - Configuraing Ven GPIO and hardware check
  352. * @nfc_dev: nfc device data structure
  353. *
  354. * Configure GPIOs post notification from TZ, ensuring it's a non-secure zone.
  355. *
  356. * Return: 0 if Success(or no issue) and error ret code otherwise
  357. */
  358. int nfc_post_init(struct nfc_dev *nfc_dev)
  359. {
  360. int ret=0;
  361. unsigned int clkreq_gpio = 0;
  362. static int post_init_success;
  363. struct platform_configs nfc_configs;
  364. struct platform_gpio *nfc_gpio;
  365. if(post_init_success)
  366. return 0;
  367. if (!nfc_dev)
  368. return -ENODEV;
  369. memcpy(&nfc_configs, &nfc_dev->configs, sizeof(struct platform_configs));
  370. nfc_gpio = &nfc_configs.gpio;
  371. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  372. if (ret) {
  373. pr_err("%s: unable to request nfc reset gpio [%d]\n",
  374. __func__, nfc_gpio->ven);
  375. return ret;
  376. }
  377. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  378. if (ret) {
  379. pr_err("%s: unable to request nfc firm downl gpio [%d]\n",
  380. __func__, nfc_gpio->dwl_req);
  381. }
  382. /* Read clkreq GPIO number from device tree*/
  383. ret = of_property_read_u32_index(nfc_dev->i2c_dev.client->dev.of_node, DTS_CLKREQ_GPIO_STR, 1, &clkreq_gpio);
  384. if (ret < 0) {
  385. pr_err("%s Failed to read clkreq gipo number, ret: %d\n", __func__, ret);
  386. return ret;
  387. }
  388. /* configure clkreq GPIO as wakeup capable */
  389. ret = msm_gpio_mpm_wake_set(clkreq_gpio, true);
  390. if (ret < 0) {
  391. pr_err("%s Failed to setup clkreq gpio %d as wakeup capable, ret: %d\n", __func__, clkreq_gpio , ret);
  392. return ret;
  393. } else {
  394. pr_info("%s clkreq gpio %d successfully setup for wakeup capable\n", __func__, clkreq_gpio);
  395. }
  396. ret = nfcc_hw_check(nfc_dev);
  397. if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
  398. pr_err("nfc hw check failed ret %d\n", ret);
  399. gpio_free(nfc_gpio->dwl_req);
  400. gpio_free(nfc_gpio->ven);
  401. return ret;
  402. }
  403. /*Initialising sempahore to disbale NFC Ven GPIO only after eSE is power off flag is set */
  404. if (nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  405. sema_init(&sem_eSE_pwr_off,0);
  406. }
  407. post_init_success = 1;
  408. pr_info("%s success\n", __func__);
  409. return 0;
  410. }
  411. /**
  412. * nfc_hw_secure_check() - Checks the NFC secure zone status
  413. *
  414. * Queries the TZ secure libraries if NFC is in secure zone statue or not.
  415. *
  416. * Return: 0 if FEATURE_NOT_SUPPORTED/PERIPHERAL_NOT_FOUND/state is 2 and
  417. * return 1(non-secure) otherwise
  418. */
  419. bool nfc_hw_secure_check(void)
  420. {
  421. struct Object client_env;
  422. struct Object app_object;
  423. u32 wifi_uid = HW_NFC_UID;
  424. union ObjectArg obj_arg[2] = {{{0, 0}}};
  425. int ret;
  426. bool retstat = 1;
  427. u8 state = 0;
  428. /* get rootObj */
  429. ret = get_client_env_object(&client_env);
  430. if (ret) {
  431. pr_err("Failed to get client_env_object, ret: %d\n", ret);
  432. return 1;
  433. }
  434. ret = IClientEnv_open(client_env, HW_STATE_UID, &app_object);
  435. if (ret) {
  436. pr_debug("Failed to get app_object, ret: %d\n", ret);
  437. if (ret == FEATURE_NOT_SUPPORTED) {
  438. retstat = 0; /* Do not Assert */
  439. pr_debug("Secure HW feature not supported\n");
  440. }
  441. goto exit_release_clientenv;
  442. }
  443. obj_arg[0].b = (struct ObjectBuf) {&wifi_uid, sizeof(u32)};
  444. obj_arg[1].b = (struct ObjectBuf) {&state, sizeof(u8)};
  445. ret = Object_invoke(app_object, HW_OP_GET_STATE, obj_arg,
  446. ObjectCounts_pack(1, 1, 0, 0));
  447. pr_info("SMC invoke ret: %d state: %d\n", ret, state);
  448. if (ret) {
  449. if (ret == PERIPHERAL_NOT_FOUND) {
  450. retstat = 0; /* Do not Assert */
  451. pr_debug("Secure HW mode is not updated. Peripheral not found\n");
  452. }
  453. goto exit_release_app_obj;
  454. }
  455. if (state == 1) {
  456. /*Secure Zone*/
  457. retstat = 1;
  458. } else {
  459. /*Non-Secure Zone*/
  460. retstat = 0;
  461. }
  462. exit_release_app_obj:
  463. Object_release(app_object);
  464. exit_release_clientenv:
  465. Object_release(client_env);
  466. return retstat;
  467. }
  468. /**
  469. * nfc_dynamic_protection_ioctl() - dynamic protection control
  470. * @nfc_dev: nfc device data structure
  471. * @sec_zone_trans: mode that we want to move to
  472. * If sec_zone_trans = 1; transition from non-secure zone to secure zone
  473. * If sec_zone_trans = 0; transition from secure zone to non - secure zone
  474. *
  475. * nfc periheral dynamic protection control. Depending on the sec_zone_trans value, device moves to
  476. * secure zone and non-secure zone
  477. *
  478. * Return: -ENOIOCTLCMD if sec_zone_trans val is not supported, 0 if Success(or no issue)
  479. * and error ret code otherwise
  480. */
  481. int nfc_dynamic_protection_ioctl(struct nfc_dev *nfc_dev, unsigned long sec_zone_trans)
  482. {
  483. int ret = 0;
  484. static int init_flag=1;
  485. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  486. if(sec_zone_trans == 1) {
  487. /*check NFC is disabled, only then set Ven GPIO low*/
  488. if(nfc_dev->cold_reset.is_nfc_enabled == false) {
  489. pr_debug("%s: value %d\n", __func__, gpio_get_value(nfc_gpio->ven));
  490. chk_eSE_pwr_off = 1;
  491. /*check if eSE is active, if yes, wait max of 1sec, until it's inactive */
  492. if(nfc_dev->is_ese_session_active == true) {
  493. if(down_timeout(&sem_eSE_pwr_off, msecs_to_jiffies(1000))) {
  494. /*waited for 1sec yet eSE not turned off, so, ignoring eSE power off*/
  495. pr_info("Forcefull shutdown of eSE\n");
  496. }
  497. }
  498. ret = nfc_ioctl_power_states(nfc_dev, 0);
  499. /*set driver as secure zone, such that no ioctl calls are allowed*/
  500. nfc_dev->secure_zone = true;
  501. pr_info("Driver Secure flag set successful\n");
  502. } else {
  503. ret = -1;
  504. }
  505. }
  506. else if(sec_zone_trans == 0) {
  507. chk_eSE_pwr_off = 0;
  508. nfc_dev->secure_zone = false;
  509. if(init_flag) {
  510. /*Initialize once,only during the first non-secure entry*/
  511. ret = nfc_post_init(nfc_dev);
  512. if(ret == 0)
  513. init_flag=0;
  514. }
  515. else {
  516. if(!gpio_get_value(nfc_gpio->ven))
  517. ret = nfc_ioctl_power_states(nfc_dev, 1);
  518. }
  519. pr_info("Func Driver Secure flag clear successful\n");
  520. } else {
  521. pr_info("INVALID ARG\n");
  522. ret = -ENOIOCTLCMD;
  523. }
  524. return ret;
  525. }
  526. /**
  527. * nfc_dev_ioctl - used to set or get data from upper layer.
  528. * @pfile file node for opened device.
  529. * @cmd ioctl type from upper layer.
  530. * @arg ioctl arg from upper layer.
  531. *
  532. * NFC and ESE Device power control, based on the argument value
  533. *
  534. * Return: -ENOIOCTLCMD if arg is not supported
  535. * 0 if Success(or no issue)
  536. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  537. * and error ret code otherwise
  538. */
  539. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  540. {
  541. int ret = 0;
  542. struct nfc_dev *nfc_dev = pfile->private_data;
  543. if (!nfc_dev)
  544. return -ENODEV;
  545. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  546. /*Avoiding ioctl call in secure zone*/
  547. if(nfc_dev->secure_zone) {
  548. if(cmd!=NFC_SECURE_ZONE) {
  549. pr_debug("nfc_dev_ioctl failed\n");
  550. return -1;
  551. }
  552. }
  553. }
  554. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  555. switch (cmd) {
  556. case NFC_SET_PWR:
  557. ret = nfc_ioctl_power_states(nfc_dev, arg);
  558. break;
  559. case NFC_SET_RESET_READ_PENDING:
  560. if (arg == NFC_SET_READ_PENDING) {
  561. nfc_dev->cold_reset.is_nfc_read_pending = true;
  562. /* Set default NFC state as NCI for Nfc read pending request */
  563. nfc_dev->nfc_state = NFC_STATE_NCI;
  564. }
  565. else if (arg == NFC_RESET_READ_PENDING){
  566. nfc_dev->cold_reset.is_nfc_read_pending = false;
  567. }
  568. else {
  569. ret = -EINVAL;
  570. }
  571. break;
  572. case ESE_SET_PWR:
  573. ret = nfc_ese_pwr(nfc_dev, arg);
  574. break;
  575. case ESE_GET_PWR:
  576. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  577. break;
  578. case NFCC_GET_INFO:
  579. ret = nfc_ioctl_nfcc_info(pfile, arg);
  580. break;
  581. case ESE_COLD_RESET:
  582. pr_debug("nfc ese cold reset ioctl\n");
  583. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  584. break;
  585. case NFC_SECURE_ZONE:
  586. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  587. ret = nfc_dynamic_protection_ioctl(nfc_dev, arg);
  588. }
  589. break;
  590. default:
  591. pr_err("%s: bad cmd %lu\n", __func__, arg);
  592. ret = -ENOIOCTLCMD;
  593. }
  594. return ret;
  595. }
  596. int nfc_dev_open(struct inode *inode, struct file *filp)
  597. {
  598. struct nfc_dev *nfc_dev = NULL;
  599. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  600. if (!nfc_dev)
  601. return -ENODEV;
  602. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  603. /* Set flag to block freezer fake signal if not set already.
  604. * Without this Signal being set, Driver is trying to do a read
  605. * which is causing the delay in moving to Hibernate Mode.
  606. */
  607. if (!(current->flags & PF_NOFREEZE)) {
  608. current->flags |= PF_NOFREEZE;
  609. pr_debug("%s: current->flags 0x%x. \n", __func__, current->flags);
  610. }
  611. mutex_lock(&nfc_dev->dev_ref_mutex);
  612. filp->private_data = nfc_dev;
  613. if (nfc_dev->dev_ref_count == 0) {
  614. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  615. nfc_dev->nfc_enable_intr(nfc_dev);
  616. }
  617. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  618. mutex_unlock(&nfc_dev->dev_ref_mutex);
  619. return 0;
  620. }
  621. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  622. {
  623. struct nfc_dev *nfc_dev = pfile->private_data;
  624. if (!nfc_dev)
  625. return -ENODEV;
  626. /*
  627. * release blocked user thread waiting for pending read during close
  628. */
  629. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  630. nfc_dev->release_read = true;
  631. nfc_dev->nfc_disable_intr(nfc_dev);
  632. wake_up(&nfc_dev->read_wq);
  633. pr_debug("%s: waiting for release of blocked read\n", __func__);
  634. mutex_lock(&nfc_dev->read_mutex);
  635. nfc_dev->release_read = false;
  636. } else {
  637. pr_debug("%s: read thread already released\n", __func__);
  638. }
  639. mutex_unlock(&nfc_dev->read_mutex);
  640. return 0;
  641. }
  642. int nfc_dev_close(struct inode *inode, struct file *filp)
  643. {
  644. struct nfc_dev *nfc_dev = NULL;
  645. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  646. if (!nfc_dev)
  647. return -ENODEV;
  648. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  649. /* unset the flag to restore to previous state */
  650. if (current->flags & PF_NOFREEZE) {
  651. current->flags &= ~PF_NOFREEZE;
  652. pr_debug("%s: current->flags 0x%x. \n", __func__, current->flags);
  653. }
  654. mutex_lock(&nfc_dev->dev_ref_mutex);
  655. if (nfc_dev->dev_ref_count == 1) {
  656. nfc_dev->nfc_disable_intr(nfc_dev);
  657. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  658. }
  659. if (nfc_dev->dev_ref_count > 0)
  660. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  661. filp->private_data = NULL;
  662. mutex_unlock(&nfc_dev->dev_ref_mutex);
  663. return 0;
  664. }
  665. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  666. {
  667. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  668. if(!nfc_dev->secure_zone) {
  669. if (!gpio_get_value(nfc_gpio->ven)) {
  670. pr_err("%s: ven low - nfcc powered off\n", __func__);
  671. return -ENODEV;
  672. }
  673. }
  674. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  675. pr_err("%s: fw download in-progress\n", __func__);
  676. return -EBUSY;
  677. }
  678. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  679. pr_err("%s: fw download state\n", __func__);
  680. return -EBUSY;
  681. }
  682. return 0;
  683. }