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 * Vendor commands implementation based on net/wireless/nl80211.c
11 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
12 * Copyright 2013-2014 Intel Mobile Communications GmbH
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
30 #include <net/genetlink.h>
31 #include <linux/nfc.h>
32 #include <linux/slab.h>
37 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
38 { .name = NFC_GENL_MCAST_EVENT_NAME, },
41 static struct genl_family nfc_genl_family = {
42 .id = GENL_ID_GENERATE,
44 .name = NFC_GENL_NAME,
45 .version = NFC_GENL_VERSION,
46 .maxattr = NFC_ATTR_MAX,
49 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
50 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
51 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
52 .len = NFC_DEVICE_NAME_MAXSIZE },
53 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
55 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
56 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
57 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
58 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
59 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
60 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
61 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
62 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
63 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
64 .len = NFC_FIRMWARE_NAME_MAXSIZE },
65 [NFC_ATTR_SE_INDEX] = { .type = NLA_U32 },
66 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
67 [NFC_ATTR_VENDOR_ID] = { .type = NLA_U32 },
68 [NFC_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
69 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
73 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
74 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
76 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
79 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
80 struct netlink_callback *cb, int flags)
84 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
85 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
89 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
91 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
92 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
93 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
94 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
96 if (target->nfcid1_len > 0 &&
97 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
100 if (target->sensb_res_len > 0 &&
101 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
103 goto nla_put_failure;
104 if (target->sensf_res_len > 0 &&
105 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
107 goto nla_put_failure;
109 if (target->is_iso15693) {
110 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
111 target->iso15693_dsfid) ||
112 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
113 sizeof(target->iso15693_uid), target->iso15693_uid))
114 goto nla_put_failure;
117 genlmsg_end(msg, hdr);
121 genlmsg_cancel(msg, hdr);
125 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
131 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
132 nfc_genl_family.attrbuf,
133 nfc_genl_family.maxattr,
138 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
139 return ERR_PTR(-EINVAL);
141 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
143 dev = nfc_get_device(idx);
145 return ERR_PTR(-ENODEV);
150 static int nfc_genl_dump_targets(struct sk_buff *skb,
151 struct netlink_callback *cb)
154 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
158 dev = __get_device_from_cb(cb);
162 cb->args[1] = (long) dev;
165 device_lock(&dev->dev);
167 cb->seq = dev->targets_generation;
169 while (i < dev->n_targets) {
170 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
178 device_unlock(&dev->dev);
185 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
187 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
195 int nfc_genl_targets_found(struct nfc_dev *dev)
200 dev->genl_data.poll_req_portid = 0;
202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
206 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
207 NFC_EVENT_TARGETS_FOUND);
211 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
212 goto nla_put_failure;
214 genlmsg_end(msg, hdr);
216 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
219 genlmsg_cancel(msg, hdr);
225 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
230 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
234 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
235 NFC_EVENT_TARGET_LOST);
239 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
240 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
241 goto nla_put_failure;
243 genlmsg_end(msg, hdr);
245 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
250 genlmsg_cancel(msg, hdr);
256 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
265 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
266 NFC_EVENT_TM_ACTIVATED);
270 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
271 goto nla_put_failure;
272 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
273 goto nla_put_failure;
275 genlmsg_end(msg, hdr);
277 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
282 genlmsg_cancel(msg, hdr);
288 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
297 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
298 NFC_EVENT_TM_DEACTIVATED);
302 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
303 goto nla_put_failure;
305 genlmsg_end(msg, hdr);
307 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
312 genlmsg_cancel(msg, hdr);
318 int nfc_genl_device_added(struct nfc_dev *dev)
323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
327 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
328 NFC_EVENT_DEVICE_ADDED);
332 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
333 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
334 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
335 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
336 goto nla_put_failure;
338 genlmsg_end(msg, hdr);
340 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
345 genlmsg_cancel(msg, hdr);
351 int nfc_genl_device_removed(struct nfc_dev *dev)
356 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
360 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
361 NFC_EVENT_DEVICE_REMOVED);
365 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
366 goto nla_put_failure;
368 genlmsg_end(msg, hdr);
370 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
375 genlmsg_cancel(msg, hdr);
381 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
384 struct nlattr *sdp_attr, *uri_attr;
385 struct nfc_llcp_sdp_tlv *sdres;
386 struct hlist_node *n;
391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
395 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
396 NFC_EVENT_LLC_SDRES);
400 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
401 goto nla_put_failure;
403 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
404 if (sdp_attr == NULL) {
406 goto nla_put_failure;
410 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
411 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
413 uri_attr = nla_nest_start(msg, i++);
414 if (uri_attr == NULL) {
416 goto nla_put_failure;
419 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
420 goto nla_put_failure;
422 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
423 goto nla_put_failure;
425 nla_nest_end(msg, uri_attr);
427 hlist_del(&sdres->node);
429 nfc_llcp_free_sdp_tlv(sdres);
432 nla_nest_end(msg, sdp_attr);
434 genlmsg_end(msg, hdr);
436 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
439 genlmsg_cancel(msg, hdr);
444 nfc_llcp_free_sdp_tlv_list(sdres_list);
449 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
454 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
458 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
463 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
464 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
465 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
466 goto nla_put_failure;
468 genlmsg_end(msg, hdr);
470 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
475 genlmsg_cancel(msg, hdr);
481 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
490 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
491 NFC_EVENT_SE_REMOVED);
495 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
496 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
497 goto nla_put_failure;
499 genlmsg_end(msg, hdr);
501 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
506 genlmsg_cancel(msg, hdr);
512 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
513 struct nfc_evt_transaction *evt_transaction)
519 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
523 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
524 NFC_EVENT_SE_TRANSACTION);
528 se = nfc_find_se(dev, se_idx);
532 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
533 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
534 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
535 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
536 evt_transaction->aid) ||
537 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
538 evt_transaction->params))
539 goto nla_put_failure;
541 /* evt_transaction is no more used */
542 devm_kfree(&dev->dev, evt_transaction);
544 genlmsg_end(msg, hdr);
546 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
551 genlmsg_cancel(msg, hdr);
553 /* evt_transaction is no more used */
554 devm_kfree(&dev->dev, evt_transaction);
559 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
561 struct netlink_callback *cb,
566 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
572 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
574 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
575 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
576 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
577 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
578 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
579 goto nla_put_failure;
581 genlmsg_end(msg, hdr);
585 genlmsg_cancel(msg, hdr);
589 static int nfc_genl_dump_devices(struct sk_buff *skb,
590 struct netlink_callback *cb)
592 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
593 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
594 bool first_call = false;
598 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
601 cb->args[0] = (long) iter;
604 mutex_lock(&nfc_devlist_mutex);
606 cb->seq = nfc_devlist_generation;
609 nfc_device_iter_init(iter);
610 dev = nfc_device_iter_next(iter);
616 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
617 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
621 dev = nfc_device_iter_next(iter);
624 mutex_unlock(&nfc_devlist_mutex);
626 cb->args[1] = (long) dev;
631 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
633 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
635 nfc_device_iter_exit(iter);
641 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
642 u8 comm_mode, u8 rf_mode)
647 pr_debug("DEP link is up\n");
649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
653 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
657 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
658 goto nla_put_failure;
659 if (rf_mode == NFC_RF_INITIATOR &&
660 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
661 goto nla_put_failure;
662 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
663 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
664 goto nla_put_failure;
666 genlmsg_end(msg, hdr);
668 dev->dep_link_up = true;
670 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
675 genlmsg_cancel(msg, hdr);
681 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
686 pr_debug("DEP link is down\n");
688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
692 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
693 NFC_CMD_DEP_LINK_DOWN);
697 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
698 goto nla_put_failure;
700 genlmsg_end(msg, hdr);
702 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
707 genlmsg_cancel(msg, hdr);
713 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
720 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
723 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
725 dev = nfc_get_device(idx);
729 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
735 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
742 return genlmsg_reply(msg, info);
751 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
757 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
760 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
762 dev = nfc_get_device(idx);
766 rc = nfc_dev_up(dev);
772 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
778 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
781 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
783 dev = nfc_get_device(idx);
787 rc = nfc_dev_down(dev);
793 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
798 u32 im_protocols = 0, tm_protocols = 0;
800 pr_debug("Poll start\n");
802 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
803 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
804 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
805 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
808 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
810 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
811 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
813 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
814 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
815 else if (info->attrs[NFC_ATTR_PROTOCOLS])
816 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
818 dev = nfc_get_device(idx);
822 mutex_lock(&dev->genl_data.genl_data_mutex);
824 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
826 dev->genl_data.poll_req_portid = info->snd_portid;
828 mutex_unlock(&dev->genl_data.genl_data_mutex);
834 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
840 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
843 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
845 dev = nfc_get_device(idx);
849 device_lock(&dev->dev);
852 device_unlock(&dev->dev);
857 device_unlock(&dev->dev);
859 mutex_lock(&dev->genl_data.genl_data_mutex);
861 if (dev->genl_data.poll_req_portid != info->snd_portid) {
866 rc = nfc_stop_poll(dev);
867 dev->genl_data.poll_req_portid = 0;
870 mutex_unlock(&dev->genl_data.genl_data_mutex);
875 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
878 u32 device_idx, target_idx, protocol;
881 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
882 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
883 !info->attrs[NFC_ATTR_PROTOCOLS])
886 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
888 dev = nfc_get_device(device_idx);
892 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
893 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
895 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
896 rc = nfc_activate_target(dev, target_idx, protocol);
902 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
909 pr_debug("DEP link up\n");
911 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
912 !info->attrs[NFC_ATTR_COMM_MODE])
915 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
916 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
917 tgt_idx = NFC_TARGET_IDX_ANY;
919 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
921 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
923 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
926 dev = nfc_get_device(idx);
930 rc = nfc_dep_link_up(dev, tgt_idx, comm);
937 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
943 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
944 !info->attrs[NFC_ATTR_TARGET_INDEX])
947 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
949 dev = nfc_get_device(idx);
953 rc = nfc_dep_link_down(dev);
959 static int nfc_genl_send_params(struct sk_buff *msg,
960 struct nfc_llcp_local *local,
965 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
966 NFC_CMD_LLC_GET_PARAMS);
970 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
971 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
972 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
973 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
974 goto nla_put_failure;
976 genlmsg_end(msg, hdr);
981 genlmsg_cancel(msg, hdr);
985 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
988 struct nfc_llcp_local *local;
990 struct sk_buff *msg = NULL;
993 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
994 !info->attrs[NFC_ATTR_FIRMWARE_NAME])
997 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
999 dev = nfc_get_device(idx);
1003 device_lock(&dev->dev);
1005 local = nfc_llcp_find_local(dev);
1011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1017 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1020 device_unlock(&dev->dev);
1022 nfc_put_device(dev);
1031 return genlmsg_reply(msg, info);
1034 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1036 struct nfc_dev *dev;
1037 struct nfc_llcp_local *local;
1043 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1044 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1045 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1046 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1049 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1050 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1052 if (rw > LLCP_MAX_RW)
1056 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1057 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1059 if (miux > LLCP_MAX_MIUX)
1063 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1065 dev = nfc_get_device(idx);
1069 device_lock(&dev->dev);
1071 local = nfc_llcp_find_local(dev);
1077 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1078 if (dev->dep_link_up) {
1083 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1086 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1089 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1090 local->miux = cpu_to_be16(miux);
1093 device_unlock(&dev->dev);
1095 nfc_put_device(dev);
1100 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1102 struct nfc_dev *dev;
1103 struct nfc_llcp_local *local;
1104 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1109 size_t uri_len, tlvs_len;
1110 struct hlist_head sdreq_list;
1111 struct nfc_llcp_sdp_tlv *sdreq;
1113 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1114 !info->attrs[NFC_ATTR_LLC_SDP])
1117 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1119 dev = nfc_get_device(idx);
1123 device_lock(&dev->dev);
1125 if (dev->dep_link_up == false) {
1130 local = nfc_llcp_find_local(dev);
1136 INIT_HLIST_HEAD(&sdreq_list);
1140 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1141 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1142 nfc_sdp_genl_policy);
1149 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1152 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1156 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1157 if (uri == NULL || *uri == 0)
1160 tid = local->sdreq_next_tid++;
1162 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1163 if (sdreq == NULL) {
1168 tlvs_len += sdreq->tlv_len;
1170 hlist_add_head(&sdreq->node, &sdreq_list);
1173 if (hlist_empty(&sdreq_list)) {
1178 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1180 device_unlock(&dev->dev);
1182 nfc_put_device(dev);
1187 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1189 struct nfc_dev *dev;
1192 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1194 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1197 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1199 dev = nfc_get_device(idx);
1203 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1204 sizeof(firmware_name));
1206 rc = nfc_fw_download(dev, firmware_name);
1208 nfc_put_device(dev);
1212 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1215 struct sk_buff *msg;
1218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1222 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1223 NFC_CMD_FW_DOWNLOAD);
1227 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1228 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1229 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1230 goto nla_put_failure;
1232 genlmsg_end(msg, hdr);
1234 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1239 genlmsg_cancel(msg, hdr);
1245 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1247 struct nfc_dev *dev;
1251 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1252 !info->attrs[NFC_ATTR_SE_INDEX])
1255 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1256 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1258 dev = nfc_get_device(idx);
1262 rc = nfc_enable_se(dev, se_idx);
1264 nfc_put_device(dev);
1268 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1270 struct nfc_dev *dev;
1274 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1275 !info->attrs[NFC_ATTR_SE_INDEX])
1278 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1279 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1281 dev = nfc_get_device(idx);
1285 rc = nfc_disable_se(dev, se_idx);
1287 nfc_put_device(dev);
1291 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1292 u32 portid, u32 seq,
1293 struct netlink_callback *cb,
1297 struct nfc_se *se, *n;
1299 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1300 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1303 goto nla_put_failure;
1306 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1308 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1309 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1310 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1311 goto nla_put_failure;
1313 genlmsg_end(msg, hdr);
1319 genlmsg_cancel(msg, hdr);
1323 static int nfc_genl_dump_ses(struct sk_buff *skb,
1324 struct netlink_callback *cb)
1326 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1327 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1328 bool first_call = false;
1332 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1335 cb->args[0] = (long) iter;
1338 mutex_lock(&nfc_devlist_mutex);
1340 cb->seq = nfc_devlist_generation;
1343 nfc_device_iter_init(iter);
1344 dev = nfc_device_iter_next(iter);
1350 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1351 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1355 dev = nfc_device_iter_next(iter);
1358 mutex_unlock(&nfc_devlist_mutex);
1360 cb->args[1] = (long) dev;
1365 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1367 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1369 nfc_device_iter_exit(iter);
1375 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1376 u8 *apdu, size_t apdu_length,
1377 se_io_cb_t cb, void *cb_context)
1382 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1384 device_lock(&dev->dev);
1386 if (!device_is_registered(&dev->dev)) {
1396 if (!dev->ops->se_io) {
1401 se = nfc_find_se(dev, se_idx);
1407 if (se->state != NFC_SE_ENABLED) {
1412 rc = dev->ops->se_io(dev, se_idx, apdu,
1413 apdu_length, cb, cb_context);
1416 device_unlock(&dev->dev);
1425 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1427 struct se_io_ctx *ctx = context;
1428 struct sk_buff *msg;
1431 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1437 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1442 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1443 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1444 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1445 goto nla_put_failure;
1447 genlmsg_end(msg, hdr);
1449 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1456 genlmsg_cancel(msg, hdr);
1464 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1466 struct nfc_dev *dev;
1467 struct se_io_ctx *ctx;
1468 u32 dev_idx, se_idx;
1472 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1473 !info->attrs[NFC_ATTR_SE_INDEX] ||
1474 !info->attrs[NFC_ATTR_SE_APDU])
1477 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1478 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1480 dev = nfc_get_device(dev_idx);
1484 if (!dev->ops || !dev->ops->se_io)
1487 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1491 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1495 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1499 ctx->dev_idx = dev_idx;
1500 ctx->se_idx = se_idx;
1502 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1505 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1506 struct genl_info *info)
1508 struct nfc_dev *dev;
1509 struct nfc_vendor_cmd *cmd;
1510 u32 dev_idx, vid, subcmd;
1515 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1516 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1517 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1520 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1521 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1522 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1524 dev = nfc_get_device(dev_idx);
1525 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1528 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1529 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1530 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1538 for (i = 0; i < dev->n_vendor_cmds; i++) {
1539 cmd = &dev->vendor_cmds[i];
1541 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1544 dev->cur_cmd_info = info;
1545 err = cmd->doit(dev, data, data_len);
1546 dev->cur_cmd_info = NULL;
1553 /* message building helper */
1554 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1557 /* since there is no private header just add the generic one */
1558 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1561 static struct sk_buff *
1562 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1563 u32 portid, u32 seq,
1564 enum nfc_attrs attr,
1565 u32 oui, u32 subcmd, gfp_t gfp)
1567 struct sk_buff *skb;
1570 skb = nlmsg_new(approxlen + 100, gfp);
1574 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1580 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1581 goto nla_put_failure;
1582 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1583 goto nla_put_failure;
1584 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1585 goto nla_put_failure;
1587 ((void **)skb->cb)[0] = dev;
1588 ((void **)skb->cb)[1] = hdr;
1597 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1598 enum nfc_attrs attr,
1599 u32 oui, u32 subcmd,
1602 if (WARN_ON(!dev->cur_cmd_info))
1605 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1606 dev->cur_cmd_info->snd_portid,
1607 dev->cur_cmd_info->snd_seq, attr,
1608 oui, subcmd, GFP_KERNEL);
1610 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1612 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1614 struct nfc_dev *dev = ((void **)skb->cb)[0];
1615 void *hdr = ((void **)skb->cb)[1];
1617 /* clear CB data for netlink core to own from now on */
1618 memset(skb->cb, 0, sizeof(skb->cb));
1620 if (WARN_ON(!dev->cur_cmd_info)) {
1625 genlmsg_end(skb, hdr);
1626 return genlmsg_reply(skb, dev->cur_cmd_info);
1628 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1630 static const struct genl_ops nfc_genl_ops[] = {
1632 .cmd = NFC_CMD_GET_DEVICE,
1633 .doit = nfc_genl_get_device,
1634 .dumpit = nfc_genl_dump_devices,
1635 .done = nfc_genl_dump_devices_done,
1636 .policy = nfc_genl_policy,
1639 .cmd = NFC_CMD_DEV_UP,
1640 .doit = nfc_genl_dev_up,
1641 .policy = nfc_genl_policy,
1644 .cmd = NFC_CMD_DEV_DOWN,
1645 .doit = nfc_genl_dev_down,
1646 .policy = nfc_genl_policy,
1649 .cmd = NFC_CMD_START_POLL,
1650 .doit = nfc_genl_start_poll,
1651 .policy = nfc_genl_policy,
1654 .cmd = NFC_CMD_STOP_POLL,
1655 .doit = nfc_genl_stop_poll,
1656 .policy = nfc_genl_policy,
1659 .cmd = NFC_CMD_DEP_LINK_UP,
1660 .doit = nfc_genl_dep_link_up,
1661 .policy = nfc_genl_policy,
1664 .cmd = NFC_CMD_DEP_LINK_DOWN,
1665 .doit = nfc_genl_dep_link_down,
1666 .policy = nfc_genl_policy,
1669 .cmd = NFC_CMD_GET_TARGET,
1670 .dumpit = nfc_genl_dump_targets,
1671 .done = nfc_genl_dump_targets_done,
1672 .policy = nfc_genl_policy,
1675 .cmd = NFC_CMD_LLC_GET_PARAMS,
1676 .doit = nfc_genl_llc_get_params,
1677 .policy = nfc_genl_policy,
1680 .cmd = NFC_CMD_LLC_SET_PARAMS,
1681 .doit = nfc_genl_llc_set_params,
1682 .policy = nfc_genl_policy,
1685 .cmd = NFC_CMD_LLC_SDREQ,
1686 .doit = nfc_genl_llc_sdreq,
1687 .policy = nfc_genl_policy,
1690 .cmd = NFC_CMD_FW_DOWNLOAD,
1691 .doit = nfc_genl_fw_download,
1692 .policy = nfc_genl_policy,
1695 .cmd = NFC_CMD_ENABLE_SE,
1696 .doit = nfc_genl_enable_se,
1697 .policy = nfc_genl_policy,
1700 .cmd = NFC_CMD_DISABLE_SE,
1701 .doit = nfc_genl_disable_se,
1702 .policy = nfc_genl_policy,
1705 .cmd = NFC_CMD_GET_SE,
1706 .dumpit = nfc_genl_dump_ses,
1707 .done = nfc_genl_dump_ses_done,
1708 .policy = nfc_genl_policy,
1711 .cmd = NFC_CMD_SE_IO,
1712 .doit = nfc_genl_se_io,
1713 .policy = nfc_genl_policy,
1716 .cmd = NFC_CMD_ACTIVATE_TARGET,
1717 .doit = nfc_genl_activate_target,
1718 .policy = nfc_genl_policy,
1721 .cmd = NFC_CMD_VENDOR,
1722 .doit = nfc_genl_vendor_cmd,
1723 .policy = nfc_genl_policy,
1728 struct urelease_work {
1729 struct work_struct w;
1733 static void nfc_urelease_event_work(struct work_struct *work)
1735 struct urelease_work *w = container_of(work, struct urelease_work, w);
1736 struct class_dev_iter iter;
1737 struct nfc_dev *dev;
1739 pr_debug("portid %d\n", w->portid);
1741 mutex_lock(&nfc_devlist_mutex);
1743 nfc_device_iter_init(&iter);
1744 dev = nfc_device_iter_next(&iter);
1747 mutex_lock(&dev->genl_data.genl_data_mutex);
1749 if (dev->genl_data.poll_req_portid == w->portid) {
1751 dev->genl_data.poll_req_portid = 0;
1754 mutex_unlock(&dev->genl_data.genl_data_mutex);
1756 dev = nfc_device_iter_next(&iter);
1759 nfc_device_iter_exit(&iter);
1761 mutex_unlock(&nfc_devlist_mutex);
1766 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1767 unsigned long event, void *ptr)
1769 struct netlink_notify *n = ptr;
1770 struct urelease_work *w;
1772 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1775 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1777 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1779 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1780 w->portid = n->portid;
1781 schedule_work((struct work_struct *) w);
1788 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1790 genl_data->poll_req_portid = 0;
1791 mutex_init(&genl_data->genl_data_mutex);
1794 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1796 mutex_destroy(&genl_data->genl_data_mutex);
1799 static struct notifier_block nl_notifier = {
1800 .notifier_call = nfc_genl_rcv_nl_event,
1804 * nfc_genl_init() - Initialize netlink interface
1806 * This initialization function registers the nfc netlink family.
1808 int __init nfc_genl_init(void)
1812 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1818 netlink_register_notifier(&nl_notifier);
1824 * nfc_genl_exit() - Deinitialize netlink interface
1826 * This exit function unregisters the nfc netlink family.
1828 void nfc_genl_exit(void)
1830 netlink_unregister_notifier(&nl_notifier);
1831 genl_unregister_family(&nfc_genl_family);