0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/nfc.h>
0011 #include <linux/module.h>
0012
0013 #include "nfc.h"
0014
0015 static DEFINE_RWLOCK(proto_tab_lock);
0016 static const struct nfc_protocol *proto_tab[NFC_SOCKPROTO_MAX];
0017
0018 static int nfc_sock_create(struct net *net, struct socket *sock, int proto,
0019 int kern)
0020 {
0021 int rc = -EPROTONOSUPPORT;
0022
0023 if (net != &init_net)
0024 return -EAFNOSUPPORT;
0025
0026 if (proto < 0 || proto >= NFC_SOCKPROTO_MAX)
0027 return -EINVAL;
0028
0029 read_lock(&proto_tab_lock);
0030 if (proto_tab[proto] && try_module_get(proto_tab[proto]->owner)) {
0031 rc = proto_tab[proto]->create(net, sock, proto_tab[proto], kern);
0032 module_put(proto_tab[proto]->owner);
0033 }
0034 read_unlock(&proto_tab_lock);
0035
0036 return rc;
0037 }
0038
0039 static const struct net_proto_family nfc_sock_family_ops = {
0040 .owner = THIS_MODULE,
0041 .family = PF_NFC,
0042 .create = nfc_sock_create,
0043 };
0044
0045 int nfc_proto_register(const struct nfc_protocol *nfc_proto)
0046 {
0047 int rc;
0048
0049 if (nfc_proto->id < 0 || nfc_proto->id >= NFC_SOCKPROTO_MAX)
0050 return -EINVAL;
0051
0052 rc = proto_register(nfc_proto->proto, 0);
0053 if (rc)
0054 return rc;
0055
0056 write_lock(&proto_tab_lock);
0057 if (proto_tab[nfc_proto->id])
0058 rc = -EBUSY;
0059 else
0060 proto_tab[nfc_proto->id] = nfc_proto;
0061 write_unlock(&proto_tab_lock);
0062
0063 if (rc)
0064 proto_unregister(nfc_proto->proto);
0065
0066 return rc;
0067 }
0068 EXPORT_SYMBOL(nfc_proto_register);
0069
0070 void nfc_proto_unregister(const struct nfc_protocol *nfc_proto)
0071 {
0072 write_lock(&proto_tab_lock);
0073 proto_tab[nfc_proto->id] = NULL;
0074 write_unlock(&proto_tab_lock);
0075
0076 proto_unregister(nfc_proto->proto);
0077 }
0078 EXPORT_SYMBOL(nfc_proto_unregister);
0079
0080 int __init af_nfc_init(void)
0081 {
0082 return sock_register(&nfc_sock_family_ops);
0083 }
0084
0085 void __exit af_nfc_exit(void)
0086 {
0087 sock_unregister(PF_NFC);
0088 }