core.c 19 KB

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