2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
8 * Vendor commands implementation based on net/wireless/nl80211.c
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 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
43 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
44 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
45 .len = NFC_DEVICE_NAME_MAXSIZE },
46 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
47 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
48 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
50 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
51 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
53 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
55 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
56 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
57 .len = NFC_FIRMWARE_NAME_MAXSIZE },
58 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
59 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
63 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
64 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
66 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
69 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
70 struct netlink_callback *cb, int flags)
74 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
75 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
79 genl_dump_check_consistent(cb, hdr);
81 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
82 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
83 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
84 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
86 if (target->nfcid1_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
90 if (target->sensb_res_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
94 if (target->sensf_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
99 if (target->is_iso15693) {
100 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
101 target->iso15693_dsfid) ||
102 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
103 sizeof(target->iso15693_uid), target->iso15693_uid))
104 goto nla_put_failure;
107 genlmsg_end(msg, hdr);
111 genlmsg_cancel(msg, hdr);
115 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
117 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
122 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
123 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
128 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
129 return ERR_PTR(-EINVAL);
131 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
133 dev = nfc_get_device(idx);
135 return ERR_PTR(-ENODEV);
140 static int nfc_genl_dump_targets(struct sk_buff *skb,
141 struct netlink_callback *cb)
144 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
148 dev = __get_device_from_cb(cb);
152 cb->args[1] = (long) dev;
155 device_lock(&dev->dev);
157 cb->seq = dev->targets_generation;
159 while (i < dev->n_targets) {
160 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
168 device_unlock(&dev->dev);
175 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
177 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
185 int nfc_genl_targets_found(struct nfc_dev *dev)
190 dev->genl_data.poll_req_portid = 0;
192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
196 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
197 NFC_EVENT_TARGETS_FOUND);
201 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
202 goto nla_put_failure;
204 genlmsg_end(msg, hdr);
206 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
214 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
223 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
224 NFC_EVENT_TARGET_LOST);
228 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
229 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
230 goto nla_put_failure;
232 genlmsg_end(msg, hdr);
234 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
244 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
253 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
254 NFC_EVENT_TM_ACTIVATED);
258 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
259 goto nla_put_failure;
260 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
261 goto nla_put_failure;
263 genlmsg_end(msg, hdr);
265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
275 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
284 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
285 NFC_EVENT_TM_DEACTIVATED);
289 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
290 goto nla_put_failure;
292 genlmsg_end(msg, hdr);
294 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
304 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
306 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
307 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
308 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
309 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
310 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
315 int nfc_genl_device_added(struct nfc_dev *dev)
320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
324 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
325 NFC_EVENT_DEVICE_ADDED);
329 if (nfc_genl_setup_device_added(dev, msg))
330 goto nla_put_failure;
332 genlmsg_end(msg, hdr);
334 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
344 int nfc_genl_device_removed(struct nfc_dev *dev)
349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
353 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
354 NFC_EVENT_DEVICE_REMOVED);
358 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
359 goto nla_put_failure;
361 genlmsg_end(msg, hdr);
363 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
373 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
376 struct nlattr *sdp_attr, *uri_attr;
377 struct nfc_llcp_sdp_tlv *sdres;
378 struct hlist_node *n;
383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
387 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
388 NFC_EVENT_LLC_SDRES);
392 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
393 goto nla_put_failure;
395 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
396 if (sdp_attr == NULL) {
398 goto nla_put_failure;
402 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
403 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
405 uri_attr = nla_nest_start(msg, i++);
406 if (uri_attr == NULL) {
408 goto nla_put_failure;
411 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
412 goto nla_put_failure;
414 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
415 goto nla_put_failure;
417 nla_nest_end(msg, uri_attr);
419 hlist_del(&sdres->node);
421 nfc_llcp_free_sdp_tlv(sdres);
424 nla_nest_end(msg, sdp_attr);
426 genlmsg_end(msg, hdr);
428 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
434 nfc_llcp_free_sdp_tlv_list(sdres_list);
439 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
448 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
453 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
454 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
455 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
456 goto nla_put_failure;
458 genlmsg_end(msg, hdr);
460 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
470 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
479 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
480 NFC_EVENT_SE_REMOVED);
484 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
485 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
486 goto nla_put_failure;
488 genlmsg_end(msg, hdr);
490 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
500 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
501 struct nfc_evt_transaction *evt_transaction)
507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
511 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
512 NFC_EVENT_SE_TRANSACTION);
516 se = nfc_find_se(dev, se_idx);
520 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
521 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
522 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
523 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
524 evt_transaction->aid) ||
525 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
526 evt_transaction->params))
527 goto nla_put_failure;
529 /* evt_transaction is no more used */
530 devm_kfree(&dev->dev, evt_transaction);
532 genlmsg_end(msg, hdr);
534 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
540 /* evt_transaction is no more used */
541 devm_kfree(&dev->dev, evt_transaction);
546 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
556 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
557 NFC_EVENT_SE_CONNECTIVITY);
561 se = nfc_find_se(dev, se_idx);
565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
566 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
567 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
568 goto nla_put_failure;
570 genlmsg_end(msg, hdr);
572 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
582 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
584 struct netlink_callback *cb,
589 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
595 genl_dump_check_consistent(cb, hdr);
597 if (nfc_genl_setup_device_added(dev, msg))
598 goto nla_put_failure;
600 genlmsg_end(msg, hdr);
604 genlmsg_cancel(msg, hdr);
608 static int nfc_genl_dump_devices(struct sk_buff *skb,
609 struct netlink_callback *cb)
611 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
612 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
613 bool first_call = false;
617 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
620 cb->args[0] = (long) iter;
623 mutex_lock(&nfc_devlist_mutex);
625 cb->seq = nfc_devlist_generation;
628 nfc_device_iter_init(iter);
629 dev = nfc_device_iter_next(iter);
635 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
636 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
640 dev = nfc_device_iter_next(iter);
643 mutex_unlock(&nfc_devlist_mutex);
645 cb->args[1] = (long) dev;
650 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
652 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
654 nfc_device_iter_exit(iter);
660 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
661 u8 comm_mode, u8 rf_mode)
666 pr_debug("DEP link is up\n");
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
672 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
676 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
677 goto nla_put_failure;
678 if (rf_mode == NFC_RF_INITIATOR &&
679 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
680 goto nla_put_failure;
681 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
682 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
683 goto nla_put_failure;
685 genlmsg_end(msg, hdr);
687 dev->dep_link_up = true;
689 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
699 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
704 pr_debug("DEP link is down\n");
706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
710 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
711 NFC_CMD_DEP_LINK_DOWN);
715 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
716 goto nla_put_failure;
718 genlmsg_end(msg, hdr);
720 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
730 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
737 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
740 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
742 dev = nfc_get_device(idx);
746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
752 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
759 return genlmsg_reply(msg, info);
768 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
774 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
777 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
779 dev = nfc_get_device(idx);
783 rc = nfc_dev_up(dev);
789 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
795 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
798 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
800 dev = nfc_get_device(idx);
804 rc = nfc_dev_down(dev);
810 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
815 u32 im_protocols = 0, tm_protocols = 0;
817 pr_debug("Poll start\n");
819 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
820 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
821 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
822 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
825 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
827 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
828 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
830 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
831 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
832 else if (info->attrs[NFC_ATTR_PROTOCOLS])
833 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
835 dev = nfc_get_device(idx);
839 mutex_lock(&dev->genl_data.genl_data_mutex);
841 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
843 dev->genl_data.poll_req_portid = info->snd_portid;
845 mutex_unlock(&dev->genl_data.genl_data_mutex);
851 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
857 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
860 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
862 dev = nfc_get_device(idx);
866 device_lock(&dev->dev);
869 device_unlock(&dev->dev);
873 device_unlock(&dev->dev);
875 mutex_lock(&dev->genl_data.genl_data_mutex);
877 if (dev->genl_data.poll_req_portid != info->snd_portid) {
882 rc = nfc_stop_poll(dev);
883 dev->genl_data.poll_req_portid = 0;
886 mutex_unlock(&dev->genl_data.genl_data_mutex);
891 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
894 u32 device_idx, target_idx, protocol;
897 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
898 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
899 !info->attrs[NFC_ATTR_PROTOCOLS])
902 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
904 dev = nfc_get_device(device_idx);
908 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
909 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
911 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
912 rc = nfc_activate_target(dev, target_idx, protocol);
918 static int nfc_genl_deactivate_target(struct sk_buff *skb,
919 struct genl_info *info)
922 u32 device_idx, target_idx;
925 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
928 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
930 dev = nfc_get_device(device_idx);
934 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
936 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
942 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
949 pr_debug("DEP link up\n");
951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
952 !info->attrs[NFC_ATTR_COMM_MODE])
955 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
956 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
957 tgt_idx = NFC_TARGET_IDX_ANY;
959 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
961 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
963 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
966 dev = nfc_get_device(idx);
970 rc = nfc_dep_link_up(dev, tgt_idx, comm);
977 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
983 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
986 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
988 dev = nfc_get_device(idx);
992 rc = nfc_dep_link_down(dev);
998 static int nfc_genl_send_params(struct sk_buff *msg,
999 struct nfc_llcp_local *local,
1000 u32 portid, u32 seq)
1004 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
1005 NFC_CMD_LLC_GET_PARAMS);
1009 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1010 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1011 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1012 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1013 goto nla_put_failure;
1015 genlmsg_end(msg, hdr);
1019 genlmsg_cancel(msg, hdr);
1023 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1025 struct nfc_dev *dev;
1026 struct nfc_llcp_local *local;
1028 struct sk_buff *msg = NULL;
1031 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1034 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1036 dev = nfc_get_device(idx);
1040 device_lock(&dev->dev);
1042 local = nfc_llcp_find_local(dev);
1048 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1054 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1057 device_unlock(&dev->dev);
1059 nfc_put_device(dev);
1068 return genlmsg_reply(msg, info);
1071 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1073 struct nfc_dev *dev;
1074 struct nfc_llcp_local *local;
1080 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1081 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1082 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1083 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1086 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1087 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1089 if (rw > LLCP_MAX_RW)
1093 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1094 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1096 if (miux > LLCP_MAX_MIUX)
1100 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1102 dev = nfc_get_device(idx);
1106 device_lock(&dev->dev);
1108 local = nfc_llcp_find_local(dev);
1110 nfc_put_device(dev);
1115 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1116 if (dev->dep_link_up) {
1121 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1124 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1127 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1128 local->miux = cpu_to_be16(miux);
1131 device_unlock(&dev->dev);
1133 nfc_put_device(dev);
1138 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1140 struct nfc_dev *dev;
1141 struct nfc_llcp_local *local;
1142 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1147 size_t uri_len, tlvs_len;
1148 struct hlist_head sdreq_list;
1149 struct nfc_llcp_sdp_tlv *sdreq;
1151 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1152 !info->attrs[NFC_ATTR_LLC_SDP])
1155 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1157 dev = nfc_get_device(idx);
1161 device_lock(&dev->dev);
1163 if (dev->dep_link_up == false) {
1168 local = nfc_llcp_find_local(dev);
1170 nfc_put_device(dev);
1175 INIT_HLIST_HEAD(&sdreq_list);
1179 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1180 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1181 nfc_sdp_genl_policy, info->extack);
1188 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1191 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1195 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1196 if (uri == NULL || *uri == 0)
1199 tid = local->sdreq_next_tid++;
1201 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1202 if (sdreq == NULL) {
1207 tlvs_len += sdreq->tlv_len;
1209 hlist_add_head(&sdreq->node, &sdreq_list);
1212 if (hlist_empty(&sdreq_list)) {
1217 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1219 device_unlock(&dev->dev);
1221 nfc_put_device(dev);
1226 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1228 struct nfc_dev *dev;
1231 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1233 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1236 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1238 dev = nfc_get_device(idx);
1242 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1243 sizeof(firmware_name));
1245 rc = nfc_fw_download(dev, firmware_name);
1247 nfc_put_device(dev);
1251 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1254 struct sk_buff *msg;
1257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1261 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1262 NFC_CMD_FW_DOWNLOAD);
1266 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1267 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1268 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1269 goto nla_put_failure;
1271 genlmsg_end(msg, hdr);
1273 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1283 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1285 struct nfc_dev *dev;
1289 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1290 !info->attrs[NFC_ATTR_SE_INDEX])
1293 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1294 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1296 dev = nfc_get_device(idx);
1300 rc = nfc_enable_se(dev, se_idx);
1302 nfc_put_device(dev);
1306 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1308 struct nfc_dev *dev;
1312 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1313 !info->attrs[NFC_ATTR_SE_INDEX])
1316 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1317 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1319 dev = nfc_get_device(idx);
1323 rc = nfc_disable_se(dev, se_idx);
1325 nfc_put_device(dev);
1329 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1330 u32 portid, u32 seq,
1331 struct netlink_callback *cb,
1335 struct nfc_se *se, *n;
1337 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1338 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1341 goto nla_put_failure;
1344 genl_dump_check_consistent(cb, hdr);
1346 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1347 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1348 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1349 goto nla_put_failure;
1351 genlmsg_end(msg, hdr);
1357 genlmsg_cancel(msg, hdr);
1361 static int nfc_genl_dump_ses(struct sk_buff *skb,
1362 struct netlink_callback *cb)
1364 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1365 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1366 bool first_call = false;
1370 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1373 cb->args[0] = (long) iter;
1376 mutex_lock(&nfc_devlist_mutex);
1378 cb->seq = nfc_devlist_generation;
1381 nfc_device_iter_init(iter);
1382 dev = nfc_device_iter_next(iter);
1388 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1389 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1393 dev = nfc_device_iter_next(iter);
1396 mutex_unlock(&nfc_devlist_mutex);
1398 cb->args[1] = (long) dev;
1403 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1405 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1407 nfc_device_iter_exit(iter);
1413 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1414 u8 *apdu, size_t apdu_length,
1415 se_io_cb_t cb, void *cb_context)
1420 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1422 device_lock(&dev->dev);
1424 if (!device_is_registered(&dev->dev)) {
1434 if (!dev->ops->se_io) {
1439 se = nfc_find_se(dev, se_idx);
1445 if (se->state != NFC_SE_ENABLED) {
1450 rc = dev->ops->se_io(dev, se_idx, apdu,
1451 apdu_length, cb, cb_context);
1454 device_unlock(&dev->dev);
1463 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1465 struct se_io_ctx *ctx = context;
1466 struct sk_buff *msg;
1469 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1475 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1480 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1481 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1482 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1483 goto nla_put_failure;
1485 genlmsg_end(msg, hdr);
1487 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1501 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1503 struct nfc_dev *dev;
1504 struct se_io_ctx *ctx;
1505 u32 dev_idx, se_idx;
1509 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1510 !info->attrs[NFC_ATTR_SE_INDEX] ||
1511 !info->attrs[NFC_ATTR_SE_APDU])
1514 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1515 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1517 dev = nfc_get_device(dev_idx);
1521 if (!dev->ops || !dev->ops->se_io)
1524 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1528 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1532 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1536 ctx->dev_idx = dev_idx;
1537 ctx->se_idx = se_idx;
1539 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1542 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1543 struct genl_info *info)
1545 struct nfc_dev *dev;
1546 struct nfc_vendor_cmd *cmd;
1547 u32 dev_idx, vid, subcmd;
1552 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1553 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1554 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1557 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1558 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1559 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1561 dev = nfc_get_device(dev_idx);
1562 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1565 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1566 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1567 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1575 for (i = 0; i < dev->n_vendor_cmds; i++) {
1576 cmd = &dev->vendor_cmds[i];
1578 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1581 dev->cur_cmd_info = info;
1582 err = cmd->doit(dev, data, data_len);
1583 dev->cur_cmd_info = NULL;
1590 /* message building helper */
1591 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1594 /* since there is no private header just add the generic one */
1595 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1598 static struct sk_buff *
1599 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1600 u32 portid, u32 seq,
1601 enum nfc_attrs attr,
1602 u32 oui, u32 subcmd, gfp_t gfp)
1604 struct sk_buff *skb;
1607 skb = nlmsg_new(approxlen + 100, gfp);
1611 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1617 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1618 goto nla_put_failure;
1619 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1620 goto nla_put_failure;
1621 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1622 goto nla_put_failure;
1624 ((void **)skb->cb)[0] = dev;
1625 ((void **)skb->cb)[1] = hdr;
1634 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1635 enum nfc_attrs attr,
1636 u32 oui, u32 subcmd,
1639 if (WARN_ON(!dev->cur_cmd_info))
1642 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1643 dev->cur_cmd_info->snd_portid,
1644 dev->cur_cmd_info->snd_seq, attr,
1645 oui, subcmd, GFP_KERNEL);
1647 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1649 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1651 struct nfc_dev *dev = ((void **)skb->cb)[0];
1652 void *hdr = ((void **)skb->cb)[1];
1654 /* clear CB data for netlink core to own from now on */
1655 memset(skb->cb, 0, sizeof(skb->cb));
1657 if (WARN_ON(!dev->cur_cmd_info)) {
1662 genlmsg_end(skb, hdr);
1663 return genlmsg_reply(skb, dev->cur_cmd_info);
1665 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1667 static const struct genl_ops nfc_genl_ops[] = {
1669 .cmd = NFC_CMD_GET_DEVICE,
1670 .doit = nfc_genl_get_device,
1671 .dumpit = nfc_genl_dump_devices,
1672 .done = nfc_genl_dump_devices_done,
1673 .policy = nfc_genl_policy,
1676 .cmd = NFC_CMD_DEV_UP,
1677 .doit = nfc_genl_dev_up,
1678 .policy = nfc_genl_policy,
1681 .cmd = NFC_CMD_DEV_DOWN,
1682 .doit = nfc_genl_dev_down,
1683 .policy = nfc_genl_policy,
1686 .cmd = NFC_CMD_START_POLL,
1687 .doit = nfc_genl_start_poll,
1688 .policy = nfc_genl_policy,
1691 .cmd = NFC_CMD_STOP_POLL,
1692 .doit = nfc_genl_stop_poll,
1693 .policy = nfc_genl_policy,
1696 .cmd = NFC_CMD_DEP_LINK_UP,
1697 .doit = nfc_genl_dep_link_up,
1698 .policy = nfc_genl_policy,
1701 .cmd = NFC_CMD_DEP_LINK_DOWN,
1702 .doit = nfc_genl_dep_link_down,
1703 .policy = nfc_genl_policy,
1706 .cmd = NFC_CMD_GET_TARGET,
1707 .dumpit = nfc_genl_dump_targets,
1708 .done = nfc_genl_dump_targets_done,
1709 .policy = nfc_genl_policy,
1712 .cmd = NFC_CMD_LLC_GET_PARAMS,
1713 .doit = nfc_genl_llc_get_params,
1714 .policy = nfc_genl_policy,
1717 .cmd = NFC_CMD_LLC_SET_PARAMS,
1718 .doit = nfc_genl_llc_set_params,
1719 .policy = nfc_genl_policy,
1722 .cmd = NFC_CMD_LLC_SDREQ,
1723 .doit = nfc_genl_llc_sdreq,
1724 .policy = nfc_genl_policy,
1727 .cmd = NFC_CMD_FW_DOWNLOAD,
1728 .doit = nfc_genl_fw_download,
1729 .policy = nfc_genl_policy,
1732 .cmd = NFC_CMD_ENABLE_SE,
1733 .doit = nfc_genl_enable_se,
1734 .policy = nfc_genl_policy,
1737 .cmd = NFC_CMD_DISABLE_SE,
1738 .doit = nfc_genl_disable_se,
1739 .policy = nfc_genl_policy,
1742 .cmd = NFC_CMD_GET_SE,
1743 .dumpit = nfc_genl_dump_ses,
1744 .done = nfc_genl_dump_ses_done,
1745 .policy = nfc_genl_policy,
1748 .cmd = NFC_CMD_SE_IO,
1749 .doit = nfc_genl_se_io,
1750 .policy = nfc_genl_policy,
1753 .cmd = NFC_CMD_ACTIVATE_TARGET,
1754 .doit = nfc_genl_activate_target,
1755 .policy = nfc_genl_policy,
1758 .cmd = NFC_CMD_VENDOR,
1759 .doit = nfc_genl_vendor_cmd,
1760 .policy = nfc_genl_policy,
1763 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1764 .doit = nfc_genl_deactivate_target,
1765 .policy = nfc_genl_policy,
1769 static struct genl_family nfc_genl_family __ro_after_init = {
1771 .name = NFC_GENL_NAME,
1772 .version = NFC_GENL_VERSION,
1773 .maxattr = NFC_ATTR_MAX,
1774 .module = THIS_MODULE,
1775 .ops = nfc_genl_ops,
1776 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1777 .mcgrps = nfc_genl_mcgrps,
1778 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1782 struct urelease_work {
1783 struct work_struct w;
1787 static void nfc_urelease_event_work(struct work_struct *work)
1789 struct urelease_work *w = container_of(work, struct urelease_work, w);
1790 struct class_dev_iter iter;
1791 struct nfc_dev *dev;
1793 pr_debug("portid %d\n", w->portid);
1795 mutex_lock(&nfc_devlist_mutex);
1797 nfc_device_iter_init(&iter);
1798 dev = nfc_device_iter_next(&iter);
1801 mutex_lock(&dev->genl_data.genl_data_mutex);
1803 if (dev->genl_data.poll_req_portid == w->portid) {
1805 dev->genl_data.poll_req_portid = 0;
1808 mutex_unlock(&dev->genl_data.genl_data_mutex);
1810 dev = nfc_device_iter_next(&iter);
1813 nfc_device_iter_exit(&iter);
1815 mutex_unlock(&nfc_devlist_mutex);
1820 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1821 unsigned long event, void *ptr)
1823 struct netlink_notify *n = ptr;
1824 struct urelease_work *w;
1826 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1829 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1831 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1833 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1834 w->portid = n->portid;
1835 schedule_work((struct work_struct *) w);
1842 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1844 genl_data->poll_req_portid = 0;
1845 mutex_init(&genl_data->genl_data_mutex);
1848 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1850 mutex_destroy(&genl_data->genl_data_mutex);
1853 static struct notifier_block nl_notifier = {
1854 .notifier_call = nfc_genl_rcv_nl_event,
1858 * nfc_genl_init() - Initialize netlink interface
1860 * This initialization function registers the nfc netlink family.
1862 int __init nfc_genl_init(void)
1866 rc = genl_register_family(&nfc_genl_family);
1870 netlink_register_notifier(&nl_notifier);
1876 * nfc_genl_exit() - Deinitialize netlink interface
1878 * This exit function unregisters the nfc netlink family.
1880 void nfc_genl_exit(void)
1882 netlink_unregister_notifier(&nl_notifier);
1883 genl_unregister_family(&nfc_genl_family);