core.c 11 KB

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