af_nfc.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "nfc.h"
  25. static DEFINE_RWLOCK(proto_tab_lock);
  26. static const struct nfc_protocol *proto_tab[NFC_SOCKPROTO_MAX];
  27. static int nfc_sock_create(struct net *net, struct socket *sock, int proto,
  28. int kern)
  29. {
  30. int rc = -EPROTONOSUPPORT;
  31. if (net != &init_net)
  32. return -EAFNOSUPPORT;
  33. if (proto < 0 || proto >= NFC_SOCKPROTO_MAX)
  34. return -EINVAL;
  35. read_lock(&proto_tab_lock);
  36. if (proto_tab[proto] && try_module_get(proto_tab[proto]->owner)) {
  37. rc = proto_tab[proto]->create(net, sock, proto_tab[proto]);
  38. module_put(proto_tab[proto]->owner);
  39. }
  40. read_unlock(&proto_tab_lock);
  41. return rc;
  42. }
  43. static struct net_proto_family nfc_sock_family_ops = {
  44. .owner = THIS_MODULE,
  45. .family = PF_NFC,
  46. .create = nfc_sock_create,
  47. };
  48. int nfc_proto_register(const struct nfc_protocol *nfc_proto)
  49. {
  50. int rc;
  51. if (nfc_proto->id < 0 || nfc_proto->id >= NFC_SOCKPROTO_MAX)
  52. return -EINVAL;
  53. rc = proto_register(nfc_proto->proto, 0);
  54. if (rc)
  55. return rc;
  56. write_lock(&proto_tab_lock);
  57. if (proto_tab[nfc_proto->id])
  58. rc = -EBUSY;
  59. else
  60. proto_tab[nfc_proto->id] = nfc_proto;
  61. write_unlock(&proto_tab_lock);
  62. return rc;
  63. }
  64. EXPORT_SYMBOL(nfc_proto_register);
  65. void nfc_proto_unregister(const struct nfc_protocol *nfc_proto)
  66. {
  67. write_lock(&proto_tab_lock);
  68. proto_tab[nfc_proto->id] = NULL;
  69. write_unlock(&proto_tab_lock);
  70. proto_unregister(nfc_proto->proto);
  71. }
  72. EXPORT_SYMBOL(nfc_proto_unregister);
  73. int __init af_nfc_init(void)
  74. {
  75. return sock_register(&nfc_sock_family_ops);
  76. }
  77. void af_nfc_exit(void)
  78. {
  79. sock_unregister(PF_NFC);
  80. }