core.c 19 KB

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