core.c 9.5 KB

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