core.c 24 KB

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