core.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  4. *
  5. * Authors:
  6. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  7. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/rfkill.h>
  15. #include <linux/nfc.h>
  16. #include <net/genetlink.h>
  17. #include "nfc.h"
  18. #define VERSION "0.1"
  19. #define NFC_CHECK_PRES_FREQ_MS 2000
  20. int nfc_devlist_generation;
  21. DEFINE_MUTEX(nfc_devlist_mutex);
  22. /* NFC device ID bitmap */
  23. static DEFINE_IDA(nfc_index_ida);
  24. int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
  25. {
  26. int rc = 0;
  27. pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
  28. device_lock(&dev->dev);
  29. if (dev->shutting_down) {
  30. rc = -ENODEV;
  31. goto error;
  32. }
  33. if (dev->dev_up) {
  34. rc = -EBUSY;
  35. goto error;
  36. }
  37. if (!dev->ops->fw_download) {
  38. rc = -EOPNOTSUPP;
  39. goto error;
  40. }
  41. dev->fw_download_in_progress = true;
  42. rc = dev->ops->fw_download(dev, firmware_name);
  43. if (rc)
  44. dev->fw_download_in_progress = false;
  45. error:
  46. device_unlock(&dev->dev);
  47. return rc;
  48. }
  49. /**
  50. * nfc_fw_download_done - inform that a firmware download was completed
  51. *
  52. * @dev: The nfc device to which firmware was downloaded
  53. * @firmware_name: The firmware filename
  54. * @result: The positive value of a standard errno value
  55. */
  56. int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  57. u32 result)
  58. {
  59. dev->fw_download_in_progress = false;
  60. return nfc_genl_fw_download_done(dev, firmware_name, result);
  61. }
  62. EXPORT_SYMBOL(nfc_fw_download_done);
  63. /**
  64. * nfc_dev_up - turn on the NFC device
  65. *
  66. * @dev: The nfc device to be turned on
  67. *
  68. * The device remains up until the nfc_dev_down function is called.
  69. */
  70. int nfc_dev_up(struct nfc_dev *dev)
  71. {
  72. int rc = 0;
  73. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  74. device_lock(&dev->dev);
  75. if (dev->shutting_down) {
  76. rc = -ENODEV;
  77. goto error;
  78. }
  79. if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
  80. rc = -ERFKILL;
  81. goto error;
  82. }
  83. if (dev->fw_download_in_progress) {
  84. rc = -EBUSY;
  85. goto error;
  86. }
  87. if (dev->dev_up) {
  88. rc = -EALREADY;
  89. goto error;
  90. }
  91. if (dev->ops->dev_up)
  92. rc = dev->ops->dev_up(dev);
  93. if (!rc)
  94. dev->dev_up = true;
  95. /* We have to enable the device before discovering SEs */
  96. if (dev->ops->discover_se && dev->ops->discover_se(dev))
  97. pr_err("SE discovery failed\n");
  98. error:
  99. device_unlock(&dev->dev);
  100. return rc;
  101. }
  102. /**
  103. * nfc_dev_down - turn off the NFC device
  104. *
  105. * @dev: The nfc device to be turned off
  106. */
  107. int nfc_dev_down(struct nfc_dev *dev)
  108. {
  109. int rc = 0;
  110. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  111. device_lock(&dev->dev);
  112. if (dev->shutting_down) {
  113. rc = -ENODEV;
  114. goto error;
  115. }
  116. if (!dev->dev_up) {
  117. rc = -EALREADY;
  118. goto error;
  119. }
  120. if (dev->polling || dev->active_target) {
  121. rc = -EBUSY;
  122. goto error;
  123. }
  124. if (dev->ops->dev_down)
  125. dev->ops->dev_down(dev);
  126. dev->dev_up = false;
  127. error:
  128. device_unlock(&dev->dev);
  129. return rc;
  130. }
  131. static int nfc_rfkill_set_block(void *data, bool blocked)
  132. {
  133. struct nfc_dev *dev = data;
  134. pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
  135. if (!blocked)
  136. return 0;
  137. nfc_dev_down(dev);
  138. return 0;
  139. }
  140. static const struct rfkill_ops nfc_rfkill_ops = {
  141. .set_block = nfc_rfkill_set_block,
  142. };
  143. /**
  144. * nfc_start_poll - start polling for nfc targets
  145. *
  146. * @dev: The nfc device that must start polling
  147. * @im_protocols: bitset of nfc initiator protocols to be used for polling
  148. * @tm_protocols: bitset of nfc transport protocols to be used for polling
  149. *
  150. * The device remains polling for targets until a target is found or
  151. * the nfc_stop_poll function is called.
  152. */
  153. int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
  154. {
  155. int rc;
  156. pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
  157. dev_name(&dev->dev), im_protocols, tm_protocols);
  158. if (!im_protocols && !tm_protocols)
  159. return -EINVAL;
  160. device_lock(&dev->dev);
  161. if (dev->shutting_down) {
  162. rc = -ENODEV;
  163. goto error;
  164. }
  165. if (!dev->dev_up) {
  166. rc = -ENODEV;
  167. goto error;
  168. }
  169. if (dev->polling) {
  170. rc = -EBUSY;
  171. goto error;
  172. }
  173. rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
  174. if (!rc) {
  175. dev->polling = true;
  176. dev->rf_mode = NFC_RF_NONE;
  177. }
  178. error:
  179. device_unlock(&dev->dev);
  180. return rc;
  181. }
  182. /**
  183. * nfc_stop_poll - stop polling for nfc targets
  184. *
  185. * @dev: The nfc device that must stop polling
  186. */
  187. int nfc_stop_poll(struct nfc_dev *dev)
  188. {
  189. int rc = 0;
  190. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  191. device_lock(&dev->dev);
  192. if (dev->shutting_down) {
  193. rc = -ENODEV;
  194. goto error;
  195. }
  196. if (!dev->polling) {
  197. rc = -EINVAL;
  198. goto error;
  199. }
  200. dev->ops->stop_poll(dev);
  201. dev->polling = false;
  202. dev->rf_mode = NFC_RF_NONE;
  203. error:
  204. device_unlock(&dev->dev);
  205. return rc;
  206. }
  207. static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
  208. {
  209. int i;
  210. for (i = 0; i < dev->n_targets; i++) {
  211. if (dev->targets[i].idx == target_idx)
  212. return &dev->targets[i];
  213. }
  214. return NULL;
  215. }
  216. int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
  217. {
  218. int rc = 0;
  219. u8 *gb;
  220. size_t gb_len;
  221. struct nfc_target *target;
  222. pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
  223. if (!dev->ops->dep_link_up)
  224. return -EOPNOTSUPP;
  225. device_lock(&dev->dev);
  226. if (dev->shutting_down) {
  227. rc = -ENODEV;
  228. goto error;
  229. }
  230. if (dev->dep_link_up == true) {
  231. rc = -EALREADY;
  232. goto error;
  233. }
  234. gb = nfc_llcp_general_bytes(dev, &gb_len);
  235. if (gb_len > NFC_MAX_GT_LEN) {
  236. rc = -EINVAL;
  237. goto error;
  238. }
  239. target = nfc_find_target(dev, target_index);
  240. if (target == NULL) {
  241. rc = -ENOTCONN;
  242. goto error;
  243. }
  244. rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
  245. if (!rc) {
  246. dev->active_target = target;
  247. dev->rf_mode = NFC_RF_INITIATOR;
  248. }
  249. error:
  250. device_unlock(&dev->dev);
  251. return rc;
  252. }
  253. int nfc_dep_link_down(struct nfc_dev *dev)
  254. {
  255. int rc = 0;
  256. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  257. if (!dev->ops->dep_link_down)
  258. return -EOPNOTSUPP;
  259. device_lock(&dev->dev);
  260. if (dev->shutting_down) {
  261. rc = -ENODEV;
  262. goto error;
  263. }
  264. if (dev->dep_link_up == false) {
  265. rc = -EALREADY;
  266. goto error;
  267. }
  268. rc = dev->ops->dep_link_down(dev);
  269. if (!rc) {
  270. dev->dep_link_up = false;
  271. dev->active_target = NULL;
  272. dev->rf_mode = NFC_RF_NONE;
  273. nfc_llcp_mac_is_down(dev);
  274. nfc_genl_dep_link_down_event(dev);
  275. }
  276. error:
  277. device_unlock(&dev->dev);
  278. return rc;
  279. }
  280. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  281. u8 comm_mode, u8 rf_mode)
  282. {
  283. dev->dep_link_up = true;
  284. if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
  285. struct nfc_target *target;
  286. target = nfc_find_target(dev, target_idx);
  287. if (target == NULL)
  288. return -ENOTCONN;
  289. dev->active_target = target;
  290. }
  291. dev->polling = false;
  292. dev->rf_mode = rf_mode;
  293. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  294. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  295. }
  296. EXPORT_SYMBOL(nfc_dep_link_is_up);
  297. /**
  298. * nfc_activate_target - prepare the target for data exchange
  299. *
  300. * @dev: The nfc device that found the target
  301. * @target_idx: index of the target that must be activated
  302. * @protocol: nfc protocol that will be used for data exchange
  303. */
  304. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  305. {
  306. int rc;
  307. struct nfc_target *target;
  308. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  309. dev_name(&dev->dev), target_idx, protocol);
  310. device_lock(&dev->dev);
  311. if (dev->shutting_down) {
  312. rc = -ENODEV;
  313. goto error;
  314. }
  315. if (dev->active_target) {
  316. rc = -EBUSY;
  317. goto error;
  318. }
  319. target = nfc_find_target(dev, target_idx);
  320. if (target == NULL) {
  321. rc = -ENOTCONN;
  322. goto error;
  323. }
  324. rc = dev->ops->activate_target(dev, target, protocol);
  325. if (!rc) {
  326. dev->active_target = target;
  327. dev->rf_mode = NFC_RF_INITIATOR;
  328. if (dev->ops->check_presence && !dev->shutting_down)
  329. mod_timer(&dev->check_pres_timer, jiffies +
  330. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  331. }
  332. error:
  333. device_unlock(&dev->dev);
  334. return rc;
  335. }
  336. /**
  337. * nfc_deactivate_target - deactivate a nfc target
  338. *
  339. * @dev: The nfc device that found the target
  340. * @target_idx: index of the target that must be deactivated
  341. * @mode: idle or sleep?
  342. */
  343. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
  344. {
  345. int rc = 0;
  346. pr_debug("dev_name=%s target_idx=%u\n",
  347. dev_name(&dev->dev), target_idx);
  348. device_lock(&dev->dev);
  349. if (dev->shutting_down) {
  350. rc = -ENODEV;
  351. goto error;
  352. }
  353. if (dev->active_target == NULL) {
  354. rc = -ENOTCONN;
  355. goto error;
  356. }
  357. if (dev->active_target->idx != target_idx) {
  358. rc = -ENOTCONN;
  359. goto error;
  360. }
  361. if (dev->ops->check_presence)
  362. timer_delete_sync(&dev->check_pres_timer);
  363. dev->ops->deactivate_target(dev, dev->active_target, mode);
  364. dev->active_target = NULL;
  365. error:
  366. device_unlock(&dev->dev);
  367. return rc;
  368. }
  369. /**
  370. * nfc_data_exchange - transceive data
  371. *
  372. * @dev: The nfc device that found the target
  373. * @target_idx: index of the target
  374. * @skb: data to be sent
  375. * @cb: callback called when the response is received
  376. * @cb_context: parameter for the callback function
  377. *
  378. * The user must wait for the callback before calling this function again.
  379. */
  380. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  381. data_exchange_cb_t cb, void *cb_context)
  382. {
  383. int rc;
  384. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  385. dev_name(&dev->dev), target_idx, skb->len);
  386. device_lock(&dev->dev);
  387. if (dev->shutting_down) {
  388. rc = -ENODEV;
  389. kfree_skb(skb);
  390. goto error;
  391. }
  392. if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
  393. if (dev->active_target->idx != target_idx) {
  394. rc = -EADDRNOTAVAIL;
  395. kfree_skb(skb);
  396. goto error;
  397. }
  398. if (dev->ops->check_presence)
  399. timer_delete_sync(&dev->check_pres_timer);
  400. rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
  401. cb_context);
  402. if (!rc && dev->ops->check_presence && !dev->shutting_down)
  403. mod_timer(&dev->check_pres_timer, jiffies +
  404. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  405. } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
  406. rc = dev->ops->tm_send(dev, skb);
  407. } else {
  408. rc = -ENOTCONN;
  409. kfree_skb(skb);
  410. goto error;
  411. }
  412. error:
  413. device_unlock(&dev->dev);
  414. return rc;
  415. }
  416. struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
  417. {
  418. struct nfc_se *se;
  419. list_for_each_entry(se, &dev->secure_elements, list)
  420. if (se->idx == se_idx)
  421. return se;
  422. return NULL;
  423. }
  424. EXPORT_SYMBOL(nfc_find_se);
  425. int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
  426. {
  427. struct nfc_se *se;
  428. int rc;
  429. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  430. device_lock(&dev->dev);
  431. if (dev->shutting_down) {
  432. rc = -ENODEV;
  433. goto error;
  434. }
  435. if (!dev->dev_up) {
  436. rc = -ENODEV;
  437. goto error;
  438. }
  439. if (dev->polling) {
  440. rc = -EBUSY;
  441. goto error;
  442. }
  443. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  444. rc = -EOPNOTSUPP;
  445. goto error;
  446. }
  447. se = nfc_find_se(dev, se_idx);
  448. if (!se) {
  449. rc = -EINVAL;
  450. goto error;
  451. }
  452. if (se->state == NFC_SE_ENABLED) {
  453. rc = -EALREADY;
  454. goto error;
  455. }
  456. rc = dev->ops->enable_se(dev, se_idx);
  457. if (rc >= 0)
  458. se->state = NFC_SE_ENABLED;
  459. error:
  460. device_unlock(&dev->dev);
  461. return rc;
  462. }
  463. int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
  464. {
  465. struct nfc_se *se;
  466. int rc;
  467. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  468. device_lock(&dev->dev);
  469. if (dev->shutting_down) {
  470. rc = -ENODEV;
  471. goto error;
  472. }
  473. if (!dev->dev_up) {
  474. rc = -ENODEV;
  475. goto error;
  476. }
  477. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  478. rc = -EOPNOTSUPP;
  479. goto error;
  480. }
  481. se = nfc_find_se(dev, se_idx);
  482. if (!se) {
  483. rc = -EINVAL;
  484. goto error;
  485. }
  486. if (se->state == NFC_SE_DISABLED) {
  487. rc = -EALREADY;
  488. goto error;
  489. }
  490. rc = dev->ops->disable_se(dev, se_idx);
  491. if (rc >= 0)
  492. se->state = NFC_SE_DISABLED;
  493. error:
  494. device_unlock(&dev->dev);
  495. return rc;
  496. }
  497. int nfc_set_remote_general_bytes(struct nfc_dev *dev, const u8 *gb, u8 gb_len)
  498. {
  499. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  500. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  501. }
  502. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  503. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
  504. {
  505. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  506. return nfc_llcp_general_bytes(dev, gb_len);
  507. }
  508. EXPORT_SYMBOL(nfc_get_local_general_bytes);
  509. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  510. {
  511. /* Only LLCP target mode for now */
  512. if (dev->dep_link_up == false) {
  513. kfree_skb(skb);
  514. return -ENOLINK;
  515. }
  516. return nfc_llcp_data_received(dev, skb);
  517. }
  518. EXPORT_SYMBOL(nfc_tm_data_received);
  519. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  520. const u8 *gb, size_t gb_len)
  521. {
  522. int rc;
  523. device_lock(&dev->dev);
  524. dev->polling = false;
  525. if (gb != NULL) {
  526. rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
  527. if (rc < 0)
  528. goto out;
  529. }
  530. dev->rf_mode = NFC_RF_TARGET;
  531. if (protocol == NFC_PROTO_NFC_DEP_MASK)
  532. nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
  533. rc = nfc_genl_tm_activated(dev, protocol);
  534. out:
  535. device_unlock(&dev->dev);
  536. return rc;
  537. }
  538. EXPORT_SYMBOL(nfc_tm_activated);
  539. int nfc_tm_deactivated(struct nfc_dev *dev)
  540. {
  541. dev->dep_link_up = false;
  542. dev->rf_mode = NFC_RF_NONE;
  543. return nfc_genl_tm_deactivated(dev);
  544. }
  545. EXPORT_SYMBOL(nfc_tm_deactivated);
  546. /**
  547. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  548. *
  549. * @dev: device sending the response
  550. * @sk: socket sending the response
  551. * @flags: MSG_DONTWAIT flag
  552. * @size: size to allocate
  553. * @err: pointer to memory to store the error code
  554. */
  555. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  556. unsigned int flags, unsigned int size,
  557. unsigned int *err)
  558. {
  559. struct sk_buff *skb;
  560. unsigned int total_size;
  561. total_size = size +
  562. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  563. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  564. if (skb)
  565. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  566. return skb;
  567. }
  568. /**
  569. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  570. *
  571. * @size: size to allocate
  572. * @gfp: gfp flags
  573. */
  574. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  575. {
  576. struct sk_buff *skb;
  577. unsigned int total_size;
  578. total_size = size + 1;
  579. skb = alloc_skb(total_size, gfp);
  580. if (skb)
  581. skb_reserve(skb, 1);
  582. return skb;
  583. }
  584. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  585. /**
  586. * nfc_targets_found - inform that targets were found
  587. *
  588. * @dev: The nfc device that found the targets
  589. * @targets: array of nfc targets found
  590. * @n_targets: targets array size
  591. *
  592. * The device driver must call this function when one or many nfc targets
  593. * are found. After calling this function, the device driver must stop
  594. * polling for targets.
  595. * NOTE: This function can be called with targets=NULL and n_targets=0 to
  596. * notify a driver error, meaning that the polling operation cannot complete.
  597. * IMPORTANT: this function must not be called from an atomic context.
  598. * In addition, it must also not be called from a context that would prevent
  599. * the NFC Core to call other nfc ops entry point concurrently.
  600. */
  601. int nfc_targets_found(struct nfc_dev *dev,
  602. struct nfc_target *targets, int n_targets)
  603. {
  604. int i;
  605. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  606. for (i = 0; i < n_targets; i++)
  607. targets[i].idx = dev->target_next_idx++;
  608. device_lock(&dev->dev);
  609. if (dev->polling == false) {
  610. device_unlock(&dev->dev);
  611. return 0;
  612. }
  613. dev->polling = false;
  614. dev->targets_generation++;
  615. kfree(dev->targets);
  616. dev->targets = NULL;
  617. if (targets) {
  618. dev->targets = kmemdup(targets,
  619. n_targets * sizeof(struct nfc_target),
  620. GFP_ATOMIC);
  621. if (!dev->targets) {
  622. dev->n_targets = 0;
  623. device_unlock(&dev->dev);
  624. return -ENOMEM;
  625. }
  626. }
  627. dev->n_targets = n_targets;
  628. device_unlock(&dev->dev);
  629. nfc_genl_targets_found(dev);
  630. return 0;
  631. }
  632. EXPORT_SYMBOL(nfc_targets_found);
  633. /**
  634. * nfc_target_lost - inform that an activated target went out of field
  635. *
  636. * @dev: The nfc device that had the activated target in field
  637. * @target_idx: the nfc index of the target
  638. *
  639. * The device driver must call this function when the activated target
  640. * goes out of the field.
  641. * IMPORTANT: this function must not be called from an atomic context.
  642. * In addition, it must also not be called from a context that would prevent
  643. * the NFC Core to call other nfc ops entry point concurrently.
  644. */
  645. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  646. {
  647. const struct nfc_target *tg;
  648. int i;
  649. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  650. device_lock(&dev->dev);
  651. for (i = 0; i < dev->n_targets; i++) {
  652. tg = &dev->targets[i];
  653. if (tg->idx == target_idx)
  654. break;
  655. }
  656. if (i == dev->n_targets) {
  657. device_unlock(&dev->dev);
  658. return -EINVAL;
  659. }
  660. dev->targets_generation++;
  661. dev->n_targets--;
  662. dev->active_target = NULL;
  663. if (dev->n_targets) {
  664. memcpy(&dev->targets[i], &dev->targets[i + 1],
  665. (dev->n_targets - i) * sizeof(struct nfc_target));
  666. } else {
  667. kfree(dev->targets);
  668. dev->targets = NULL;
  669. }
  670. device_unlock(&dev->dev);
  671. nfc_genl_target_lost(dev, target_idx);
  672. return 0;
  673. }
  674. EXPORT_SYMBOL(nfc_target_lost);
  675. inline void nfc_driver_failure(struct nfc_dev *dev, int err)
  676. {
  677. nfc_targets_found(dev, NULL, 0);
  678. }
  679. EXPORT_SYMBOL(nfc_driver_failure);
  680. int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
  681. {
  682. struct nfc_se *se;
  683. int rc;
  684. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  685. se = nfc_find_se(dev, se_idx);
  686. if (se)
  687. return -EALREADY;
  688. se = kzalloc_obj(struct nfc_se);
  689. if (!se)
  690. return -ENOMEM;
  691. se->idx = se_idx;
  692. se->type = type;
  693. se->state = NFC_SE_DISABLED;
  694. INIT_LIST_HEAD(&se->list);
  695. list_add(&se->list, &dev->secure_elements);
  696. rc = nfc_genl_se_added(dev, se_idx, type);
  697. if (rc < 0) {
  698. list_del(&se->list);
  699. kfree(se);
  700. return rc;
  701. }
  702. return 0;
  703. }
  704. EXPORT_SYMBOL(nfc_add_se);
  705. int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
  706. {
  707. struct nfc_se *se, *n;
  708. int rc;
  709. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  710. list_for_each_entry_safe(se, n, &dev->secure_elements, list)
  711. if (se->idx == se_idx) {
  712. rc = nfc_genl_se_removed(dev, se_idx);
  713. if (rc < 0)
  714. return rc;
  715. list_del(&se->list);
  716. kfree(se);
  717. return 0;
  718. }
  719. return -EINVAL;
  720. }
  721. EXPORT_SYMBOL(nfc_remove_se);
  722. int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
  723. struct nfc_evt_transaction *evt_transaction)
  724. {
  725. int rc;
  726. pr_debug("transaction: %x\n", se_idx);
  727. device_lock(&dev->dev);
  728. if (!evt_transaction) {
  729. rc = -EPROTO;
  730. goto out;
  731. }
  732. rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
  733. out:
  734. device_unlock(&dev->dev);
  735. return rc;
  736. }
  737. EXPORT_SYMBOL(nfc_se_transaction);
  738. int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
  739. {
  740. int rc;
  741. pr_debug("connectivity: %x\n", se_idx);
  742. device_lock(&dev->dev);
  743. rc = nfc_genl_se_connectivity(dev, se_idx);
  744. device_unlock(&dev->dev);
  745. return rc;
  746. }
  747. EXPORT_SYMBOL(nfc_se_connectivity);
  748. static void nfc_release(struct device *d)
  749. {
  750. struct nfc_dev *dev = to_nfc_dev(d);
  751. struct nfc_se *se, *n;
  752. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  753. nfc_genl_data_exit(&dev->genl_data);
  754. kfree(dev->targets);
  755. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  756. nfc_genl_se_removed(dev, se->idx);
  757. list_del(&se->list);
  758. kfree(se);
  759. }
  760. ida_free(&nfc_index_ida, dev->idx);
  761. kfree(dev);
  762. }
  763. static void nfc_check_pres_work(struct work_struct *work)
  764. {
  765. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  766. check_pres_work);
  767. int rc;
  768. device_lock(&dev->dev);
  769. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  770. rc = dev->ops->check_presence(dev, dev->active_target);
  771. if (rc == -EOPNOTSUPP)
  772. goto exit;
  773. if (rc) {
  774. u32 active_target_idx = dev->active_target->idx;
  775. device_unlock(&dev->dev);
  776. nfc_target_lost(dev, active_target_idx);
  777. return;
  778. }
  779. if (!dev->shutting_down)
  780. mod_timer(&dev->check_pres_timer, jiffies +
  781. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  782. }
  783. exit:
  784. device_unlock(&dev->dev);
  785. }
  786. static void nfc_check_pres_timeout(struct timer_list *t)
  787. {
  788. struct nfc_dev *dev = timer_container_of(dev, t, check_pres_timer);
  789. schedule_work(&dev->check_pres_work);
  790. }
  791. const struct class nfc_class = {
  792. .name = "nfc",
  793. .dev_release = nfc_release,
  794. };
  795. EXPORT_SYMBOL(nfc_class);
  796. static int match_idx(struct device *d, const void *data)
  797. {
  798. struct nfc_dev *dev = to_nfc_dev(d);
  799. const unsigned int *idx = data;
  800. return dev->idx == *idx;
  801. }
  802. struct nfc_dev *nfc_get_device(unsigned int idx)
  803. {
  804. struct device *d;
  805. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  806. if (!d)
  807. return NULL;
  808. return to_nfc_dev(d);
  809. }
  810. /**
  811. * nfc_allocate_device - allocate a new nfc device
  812. *
  813. * @ops: device operations
  814. * @supported_protocols: NFC protocols supported by the device
  815. * @tx_headroom: reserved space at beginning of skb
  816. * @tx_tailroom: reserved space at end of skb
  817. */
  818. struct nfc_dev *nfc_allocate_device(const struct nfc_ops *ops,
  819. u32 supported_protocols,
  820. int tx_headroom, int tx_tailroom)
  821. {
  822. struct nfc_dev *dev;
  823. int rc;
  824. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  825. !ops->deactivate_target || !ops->im_transceive)
  826. return NULL;
  827. if (!supported_protocols)
  828. return NULL;
  829. dev = kzalloc_obj(struct nfc_dev);
  830. if (!dev)
  831. return NULL;
  832. rc = ida_alloc(&nfc_index_ida, GFP_KERNEL);
  833. if (rc < 0)
  834. goto err_free_dev;
  835. dev->idx = rc;
  836. dev->dev.class = &nfc_class;
  837. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  838. device_initialize(&dev->dev);
  839. dev->ops = ops;
  840. dev->supported_protocols = supported_protocols;
  841. dev->tx_headroom = tx_headroom;
  842. dev->tx_tailroom = tx_tailroom;
  843. INIT_LIST_HEAD(&dev->secure_elements);
  844. nfc_genl_data_init(&dev->genl_data);
  845. dev->rf_mode = NFC_RF_NONE;
  846. /* first generation must not be 0 */
  847. dev->targets_generation = 1;
  848. if (ops->check_presence) {
  849. timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
  850. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  851. }
  852. return dev;
  853. err_free_dev:
  854. kfree(dev);
  855. return NULL;
  856. }
  857. EXPORT_SYMBOL(nfc_allocate_device);
  858. /**
  859. * nfc_register_device - register a nfc device in the nfc subsystem
  860. *
  861. * @dev: The nfc device to register
  862. */
  863. int nfc_register_device(struct nfc_dev *dev)
  864. {
  865. int rc;
  866. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  867. mutex_lock(&nfc_devlist_mutex);
  868. nfc_devlist_generation++;
  869. rc = device_add(&dev->dev);
  870. mutex_unlock(&nfc_devlist_mutex);
  871. if (rc < 0)
  872. return rc;
  873. rc = nfc_llcp_register_device(dev);
  874. if (rc)
  875. pr_err("Could not register llcp device\n");
  876. device_lock(&dev->dev);
  877. dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
  878. RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
  879. if (dev->rfkill) {
  880. if (rfkill_register(dev->rfkill) < 0) {
  881. rfkill_destroy(dev->rfkill);
  882. dev->rfkill = NULL;
  883. }
  884. }
  885. dev->shutting_down = false;
  886. device_unlock(&dev->dev);
  887. rc = nfc_genl_device_added(dev);
  888. if (rc)
  889. pr_debug("The userspace won't be notified that the device %s was added\n",
  890. dev_name(&dev->dev));
  891. return 0;
  892. }
  893. EXPORT_SYMBOL(nfc_register_device);
  894. /**
  895. * nfc_unregister_rfkill - unregister a nfc device in the rfkill subsystem
  896. *
  897. * @dev: The nfc device to unregister
  898. */
  899. void nfc_unregister_rfkill(struct nfc_dev *dev)
  900. {
  901. struct rfkill *rfk = NULL;
  902. int rc;
  903. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  904. rc = nfc_genl_device_removed(dev);
  905. if (rc)
  906. pr_debug("The userspace won't be notified that the device %s "
  907. "was removed\n", dev_name(&dev->dev));
  908. device_lock(&dev->dev);
  909. if (dev->rfkill) {
  910. rfk = dev->rfkill;
  911. dev->rfkill = NULL;
  912. }
  913. dev->shutting_down = true;
  914. device_unlock(&dev->dev);
  915. if (rfk) {
  916. rfkill_unregister(rfk);
  917. rfkill_destroy(rfk);
  918. }
  919. }
  920. EXPORT_SYMBOL(nfc_unregister_rfkill);
  921. /**
  922. * nfc_remove_device - remove a nfc device in the nfc subsystem
  923. *
  924. * @dev: The nfc device to remove
  925. */
  926. void nfc_remove_device(struct nfc_dev *dev)
  927. {
  928. if (dev->ops->check_presence) {
  929. timer_delete_sync(&dev->check_pres_timer);
  930. cancel_work_sync(&dev->check_pres_work);
  931. }
  932. nfc_llcp_unregister_device(dev);
  933. mutex_lock(&nfc_devlist_mutex);
  934. nfc_devlist_generation++;
  935. device_del(&dev->dev);
  936. mutex_unlock(&nfc_devlist_mutex);
  937. }
  938. EXPORT_SYMBOL(nfc_remove_device);
  939. /**
  940. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  941. *
  942. * @dev: The nfc device to unregister
  943. */
  944. void nfc_unregister_device(struct nfc_dev *dev)
  945. {
  946. nfc_unregister_rfkill(dev);
  947. nfc_remove_device(dev);
  948. }
  949. EXPORT_SYMBOL(nfc_unregister_device);
  950. static int __init nfc_init(void)
  951. {
  952. int rc;
  953. pr_info("NFC Core ver %s\n", VERSION);
  954. rc = class_register(&nfc_class);
  955. if (rc)
  956. return rc;
  957. rc = nfc_genl_init();
  958. if (rc)
  959. goto err_genl;
  960. /* the first generation must not be 0 */
  961. nfc_devlist_generation = 1;
  962. rc = rawsock_init();
  963. if (rc)
  964. goto err_rawsock;
  965. rc = nfc_llcp_init();
  966. if (rc)
  967. goto err_llcp_sock;
  968. rc = af_nfc_init();
  969. if (rc)
  970. goto err_af_nfc;
  971. return 0;
  972. err_af_nfc:
  973. nfc_llcp_exit();
  974. err_llcp_sock:
  975. rawsock_exit();
  976. err_rawsock:
  977. nfc_genl_exit();
  978. err_genl:
  979. class_unregister(&nfc_class);
  980. return rc;
  981. }
  982. static void __exit nfc_exit(void)
  983. {
  984. af_nfc_exit();
  985. nfc_llcp_exit();
  986. rawsock_exit();
  987. nfc_genl_exit();
  988. class_unregister(&nfc_class);
  989. }
  990. subsys_initcall(nfc_init);
  991. module_exit(nfc_exit);
  992. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  993. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  994. MODULE_VERSION(VERSION);
  995. MODULE_LICENSE("GPL");
  996. MODULE_ALIAS_NETPROTO(PF_NFC);
  997. MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);