core.c 24 KB

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