core.c 11 KB

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