core.c 20 KB

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