core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 im_protocols, u32 tm_protocols)
  101. {
  102. int rc;
  103. pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
  104. dev_name(&dev->dev), im_protocols, tm_protocols);
  105. if (!im_protocols && !tm_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, im_protocols, tm_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. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
  355. {
  356. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  357. return nfc_llcp_general_bytes(dev, gb_len);
  358. }
  359. EXPORT_SYMBOL(nfc_get_local_general_bytes);
  360. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  361. u8 *gb, size_t gb_len)
  362. {
  363. int rc;
  364. device_lock(&dev->dev);
  365. dev->polling = false;
  366. if (gb != NULL) {
  367. rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
  368. if (rc < 0)
  369. goto out;
  370. }
  371. if (protocol == NFC_PROTO_NFC_DEP_MASK)
  372. nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
  373. rc = nfc_genl_tm_activated(dev, protocol);
  374. out:
  375. device_unlock(&dev->dev);
  376. return rc;
  377. }
  378. EXPORT_SYMBOL(nfc_tm_activated);
  379. int nfc_tm_deactivated(struct nfc_dev *dev)
  380. {
  381. dev->dep_link_up = false;
  382. return nfc_genl_tm_deactivated(dev);
  383. }
  384. EXPORT_SYMBOL(nfc_tm_deactivated);
  385. /**
  386. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  387. *
  388. * @size: size to allocate
  389. * @gfp: gfp flags
  390. */
  391. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  392. unsigned int flags, unsigned int size,
  393. unsigned int *err)
  394. {
  395. struct sk_buff *skb;
  396. unsigned int total_size;
  397. total_size = size +
  398. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  399. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  400. if (skb)
  401. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  402. return skb;
  403. }
  404. /**
  405. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  406. *
  407. * @size: size to allocate
  408. * @gfp: gfp flags
  409. */
  410. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  411. {
  412. struct sk_buff *skb;
  413. unsigned int total_size;
  414. total_size = size + 1;
  415. skb = alloc_skb(total_size, gfp);
  416. if (skb)
  417. skb_reserve(skb, 1);
  418. return skb;
  419. }
  420. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  421. /**
  422. * nfc_targets_found - inform that targets were found
  423. *
  424. * @dev: The nfc device that found the targets
  425. * @targets: array of nfc targets found
  426. * @ntargets: targets array size
  427. *
  428. * The device driver must call this function when one or many nfc targets
  429. * are found. After calling this function, the device driver must stop
  430. * polling for targets.
  431. * IMPORTANT: this function must not be called from an atomic context.
  432. * In addition, it must also not be called from a context that would prevent
  433. * the NFC Core to call other nfc ops entry point concurrently.
  434. */
  435. int nfc_targets_found(struct nfc_dev *dev,
  436. struct nfc_target *targets, int n_targets)
  437. {
  438. int i;
  439. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  440. dev->polling = false;
  441. for (i = 0; i < n_targets; i++)
  442. targets[i].idx = dev->target_next_idx++;
  443. device_lock(&dev->dev);
  444. dev->targets_generation++;
  445. kfree(dev->targets);
  446. dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
  447. GFP_ATOMIC);
  448. if (!dev->targets) {
  449. dev->n_targets = 0;
  450. device_unlock(&dev->dev);
  451. return -ENOMEM;
  452. }
  453. dev->n_targets = n_targets;
  454. device_unlock(&dev->dev);
  455. nfc_genl_targets_found(dev);
  456. return 0;
  457. }
  458. EXPORT_SYMBOL(nfc_targets_found);
  459. /**
  460. * nfc_target_lost - inform that an activated target went out of field
  461. *
  462. * @dev: The nfc device that had the activated target in field
  463. * @target_idx: the nfc index of the target
  464. *
  465. * The device driver must call this function when the activated target
  466. * goes out of the field.
  467. * IMPORTANT: this function must not be called from an atomic context.
  468. * In addition, it must also not be called from a context that would prevent
  469. * the NFC Core to call other nfc ops entry point concurrently.
  470. */
  471. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  472. {
  473. struct nfc_target *tg;
  474. int i;
  475. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  476. device_lock(&dev->dev);
  477. for (i = 0; i < dev->n_targets; i++) {
  478. tg = &dev->targets[i];
  479. if (tg->idx == target_idx)
  480. break;
  481. }
  482. if (i == dev->n_targets) {
  483. device_unlock(&dev->dev);
  484. return -EINVAL;
  485. }
  486. dev->targets_generation++;
  487. dev->n_targets--;
  488. dev->active_target = NULL;
  489. if (dev->n_targets) {
  490. memcpy(&dev->targets[i], &dev->targets[i + 1],
  491. (dev->n_targets - i) * sizeof(struct nfc_target));
  492. } else {
  493. kfree(dev->targets);
  494. dev->targets = NULL;
  495. }
  496. device_unlock(&dev->dev);
  497. nfc_genl_target_lost(dev, target_idx);
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(nfc_target_lost);
  501. static void nfc_release(struct device *d)
  502. {
  503. struct nfc_dev *dev = to_nfc_dev(d);
  504. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  505. if (dev->ops->check_presence) {
  506. del_timer_sync(&dev->check_pres_timer);
  507. destroy_workqueue(dev->check_pres_wq);
  508. }
  509. nfc_genl_data_exit(&dev->genl_data);
  510. kfree(dev->targets);
  511. kfree(dev);
  512. }
  513. static void nfc_check_pres_work(struct work_struct *work)
  514. {
  515. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  516. check_pres_work);
  517. int rc;
  518. device_lock(&dev->dev);
  519. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  520. rc = dev->ops->check_presence(dev, dev->active_target);
  521. if (!rc) {
  522. mod_timer(&dev->check_pres_timer, jiffies +
  523. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  524. } else {
  525. u32 active_target_idx = dev->active_target->idx;
  526. device_unlock(&dev->dev);
  527. nfc_target_lost(dev, active_target_idx);
  528. return;
  529. }
  530. }
  531. device_unlock(&dev->dev);
  532. }
  533. static void nfc_check_pres_timeout(unsigned long data)
  534. {
  535. struct nfc_dev *dev = (struct nfc_dev *)data;
  536. queue_work(dev->check_pres_wq, &dev->check_pres_work);
  537. }
  538. struct class nfc_class = {
  539. .name = "nfc",
  540. .dev_release = nfc_release,
  541. };
  542. EXPORT_SYMBOL(nfc_class);
  543. static int match_idx(struct device *d, void *data)
  544. {
  545. struct nfc_dev *dev = to_nfc_dev(d);
  546. unsigned int *idx = data;
  547. return dev->idx == *idx;
  548. }
  549. struct nfc_dev *nfc_get_device(unsigned int idx)
  550. {
  551. struct device *d;
  552. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  553. if (!d)
  554. return NULL;
  555. return to_nfc_dev(d);
  556. }
  557. /**
  558. * nfc_allocate_device - allocate a new nfc device
  559. *
  560. * @ops: device operations
  561. * @supported_protocols: NFC protocols supported by the device
  562. */
  563. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  564. u32 supported_protocols,
  565. int tx_headroom, int tx_tailroom)
  566. {
  567. static atomic_t dev_no = ATOMIC_INIT(0);
  568. struct nfc_dev *dev;
  569. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  570. !ops->deactivate_target || !ops->data_exchange)
  571. return NULL;
  572. if (!supported_protocols)
  573. return NULL;
  574. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  575. if (!dev)
  576. return NULL;
  577. dev->dev.class = &nfc_class;
  578. dev->idx = atomic_inc_return(&dev_no) - 1;
  579. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  580. device_initialize(&dev->dev);
  581. dev->ops = ops;
  582. dev->supported_protocols = supported_protocols;
  583. dev->tx_headroom = tx_headroom;
  584. dev->tx_tailroom = tx_tailroom;
  585. nfc_genl_data_init(&dev->genl_data);
  586. /* first generation must not be 0 */
  587. dev->targets_generation = 1;
  588. if (ops->check_presence) {
  589. char name[32];
  590. init_timer(&dev->check_pres_timer);
  591. dev->check_pres_timer.data = (unsigned long)dev;
  592. dev->check_pres_timer.function = nfc_check_pres_timeout;
  593. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  594. snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
  595. dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
  596. WQ_UNBOUND |
  597. WQ_MEM_RECLAIM, 1);
  598. if (dev->check_pres_wq == NULL) {
  599. kfree(dev);
  600. return NULL;
  601. }
  602. }
  603. return dev;
  604. }
  605. EXPORT_SYMBOL(nfc_allocate_device);
  606. /**
  607. * nfc_register_device - register a nfc device in the nfc subsystem
  608. *
  609. * @dev: The nfc device to register
  610. */
  611. int nfc_register_device(struct nfc_dev *dev)
  612. {
  613. int rc;
  614. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  615. mutex_lock(&nfc_devlist_mutex);
  616. nfc_devlist_generation++;
  617. rc = device_add(&dev->dev);
  618. mutex_unlock(&nfc_devlist_mutex);
  619. if (rc < 0)
  620. return rc;
  621. rc = nfc_llcp_register_device(dev);
  622. if (rc)
  623. pr_err("Could not register llcp device\n");
  624. rc = nfc_genl_device_added(dev);
  625. if (rc)
  626. pr_debug("The userspace won't be notified that the device %s was added\n",
  627. dev_name(&dev->dev));
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(nfc_register_device);
  631. /**
  632. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  633. *
  634. * @dev: The nfc device to unregister
  635. */
  636. void nfc_unregister_device(struct nfc_dev *dev)
  637. {
  638. int rc;
  639. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  640. mutex_lock(&nfc_devlist_mutex);
  641. nfc_devlist_generation++;
  642. /* lock to avoid unregistering a device while an operation
  643. is in progress */
  644. device_lock(&dev->dev);
  645. device_del(&dev->dev);
  646. device_unlock(&dev->dev);
  647. mutex_unlock(&nfc_devlist_mutex);
  648. nfc_llcp_unregister_device(dev);
  649. rc = nfc_genl_device_removed(dev);
  650. if (rc)
  651. pr_debug("The userspace won't be notified that the device %s was removed\n",
  652. dev_name(&dev->dev));
  653. }
  654. EXPORT_SYMBOL(nfc_unregister_device);
  655. static int __init nfc_init(void)
  656. {
  657. int rc;
  658. pr_info("NFC Core ver %s\n", VERSION);
  659. rc = class_register(&nfc_class);
  660. if (rc)
  661. return rc;
  662. rc = nfc_genl_init();
  663. if (rc)
  664. goto err_genl;
  665. /* the first generation must not be 0 */
  666. nfc_devlist_generation = 1;
  667. rc = rawsock_init();
  668. if (rc)
  669. goto err_rawsock;
  670. rc = nfc_llcp_init();
  671. if (rc)
  672. goto err_llcp_sock;
  673. rc = af_nfc_init();
  674. if (rc)
  675. goto err_af_nfc;
  676. return 0;
  677. err_af_nfc:
  678. nfc_llcp_exit();
  679. err_llcp_sock:
  680. rawsock_exit();
  681. err_rawsock:
  682. nfc_genl_exit();
  683. err_genl:
  684. class_unregister(&nfc_class);
  685. return rc;
  686. }
  687. static void __exit nfc_exit(void)
  688. {
  689. af_nfc_exit();
  690. nfc_llcp_exit();
  691. rawsock_exit();
  692. nfc_genl_exit();
  693. class_unregister(&nfc_class);
  694. }
  695. subsys_initcall(nfc_init);
  696. module_exit(nfc_exit);
  697. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  698. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  699. MODULE_VERSION(VERSION);
  700. MODULE_LICENSE("GPL");