core.c 13 KB

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