af_nfc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  6. * Lauro Ramos Venancio <lauro.venancio@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/nfc.h>
  24. #include <linux/module.h>
  25. #include "nfc.h"
  26. static DEFINE_RWLOCK(proto_tab_lock);
  27. static const struct nfc_protocol *proto_tab[NFC_SOCKPROTO_MAX];
  28. static int nfc_sock_create(struct net *net, struct socket *sock, int proto,
  29. int kern)
  30. {
  31. int rc = -EPROTONOSUPPORT;
  32. if (net != &init_net)
  33. return -EAFNOSUPPORT;
  34. if (proto < 0 || proto >= NFC_SOCKPROTO_MAX)
  35. return -EINVAL;
  36. read_lock(&proto_tab_lock);
  37. if (proto_tab[proto] && try_module_get(proto_tab[proto]->owner)) {
  38. rc = proto_tab[proto]->create(net, sock, proto_tab[proto]);
  39. module_put(proto_tab[proto]->owner);
  40. }
  41. read_unlock(&proto_tab_lock);
  42. return rc;
  43. }
  44. static struct net_proto_family nfc_sock_family_ops = {
  45. .owner = THIS_MODULE,
  46. .family = PF_NFC,
  47. .create = nfc_sock_create,
  48. };
  49. int nfc_proto_register(const struct nfc_protocol *nfc_proto)
  50. {
  51. int rc;
  52. if (nfc_proto->id < 0 || nfc_proto->id >= NFC_SOCKPROTO_MAX)
  53. return -EINVAL;
  54. rc = proto_register(nfc_proto->proto, 0);
  55. if (rc)
  56. return rc;
  57. write_lock(&proto_tab_lock);
  58. if (proto_tab[nfc_proto->id])
  59. rc = -EBUSY;
  60. else
  61. proto_tab[nfc_proto->id] = nfc_proto;
  62. write_unlock(&proto_tab_lock);
  63. return rc;
  64. }
  65. EXPORT_SYMBOL(nfc_proto_register);
  66. void nfc_proto_unregister(const struct nfc_protocol *nfc_proto)
  67. {
  68. write_lock(&proto_tab_lock);
  69. proto_tab[nfc_proto->id] = NULL;
  70. write_unlock(&proto_tab_lock);
  71. proto_unregister(nfc_proto->proto);
  72. }
  73. EXPORT_SYMBOL(nfc_proto_unregister);
  74. int __init af_nfc_init(void)
  75. {
  76. return sock_register(&nfc_sock_family_ops);
  77. }
  78. void af_nfc_exit(void)
  79. {
  80. sock_unregister(PF_NFC);
  81. }