common.c 23 KB

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