common.c 23 KB

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