core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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/nfc.h>
  29. #include "nfc.h"
  30. #define VERSION "0.1"
  31. #define NFC_CHECK_PRES_FREQ_MS 2000
  32. int nfc_devlist_generation;
  33. DEFINE_MUTEX(nfc_devlist_mutex);
  34. /**
  35. * nfc_dev_up - turn on the NFC device
  36. *
  37. * @dev: The nfc device to be turned on
  38. *
  39. * The device remains up until the nfc_dev_down function is called.
  40. */
  41. int nfc_dev_up(struct nfc_dev *dev)
  42. {
  43. int rc = 0;
  44. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  45. device_lock(&dev->dev);
  46. if (!device_is_registered(&dev->dev)) {
  47. rc = -ENODEV;
  48. goto error;
  49. }
  50. if (dev->dev_up) {
  51. rc = -EALREADY;
  52. goto error;
  53. }
  54. if (dev->ops->dev_up)
  55. rc = dev->ops->dev_up(dev);
  56. if (!rc)
  57. dev->dev_up = true;
  58. error:
  59. device_unlock(&dev->dev);
  60. return rc;
  61. }
  62. /**
  63. * nfc_dev_down - turn off the NFC device
  64. *
  65. * @dev: The nfc device to be turned off
  66. */
  67. int nfc_dev_down(struct nfc_dev *dev)
  68. {
  69. int rc = 0;
  70. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  71. device_lock(&dev->dev);
  72. if (!device_is_registered(&dev->dev)) {
  73. rc = -ENODEV;
  74. goto error;
  75. }
  76. if (!dev->dev_up) {
  77. rc = -EALREADY;
  78. goto error;
  79. }
  80. if (dev->polling || dev->active_target) {
  81. rc = -EBUSY;
  82. goto error;
  83. }
  84. if (dev->ops->dev_down)
  85. dev->ops->dev_down(dev);
  86. dev->dev_up = false;
  87. error:
  88. device_unlock(&dev->dev);
  89. return rc;
  90. }
  91. /**
  92. * nfc_start_poll - start polling for nfc targets
  93. *
  94. * @dev: The nfc device that must start polling
  95. * @protocols: bitset of nfc protocols that must be used for polling
  96. *
  97. * The device remains polling for targets until a target is found or
  98. * the nfc_stop_poll function is called.
  99. */
  100. int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
  101. {
  102. int rc;
  103. pr_debug("dev_name=%s protocols=0x%x\n",
  104. dev_name(&dev->dev), protocols);
  105. if (!protocols)
  106. return -EINVAL;
  107. device_lock(&dev->dev);
  108. if (!device_is_registered(&dev->dev)) {
  109. rc = -ENODEV;
  110. goto error;
  111. }
  112. if (dev->polling) {
  113. rc = -EBUSY;
  114. goto error;
  115. }
  116. rc = dev->ops->start_poll(dev, protocols);
  117. if (!rc)
  118. dev->polling = true;
  119. error:
  120. device_unlock(&dev->dev);
  121. return rc;
  122. }
  123. /**
  124. * nfc_stop_poll - stop polling for nfc targets
  125. *
  126. * @dev: The nfc device that must stop polling
  127. */
  128. int nfc_stop_poll(struct nfc_dev *dev)
  129. {
  130. int rc = 0;
  131. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  132. device_lock(&dev->dev);
  133. if (!device_is_registered(&dev->dev)) {
  134. rc = -ENODEV;
  135. goto error;
  136. }
  137. if (!dev->polling) {
  138. rc = -EINVAL;
  139. goto error;
  140. }
  141. dev->ops->stop_poll(dev);
  142. dev->polling = false;
  143. error:
  144. device_unlock(&dev->dev);
  145. return rc;
  146. }
  147. static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
  148. {
  149. int i;
  150. if (dev->n_targets == 0)
  151. return NULL;
  152. for (i = 0; i < dev->n_targets ; i++) {
  153. if (dev->targets[i].idx == target_idx)
  154. return &dev->targets[i];
  155. }
  156. return NULL;
  157. }
  158. int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
  159. {
  160. int rc = 0;
  161. u8 *gb;
  162. size_t gb_len;
  163. struct nfc_target *target;
  164. pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
  165. if (!dev->ops->dep_link_up)
  166. return -EOPNOTSUPP;
  167. device_lock(&dev->dev);
  168. if (!device_is_registered(&dev->dev)) {
  169. rc = -ENODEV;
  170. goto error;
  171. }
  172. if (dev->dep_link_up == true) {
  173. rc = -EALREADY;
  174. goto error;
  175. }
  176. gb = nfc_llcp_general_bytes(dev, &gb_len);
  177. if (gb_len > NFC_MAX_GT_LEN) {
  178. rc = -EINVAL;
  179. goto error;
  180. }
  181. target = nfc_find_target(dev, target_index);
  182. if (target == NULL) {
  183. rc = -ENOTCONN;
  184. goto error;
  185. }
  186. rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
  187. if (!rc)
  188. dev->active_target = target;
  189. error:
  190. device_unlock(&dev->dev);
  191. return rc;
  192. }
  193. int nfc_dep_link_down(struct nfc_dev *dev)
  194. {
  195. int rc = 0;
  196. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  197. if (!dev->ops->dep_link_down)
  198. return -EOPNOTSUPP;
  199. device_lock(&dev->dev);
  200. if (!device_is_registered(&dev->dev)) {
  201. rc = -ENODEV;
  202. goto error;
  203. }
  204. if (dev->dep_link_up == false) {
  205. rc = -EALREADY;
  206. goto error;
  207. }
  208. if (dev->dep_rf_mode == NFC_RF_TARGET) {
  209. rc = -EOPNOTSUPP;
  210. goto error;
  211. }
  212. rc = dev->ops->dep_link_down(dev);
  213. if (!rc) {
  214. dev->dep_link_up = false;
  215. dev->active_target = NULL;
  216. nfc_llcp_mac_is_down(dev);
  217. nfc_genl_dep_link_down_event(dev);
  218. }
  219. error:
  220. device_unlock(&dev->dev);
  221. return rc;
  222. }
  223. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  224. u8 comm_mode, u8 rf_mode)
  225. {
  226. dev->dep_link_up = true;
  227. dev->dep_rf_mode = rf_mode;
  228. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  229. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  230. }
  231. EXPORT_SYMBOL(nfc_dep_link_is_up);
  232. /**
  233. * nfc_activate_target - prepare the target for data exchange
  234. *
  235. * @dev: The nfc device that found the target
  236. * @target_idx: index of the target that must be activated
  237. * @protocol: nfc protocol that will be used for data exchange
  238. */
  239. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  240. {
  241. int rc;
  242. struct nfc_target *target;
  243. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  244. dev_name(&dev->dev), target_idx, protocol);
  245. device_lock(&dev->dev);
  246. if (!device_is_registered(&dev->dev)) {
  247. rc = -ENODEV;
  248. goto error;
  249. }
  250. if (dev->active_target) {
  251. rc = -EBUSY;
  252. goto error;
  253. }
  254. target = nfc_find_target(dev, target_idx);
  255. if (target == NULL) {
  256. rc = -ENOTCONN;
  257. goto error;
  258. }
  259. rc = dev->ops->activate_target(dev, target, protocol);
  260. if (!rc) {
  261. dev->active_target = target;
  262. if (dev->ops->check_presence)
  263. mod_timer(&dev->check_pres_timer, jiffies +
  264. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  265. }
  266. error:
  267. device_unlock(&dev->dev);
  268. return rc;
  269. }
  270. /**
  271. * nfc_deactivate_target - deactivate a nfc target
  272. *
  273. * @dev: The nfc device that found the target
  274. * @target_idx: index of the target that must be deactivated
  275. */
  276. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
  277. {
  278. int rc = 0;
  279. pr_debug("dev_name=%s target_idx=%u\n",
  280. dev_name(&dev->dev), target_idx);
  281. device_lock(&dev->dev);
  282. if (!device_is_registered(&dev->dev)) {
  283. rc = -ENODEV;
  284. goto error;
  285. }
  286. if (dev->active_target == NULL) {
  287. rc = -ENOTCONN;
  288. goto error;
  289. }
  290. if (dev->active_target->idx != target_idx) {
  291. rc = -ENOTCONN;
  292. goto error;
  293. }
  294. if (dev->ops->check_presence)
  295. del_timer_sync(&dev->check_pres_timer);
  296. dev->ops->deactivate_target(dev, dev->active_target);
  297. dev->active_target = NULL;
  298. error:
  299. device_unlock(&dev->dev);
  300. return rc;
  301. }
  302. /**
  303. * nfc_data_exchange - transceive data
  304. *
  305. * @dev: The nfc device that found the target
  306. * @target_idx: index of the target
  307. * @skb: data to be sent
  308. * @cb: callback called when the response is received
  309. * @cb_context: parameter for the callback function
  310. *
  311. * The user must wait for the callback before calling this function again.
  312. */
  313. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  314. data_exchange_cb_t cb, void *cb_context)
  315. {
  316. int rc;
  317. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  318. dev_name(&dev->dev), target_idx, skb->len);
  319. device_lock(&dev->dev);
  320. if (!device_is_registered(&dev->dev)) {
  321. rc = -ENODEV;
  322. kfree_skb(skb);
  323. goto error;
  324. }
  325. if (dev->active_target == NULL) {
  326. rc = -ENOTCONN;
  327. kfree_skb(skb);
  328. goto error;
  329. }
  330. if (dev->active_target->idx != target_idx) {
  331. rc = -EADDRNOTAVAIL;
  332. kfree_skb(skb);
  333. goto error;
  334. }
  335. if (dev->ops->check_presence)
  336. del_timer_sync(&dev->check_pres_timer);
  337. rc = dev->ops->data_exchange(dev, dev->active_target, skb, cb,
  338. cb_context);
  339. if (!rc && dev->ops->check_presence)
  340. mod_timer(&dev->check_pres_timer, jiffies +
  341. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  342. error:
  343. device_unlock(&dev->dev);
  344. return rc;
  345. }
  346. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  347. {
  348. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  349. if (gb_len > NFC_MAX_GT_LEN)
  350. return -EINVAL;
  351. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  352. }
  353. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  354. /**
  355. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  356. *
  357. * @size: size to allocate
  358. * @gfp: gfp flags
  359. */
  360. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  361. unsigned int flags, unsigned int size,
  362. unsigned int *err)
  363. {
  364. struct sk_buff *skb;
  365. unsigned int total_size;
  366. total_size = size +
  367. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  368. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  369. if (skb)
  370. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  371. return skb;
  372. }
  373. /**
  374. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  375. *
  376. * @size: size to allocate
  377. * @gfp: gfp flags
  378. */
  379. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  380. {
  381. struct sk_buff *skb;
  382. unsigned int total_size;
  383. total_size = size + 1;
  384. skb = alloc_skb(total_size, gfp);
  385. if (skb)
  386. skb_reserve(skb, 1);
  387. return skb;
  388. }
  389. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  390. /**
  391. * nfc_targets_found - inform that targets were found
  392. *
  393. * @dev: The nfc device that found the targets
  394. * @targets: array of nfc targets found
  395. * @ntargets: targets array size
  396. *
  397. * The device driver must call this function when one or many nfc targets
  398. * are found. After calling this function, the device driver must stop
  399. * polling for targets.
  400. * IMPORTANT: this function must not be called from an atomic context.
  401. * In addition, it must also not be called from a context that would prevent
  402. * the NFC Core to call other nfc ops entry point concurrently.
  403. */
  404. int nfc_targets_found(struct nfc_dev *dev,
  405. struct nfc_target *targets, int n_targets)
  406. {
  407. int i;
  408. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  409. dev->polling = false;
  410. for (i = 0; i < n_targets; i++)
  411. targets[i].idx = dev->target_next_idx++;
  412. device_lock(&dev->dev);
  413. dev->targets_generation++;
  414. kfree(dev->targets);
  415. dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
  416. GFP_ATOMIC);
  417. if (!dev->targets) {
  418. dev->n_targets = 0;
  419. device_unlock(&dev->dev);
  420. return -ENOMEM;
  421. }
  422. dev->n_targets = n_targets;
  423. device_unlock(&dev->dev);
  424. nfc_genl_targets_found(dev);
  425. return 0;
  426. }
  427. EXPORT_SYMBOL(nfc_targets_found);
  428. /**
  429. * nfc_target_lost - inform that an activated target went out of field
  430. *
  431. * @dev: The nfc device that had the activated target in field
  432. * @target_idx: the nfc index of the target
  433. *
  434. * The device driver must call this function when the activated target
  435. * goes out of the field.
  436. * IMPORTANT: this function must not be called from an atomic context.
  437. * In addition, it must also not be called from a context that would prevent
  438. * the NFC Core to call other nfc ops entry point concurrently.
  439. */
  440. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  441. {
  442. struct nfc_target *tg;
  443. int i;
  444. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  445. device_lock(&dev->dev);
  446. for (i = 0; i < dev->n_targets; i++) {
  447. tg = &dev->targets[i];
  448. if (tg->idx == target_idx)
  449. break;
  450. }
  451. if (i == dev->n_targets) {
  452. device_unlock(&dev->dev);
  453. return -EINVAL;
  454. }
  455. dev->targets_generation++;
  456. dev->n_targets--;
  457. dev->active_target = NULL;
  458. if (dev->n_targets) {
  459. memcpy(&dev->targets[i], &dev->targets[i + 1],
  460. (dev->n_targets - i) * sizeof(struct nfc_target));
  461. } else {
  462. kfree(dev->targets);
  463. dev->targets = NULL;
  464. }
  465. device_unlock(&dev->dev);
  466. nfc_genl_target_lost(dev, target_idx);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL(nfc_target_lost);
  470. static void nfc_release(struct device *d)
  471. {
  472. struct nfc_dev *dev = to_nfc_dev(d);
  473. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  474. if (dev->ops->check_presence) {
  475. del_timer_sync(&dev->check_pres_timer);
  476. destroy_workqueue(dev->check_pres_wq);
  477. }
  478. nfc_genl_data_exit(&dev->genl_data);
  479. kfree(dev->targets);
  480. kfree(dev);
  481. }
  482. static void nfc_check_pres_work(struct work_struct *work)
  483. {
  484. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  485. check_pres_work);
  486. int rc;
  487. device_lock(&dev->dev);
  488. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  489. rc = dev->ops->check_presence(dev, dev->active_target);
  490. if (!rc) {
  491. mod_timer(&dev->check_pres_timer, jiffies +
  492. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  493. } else {
  494. u32 active_target_idx = dev->active_target->idx;
  495. device_unlock(&dev->dev);
  496. nfc_target_lost(dev, active_target_idx);
  497. return;
  498. }
  499. }
  500. device_unlock(&dev->dev);
  501. }
  502. static void nfc_check_pres_timeout(unsigned long data)
  503. {
  504. struct nfc_dev *dev = (struct nfc_dev *)data;
  505. queue_work(dev->check_pres_wq, &dev->check_pres_work);
  506. }
  507. struct class nfc_class = {
  508. .name = "nfc",
  509. .dev_release = nfc_release,
  510. };
  511. EXPORT_SYMBOL(nfc_class);
  512. static int match_idx(struct device *d, void *data)
  513. {
  514. struct nfc_dev *dev = to_nfc_dev(d);
  515. unsigned int *idx = data;
  516. return dev->idx == *idx;
  517. }
  518. struct nfc_dev *nfc_get_device(unsigned int idx)
  519. {
  520. struct device *d;
  521. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  522. if (!d)
  523. return NULL;
  524. return to_nfc_dev(d);
  525. }
  526. /**
  527. * nfc_allocate_device - allocate a new nfc device
  528. *
  529. * @ops: device operations
  530. * @supported_protocols: NFC protocols supported by the device
  531. */
  532. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  533. u32 supported_protocols,
  534. int tx_headroom, int tx_tailroom)
  535. {
  536. static atomic_t dev_no = ATOMIC_INIT(0);
  537. struct nfc_dev *dev;
  538. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  539. !ops->deactivate_target || !ops->data_exchange)
  540. return NULL;
  541. if (!supported_protocols)
  542. return NULL;
  543. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  544. if (!dev)
  545. return NULL;
  546. dev->dev.class = &nfc_class;
  547. dev->idx = atomic_inc_return(&dev_no) - 1;
  548. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  549. device_initialize(&dev->dev);
  550. dev->ops = ops;
  551. dev->supported_protocols = supported_protocols;
  552. dev->tx_headroom = tx_headroom;
  553. dev->tx_tailroom = tx_tailroom;
  554. nfc_genl_data_init(&dev->genl_data);
  555. /* first generation must not be 0 */
  556. dev->targets_generation = 1;
  557. if (ops->check_presence) {
  558. char name[32];
  559. init_timer(&dev->check_pres_timer);
  560. dev->check_pres_timer.data = (unsigned long)dev;
  561. dev->check_pres_timer.function = nfc_check_pres_timeout;
  562. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  563. snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
  564. dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
  565. WQ_UNBOUND |
  566. WQ_MEM_RECLAIM, 1);
  567. if (dev->check_pres_wq == NULL) {
  568. kfree(dev);
  569. return NULL;
  570. }
  571. }
  572. return dev;
  573. }
  574. EXPORT_SYMBOL(nfc_allocate_device);
  575. /**
  576. * nfc_register_device - register a nfc device in the nfc subsystem
  577. *
  578. * @dev: The nfc device to register
  579. */
  580. int nfc_register_device(struct nfc_dev *dev)
  581. {
  582. int rc;
  583. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  584. mutex_lock(&nfc_devlist_mutex);
  585. nfc_devlist_generation++;
  586. rc = device_add(&dev->dev);
  587. mutex_unlock(&nfc_devlist_mutex);
  588. if (rc < 0)
  589. return rc;
  590. rc = nfc_llcp_register_device(dev);
  591. if (rc)
  592. pr_err("Could not register llcp device\n");
  593. rc = nfc_genl_device_added(dev);
  594. if (rc)
  595. pr_debug("The userspace won't be notified that the device %s was added\n",
  596. dev_name(&dev->dev));
  597. return 0;
  598. }
  599. EXPORT_SYMBOL(nfc_register_device);
  600. /**
  601. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  602. *
  603. * @dev: The nfc device to unregister
  604. */
  605. void nfc_unregister_device(struct nfc_dev *dev)
  606. {
  607. int rc;
  608. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  609. mutex_lock(&nfc_devlist_mutex);
  610. nfc_devlist_generation++;
  611. /* lock to avoid unregistering a device while an operation
  612. is in progress */
  613. device_lock(&dev->dev);
  614. device_del(&dev->dev);
  615. device_unlock(&dev->dev);
  616. mutex_unlock(&nfc_devlist_mutex);
  617. nfc_llcp_unregister_device(dev);
  618. rc = nfc_genl_device_removed(dev);
  619. if (rc)
  620. pr_debug("The userspace won't be notified that the device %s was removed\n",
  621. dev_name(&dev->dev));
  622. }
  623. EXPORT_SYMBOL(nfc_unregister_device);
  624. static int __init nfc_init(void)
  625. {
  626. int rc;
  627. pr_info("NFC Core ver %s\n", VERSION);
  628. rc = class_register(&nfc_class);
  629. if (rc)
  630. return rc;
  631. rc = nfc_genl_init();
  632. if (rc)
  633. goto err_genl;
  634. /* the first generation must not be 0 */
  635. nfc_devlist_generation = 1;
  636. rc = rawsock_init();
  637. if (rc)
  638. goto err_rawsock;
  639. rc = nfc_llcp_init();
  640. if (rc)
  641. goto err_llcp_sock;
  642. rc = af_nfc_init();
  643. if (rc)
  644. goto err_af_nfc;
  645. return 0;
  646. err_af_nfc:
  647. nfc_llcp_exit();
  648. err_llcp_sock:
  649. rawsock_exit();
  650. err_rawsock:
  651. nfc_genl_exit();
  652. err_genl:
  653. class_unregister(&nfc_class);
  654. return rc;
  655. }
  656. static void __exit nfc_exit(void)
  657. {
  658. af_nfc_exit();
  659. nfc_llcp_exit();
  660. rawsock_exit();
  661. nfc_genl_exit();
  662. class_unregister(&nfc_class);
  663. }
  664. subsys_initcall(nfc_init);
  665. module_exit(nfc_exit);
  666. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  667. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  668. MODULE_VERSION(VERSION);
  669. MODULE_LICENSE("GPL");