core.c 25 KB

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