2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
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.
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.
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.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
26 #include <net/genetlink.h>
27 #include <linux/nfc.h>
28 #include <linux/slab.h>
32 static struct genl_multicast_group nfc_genl_event_mcgrp = {
33 .name = NFC_GENL_MCAST_EVENT_NAME,
36 struct genl_family nfc_genl_family = {
37 .id = GENL_ID_GENERATE,
39 .name = NFC_GENL_NAME,
40 .version = NFC_GENL_VERSION,
41 .maxattr = NFC_ATTR_MAX,
44 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
45 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
46 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
47 .len = NFC_DEVICE_NAME_MAXSIZE },
48 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
49 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
54 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
55 struct netlink_callback *cb, int flags)
59 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
60 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
64 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
66 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
67 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols);
68 NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
69 NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
70 if (target->nfcid1_len > 0)
71 NLA_PUT(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
73 if (target->sensb_res_len > 0)
74 NLA_PUT(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
76 if (target->sensf_res_len > 0)
77 NLA_PUT(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
80 return genlmsg_end(msg, hdr);
83 genlmsg_cancel(msg, hdr);
87 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
93 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
94 nfc_genl_family.attrbuf,
95 nfc_genl_family.maxattr,
100 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
101 return ERR_PTR(-EINVAL);
103 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
105 dev = nfc_get_device(idx);
107 return ERR_PTR(-ENODEV);
112 static int nfc_genl_dump_targets(struct sk_buff *skb,
113 struct netlink_callback *cb)
116 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
120 dev = __get_device_from_cb(cb);
124 cb->args[1] = (long) dev;
127 spin_lock_bh(&dev->targets_lock);
129 cb->seq = dev->targets_generation;
131 while (i < dev->n_targets) {
132 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
140 spin_unlock_bh(&dev->targets_lock);
147 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
149 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
157 int nfc_genl_targets_found(struct nfc_dev *dev)
162 dev->genl_data.poll_req_pid = 0;
164 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
168 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
169 NFC_EVENT_TARGETS_FOUND);
173 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
175 genlmsg_end(msg, hdr);
177 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
180 genlmsg_cancel(msg, hdr);
186 int nfc_genl_device_added(struct nfc_dev *dev)
191 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 NFC_EVENT_DEVICE_ADDED);
200 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
201 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
202 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
203 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
205 genlmsg_end(msg, hdr);
207 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
212 genlmsg_cancel(msg, hdr);
218 int nfc_genl_device_removed(struct nfc_dev *dev)
223 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
227 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
228 NFC_EVENT_DEVICE_REMOVED);
232 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
234 genlmsg_end(msg, hdr);
236 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
241 genlmsg_cancel(msg, hdr);
247 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
249 struct netlink_callback *cb,
254 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
260 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
262 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
263 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
264 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
265 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
267 return genlmsg_end(msg, hdr);
270 genlmsg_cancel(msg, hdr);
274 static int nfc_genl_dump_devices(struct sk_buff *skb,
275 struct netlink_callback *cb)
277 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
278 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
279 bool first_call = false;
283 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
286 cb->args[0] = (long) iter;
289 mutex_lock(&nfc_devlist_mutex);
291 cb->seq = nfc_devlist_generation;
294 nfc_device_iter_init(iter);
295 dev = nfc_device_iter_next(iter);
301 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
302 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
306 dev = nfc_device_iter_next(iter);
309 mutex_unlock(&nfc_devlist_mutex);
311 cb->args[1] = (long) dev;
316 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
318 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
320 nfc_device_iter_exit(iter);
326 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
327 u8 comm_mode, u8 rf_mode)
332 pr_debug("DEP link is up\n");
334 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
342 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
343 if (rf_mode == NFC_RF_INITIATOR)
344 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
345 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
346 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
348 genlmsg_end(msg, hdr);
350 dev->dep_link_up = true;
352 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
357 genlmsg_cancel(msg, hdr);
363 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
368 pr_debug("DEP link is down\n");
370 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
374 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
375 NFC_CMD_DEP_LINK_DOWN);
379 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
381 genlmsg_end(msg, hdr);
383 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
388 genlmsg_cancel(msg, hdr);
394 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
401 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
404 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
406 dev = nfc_get_device(idx);
410 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
416 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
423 return genlmsg_reply(msg, info);
432 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
438 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
441 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
443 dev = nfc_get_device(idx);
447 rc = nfc_dev_up(dev);
453 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
462 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
464 dev = nfc_get_device(idx);
468 rc = nfc_dev_down(dev);
474 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
481 pr_debug("Poll start\n");
483 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
484 !info->attrs[NFC_ATTR_PROTOCOLS])
487 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
488 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
490 dev = nfc_get_device(idx);
494 mutex_lock(&dev->genl_data.genl_data_mutex);
496 rc = nfc_start_poll(dev, protocols);
498 dev->genl_data.poll_req_pid = info->snd_pid;
500 mutex_unlock(&dev->genl_data.genl_data_mutex);
506 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
512 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
515 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
517 dev = nfc_get_device(idx);
521 mutex_lock(&dev->genl_data.genl_data_mutex);
523 if (dev->genl_data.poll_req_pid != info->snd_pid) {
528 rc = nfc_stop_poll(dev);
529 dev->genl_data.poll_req_pid = 0;
532 mutex_unlock(&dev->genl_data.genl_data_mutex);
537 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
544 pr_debug("DEP link up\n");
546 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
547 !info->attrs[NFC_ATTR_COMM_MODE])
550 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
551 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
552 tgt_idx = NFC_TARGET_IDX_ANY;
554 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
556 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
558 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
561 dev = nfc_get_device(idx);
565 rc = nfc_dep_link_up(dev, tgt_idx, comm);
572 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
578 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
581 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
583 dev = nfc_get_device(idx);
587 rc = nfc_dep_link_down(dev);
593 static struct genl_ops nfc_genl_ops[] = {
595 .cmd = NFC_CMD_GET_DEVICE,
596 .doit = nfc_genl_get_device,
597 .dumpit = nfc_genl_dump_devices,
598 .done = nfc_genl_dump_devices_done,
599 .policy = nfc_genl_policy,
602 .cmd = NFC_CMD_DEV_UP,
603 .doit = nfc_genl_dev_up,
604 .policy = nfc_genl_policy,
607 .cmd = NFC_CMD_DEV_DOWN,
608 .doit = nfc_genl_dev_down,
609 .policy = nfc_genl_policy,
612 .cmd = NFC_CMD_START_POLL,
613 .doit = nfc_genl_start_poll,
614 .policy = nfc_genl_policy,
617 .cmd = NFC_CMD_STOP_POLL,
618 .doit = nfc_genl_stop_poll,
619 .policy = nfc_genl_policy,
622 .cmd = NFC_CMD_DEP_LINK_UP,
623 .doit = nfc_genl_dep_link_up,
624 .policy = nfc_genl_policy,
627 .cmd = NFC_CMD_DEP_LINK_DOWN,
628 .doit = nfc_genl_dep_link_down,
629 .policy = nfc_genl_policy,
632 .cmd = NFC_CMD_GET_TARGET,
633 .dumpit = nfc_genl_dump_targets,
634 .done = nfc_genl_dump_targets_done,
635 .policy = nfc_genl_policy,
639 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
640 unsigned long event, void *ptr)
642 struct netlink_notify *n = ptr;
643 struct class_dev_iter iter;
646 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
649 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
651 nfc_device_iter_init(&iter);
652 dev = nfc_device_iter_next(&iter);
655 if (dev->genl_data.poll_req_pid == n->pid) {
657 dev->genl_data.poll_req_pid = 0;
659 dev = nfc_device_iter_next(&iter);
662 nfc_device_iter_exit(&iter);
668 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
670 genl_data->poll_req_pid = 0;
671 mutex_init(&genl_data->genl_data_mutex);
674 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
676 mutex_destroy(&genl_data->genl_data_mutex);
679 static struct notifier_block nl_notifier = {
680 .notifier_call = nfc_genl_rcv_nl_event,
684 * nfc_genl_init() - Initialize netlink interface
686 * This initialization function registers the nfc netlink family.
688 int __init nfc_genl_init(void)
692 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
693 ARRAY_SIZE(nfc_genl_ops));
697 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
699 netlink_register_notifier(&nl_notifier);
705 * nfc_genl_exit() - Deinitialize netlink interface
707 * This exit function unregisters the nfc netlink family.
709 void nfc_genl_exit(void)
711 netlink_unregister_notifier(&nl_notifier);
712 genl_unregister_family(&nfc_genl_family);