core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. int nfc_devlist_generation;
  32. DEFINE_MUTEX(nfc_devlist_mutex);
  33. /**
  34. * nfc_dev_up - turn on the NFC device
  35. *
  36. * @dev: The nfc device to be turned on
  37. *
  38. * The device remains up until the nfc_dev_down function is called.
  39. */
  40. int nfc_dev_up(struct nfc_dev *dev)
  41. {
  42. int rc = 0;
  43. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  44. device_lock(&dev->dev);
  45. if (!device_is_registered(&dev->dev)) {
  46. rc = -ENODEV;
  47. goto error;
  48. }
  49. if (dev->dev_up) {
  50. rc = -EALREADY;
  51. goto error;
  52. }
  53. if (dev->ops->dev_up)
  54. rc = dev->ops->dev_up(dev);
  55. if (!rc)
  56. dev->dev_up = true;
  57. error:
  58. device_unlock(&dev->dev);
  59. return rc;
  60. }
  61. /**
  62. * nfc_dev_down - turn off the NFC device
  63. *
  64. * @dev: The nfc device to be turned off
  65. */
  66. int nfc_dev_down(struct nfc_dev *dev)
  67. {
  68. int rc = 0;
  69. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  70. device_lock(&dev->dev);
  71. if (!device_is_registered(&dev->dev)) {
  72. rc = -ENODEV;
  73. goto error;
  74. }
  75. if (!dev->dev_up) {
  76. rc = -EALREADY;
  77. goto error;
  78. }
  79. if (dev->polling || dev->remote_activated) {
  80. rc = -EBUSY;
  81. goto error;
  82. }
  83. if (dev->ops->dev_down)
  84. dev->ops->dev_down(dev);
  85. dev->dev_up = false;
  86. error:
  87. device_unlock(&dev->dev);
  88. return rc;
  89. }
  90. /**
  91. * nfc_start_poll - start polling for nfc targets
  92. *
  93. * @dev: The nfc device that must start polling
  94. * @protocols: bitset of nfc protocols that must be used for polling
  95. *
  96. * The device remains polling for targets until a target is found or
  97. * the nfc_stop_poll function is called.
  98. */
  99. int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
  100. {
  101. int rc;
  102. pr_debug("dev_name=%s protocols=0x%x\n",
  103. dev_name(&dev->dev), protocols);
  104. if (!protocols)
  105. return -EINVAL;
  106. device_lock(&dev->dev);
  107. if (!device_is_registered(&dev->dev)) {
  108. rc = -ENODEV;
  109. goto error;
  110. }
  111. if (dev->polling) {
  112. rc = -EBUSY;
  113. goto error;
  114. }
  115. rc = dev->ops->start_poll(dev, protocols);
  116. if (!rc)
  117. dev->polling = true;
  118. error:
  119. device_unlock(&dev->dev);
  120. return rc;
  121. }
  122. /**
  123. * nfc_stop_poll - stop polling for nfc targets
  124. *
  125. * @dev: The nfc device that must stop polling
  126. */
  127. int nfc_stop_poll(struct nfc_dev *dev)
  128. {
  129. int rc = 0;
  130. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  131. device_lock(&dev->dev);
  132. if (!device_is_registered(&dev->dev)) {
  133. rc = -ENODEV;
  134. goto error;
  135. }
  136. if (!dev->polling) {
  137. rc = -EINVAL;
  138. goto error;
  139. }
  140. dev->ops->stop_poll(dev);
  141. dev->polling = false;
  142. error:
  143. device_unlock(&dev->dev);
  144. return rc;
  145. }
  146. int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
  147. {
  148. int rc = 0;
  149. u8 *gb;
  150. #ifdef CONFIG_NFC_LLCP
  151. size_t gb_len;
  152. #else
  153. u8 gb_len;
  154. #endif
  155. pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
  156. if (!dev->ops->dep_link_up)
  157. return -EOPNOTSUPP;
  158. device_lock(&dev->dev);
  159. if (!device_is_registered(&dev->dev)) {
  160. rc = -ENODEV;
  161. goto error;
  162. }
  163. if (dev->dep_link_up == true) {
  164. rc = -EALREADY;
  165. goto error;
  166. }
  167. gb = nfc_llcp_general_bytes(dev, &gb_len);
  168. if (gb_len > NFC_MAX_GT_LEN) {
  169. rc = -EINVAL;
  170. goto error;
  171. }
  172. rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len);
  173. error:
  174. device_unlock(&dev->dev);
  175. return rc;
  176. }
  177. int nfc_dep_link_down(struct nfc_dev *dev)
  178. {
  179. int rc = 0;
  180. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  181. if (!dev->ops->dep_link_down)
  182. return -EOPNOTSUPP;
  183. device_lock(&dev->dev);
  184. if (!device_is_registered(&dev->dev)) {
  185. rc = -ENODEV;
  186. goto error;
  187. }
  188. if (dev->dep_link_up == false) {
  189. rc = -EALREADY;
  190. goto error;
  191. }
  192. if (dev->dep_rf_mode == NFC_RF_TARGET) {
  193. rc = -EOPNOTSUPP;
  194. goto error;
  195. }
  196. rc = dev->ops->dep_link_down(dev);
  197. if (!rc) {
  198. dev->dep_link_up = false;
  199. nfc_llcp_mac_is_down(dev);
  200. nfc_genl_dep_link_down_event(dev);
  201. }
  202. error:
  203. device_unlock(&dev->dev);
  204. return rc;
  205. }
  206. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  207. u8 comm_mode, u8 rf_mode)
  208. {
  209. dev->dep_link_up = true;
  210. dev->dep_rf_mode = rf_mode;
  211. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  212. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  213. }
  214. EXPORT_SYMBOL(nfc_dep_link_is_up);
  215. /**
  216. * nfc_activate_target - prepare the target for data exchange
  217. *
  218. * @dev: The nfc device that found the target
  219. * @target_idx: index of the target that must be activated
  220. * @protocol: nfc protocol that will be used for data exchange
  221. */
  222. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  223. {
  224. int rc;
  225. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  226. dev_name(&dev->dev), target_idx, protocol);
  227. device_lock(&dev->dev);
  228. if (!device_is_registered(&dev->dev)) {
  229. rc = -ENODEV;
  230. goto error;
  231. }
  232. rc = dev->ops->activate_target(dev, target_idx, protocol);
  233. if (!rc)
  234. dev->remote_activated = true;
  235. error:
  236. device_unlock(&dev->dev);
  237. return rc;
  238. }
  239. /**
  240. * nfc_deactivate_target - deactivate a nfc target
  241. *
  242. * @dev: The nfc device that found the target
  243. * @target_idx: index of the target that must be deactivated
  244. */
  245. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
  246. {
  247. int rc = 0;
  248. pr_debug("dev_name=%s target_idx=%u\n",
  249. dev_name(&dev->dev), target_idx);
  250. device_lock(&dev->dev);
  251. if (!device_is_registered(&dev->dev)) {
  252. rc = -ENODEV;
  253. goto error;
  254. }
  255. dev->ops->deactivate_target(dev, target_idx);
  256. dev->remote_activated = false;
  257. error:
  258. device_unlock(&dev->dev);
  259. return rc;
  260. }
  261. /**
  262. * nfc_data_exchange - transceive data
  263. *
  264. * @dev: The nfc device that found the target
  265. * @target_idx: index of the target
  266. * @skb: data to be sent
  267. * @cb: callback called when the response is received
  268. * @cb_context: parameter for the callback function
  269. *
  270. * The user must wait for the callback before calling this function again.
  271. */
  272. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  273. data_exchange_cb_t cb, void *cb_context)
  274. {
  275. int rc;
  276. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  277. dev_name(&dev->dev), target_idx, skb->len);
  278. device_lock(&dev->dev);
  279. if (!device_is_registered(&dev->dev)) {
  280. rc = -ENODEV;
  281. kfree_skb(skb);
  282. goto error;
  283. }
  284. rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
  285. error:
  286. device_unlock(&dev->dev);
  287. return rc;
  288. }
  289. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  290. {
  291. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  292. if (gb_len > NFC_MAX_GT_LEN)
  293. return -EINVAL;
  294. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  295. }
  296. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  297. /**
  298. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  299. *
  300. * @size: size to allocate
  301. * @gfp: gfp flags
  302. */
  303. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  304. unsigned int flags, unsigned int size,
  305. unsigned int *err)
  306. {
  307. struct sk_buff *skb;
  308. unsigned int total_size;
  309. total_size = size +
  310. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  311. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  312. if (skb)
  313. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  314. return skb;
  315. }
  316. /**
  317. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  318. *
  319. * @size: size to allocate
  320. * @gfp: gfp flags
  321. */
  322. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  323. {
  324. struct sk_buff *skb;
  325. unsigned int total_size;
  326. total_size = size + 1;
  327. skb = alloc_skb(total_size, gfp);
  328. if (skb)
  329. skb_reserve(skb, 1);
  330. return skb;
  331. }
  332. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  333. /**
  334. * nfc_targets_found - inform that targets were found
  335. *
  336. * @dev: The nfc device that found the targets
  337. * @targets: array of nfc targets found
  338. * @ntargets: targets array size
  339. *
  340. * The device driver must call this function when one or many nfc targets
  341. * are found. After calling this function, the device driver must stop
  342. * polling for targets.
  343. */
  344. int nfc_targets_found(struct nfc_dev *dev,
  345. struct nfc_target *targets, int n_targets)
  346. {
  347. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  348. dev->polling = false;
  349. spin_lock_bh(&dev->targets_lock);
  350. dev->targets_generation++;
  351. kfree(dev->targets);
  352. dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
  353. GFP_ATOMIC);
  354. if (!dev->targets) {
  355. dev->n_targets = 0;
  356. spin_unlock_bh(&dev->targets_lock);
  357. return -ENOMEM;
  358. }
  359. dev->n_targets = n_targets;
  360. spin_unlock_bh(&dev->targets_lock);
  361. nfc_genl_targets_found(dev);
  362. return 0;
  363. }
  364. EXPORT_SYMBOL(nfc_targets_found);
  365. static void nfc_release(struct device *d)
  366. {
  367. struct nfc_dev *dev = to_nfc_dev(d);
  368. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  369. nfc_genl_data_exit(&dev->genl_data);
  370. kfree(dev->targets);
  371. kfree(dev);
  372. }
  373. struct class nfc_class = {
  374. .name = "nfc",
  375. .dev_release = nfc_release,
  376. };
  377. EXPORT_SYMBOL(nfc_class);
  378. static int match_idx(struct device *d, void *data)
  379. {
  380. struct nfc_dev *dev = to_nfc_dev(d);
  381. unsigned int *idx = data;
  382. return dev->idx == *idx;
  383. }
  384. struct nfc_dev *nfc_get_device(unsigned int idx)
  385. {
  386. struct device *d;
  387. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  388. if (!d)
  389. return NULL;
  390. return to_nfc_dev(d);
  391. }
  392. /**
  393. * nfc_allocate_device - allocate a new nfc device
  394. *
  395. * @ops: device operations
  396. * @supported_protocols: NFC protocols supported by the device
  397. */
  398. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  399. u32 supported_protocols,
  400. int tx_headroom, int tx_tailroom)
  401. {
  402. static atomic_t dev_no = ATOMIC_INIT(0);
  403. struct nfc_dev *dev;
  404. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  405. !ops->deactivate_target || !ops->data_exchange)
  406. return NULL;
  407. if (!supported_protocols)
  408. return NULL;
  409. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  410. if (!dev)
  411. return NULL;
  412. dev->dev.class = &nfc_class;
  413. dev->idx = atomic_inc_return(&dev_no) - 1;
  414. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  415. device_initialize(&dev->dev);
  416. dev->ops = ops;
  417. dev->supported_protocols = supported_protocols;
  418. dev->tx_headroom = tx_headroom;
  419. dev->tx_tailroom = tx_tailroom;
  420. spin_lock_init(&dev->targets_lock);
  421. nfc_genl_data_init(&dev->genl_data);
  422. /* first generation must not be 0 */
  423. dev->targets_generation = 1;
  424. return dev;
  425. }
  426. EXPORT_SYMBOL(nfc_allocate_device);
  427. /**
  428. * nfc_register_device - register a nfc device in the nfc subsystem
  429. *
  430. * @dev: The nfc device to register
  431. */
  432. int nfc_register_device(struct nfc_dev *dev)
  433. {
  434. int rc;
  435. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  436. mutex_lock(&nfc_devlist_mutex);
  437. nfc_devlist_generation++;
  438. rc = device_add(&dev->dev);
  439. mutex_unlock(&nfc_devlist_mutex);
  440. if (rc < 0)
  441. return rc;
  442. rc = nfc_llcp_register_device(dev);
  443. if (rc)
  444. pr_err("Could not register llcp device\n");
  445. rc = nfc_genl_device_added(dev);
  446. if (rc)
  447. pr_debug("The userspace won't be notified that the device %s was added\n",
  448. dev_name(&dev->dev));
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(nfc_register_device);
  452. /**
  453. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  454. *
  455. * @dev: The nfc device to unregister
  456. */
  457. void nfc_unregister_device(struct nfc_dev *dev)
  458. {
  459. int rc;
  460. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  461. mutex_lock(&nfc_devlist_mutex);
  462. nfc_devlist_generation++;
  463. /* lock to avoid unregistering a device while an operation
  464. is in progress */
  465. device_lock(&dev->dev);
  466. device_del(&dev->dev);
  467. device_unlock(&dev->dev);
  468. mutex_unlock(&nfc_devlist_mutex);
  469. nfc_llcp_unregister_device(dev);
  470. rc = nfc_genl_device_removed(dev);
  471. if (rc)
  472. pr_debug("The userspace won't be notified that the device %s was removed\n",
  473. dev_name(&dev->dev));
  474. }
  475. EXPORT_SYMBOL(nfc_unregister_device);
  476. static int __init nfc_init(void)
  477. {
  478. int rc;
  479. pr_info("NFC Core ver %s\n", VERSION);
  480. rc = class_register(&nfc_class);
  481. if (rc)
  482. return rc;
  483. rc = nfc_genl_init();
  484. if (rc)
  485. goto err_genl;
  486. /* the first generation must not be 0 */
  487. nfc_devlist_generation = 1;
  488. rc = rawsock_init();
  489. if (rc)
  490. goto err_rawsock;
  491. rc = nfc_llcp_init();
  492. if (rc)
  493. goto err_llcp_sock;
  494. rc = af_nfc_init();
  495. if (rc)
  496. goto err_af_nfc;
  497. return 0;
  498. err_af_nfc:
  499. nfc_llcp_exit();
  500. err_llcp_sock:
  501. rawsock_exit();
  502. err_rawsock:
  503. nfc_genl_exit();
  504. err_genl:
  505. class_unregister(&nfc_class);
  506. return rc;
  507. }
  508. static void __exit nfc_exit(void)
  509. {
  510. af_nfc_exit();
  511. nfc_llcp_exit();
  512. rawsock_exit();
  513. nfc_genl_exit();
  514. class_unregister(&nfc_class);
  515. }
  516. subsys_initcall(nfc_init);
  517. module_exit(nfc_exit);
  518. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  519. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  520. MODULE_VERSION(VERSION);
  521. MODULE_LICENSE("GPL");