core.c 19 KB

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