core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  303. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  304. }
  305. EXPORT_SYMBOL(nfc_dep_link_is_up);
  306. /**
  307. * nfc_activate_target - prepare the target for data exchange
  308. *
  309. * @dev: The nfc device that found the target
  310. * @target_idx: index of the target that must be activated
  311. * @protocol: nfc protocol that will be used for data exchange
  312. */
  313. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  314. {
  315. int rc;
  316. struct nfc_target *target;
  317. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  318. dev_name(&dev->dev), target_idx, protocol);
  319. device_lock(&dev->dev);
  320. if (!device_is_registered(&dev->dev)) {
  321. rc = -ENODEV;
  322. goto error;
  323. }
  324. if (dev->active_target) {
  325. rc = -EBUSY;
  326. goto error;
  327. }
  328. target = nfc_find_target(dev, target_idx);
  329. if (target == NULL) {
  330. rc = -ENOTCONN;
  331. goto error;
  332. }
  333. rc = dev->ops->activate_target(dev, target, protocol);
  334. if (!rc) {
  335. dev->active_target = target;
  336. dev->rf_mode = NFC_RF_INITIATOR;
  337. if (dev->ops->check_presence && !dev->shutting_down)
  338. mod_timer(&dev->check_pres_timer, jiffies +
  339. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  340. }
  341. error:
  342. device_unlock(&dev->dev);
  343. return rc;
  344. }
  345. /**
  346. * nfc_deactivate_target - deactivate a nfc target
  347. *
  348. * @dev: The nfc device that found the target
  349. * @target_idx: index of the target that must be deactivated
  350. */
  351. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
  352. {
  353. int rc = 0;
  354. pr_debug("dev_name=%s target_idx=%u\n",
  355. dev_name(&dev->dev), target_idx);
  356. device_lock(&dev->dev);
  357. if (!device_is_registered(&dev->dev)) {
  358. rc = -ENODEV;
  359. goto error;
  360. }
  361. if (dev->active_target == NULL) {
  362. rc = -ENOTCONN;
  363. goto error;
  364. }
  365. if (dev->active_target->idx != target_idx) {
  366. rc = -ENOTCONN;
  367. goto error;
  368. }
  369. if (dev->ops->check_presence)
  370. del_timer_sync(&dev->check_pres_timer);
  371. dev->ops->deactivate_target(dev, dev->active_target);
  372. dev->active_target = NULL;
  373. error:
  374. device_unlock(&dev->dev);
  375. return rc;
  376. }
  377. /**
  378. * nfc_data_exchange - transceive data
  379. *
  380. * @dev: The nfc device that found the target
  381. * @target_idx: index of the target
  382. * @skb: data to be sent
  383. * @cb: callback called when the response is received
  384. * @cb_context: parameter for the callback function
  385. *
  386. * The user must wait for the callback before calling this function again.
  387. */
  388. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  389. data_exchange_cb_t cb, void *cb_context)
  390. {
  391. int rc;
  392. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  393. dev_name(&dev->dev), target_idx, skb->len);
  394. device_lock(&dev->dev);
  395. if (!device_is_registered(&dev->dev)) {
  396. rc = -ENODEV;
  397. kfree_skb(skb);
  398. goto error;
  399. }
  400. if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
  401. if (dev->active_target->idx != target_idx) {
  402. rc = -EADDRNOTAVAIL;
  403. kfree_skb(skb);
  404. goto error;
  405. }
  406. if (dev->ops->check_presence)
  407. del_timer_sync(&dev->check_pres_timer);
  408. rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
  409. cb_context);
  410. if (!rc && dev->ops->check_presence && !dev->shutting_down)
  411. mod_timer(&dev->check_pres_timer, jiffies +
  412. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  413. } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
  414. rc = dev->ops->tm_send(dev, skb);
  415. } else {
  416. rc = -ENOTCONN;
  417. kfree_skb(skb);
  418. goto error;
  419. }
  420. error:
  421. device_unlock(&dev->dev);
  422. return rc;
  423. }
  424. struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
  425. {
  426. struct nfc_se *se, *n;
  427. list_for_each_entry_safe(se, n, &dev->secure_elements, list)
  428. if (se->idx == se_idx)
  429. return se;
  430. return NULL;
  431. }
  432. EXPORT_SYMBOL(nfc_find_se);
  433. int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
  434. {
  435. struct nfc_se *se;
  436. int rc;
  437. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  438. device_lock(&dev->dev);
  439. if (!device_is_registered(&dev->dev)) {
  440. rc = -ENODEV;
  441. goto error;
  442. }
  443. if (!dev->dev_up) {
  444. rc = -ENODEV;
  445. goto error;
  446. }
  447. if (dev->polling) {
  448. rc = -EBUSY;
  449. goto error;
  450. }
  451. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  452. rc = -EOPNOTSUPP;
  453. goto error;
  454. }
  455. se = nfc_find_se(dev, se_idx);
  456. if (!se) {
  457. rc = -EINVAL;
  458. goto error;
  459. }
  460. if (se->state == NFC_SE_ENABLED) {
  461. rc = -EALREADY;
  462. goto error;
  463. }
  464. rc = dev->ops->enable_se(dev, se_idx);
  465. if (rc >= 0)
  466. se->state = NFC_SE_ENABLED;
  467. error:
  468. device_unlock(&dev->dev);
  469. return rc;
  470. }
  471. int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
  472. {
  473. struct nfc_se *se;
  474. int rc;
  475. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  476. device_lock(&dev->dev);
  477. if (!device_is_registered(&dev->dev)) {
  478. rc = -ENODEV;
  479. goto error;
  480. }
  481. if (!dev->dev_up) {
  482. rc = -ENODEV;
  483. goto error;
  484. }
  485. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  486. rc = -EOPNOTSUPP;
  487. goto error;
  488. }
  489. se = nfc_find_se(dev, se_idx);
  490. if (!se) {
  491. rc = -EINVAL;
  492. goto error;
  493. }
  494. if (se->state == NFC_SE_DISABLED) {
  495. rc = -EALREADY;
  496. goto error;
  497. }
  498. rc = dev->ops->disable_se(dev, se_idx);
  499. if (rc >= 0)
  500. se->state = NFC_SE_DISABLED;
  501. error:
  502. device_unlock(&dev->dev);
  503. return rc;
  504. }
  505. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  506. {
  507. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  508. if (gb_len > NFC_MAX_GT_LEN)
  509. return -EINVAL;
  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. static void nfc_release(struct device *d)
  730. {
  731. struct nfc_dev *dev = to_nfc_dev(d);
  732. struct nfc_se *se, *n;
  733. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  734. nfc_genl_data_exit(&dev->genl_data);
  735. kfree(dev->targets);
  736. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  737. nfc_genl_se_removed(dev, se->idx);
  738. list_del(&se->list);
  739. kfree(se);
  740. }
  741. kfree(dev);
  742. }
  743. static void nfc_check_pres_work(struct work_struct *work)
  744. {
  745. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  746. check_pres_work);
  747. int rc;
  748. device_lock(&dev->dev);
  749. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  750. rc = dev->ops->check_presence(dev, dev->active_target);
  751. if (rc == -EOPNOTSUPP)
  752. goto exit;
  753. if (rc) {
  754. u32 active_target_idx = dev->active_target->idx;
  755. device_unlock(&dev->dev);
  756. nfc_target_lost(dev, active_target_idx);
  757. return;
  758. }
  759. if (!dev->shutting_down)
  760. mod_timer(&dev->check_pres_timer, jiffies +
  761. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  762. }
  763. exit:
  764. device_unlock(&dev->dev);
  765. }
  766. static void nfc_check_pres_timeout(unsigned long data)
  767. {
  768. struct nfc_dev *dev = (struct nfc_dev *)data;
  769. schedule_work(&dev->check_pres_work);
  770. }
  771. struct class nfc_class = {
  772. .name = "nfc",
  773. .dev_release = nfc_release,
  774. };
  775. EXPORT_SYMBOL(nfc_class);
  776. static int match_idx(struct device *d, const void *data)
  777. {
  778. struct nfc_dev *dev = to_nfc_dev(d);
  779. const unsigned int *idx = data;
  780. return dev->idx == *idx;
  781. }
  782. struct nfc_dev *nfc_get_device(unsigned int idx)
  783. {
  784. struct device *d;
  785. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  786. if (!d)
  787. return NULL;
  788. return to_nfc_dev(d);
  789. }
  790. /**
  791. * nfc_allocate_device - allocate a new nfc device
  792. *
  793. * @ops: device operations
  794. * @supported_protocols: NFC protocols supported by the device
  795. */
  796. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  797. u32 supported_protocols,
  798. int tx_headroom, int tx_tailroom)
  799. {
  800. struct nfc_dev *dev;
  801. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  802. !ops->deactivate_target || !ops->im_transceive)
  803. return NULL;
  804. if (!supported_protocols)
  805. return NULL;
  806. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  807. if (!dev)
  808. return NULL;
  809. dev->ops = ops;
  810. dev->supported_protocols = supported_protocols;
  811. dev->tx_headroom = tx_headroom;
  812. dev->tx_tailroom = tx_tailroom;
  813. INIT_LIST_HEAD(&dev->secure_elements);
  814. nfc_genl_data_init(&dev->genl_data);
  815. dev->rf_mode = NFC_RF_NONE;
  816. /* first generation must not be 0 */
  817. dev->targets_generation = 1;
  818. if (ops->check_presence) {
  819. init_timer(&dev->check_pres_timer);
  820. dev->check_pres_timer.data = (unsigned long)dev;
  821. dev->check_pres_timer.function = nfc_check_pres_timeout;
  822. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  823. }
  824. return dev;
  825. }
  826. EXPORT_SYMBOL(nfc_allocate_device);
  827. /**
  828. * nfc_register_device - register a nfc device in the nfc subsystem
  829. *
  830. * @dev: The nfc device to register
  831. */
  832. int nfc_register_device(struct nfc_dev *dev)
  833. {
  834. int rc;
  835. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  836. dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
  837. if (dev->idx < 0)
  838. return dev->idx;
  839. dev->dev.class = &nfc_class;
  840. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  841. device_initialize(&dev->dev);
  842. mutex_lock(&nfc_devlist_mutex);
  843. nfc_devlist_generation++;
  844. rc = device_add(&dev->dev);
  845. mutex_unlock(&nfc_devlist_mutex);
  846. if (rc < 0)
  847. return rc;
  848. rc = nfc_llcp_register_device(dev);
  849. if (rc)
  850. pr_err("Could not register llcp device\n");
  851. rc = nfc_genl_device_added(dev);
  852. if (rc)
  853. pr_debug("The userspace won't be notified that the device %s was added\n",
  854. dev_name(&dev->dev));
  855. dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
  856. RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
  857. if (dev->rfkill) {
  858. if (rfkill_register(dev->rfkill) < 0) {
  859. rfkill_destroy(dev->rfkill);
  860. dev->rfkill = NULL;
  861. }
  862. }
  863. return 0;
  864. }
  865. EXPORT_SYMBOL(nfc_register_device);
  866. /**
  867. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  868. *
  869. * @dev: The nfc device to unregister
  870. */
  871. void nfc_unregister_device(struct nfc_dev *dev)
  872. {
  873. int rc, id;
  874. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  875. id = dev->idx;
  876. if (dev->rfkill) {
  877. rfkill_unregister(dev->rfkill);
  878. rfkill_destroy(dev->rfkill);
  879. }
  880. if (dev->ops->check_presence) {
  881. device_lock(&dev->dev);
  882. dev->shutting_down = true;
  883. device_unlock(&dev->dev);
  884. del_timer_sync(&dev->check_pres_timer);
  885. cancel_work_sync(&dev->check_pres_work);
  886. }
  887. rc = nfc_genl_device_removed(dev);
  888. if (rc)
  889. pr_debug("The userspace won't be notified that the device %s "
  890. "was removed\n", dev_name(&dev->dev));
  891. nfc_llcp_unregister_device(dev);
  892. mutex_lock(&nfc_devlist_mutex);
  893. nfc_devlist_generation++;
  894. device_del(&dev->dev);
  895. mutex_unlock(&nfc_devlist_mutex);
  896. ida_simple_remove(&nfc_index_ida, id);
  897. }
  898. EXPORT_SYMBOL(nfc_unregister_device);
  899. static int __init nfc_init(void)
  900. {
  901. int rc;
  902. pr_info("NFC Core ver %s\n", VERSION);
  903. rc = class_register(&nfc_class);
  904. if (rc)
  905. return rc;
  906. rc = nfc_genl_init();
  907. if (rc)
  908. goto err_genl;
  909. /* the first generation must not be 0 */
  910. nfc_devlist_generation = 1;
  911. rc = rawsock_init();
  912. if (rc)
  913. goto err_rawsock;
  914. rc = nfc_llcp_init();
  915. if (rc)
  916. goto err_llcp_sock;
  917. rc = af_nfc_init();
  918. if (rc)
  919. goto err_af_nfc;
  920. return 0;
  921. err_af_nfc:
  922. nfc_llcp_exit();
  923. err_llcp_sock:
  924. rawsock_exit();
  925. err_rawsock:
  926. nfc_genl_exit();
  927. err_genl:
  928. class_unregister(&nfc_class);
  929. return rc;
  930. }
  931. static void __exit nfc_exit(void)
  932. {
  933. af_nfc_exit();
  934. nfc_llcp_exit();
  935. rawsock_exit();
  936. nfc_genl_exit();
  937. class_unregister(&nfc_class);
  938. }
  939. subsys_initcall(nfc_init);
  940. module_exit(nfc_exit);
  941. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  942. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  943. MODULE_VERSION(VERSION);
  944. MODULE_LICENSE("GPL");
  945. MODULE_ALIAS_NETPROTO(PF_NFC);
  946. MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);