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 static 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 if (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))
71 if (target->nfcid1_len > 0 &&
72 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
75 if (target->sensb_res_len > 0 &&
76 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
79 if (target->sensf_res_len > 0 &&
80 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
84 return genlmsg_end(msg, hdr);
87 genlmsg_cancel(msg, hdr);
91 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
97 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
98 nfc_genl_family.attrbuf,
99 nfc_genl_family.maxattr,
104 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
105 return ERR_PTR(-EINVAL);
107 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
109 dev = nfc_get_device(idx);
111 return ERR_PTR(-ENODEV);
116 static int nfc_genl_dump_targets(struct sk_buff *skb,
117 struct netlink_callback *cb)
120 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
124 dev = __get_device_from_cb(cb);
128 cb->args[1] = (long) dev;
131 device_lock(&dev->dev);
133 cb->seq = dev->targets_generation;
135 while (i < dev->n_targets) {
136 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
144 device_unlock(&dev->dev);
151 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
153 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
161 int nfc_genl_targets_found(struct nfc_dev *dev)
166 dev->genl_data.poll_req_pid = 0;
168 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
172 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
173 NFC_EVENT_TARGETS_FOUND);
177 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
178 goto nla_put_failure;
180 genlmsg_end(msg, hdr);
182 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
185 genlmsg_cancel(msg, hdr);
191 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
196 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
200 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
201 NFC_EVENT_TARGET_LOST);
205 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
206 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
207 goto nla_put_failure;
209 genlmsg_end(msg, hdr);
211 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
216 genlmsg_cancel(msg, hdr);
222 int nfc_genl_device_added(struct nfc_dev *dev)
227 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
231 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
232 NFC_EVENT_DEVICE_ADDED);
236 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
237 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
238 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
239 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
240 goto nla_put_failure;
242 genlmsg_end(msg, hdr);
244 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
249 genlmsg_cancel(msg, hdr);
255 int nfc_genl_device_removed(struct nfc_dev *dev)
260 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
264 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
265 NFC_EVENT_DEVICE_REMOVED);
269 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
270 goto nla_put_failure;
272 genlmsg_end(msg, hdr);
274 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
279 genlmsg_cancel(msg, hdr);
285 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
287 struct netlink_callback *cb,
292 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
298 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
300 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
301 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
302 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
303 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
304 goto nla_put_failure;
306 return genlmsg_end(msg, hdr);
309 genlmsg_cancel(msg, hdr);
313 static int nfc_genl_dump_devices(struct sk_buff *skb,
314 struct netlink_callback *cb)
316 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
317 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
318 bool first_call = false;
322 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
325 cb->args[0] = (long) iter;
328 mutex_lock(&nfc_devlist_mutex);
330 cb->seq = nfc_devlist_generation;
333 nfc_device_iter_init(iter);
334 dev = nfc_device_iter_next(iter);
340 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
341 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
345 dev = nfc_device_iter_next(iter);
348 mutex_unlock(&nfc_devlist_mutex);
350 cb->args[1] = (long) dev;
355 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
357 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
359 nfc_device_iter_exit(iter);
365 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
366 u8 comm_mode, u8 rf_mode)
371 pr_debug("DEP link is up\n");
373 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
377 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
381 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
382 goto nla_put_failure;
383 if (rf_mode == NFC_RF_INITIATOR &&
384 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
385 goto nla_put_failure;
386 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
387 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
388 goto nla_put_failure;
390 genlmsg_end(msg, hdr);
392 dev->dep_link_up = true;
394 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
399 genlmsg_cancel(msg, hdr);
405 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
410 pr_debug("DEP link is down\n");
412 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
416 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
417 NFC_CMD_DEP_LINK_DOWN);
421 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
422 goto nla_put_failure;
424 genlmsg_end(msg, hdr);
426 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
431 genlmsg_cancel(msg, hdr);
437 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
444 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
447 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
449 dev = nfc_get_device(idx);
453 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
459 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
466 return genlmsg_reply(msg, info);
475 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
481 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
484 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
486 dev = nfc_get_device(idx);
490 rc = nfc_dev_up(dev);
496 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
502 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
505 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
507 dev = nfc_get_device(idx);
511 rc = nfc_dev_down(dev);
517 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
524 pr_debug("Poll start\n");
526 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
527 !info->attrs[NFC_ATTR_PROTOCOLS])
530 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
531 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
533 dev = nfc_get_device(idx);
537 mutex_lock(&dev->genl_data.genl_data_mutex);
539 rc = nfc_start_poll(dev, protocols);
541 dev->genl_data.poll_req_pid = info->snd_pid;
543 mutex_unlock(&dev->genl_data.genl_data_mutex);
549 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
555 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
558 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
560 dev = nfc_get_device(idx);
564 mutex_lock(&dev->genl_data.genl_data_mutex);
566 if (dev->genl_data.poll_req_pid != info->snd_pid) {
571 rc = nfc_stop_poll(dev);
572 dev->genl_data.poll_req_pid = 0;
575 mutex_unlock(&dev->genl_data.genl_data_mutex);
580 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
587 pr_debug("DEP link up\n");
589 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
590 !info->attrs[NFC_ATTR_COMM_MODE])
593 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
594 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
595 tgt_idx = NFC_TARGET_IDX_ANY;
597 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
599 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
601 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
604 dev = nfc_get_device(idx);
608 rc = nfc_dep_link_up(dev, tgt_idx, comm);
615 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
621 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
624 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
626 dev = nfc_get_device(idx);
630 rc = nfc_dep_link_down(dev);
636 static struct genl_ops nfc_genl_ops[] = {
638 .cmd = NFC_CMD_GET_DEVICE,
639 .doit = nfc_genl_get_device,
640 .dumpit = nfc_genl_dump_devices,
641 .done = nfc_genl_dump_devices_done,
642 .policy = nfc_genl_policy,
645 .cmd = NFC_CMD_DEV_UP,
646 .doit = nfc_genl_dev_up,
647 .policy = nfc_genl_policy,
650 .cmd = NFC_CMD_DEV_DOWN,
651 .doit = nfc_genl_dev_down,
652 .policy = nfc_genl_policy,
655 .cmd = NFC_CMD_START_POLL,
656 .doit = nfc_genl_start_poll,
657 .policy = nfc_genl_policy,
660 .cmd = NFC_CMD_STOP_POLL,
661 .doit = nfc_genl_stop_poll,
662 .policy = nfc_genl_policy,
665 .cmd = NFC_CMD_DEP_LINK_UP,
666 .doit = nfc_genl_dep_link_up,
667 .policy = nfc_genl_policy,
670 .cmd = NFC_CMD_DEP_LINK_DOWN,
671 .doit = nfc_genl_dep_link_down,
672 .policy = nfc_genl_policy,
675 .cmd = NFC_CMD_GET_TARGET,
676 .dumpit = nfc_genl_dump_targets,
677 .done = nfc_genl_dump_targets_done,
678 .policy = nfc_genl_policy,
682 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
683 unsigned long event, void *ptr)
685 struct netlink_notify *n = ptr;
686 struct class_dev_iter iter;
689 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
692 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
694 nfc_device_iter_init(&iter);
695 dev = nfc_device_iter_next(&iter);
698 if (dev->genl_data.poll_req_pid == n->pid) {
700 dev->genl_data.poll_req_pid = 0;
702 dev = nfc_device_iter_next(&iter);
705 nfc_device_iter_exit(&iter);
711 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
713 genl_data->poll_req_pid = 0;
714 mutex_init(&genl_data->genl_data_mutex);
717 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
719 mutex_destroy(&genl_data->genl_data_mutex);
722 static struct notifier_block nl_notifier = {
723 .notifier_call = nfc_genl_rcv_nl_event,
727 * nfc_genl_init() - Initialize netlink interface
729 * This initialization function registers the nfc netlink family.
731 int __init nfc_genl_init(void)
735 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
736 ARRAY_SIZE(nfc_genl_ops));
740 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
742 netlink_register_notifier(&nl_notifier);
748 * nfc_genl_exit() - Deinitialize netlink interface
750 * This exit function unregisters the nfc netlink family.
752 void nfc_genl_exit(void)
754 netlink_unregister_notifier(&nl_notifier);
755 genl_unregister_family(&nfc_genl_family);