common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 "common.h"
  28. static int secure_zone = 1;
  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. /* Read clock request gpio configuration if MGPIO configurations are not preasent */
  66. if (of_property_read_string(np, DTS_CLKSRC_GPIO_STR, &nfc_configs->clk_src_name)) {
  67. nfc_configs->clk_pin_voting = false;
  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. }
  74. else {
  75. nfc_configs->clk_pin_voting = true;
  76. }
  77. pr_info("%s: irq %d, ven %d, dwl %d, clkreq %d, clk_pin_voting %d \n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  78. nfc_gpio->dwl_req, nfc_gpio->clkreq, nfc_configs->clk_pin_voting);
  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 (gpio_get_value(nfc_gpio->ven) != value) {
  121. pr_debug("%s: value %d\n", __func__, value);
  122. gpio_set_value(nfc_gpio->ven, value);
  123. /* hardware dependent delay */
  124. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  125. NFC_GPIO_SET_WAIT_TIME_US + 100);
  126. }
  127. }
  128. int configure_gpio(unsigned int gpio, int flag)
  129. {
  130. int ret;
  131. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  132. if (gpio_is_valid(gpio)) {
  133. ret = gpio_request(gpio, "nfc_gpio");
  134. if (ret) {
  135. pr_err("%s: unable to request nfc gpio [%d]\n",
  136. __func__, gpio);
  137. return ret;
  138. }
  139. /* set direction and value for output pin */
  140. if (flag & GPIO_OUTPUT) {
  141. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  142. pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
  143. gpio, gpio_get_value(gpio));
  144. } else {
  145. ret = gpio_direction_input(gpio);
  146. pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
  147. }
  148. if (ret) {
  149. pr_err("%s: unable to set direction for nfc gpio [%d]\n",
  150. __func__, gpio);
  151. gpio_free(gpio);
  152. return ret;
  153. }
  154. /* Consider value as control for input IRQ pin */
  155. if (flag & GPIO_IRQ) {
  156. ret = gpio_to_irq(gpio);
  157. if (ret < 0) {
  158. pr_err("%s: unable to set irq [%d]\n", __func__,
  159. gpio);
  160. gpio_free(gpio);
  161. return ret;
  162. }
  163. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
  164. gpio);
  165. return ret;
  166. }
  167. } else {
  168. pr_err("%s: invalid gpio\n", __func__);
  169. ret = -EINVAL;
  170. }
  171. return ret;
  172. }
  173. void gpio_free_all(struct nfc_dev *nfc_dev)
  174. {
  175. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  176. if (gpio_is_valid(nfc_gpio->clkreq))
  177. gpio_free(nfc_gpio->clkreq);
  178. if (gpio_is_valid(nfc_gpio->dwl_req))
  179. gpio_free(nfc_gpio->dwl_req);
  180. if (gpio_is_valid(nfc_gpio->irq))
  181. gpio_free(nfc_gpio->irq);
  182. if (gpio_is_valid(nfc_gpio->ven))
  183. gpio_free(nfc_gpio->ven);
  184. }
  185. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  186. {
  187. pr_debug("%s: entry\n", __func__);
  188. kfree(nfc_dev->kbuf);
  189. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  190. cdev_del(&nfc_dev->c_dev);
  191. class_destroy(nfc_dev->nfc_class);
  192. unregister_chrdev_region(nfc_dev->devno, count);
  193. if (nfc_dev->ipcl)
  194. ipc_log_context_destroy(nfc_dev->ipcl);
  195. }
  196. int nfc_misc_register(struct nfc_dev *nfc_dev,
  197. const struct file_operations *nfc_fops, int count,
  198. char *devname, char *classname)
  199. {
  200. int ret = 0;
  201. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  202. if (ret < 0) {
  203. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
  204. ret);
  205. return ret;
  206. }
  207. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  208. if (IS_ERR(nfc_dev->nfc_class)) {
  209. ret = PTR_ERR(nfc_dev->nfc_class);
  210. pr_err("%s: failed to register device class ret %d\n", __func__,
  211. ret);
  212. unregister_chrdev_region(nfc_dev->devno, count);
  213. return ret;
  214. }
  215. cdev_init(&nfc_dev->c_dev, nfc_fops);
  216. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  217. if (ret < 0) {
  218. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  219. class_destroy(nfc_dev->nfc_class);
  220. unregister_chrdev_region(nfc_dev->devno, count);
  221. return ret;
  222. }
  223. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  224. nfc_dev->devno, nfc_dev, devname);
  225. if (IS_ERR(nfc_dev->nfc_device)) {
  226. ret = PTR_ERR(nfc_dev->nfc_device);
  227. pr_err("%s: failed to create the device ret %d\n", __func__,
  228. ret);
  229. cdev_del(&nfc_dev->c_dev);
  230. class_destroy(nfc_dev->nfc_class);
  231. unregister_chrdev_region(nfc_dev->devno, count);
  232. return ret;
  233. }
  234. nfc_dev->ipcl = ipc_log_context_create(NUM_OF_IPC_LOG_PAGES,
  235. dev_name(nfc_dev->nfc_device), 0);
  236. nfc_dev->kbuflen = MAX_NCI_BUFFER_SIZE;
  237. nfc_dev->kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  238. if (!nfc_dev->kbuf) {
  239. nfc_misc_unregister(nfc_dev, count);
  240. return -ENOMEM;
  241. }
  242. nfc_dev->cold_reset.rsp_pending = false;
  243. nfc_dev->cold_reset.is_nfc_enabled = false;
  244. nfc_dev->cold_reset.is_crp_en = false;
  245. nfc_dev->cold_reset.last_src_ese_prot = ESE_COLD_RESET_ORIGIN_NONE;
  246. init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
  247. return 0;
  248. }
  249. /**
  250. * nfc_ioctl_power_states() - power control
  251. * @nfc_dev: nfc device data structure
  252. * @arg: mode that we want to move to
  253. *
  254. * Device power control. Depending on the arg value, device moves to
  255. * different states, refer common.h for args
  256. *
  257. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  258. * and error ret code otherwise
  259. */
  260. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  261. {
  262. int ret = 0;
  263. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  264. if (arg == NFC_POWER_OFF) {
  265. /*
  266. * We are attempting a hardware reset so let us disable
  267. * interrupts to avoid spurious notifications to upper
  268. * layers.
  269. */
  270. nfc_dev->nfc_disable_intr(nfc_dev);
  271. set_valid_gpio(nfc_gpio->dwl_req, 0);
  272. gpio_set_ven(nfc_dev, 0);
  273. nfc_dev->nfc_ven_enabled = false;
  274. } else if (arg == NFC_POWER_ON) {
  275. nfc_dev->nfc_enable_intr(nfc_dev);
  276. set_valid_gpio(nfc_gpio->dwl_req, 0);
  277. gpio_set_ven(nfc_dev, 1);
  278. nfc_dev->nfc_ven_enabled = true;
  279. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  280. /*
  281. * We are switching to download Mode, toggle the enable pin
  282. * in order to set the NFCC in the new mode
  283. */
  284. nfc_dev->nfc_disable_intr(nfc_dev);
  285. set_valid_gpio(nfc_gpio->dwl_req, 1);
  286. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  287. gpio_set_ven(nfc_dev, 0);
  288. gpio_set_ven(nfc_dev, 1);
  289. nfc_dev->nfc_enable_intr(nfc_dev);
  290. } else if (arg == NFC_FW_DWL_HIGH) {
  291. /*
  292. * Setting firmware download gpio to HIGH
  293. * before FW download start
  294. */
  295. pr_debug("set fw gpio high\n");
  296. set_valid_gpio(nfc_gpio->dwl_req, 1);
  297. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  298. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  299. nfc_dev->nfc_disable_intr(nfc_dev);
  300. gpio_set_ven(nfc_dev, 0);
  301. gpio_set_ven(nfc_dev, 1);
  302. nfc_dev->nfc_enable_intr(nfc_dev);
  303. pr_info("%s VEN forced reset done\n", __func__);
  304. } else if (arg == NFC_FW_DWL_LOW) {
  305. /*
  306. * Setting firmware download gpio to LOW
  307. * FW download finished
  308. */
  309. pr_debug("set fw gpio LOW\n");
  310. set_valid_gpio(nfc_gpio->dwl_req, 0);
  311. nfc_dev->nfc_state = NFC_STATE_NCI;
  312. } else if (arg == NFC_ENABLE) {
  313. if(nfc_dev->configs.clk_pin_voting) {
  314. /* Enabling nfc clock */
  315. ret = nfc_clock_select(nfc_dev);
  316. if (ret)
  317. pr_err("%s unable to select clock\n", __func__);
  318. }
  319. /* Setting flag true when NFC is enabled */
  320. nfc_dev->cold_reset.is_nfc_enabled = true;
  321. } else if (arg == NFC_DISABLE) {
  322. if(nfc_dev->configs.clk_pin_voting) {
  323. /* Disabling nfc clock */
  324. ret = nfc_clock_deselect(nfc_dev);
  325. if (ret)
  326. pr_err("%s unable to disable clock\n", __func__);
  327. }
  328. /* Setting flag true when NFC is disabled */
  329. nfc_dev->cold_reset.is_nfc_enabled = false;
  330. } else {
  331. pr_err("%s bad arg %lu\n", __func__, arg);
  332. ret = -ENOIOCTLCMD;
  333. }
  334. return ret;
  335. }
  336. #ifdef CONFIG_COMPAT
  337. /**
  338. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  339. * @pfile file node for opened device.
  340. * @cmd ioctl type from upper layer.
  341. * @arg ioctl arg from upper layer.
  342. *
  343. * NFC and ESE Device power control, based on the argument value
  344. *
  345. * Return: -ENOIOCTLCMD if arg is not supported
  346. * 0 if Success(or no issue)
  347. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  348. * and error ret code otherwise
  349. */
  350. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  351. unsigned long arg)
  352. {
  353. int ret = 0;
  354. arg = (compat_u64)arg;
  355. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  356. ret = nfc_dev_ioctl(pfile, cmd, arg);
  357. return ret;
  358. }
  359. #endif
  360. /**
  361. * nfc_post_init() - Configuraing Ven GPIO and hardware check
  362. * @nfc_dev: nfc device data structure
  363. *
  364. * Configure GPIOs post notification from TZ, ensuring it's a non-secure zone.
  365. *
  366. * Return: 0 if Success(or no issue) and error ret code otherwise
  367. */
  368. int nfc_post_init(struct nfc_dev *nfc_dev)
  369. {
  370. int ret=0;
  371. struct platform_configs nfc_configs;
  372. struct platform_gpio *nfc_gpio;
  373. if (!nfc_dev)
  374. return -ENODEV;
  375. memcpy(&nfc_configs, &nfc_dev->configs, sizeof(struct platform_configs));
  376. nfc_gpio = &nfc_configs.gpio;
  377. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  378. if (ret) {
  379. pr_err("%s: unable to request nfc reset gpio [%d]\n",
  380. __func__, nfc_gpio->ven);
  381. return ret;
  382. }
  383. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  384. if (ret) {
  385. pr_err("%s: unable to request nfc firm downl gpio [%d]\n",
  386. __func__, nfc_gpio->dwl_req);
  387. }
  388. if(!(nfc_configs.clk_pin_voting)){
  389. ret = configure_gpio(nfc_gpio->clkreq, GPIO_INPUT);
  390. if (ret) {
  391. pr_err("%s: unable to request nfc clkreq gpio [%d]\n",
  392. __func__, nfc_gpio->clkreq);
  393. return ret;
  394. }
  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. sema_init(&sem_eSE_pwr_off,0);
  405. pr_info("%s success\n", __func__);
  406. return 0;
  407. }
  408. /**
  409. * nfc_dynamic_protection_ioctl() - dynamic protection control
  410. * @nfc_dev: nfc device data structure
  411. * @sec_zone_trans: mode that we want to move to
  412. * If sec_zone_trans = 1; transition from non-secure zone to secure zone
  413. * If sec_zone_trans = 0; transition from secure zone to non - secure zone
  414. *
  415. * nfc periheral dynamic protection control. Depending on the sec_zone_trans value, device moves to
  416. * secure zone and non-secure zone
  417. *
  418. * Return: -ENOIOCTLCMD if sec_zone_trans val is not supported, 0 if Success(or no issue)
  419. * and error ret code otherwise
  420. */
  421. int nfc_dynamic_protection_ioctl(struct nfc_dev *nfc_dev, unsigned long sec_zone_trans)
  422. {
  423. int ret = 0;
  424. static int init_flag=1;
  425. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  426. if(sec_zone_trans == 1) {
  427. /*check NFC is disabled, only then set Ven GPIO low*/
  428. if(nfc_dev->cold_reset.is_nfc_enabled == false) {
  429. pr_debug("%s: value %d\n", __func__, gpio_get_value(nfc_gpio->ven));
  430. chk_eSE_pwr_off = 1;
  431. /*check if eSE is active, if yes, wait max of 1sec, until it's inactive */
  432. if(nfc_dev->is_ese_session_active == true) {
  433. if(down_timeout(&sem_eSE_pwr_off, msecs_to_jiffies(1000))) {
  434. /*waited for 1sec yet eSE not turned off, so, ignoring eSE power off*/
  435. pr_info("Forcefull shutdown of eSE\n");
  436. }
  437. }
  438. ret = nfc_ioctl_power_states(nfc_dev, 0);
  439. /*set driver as secure zone, such that no ioctl calls are allowed*/
  440. secure_zone=1;
  441. pr_info("Driver Secure flag set successful\n");
  442. } else {
  443. ret = -1;
  444. }
  445. }
  446. else if(sec_zone_trans == 0) {
  447. chk_eSE_pwr_off = 0;
  448. secure_zone=0;
  449. if(init_flag) {
  450. /*Initialize once,only during the first non-secure entry*/
  451. ret = nfc_post_init(nfc_dev);
  452. if(ret == 0)
  453. init_flag=0;
  454. }
  455. else {
  456. ret = nfc_ioctl_power_states(nfc_dev, 1);
  457. }
  458. pr_info("Func Driver Secure flag clear successful\n");
  459. } else {
  460. pr_info("INVALID ARG\n");
  461. ret = -ENOIOCTLCMD;
  462. }
  463. return ret;
  464. }
  465. /**
  466. * nfc_dev_ioctl - used to set or get data from upper layer.
  467. * @pfile file node for opened device.
  468. * @cmd ioctl type from upper layer.
  469. * @arg ioctl arg from upper layer.
  470. *
  471. * NFC and ESE Device power control, based on the argument value
  472. *
  473. * Return: -ENOIOCTLCMD if arg is not supported
  474. * 0 if Success(or no issue)
  475. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  476. * and error ret code otherwise
  477. */
  478. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  479. {
  480. int ret = 0;
  481. struct nfc_dev *nfc_dev = pfile->private_data;
  482. if (!nfc_dev)
  483. return -ENODEV;
  484. /*Avoiding ioctl call in secure zone*/
  485. if(secure_zone) {
  486. if(cmd!=NFC_SECURE_ZONE) {
  487. pr_debug("nfc_dev_ioctl failed\n");
  488. return -1;
  489. }
  490. }
  491. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  492. switch (cmd) {
  493. case NFC_SET_PWR:
  494. ret = nfc_ioctl_power_states(nfc_dev, arg);
  495. break;
  496. case ESE_SET_PWR:
  497. ret = nfc_ese_pwr(nfc_dev, arg);
  498. break;
  499. case ESE_GET_PWR:
  500. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  501. break;
  502. case NFCC_GET_INFO:
  503. ret = nfc_ioctl_nfcc_info(pfile, arg);
  504. break;
  505. case ESE_COLD_RESET:
  506. pr_debug("nfc ese cold reset ioctl\n");
  507. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  508. break;
  509. case NFC_SECURE_ZONE:
  510. ret = nfc_dynamic_protection_ioctl(nfc_dev, arg);
  511. break;
  512. default:
  513. pr_err("%s: bad cmd %lu\n", __func__, arg);
  514. ret = -ENOIOCTLCMD;
  515. }
  516. return ret;
  517. }
  518. int nfc_dev_open(struct inode *inode, struct file *filp)
  519. {
  520. struct nfc_dev *nfc_dev = NULL;
  521. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  522. if (!nfc_dev)
  523. return -ENODEV;
  524. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  525. mutex_lock(&nfc_dev->dev_ref_mutex);
  526. filp->private_data = nfc_dev;
  527. if (nfc_dev->dev_ref_count == 0) {
  528. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  529. nfc_dev->nfc_enable_intr(nfc_dev);
  530. }
  531. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  532. mutex_unlock(&nfc_dev->dev_ref_mutex);
  533. return 0;
  534. }
  535. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  536. {
  537. struct nfc_dev *nfc_dev = pfile->private_data;
  538. if (!nfc_dev)
  539. return -ENODEV;
  540. /*
  541. * release blocked user thread waiting for pending read during close
  542. */
  543. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  544. nfc_dev->release_read = true;
  545. nfc_dev->nfc_disable_intr(nfc_dev);
  546. wake_up(&nfc_dev->read_wq);
  547. pr_debug("%s: waiting for release of blocked read\n", __func__);
  548. mutex_lock(&nfc_dev->read_mutex);
  549. nfc_dev->release_read = false;
  550. } else {
  551. pr_debug("%s: read thread already released\n", __func__);
  552. }
  553. mutex_unlock(&nfc_dev->read_mutex);
  554. return 0;
  555. }
  556. int nfc_dev_close(struct inode *inode, struct file *filp)
  557. {
  558. struct nfc_dev *nfc_dev = NULL;
  559. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  560. if (!nfc_dev)
  561. return -ENODEV;
  562. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  563. mutex_lock(&nfc_dev->dev_ref_mutex);
  564. if (nfc_dev->dev_ref_count == 1) {
  565. nfc_dev->nfc_disable_intr(nfc_dev);
  566. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  567. }
  568. if (nfc_dev->dev_ref_count > 0)
  569. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  570. filp->private_data = NULL;
  571. mutex_unlock(&nfc_dev->dev_ref_mutex);
  572. return 0;
  573. }
  574. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  575. {
  576. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  577. if (!gpio_get_value(nfc_gpio->ven)) {
  578. pr_err("%s: ven low - nfcc powered off\n", __func__);
  579. return -ENODEV;
  580. }
  581. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  582. pr_err("%s: fw download in-progress\n", __func__);
  583. return -EBUSY;
  584. }
  585. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  586. pr_err("%s: fw download state\n", __func__);
  587. return -EBUSY;
  588. }
  589. return 0;
  590. }