core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include "nfc.h"
  29. #define VERSION "0.1"
  30. int nfc_devlist_generation;
  31. DEFINE_MUTEX(nfc_devlist_mutex);
  32. /**
  33. * nfc_dev_up - turn on the NFC device
  34. *
  35. * @dev: The nfc device to be turned on
  36. *
  37. * The device remains up until the nfc_dev_down function is called.
  38. */
  39. int nfc_dev_up(struct nfc_dev *dev)
  40. {
  41. int rc = 0;
  42. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  43. device_lock(&dev->dev);
  44. if (!device_is_registered(&dev->dev)) {
  45. rc = -ENODEV;
  46. goto error;
  47. }
  48. if (dev->dev_up) {
  49. rc = -EALREADY;
  50. goto error;
  51. }
  52. if (dev->ops->dev_up)
  53. rc = dev->ops->dev_up(dev);
  54. if (!rc)
  55. dev->dev_up = true;
  56. error:
  57. device_unlock(&dev->dev);
  58. return rc;
  59. }
  60. /**
  61. * nfc_dev_down - turn off the NFC device
  62. *
  63. * @dev: The nfc device to be turned off
  64. */
  65. int nfc_dev_down(struct nfc_dev *dev)
  66. {
  67. int rc = 0;
  68. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  69. device_lock(&dev->dev);
  70. if (!device_is_registered(&dev->dev)) {
  71. rc = -ENODEV;
  72. goto error;
  73. }
  74. if (!dev->dev_up) {
  75. rc = -EALREADY;
  76. goto error;
  77. }
  78. if (dev->polling || dev->remote_activated) {
  79. rc = -EBUSY;
  80. goto error;
  81. }
  82. if (dev->ops->dev_down)
  83. dev->ops->dev_down(dev);
  84. dev->dev_up = false;
  85. error:
  86. device_unlock(&dev->dev);
  87. return rc;
  88. }
  89. /**
  90. * nfc_start_poll - start polling for nfc targets
  91. *
  92. * @dev: The nfc device that must start polling
  93. * @protocols: bitset of nfc protocols that must be used for polling
  94. *
  95. * The device remains polling for targets until a target is found or
  96. * the nfc_stop_poll function is called.
  97. */
  98. int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
  99. {
  100. int rc;
  101. pr_debug("dev_name=%s protocols=0x%x\n",
  102. dev_name(&dev->dev), protocols);
  103. if (!protocols)
  104. return -EINVAL;
  105. device_lock(&dev->dev);
  106. if (!device_is_registered(&dev->dev)) {
  107. rc = -ENODEV;
  108. goto error;
  109. }
  110. if (dev->polling) {
  111. rc = -EBUSY;
  112. goto error;
  113. }
  114. rc = dev->ops->start_poll(dev, protocols);
  115. if (!rc)
  116. dev->polling = true;
  117. error:
  118. device_unlock(&dev->dev);
  119. return rc;
  120. }
  121. /**
  122. * nfc_stop_poll - stop polling for nfc targets
  123. *
  124. * @dev: The nfc device that must stop polling
  125. */
  126. int nfc_stop_poll(struct nfc_dev *dev)
  127. {
  128. int rc = 0;
  129. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  130. device_lock(&dev->dev);
  131. if (!device_is_registered(&dev->dev)) {
  132. rc = -ENODEV;
  133. goto error;
  134. }
  135. if (!dev->polling) {
  136. rc = -EINVAL;
  137. goto error;
  138. }
  139. dev->ops->stop_poll(dev);
  140. dev->polling = false;
  141. error:
  142. device_unlock(&dev->dev);
  143. return rc;
  144. }
  145. /**
  146. * nfc_activate_target - prepare the target for data exchange
  147. *
  148. * @dev: The nfc device that found the target
  149. * @target_idx: index of the target that must be activated
  150. * @protocol: nfc protocol that will be used for data exchange
  151. */
  152. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  153. {
  154. int rc;
  155. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  156. dev_name(&dev->dev), target_idx, protocol);
  157. device_lock(&dev->dev);
  158. if (!device_is_registered(&dev->dev)) {
  159. rc = -ENODEV;
  160. goto error;
  161. }
  162. rc = dev->ops->activate_target(dev, target_idx, protocol);
  163. if (!rc)
  164. dev->remote_activated = true;
  165. error:
  166. device_unlock(&dev->dev);
  167. return rc;
  168. }
  169. /**
  170. * nfc_deactivate_target - deactivate a nfc target
  171. *
  172. * @dev: The nfc device that found the target
  173. * @target_idx: index of the target that must be deactivated
  174. */
  175. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
  176. {
  177. int rc = 0;
  178. pr_debug("dev_name=%s target_idx=%u\n",
  179. dev_name(&dev->dev), target_idx);
  180. device_lock(&dev->dev);
  181. if (!device_is_registered(&dev->dev)) {
  182. rc = -ENODEV;
  183. goto error;
  184. }
  185. dev->ops->deactivate_target(dev, target_idx);
  186. dev->remote_activated = false;
  187. error:
  188. device_unlock(&dev->dev);
  189. return rc;
  190. }
  191. /**
  192. * nfc_data_exchange - transceive data
  193. *
  194. * @dev: The nfc device that found the target
  195. * @target_idx: index of the target
  196. * @skb: data to be sent
  197. * @cb: callback called when the response is received
  198. * @cb_context: parameter for the callback function
  199. *
  200. * The user must wait for the callback before calling this function again.
  201. */
  202. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
  203. struct sk_buff *skb,
  204. data_exchange_cb_t cb,
  205. void *cb_context)
  206. {
  207. int rc;
  208. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  209. dev_name(&dev->dev), target_idx, skb->len);
  210. device_lock(&dev->dev);
  211. if (!device_is_registered(&dev->dev)) {
  212. rc = -ENODEV;
  213. kfree_skb(skb);
  214. goto error;
  215. }
  216. rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
  217. error:
  218. device_unlock(&dev->dev);
  219. return rc;
  220. }
  221. /**
  222. * nfc_alloc_skb - allocate a skb for data exchange responses
  223. *
  224. * @size: size to allocate
  225. * @gfp: gfp flags
  226. */
  227. struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
  228. {
  229. struct sk_buff *skb;
  230. unsigned int total_size;
  231. total_size = size + 1;
  232. skb = alloc_skb(total_size, gfp);
  233. if (skb)
  234. skb_reserve(skb, 1);
  235. return skb;
  236. }
  237. EXPORT_SYMBOL(nfc_alloc_skb);
  238. /**
  239. * nfc_targets_found - inform that targets were found
  240. *
  241. * @dev: The nfc device that found the targets
  242. * @targets: array of nfc targets found
  243. * @ntargets: targets array size
  244. *
  245. * The device driver must call this function when one or many nfc targets
  246. * are found. After calling this function, the device driver must stop
  247. * polling for targets.
  248. */
  249. int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
  250. int n_targets)
  251. {
  252. int i;
  253. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  254. dev->polling = false;
  255. for (i = 0; i < n_targets; i++)
  256. targets[i].idx = dev->target_idx++;
  257. spin_lock_bh(&dev->targets_lock);
  258. dev->targets_generation++;
  259. kfree(dev->targets);
  260. dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
  261. GFP_ATOMIC);
  262. if (!dev->targets) {
  263. dev->n_targets = 0;
  264. spin_unlock_bh(&dev->targets_lock);
  265. return -ENOMEM;
  266. }
  267. dev->n_targets = n_targets;
  268. spin_unlock_bh(&dev->targets_lock);
  269. nfc_genl_targets_found(dev);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(nfc_targets_found);
  273. static void nfc_release(struct device *d)
  274. {
  275. struct nfc_dev *dev = to_nfc_dev(d);
  276. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  277. nfc_genl_data_exit(&dev->genl_data);
  278. kfree(dev->targets);
  279. kfree(dev);
  280. }
  281. struct class nfc_class = {
  282. .name = "nfc",
  283. .dev_release = nfc_release,
  284. };
  285. EXPORT_SYMBOL(nfc_class);
  286. static int match_idx(struct device *d, void *data)
  287. {
  288. struct nfc_dev *dev = to_nfc_dev(d);
  289. unsigned *idx = data;
  290. return dev->idx == *idx;
  291. }
  292. struct nfc_dev *nfc_get_device(unsigned idx)
  293. {
  294. struct device *d;
  295. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  296. if (!d)
  297. return NULL;
  298. return to_nfc_dev(d);
  299. }
  300. /**
  301. * nfc_allocate_device - allocate a new nfc device
  302. *
  303. * @ops: device operations
  304. * @supported_protocols: NFC protocols supported by the device
  305. */
  306. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  307. u32 supported_protocols,
  308. int tx_headroom,
  309. int tx_tailroom)
  310. {
  311. static atomic_t dev_no = ATOMIC_INIT(0);
  312. struct nfc_dev *dev;
  313. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  314. !ops->deactivate_target || !ops->data_exchange)
  315. return NULL;
  316. if (!supported_protocols)
  317. return NULL;
  318. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  319. if (!dev)
  320. return NULL;
  321. dev->dev.class = &nfc_class;
  322. dev->idx = atomic_inc_return(&dev_no) - 1;
  323. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  324. device_initialize(&dev->dev);
  325. dev->ops = ops;
  326. dev->supported_protocols = supported_protocols;
  327. dev->tx_headroom = tx_headroom;
  328. dev->tx_tailroom = tx_tailroom;
  329. spin_lock_init(&dev->targets_lock);
  330. nfc_genl_data_init(&dev->genl_data);
  331. /* first generation must not be 0 */
  332. dev->targets_generation = 1;
  333. return dev;
  334. }
  335. EXPORT_SYMBOL(nfc_allocate_device);
  336. /**
  337. * nfc_register_device - register a nfc device in the nfc subsystem
  338. *
  339. * @dev: The nfc device to register
  340. */
  341. int nfc_register_device(struct nfc_dev *dev)
  342. {
  343. int rc;
  344. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  345. mutex_lock(&nfc_devlist_mutex);
  346. nfc_devlist_generation++;
  347. rc = device_add(&dev->dev);
  348. mutex_unlock(&nfc_devlist_mutex);
  349. if (rc < 0)
  350. return rc;
  351. rc = nfc_genl_device_added(dev);
  352. if (rc)
  353. pr_debug("The userspace won't be notified that the device %s was added\n",
  354. dev_name(&dev->dev));
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(nfc_register_device);
  358. /**
  359. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  360. *
  361. * @dev: The nfc device to unregister
  362. */
  363. void nfc_unregister_device(struct nfc_dev *dev)
  364. {
  365. int rc;
  366. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  367. mutex_lock(&nfc_devlist_mutex);
  368. nfc_devlist_generation++;
  369. /* lock to avoid unregistering a device while an operation
  370. is in progress */
  371. device_lock(&dev->dev);
  372. device_del(&dev->dev);
  373. device_unlock(&dev->dev);
  374. mutex_unlock(&nfc_devlist_mutex);
  375. rc = nfc_genl_device_removed(dev);
  376. if (rc)
  377. pr_debug("The userspace won't be notified that the device %s was removed\n",
  378. dev_name(&dev->dev));
  379. }
  380. EXPORT_SYMBOL(nfc_unregister_device);
  381. static int __init nfc_init(void)
  382. {
  383. int rc;
  384. pr_info("NFC Core ver %s\n", VERSION);
  385. rc = class_register(&nfc_class);
  386. if (rc)
  387. return rc;
  388. rc = nfc_genl_init();
  389. if (rc)
  390. goto err_genl;
  391. /* the first generation must not be 0 */
  392. nfc_devlist_generation = 1;
  393. rc = rawsock_init();
  394. if (rc)
  395. goto err_rawsock;
  396. rc = af_nfc_init();
  397. if (rc)
  398. goto err_af_nfc;
  399. return 0;
  400. err_af_nfc:
  401. rawsock_exit();
  402. err_rawsock:
  403. nfc_genl_exit();
  404. err_genl:
  405. class_unregister(&nfc_class);
  406. return rc;
  407. }
  408. static void __exit nfc_exit(void)
  409. {
  410. af_nfc_exit();
  411. rawsock_exit();
  412. nfc_genl_exit();
  413. class_unregister(&nfc_class);
  414. }
  415. subsys_initcall(nfc_init);
  416. module_exit(nfc_exit);
  417. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  418. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  419. MODULE_VERSION(VERSION);
  420. MODULE_LICENSE("GPL");