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 },
53 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
54 struct netlink_callback *cb, int flags)
58 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
59 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
63 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
65 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
66 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS,
67 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,
74 return genlmsg_end(msg, hdr);
77 genlmsg_cancel(msg, hdr);
81 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
87 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
88 nfc_genl_family.attrbuf,
89 nfc_genl_family.maxattr,
94 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
95 return ERR_PTR(-EINVAL);
97 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
99 dev = nfc_get_device(idx);
101 return ERR_PTR(-ENODEV);
106 static int nfc_genl_dump_targets(struct sk_buff *skb,
107 struct netlink_callback *cb)
110 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
114 dev = __get_device_from_cb(cb);
118 cb->args[1] = (long) dev;
121 spin_lock_bh(&dev->targets_lock);
123 cb->seq = dev->targets_generation;
125 while (i < dev->n_targets) {
126 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
134 spin_unlock_bh(&dev->targets_lock);
141 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
143 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
151 int nfc_genl_targets_found(struct nfc_dev *dev)
156 dev->genl_data.poll_req_pid = 0;
158 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
162 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
163 NFC_EVENT_TARGETS_FOUND);
167 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
169 genlmsg_end(msg, hdr);
171 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
174 genlmsg_cancel(msg, hdr);
180 int nfc_genl_device_added(struct nfc_dev *dev)
185 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
189 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
190 NFC_EVENT_DEVICE_ADDED);
194 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
195 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
196 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
198 genlmsg_end(msg, hdr);
200 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
205 genlmsg_cancel(msg, hdr);
211 int nfc_genl_device_removed(struct nfc_dev *dev)
216 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
220 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
221 NFC_EVENT_DEVICE_REMOVED);
225 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
227 genlmsg_end(msg, hdr);
229 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
234 genlmsg_cancel(msg, hdr);
240 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
242 struct netlink_callback *cb,
247 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
253 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
255 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
256 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
257 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
259 return genlmsg_end(msg, hdr);
262 genlmsg_cancel(msg, hdr);
266 static int nfc_genl_dump_devices(struct sk_buff *skb,
267 struct netlink_callback *cb)
269 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
270 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
271 bool first_call = false;
275 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
278 cb->args[0] = (long) iter;
281 mutex_lock(&nfc_devlist_mutex);
283 cb->seq = nfc_devlist_generation;
286 nfc_device_iter_init(iter);
287 dev = nfc_device_iter_next(iter);
293 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
299 dev = nfc_device_iter_next(iter);
302 mutex_unlock(&nfc_devlist_mutex);
304 cb->args[1] = (long) dev;
309 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
311 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
313 nfc_device_iter_exit(iter);
319 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
320 u8 comm_mode, u8 rf_mode)
325 pr_debug("DEP link is up\n");
327 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
331 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
332 NFC_CMD_DEP_LINK_UP);
336 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
337 if (rf_mode == NFC_RF_INITIATOR)
338 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
339 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
340 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
342 genlmsg_end(msg, hdr);
344 dev->dep_link_up = true;
346 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
351 genlmsg_cancel(msg, hdr);
357 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
362 pr_debug("DEP link is down\n");
364 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
368 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
369 NFC_CMD_DEP_LINK_DOWN);
373 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
375 genlmsg_end(msg, hdr);
377 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
382 genlmsg_cancel(msg, hdr);
388 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
395 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
398 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
400 dev = nfc_get_device(idx);
404 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
410 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
417 return genlmsg_reply(msg, info);
426 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
432 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
435 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
437 dev = nfc_get_device(idx);
441 rc = nfc_dev_up(dev);
447 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
453 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
456 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
458 dev = nfc_get_device(idx);
462 rc = nfc_dev_down(dev);
468 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
475 pr_debug("Poll start\n");
477 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
478 !info->attrs[NFC_ATTR_PROTOCOLS])
481 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
482 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
484 dev = nfc_get_device(idx);
488 mutex_lock(&dev->genl_data.genl_data_mutex);
490 rc = nfc_start_poll(dev, protocols);
492 dev->genl_data.poll_req_pid = info->snd_pid;
494 mutex_unlock(&dev->genl_data.genl_data_mutex);
500 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
506 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
509 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
511 dev = nfc_get_device(idx);
515 mutex_lock(&dev->genl_data.genl_data_mutex);
517 if (dev->genl_data.poll_req_pid != info->snd_pid) {
522 rc = nfc_stop_poll(dev);
523 dev->genl_data.poll_req_pid = 0;
526 mutex_unlock(&dev->genl_data.genl_data_mutex);
531 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
538 pr_debug("DEP link up\n");
540 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
541 !info->attrs[NFC_ATTR_COMM_MODE] ||
542 !info->attrs[NFC_ATTR_RF_MODE])
545 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
546 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
547 tgt_idx = NFC_TARGET_IDX_ANY;
549 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
551 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
552 rf = nla_get_u8(info->attrs[NFC_ATTR_RF_MODE]);
554 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
557 if (rf != NFC_RF_INITIATOR && comm != NFC_RF_TARGET)
560 dev = nfc_get_device(idx);
564 rc = nfc_dep_link_up(dev, tgt_idx, comm, rf);
571 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
577 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
580 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
582 dev = nfc_get_device(idx);
586 rc = nfc_dep_link_down(dev);
592 static struct genl_ops nfc_genl_ops[] = {
594 .cmd = NFC_CMD_GET_DEVICE,
595 .doit = nfc_genl_get_device,
596 .dumpit = nfc_genl_dump_devices,
597 .done = nfc_genl_dump_devices_done,
598 .policy = nfc_genl_policy,
601 .cmd = NFC_CMD_DEV_UP,
602 .doit = nfc_genl_dev_up,
603 .policy = nfc_genl_policy,
606 .cmd = NFC_CMD_DEV_DOWN,
607 .doit = nfc_genl_dev_down,
608 .policy = nfc_genl_policy,
611 .cmd = NFC_CMD_START_POLL,
612 .doit = nfc_genl_start_poll,
613 .policy = nfc_genl_policy,
616 .cmd = NFC_CMD_STOP_POLL,
617 .doit = nfc_genl_stop_poll,
618 .policy = nfc_genl_policy,
621 .cmd = NFC_CMD_DEP_LINK_UP,
622 .doit = nfc_genl_dep_link_up,
623 .policy = nfc_genl_policy,
626 .cmd = NFC_CMD_DEP_LINK_DOWN,
627 .doit = nfc_genl_dep_link_down,
628 .policy = nfc_genl_policy,
631 .cmd = NFC_CMD_GET_TARGET,
632 .dumpit = nfc_genl_dump_targets,
633 .done = nfc_genl_dump_targets_done,
634 .policy = nfc_genl_policy,
638 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
639 unsigned long event, void *ptr)
641 struct netlink_notify *n = ptr;
642 struct class_dev_iter iter;
645 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
648 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
650 nfc_device_iter_init(&iter);
651 dev = nfc_device_iter_next(&iter);
654 if (dev->genl_data.poll_req_pid == n->pid) {
656 dev->genl_data.poll_req_pid = 0;
658 dev = nfc_device_iter_next(&iter);
661 nfc_device_iter_exit(&iter);
667 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
669 genl_data->poll_req_pid = 0;
670 mutex_init(&genl_data->genl_data_mutex);
673 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
675 mutex_destroy(&genl_data->genl_data_mutex);
678 static struct notifier_block nl_notifier = {
679 .notifier_call = nfc_genl_rcv_nl_event,
683 * nfc_genl_init() - Initialize netlink interface
685 * This initialization function registers the nfc netlink family.
687 int __init nfc_genl_init(void)
691 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
692 ARRAY_SIZE(nfc_genl_ops));
696 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
698 netlink_register_notifier(&nl_notifier);
704 * nfc_genl_exit() - Deinitialize netlink interface
706 * This exit function unregisters the nfc netlink family.
708 void nfc_genl_exit(void)
710 netlink_unregister_notifier(&nl_notifier);
711 genl_unregister_family(&nfc_genl_family);