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 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 },
65 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
68 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
69 struct netlink_callback *cb, int flags)
73 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
74 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
78 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
80 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
81 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
82 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
83 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
85 if (target->nfcid1_len > 0 &&
86 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
89 if (target->sensb_res_len > 0 &&
90 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
93 if (target->sensf_res_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
98 if (target->is_iso15693) {
99 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
100 target->iso15693_dsfid) ||
101 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
102 sizeof(target->iso15693_uid), target->iso15693_uid))
103 goto nla_put_failure;
106 genlmsg_end(msg, hdr);
110 genlmsg_cancel(msg, hdr);
114 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
116 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
121 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
122 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
127 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
128 return ERR_PTR(-EINVAL);
130 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
132 dev = nfc_get_device(idx);
134 return ERR_PTR(-ENODEV);
139 static int nfc_genl_dump_targets(struct sk_buff *skb,
140 struct netlink_callback *cb)
143 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
147 dev = __get_device_from_cb(cb);
151 cb->args[1] = (long) dev;
154 device_lock(&dev->dev);
156 cb->seq = dev->targets_generation;
158 while (i < dev->n_targets) {
159 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
167 device_unlock(&dev->dev);
174 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
176 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
184 int nfc_genl_targets_found(struct nfc_dev *dev)
189 dev->genl_data.poll_req_portid = 0;
191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 NFC_EVENT_TARGETS_FOUND);
200 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
201 goto nla_put_failure;
203 genlmsg_end(msg, hdr);
205 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
208 genlmsg_cancel(msg, hdr);
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);
239 genlmsg_cancel(msg, hdr);
245 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
254 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
255 NFC_EVENT_TM_ACTIVATED);
259 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
260 goto nla_put_failure;
261 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
262 goto nla_put_failure;
264 genlmsg_end(msg, hdr);
266 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
271 genlmsg_cancel(msg, hdr);
277 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
286 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
287 NFC_EVENT_TM_DEACTIVATED);
291 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
292 goto nla_put_failure;
294 genlmsg_end(msg, hdr);
296 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
301 genlmsg_cancel(msg, hdr);
307 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
309 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
310 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
311 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
312 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
313 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
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 (nfc_genl_setup_device_added(dev, msg))
333 goto nla_put_failure;
335 genlmsg_end(msg, hdr);
337 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
342 genlmsg_cancel(msg, hdr);
348 int nfc_genl_device_removed(struct nfc_dev *dev)
353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
357 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
358 NFC_EVENT_DEVICE_REMOVED);
362 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
363 goto nla_put_failure;
365 genlmsg_end(msg, hdr);
367 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
372 genlmsg_cancel(msg, hdr);
378 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
381 struct nlattr *sdp_attr, *uri_attr;
382 struct nfc_llcp_sdp_tlv *sdres;
383 struct hlist_node *n;
388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
392 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
393 NFC_EVENT_LLC_SDRES);
397 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
398 goto nla_put_failure;
400 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
401 if (sdp_attr == NULL) {
403 goto nla_put_failure;
407 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
408 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
410 uri_attr = nla_nest_start(msg, i++);
411 if (uri_attr == NULL) {
413 goto nla_put_failure;
416 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
417 goto nla_put_failure;
419 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
420 goto nla_put_failure;
422 nla_nest_end(msg, uri_attr);
424 hlist_del(&sdres->node);
426 nfc_llcp_free_sdp_tlv(sdres);
429 nla_nest_end(msg, sdp_attr);
431 genlmsg_end(msg, hdr);
433 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
436 genlmsg_cancel(msg, hdr);
441 nfc_llcp_free_sdp_tlv_list(sdres_list);
446 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
455 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
460 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
461 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
462 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
463 goto nla_put_failure;
465 genlmsg_end(msg, hdr);
467 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
472 genlmsg_cancel(msg, hdr);
478 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
487 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
488 NFC_EVENT_SE_REMOVED);
492 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
493 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
494 goto nla_put_failure;
496 genlmsg_end(msg, hdr);
498 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
503 genlmsg_cancel(msg, hdr);
509 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
510 struct nfc_evt_transaction *evt_transaction)
516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
520 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
521 NFC_EVENT_SE_TRANSACTION);
525 se = nfc_find_se(dev, se_idx);
529 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
530 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
531 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
532 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
533 evt_transaction->aid) ||
534 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
535 evt_transaction->params))
536 goto nla_put_failure;
538 /* evt_transaction is no more used */
539 devm_kfree(&dev->dev, evt_transaction);
541 genlmsg_end(msg, hdr);
543 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
548 genlmsg_cancel(msg, hdr);
550 /* evt_transaction is no more used */
551 devm_kfree(&dev->dev, evt_transaction);
556 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
566 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
567 NFC_EVENT_SE_CONNECTIVITY);
571 se = nfc_find_se(dev, se_idx);
575 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
576 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
577 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
578 goto nla_put_failure;
580 genlmsg_end(msg, hdr);
582 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
587 genlmsg_cancel(msg, hdr);
593 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
595 struct netlink_callback *cb,
600 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
606 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
608 if (nfc_genl_setup_device_added(dev, msg))
609 goto nla_put_failure;
611 genlmsg_end(msg, hdr);
615 genlmsg_cancel(msg, hdr);
619 static int nfc_genl_dump_devices(struct sk_buff *skb,
620 struct netlink_callback *cb)
622 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
623 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
624 bool first_call = false;
628 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
631 cb->args[0] = (long) iter;
634 mutex_lock(&nfc_devlist_mutex);
636 cb->seq = nfc_devlist_generation;
639 nfc_device_iter_init(iter);
640 dev = nfc_device_iter_next(iter);
646 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
647 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
651 dev = nfc_device_iter_next(iter);
654 mutex_unlock(&nfc_devlist_mutex);
656 cb->args[1] = (long) dev;
661 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
663 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
665 nfc_device_iter_exit(iter);
671 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
672 u8 comm_mode, u8 rf_mode)
677 pr_debug("DEP link is up\n");
679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
683 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
687 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
688 goto nla_put_failure;
689 if (rf_mode == NFC_RF_INITIATOR &&
690 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
691 goto nla_put_failure;
692 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
693 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
694 goto nla_put_failure;
696 genlmsg_end(msg, hdr);
698 dev->dep_link_up = true;
700 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
705 genlmsg_cancel(msg, hdr);
711 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
716 pr_debug("DEP link is down\n");
718 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
722 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
723 NFC_CMD_DEP_LINK_DOWN);
727 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
728 goto nla_put_failure;
730 genlmsg_end(msg, hdr);
732 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
737 genlmsg_cancel(msg, hdr);
743 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
750 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
753 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
755 dev = nfc_get_device(idx);
759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
765 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
772 return genlmsg_reply(msg, info);
781 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
787 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
790 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
792 dev = nfc_get_device(idx);
796 rc = nfc_dev_up(dev);
802 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
808 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
811 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
813 dev = nfc_get_device(idx);
817 rc = nfc_dev_down(dev);
823 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
828 u32 im_protocols = 0, tm_protocols = 0;
830 pr_debug("Poll start\n");
832 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
833 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
834 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
835 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
838 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
840 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
841 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
843 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
844 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
845 else if (info->attrs[NFC_ATTR_PROTOCOLS])
846 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
848 dev = nfc_get_device(idx);
852 mutex_lock(&dev->genl_data.genl_data_mutex);
854 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
856 dev->genl_data.poll_req_portid = info->snd_portid;
858 mutex_unlock(&dev->genl_data.genl_data_mutex);
864 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
870 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
873 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
875 dev = nfc_get_device(idx);
879 device_lock(&dev->dev);
882 device_unlock(&dev->dev);
886 device_unlock(&dev->dev);
888 mutex_lock(&dev->genl_data.genl_data_mutex);
890 if (dev->genl_data.poll_req_portid != info->snd_portid) {
895 rc = nfc_stop_poll(dev);
896 dev->genl_data.poll_req_portid = 0;
899 mutex_unlock(&dev->genl_data.genl_data_mutex);
904 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
907 u32 device_idx, target_idx, protocol;
910 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
913 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
915 dev = nfc_get_device(device_idx);
919 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
920 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
922 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
923 rc = nfc_activate_target(dev, target_idx, protocol);
929 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
936 pr_debug("DEP link up\n");
938 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
939 !info->attrs[NFC_ATTR_COMM_MODE])
942 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
943 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
944 tgt_idx = NFC_TARGET_IDX_ANY;
946 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
948 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
950 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
953 dev = nfc_get_device(idx);
957 rc = nfc_dep_link_up(dev, tgt_idx, comm);
964 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
970 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
973 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
975 dev = nfc_get_device(idx);
979 rc = nfc_dep_link_down(dev);
985 static int nfc_genl_send_params(struct sk_buff *msg,
986 struct nfc_llcp_local *local,
991 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
992 NFC_CMD_LLC_GET_PARAMS);
996 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
997 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
998 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
999 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1000 goto nla_put_failure;
1002 genlmsg_end(msg, hdr);
1007 genlmsg_cancel(msg, hdr);
1011 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1013 struct nfc_dev *dev;
1014 struct nfc_llcp_local *local;
1016 struct sk_buff *msg = NULL;
1019 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1022 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1024 dev = nfc_get_device(idx);
1028 device_lock(&dev->dev);
1030 local = nfc_llcp_find_local(dev);
1036 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1042 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1045 device_unlock(&dev->dev);
1047 nfc_put_device(dev);
1056 return genlmsg_reply(msg, info);
1059 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1061 struct nfc_dev *dev;
1062 struct nfc_llcp_local *local;
1068 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1069 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1070 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1071 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1074 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1075 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1077 if (rw > LLCP_MAX_RW)
1081 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1082 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1084 if (miux > LLCP_MAX_MIUX)
1088 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1090 dev = nfc_get_device(idx);
1094 device_lock(&dev->dev);
1096 local = nfc_llcp_find_local(dev);
1098 nfc_put_device(dev);
1103 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1104 if (dev->dep_link_up) {
1109 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1112 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1115 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1116 local->miux = cpu_to_be16(miux);
1119 device_unlock(&dev->dev);
1121 nfc_put_device(dev);
1126 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1128 struct nfc_dev *dev;
1129 struct nfc_llcp_local *local;
1130 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1135 size_t uri_len, tlvs_len;
1136 struct hlist_head sdreq_list;
1137 struct nfc_llcp_sdp_tlv *sdreq;
1139 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1140 !info->attrs[NFC_ATTR_LLC_SDP])
1143 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1145 dev = nfc_get_device(idx);
1149 device_lock(&dev->dev);
1151 if (dev->dep_link_up == false) {
1156 local = nfc_llcp_find_local(dev);
1158 nfc_put_device(dev);
1163 INIT_HLIST_HEAD(&sdreq_list);
1167 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1168 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1169 nfc_sdp_genl_policy, info->extack);
1176 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1179 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1183 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1184 if (uri == NULL || *uri == 0)
1187 tid = local->sdreq_next_tid++;
1189 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1190 if (sdreq == NULL) {
1195 tlvs_len += sdreq->tlv_len;
1197 hlist_add_head(&sdreq->node, &sdreq_list);
1200 if (hlist_empty(&sdreq_list)) {
1205 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1207 device_unlock(&dev->dev);
1209 nfc_put_device(dev);
1214 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1216 struct nfc_dev *dev;
1219 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1221 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1224 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1226 dev = nfc_get_device(idx);
1230 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1231 sizeof(firmware_name));
1233 rc = nfc_fw_download(dev, firmware_name);
1235 nfc_put_device(dev);
1239 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1242 struct sk_buff *msg;
1245 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1249 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1250 NFC_CMD_FW_DOWNLOAD);
1254 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1255 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1256 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1257 goto nla_put_failure;
1259 genlmsg_end(msg, hdr);
1261 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1266 genlmsg_cancel(msg, hdr);
1272 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1274 struct nfc_dev *dev;
1278 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1279 !info->attrs[NFC_ATTR_SE_INDEX])
1282 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1283 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1285 dev = nfc_get_device(idx);
1289 rc = nfc_enable_se(dev, se_idx);
1291 nfc_put_device(dev);
1295 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1297 struct nfc_dev *dev;
1301 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1302 !info->attrs[NFC_ATTR_SE_INDEX])
1305 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1306 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1308 dev = nfc_get_device(idx);
1312 rc = nfc_disable_se(dev, se_idx);
1314 nfc_put_device(dev);
1318 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1319 u32 portid, u32 seq,
1320 struct netlink_callback *cb,
1324 struct nfc_se *se, *n;
1326 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1327 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1330 goto nla_put_failure;
1333 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1335 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1336 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1337 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1338 goto nla_put_failure;
1340 genlmsg_end(msg, hdr);
1346 genlmsg_cancel(msg, hdr);
1350 static int nfc_genl_dump_ses(struct sk_buff *skb,
1351 struct netlink_callback *cb)
1353 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1354 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1355 bool first_call = false;
1359 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1362 cb->args[0] = (long) iter;
1365 mutex_lock(&nfc_devlist_mutex);
1367 cb->seq = nfc_devlist_generation;
1370 nfc_device_iter_init(iter);
1371 dev = nfc_device_iter_next(iter);
1377 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1378 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1382 dev = nfc_device_iter_next(iter);
1385 mutex_unlock(&nfc_devlist_mutex);
1387 cb->args[1] = (long) dev;
1392 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1394 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1396 nfc_device_iter_exit(iter);
1402 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1403 u8 *apdu, size_t apdu_length,
1404 se_io_cb_t cb, void *cb_context)
1409 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1411 device_lock(&dev->dev);
1413 if (!device_is_registered(&dev->dev)) {
1423 if (!dev->ops->se_io) {
1428 se = nfc_find_se(dev, se_idx);
1434 if (se->state != NFC_SE_ENABLED) {
1439 rc = dev->ops->se_io(dev, se_idx, apdu,
1440 apdu_length, cb, cb_context);
1443 device_unlock(&dev->dev);
1452 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1454 struct se_io_ctx *ctx = context;
1455 struct sk_buff *msg;
1458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1464 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1469 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1470 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1471 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1472 goto nla_put_failure;
1474 genlmsg_end(msg, hdr);
1476 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1483 genlmsg_cancel(msg, hdr);
1491 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1493 struct nfc_dev *dev;
1494 struct se_io_ctx *ctx;
1495 u32 dev_idx, se_idx;
1499 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1500 !info->attrs[NFC_ATTR_SE_INDEX] ||
1501 !info->attrs[NFC_ATTR_SE_APDU])
1504 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1505 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1507 dev = nfc_get_device(dev_idx);
1511 if (!dev->ops || !dev->ops->se_io)
1514 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1518 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1522 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1526 ctx->dev_idx = dev_idx;
1527 ctx->se_idx = se_idx;
1529 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1532 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1533 struct genl_info *info)
1535 struct nfc_dev *dev;
1536 struct nfc_vendor_cmd *cmd;
1537 u32 dev_idx, vid, subcmd;
1542 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1543 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1544 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1547 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1548 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1549 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1551 dev = nfc_get_device(dev_idx);
1552 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1555 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1556 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1557 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1565 for (i = 0; i < dev->n_vendor_cmds; i++) {
1566 cmd = &dev->vendor_cmds[i];
1568 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1571 dev->cur_cmd_info = info;
1572 err = cmd->doit(dev, data, data_len);
1573 dev->cur_cmd_info = NULL;
1580 /* message building helper */
1581 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1584 /* since there is no private header just add the generic one */
1585 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1588 static struct sk_buff *
1589 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1590 u32 portid, u32 seq,
1591 enum nfc_attrs attr,
1592 u32 oui, u32 subcmd, gfp_t gfp)
1594 struct sk_buff *skb;
1597 skb = nlmsg_new(approxlen + 100, gfp);
1601 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1607 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1608 goto nla_put_failure;
1609 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1610 goto nla_put_failure;
1611 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1612 goto nla_put_failure;
1614 ((void **)skb->cb)[0] = dev;
1615 ((void **)skb->cb)[1] = hdr;
1624 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1625 enum nfc_attrs attr,
1626 u32 oui, u32 subcmd,
1629 if (WARN_ON(!dev->cur_cmd_info))
1632 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1633 dev->cur_cmd_info->snd_portid,
1634 dev->cur_cmd_info->snd_seq, attr,
1635 oui, subcmd, GFP_KERNEL);
1637 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1639 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1641 struct nfc_dev *dev = ((void **)skb->cb)[0];
1642 void *hdr = ((void **)skb->cb)[1];
1644 /* clear CB data for netlink core to own from now on */
1645 memset(skb->cb, 0, sizeof(skb->cb));
1647 if (WARN_ON(!dev->cur_cmd_info)) {
1652 genlmsg_end(skb, hdr);
1653 return genlmsg_reply(skb, dev->cur_cmd_info);
1655 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1657 static const struct genl_ops nfc_genl_ops[] = {
1659 .cmd = NFC_CMD_GET_DEVICE,
1660 .doit = nfc_genl_get_device,
1661 .dumpit = nfc_genl_dump_devices,
1662 .done = nfc_genl_dump_devices_done,
1663 .policy = nfc_genl_policy,
1666 .cmd = NFC_CMD_DEV_UP,
1667 .doit = nfc_genl_dev_up,
1668 .policy = nfc_genl_policy,
1671 .cmd = NFC_CMD_DEV_DOWN,
1672 .doit = nfc_genl_dev_down,
1673 .policy = nfc_genl_policy,
1676 .cmd = NFC_CMD_START_POLL,
1677 .doit = nfc_genl_start_poll,
1678 .policy = nfc_genl_policy,
1681 .cmd = NFC_CMD_STOP_POLL,
1682 .doit = nfc_genl_stop_poll,
1683 .policy = nfc_genl_policy,
1686 .cmd = NFC_CMD_DEP_LINK_UP,
1687 .doit = nfc_genl_dep_link_up,
1688 .policy = nfc_genl_policy,
1691 .cmd = NFC_CMD_DEP_LINK_DOWN,
1692 .doit = nfc_genl_dep_link_down,
1693 .policy = nfc_genl_policy,
1696 .cmd = NFC_CMD_GET_TARGET,
1697 .dumpit = nfc_genl_dump_targets,
1698 .done = nfc_genl_dump_targets_done,
1699 .policy = nfc_genl_policy,
1702 .cmd = NFC_CMD_LLC_GET_PARAMS,
1703 .doit = nfc_genl_llc_get_params,
1704 .policy = nfc_genl_policy,
1707 .cmd = NFC_CMD_LLC_SET_PARAMS,
1708 .doit = nfc_genl_llc_set_params,
1709 .policy = nfc_genl_policy,
1712 .cmd = NFC_CMD_LLC_SDREQ,
1713 .doit = nfc_genl_llc_sdreq,
1714 .policy = nfc_genl_policy,
1717 .cmd = NFC_CMD_FW_DOWNLOAD,
1718 .doit = nfc_genl_fw_download,
1719 .policy = nfc_genl_policy,
1722 .cmd = NFC_CMD_ENABLE_SE,
1723 .doit = nfc_genl_enable_se,
1724 .policy = nfc_genl_policy,
1727 .cmd = NFC_CMD_DISABLE_SE,
1728 .doit = nfc_genl_disable_se,
1729 .policy = nfc_genl_policy,
1732 .cmd = NFC_CMD_GET_SE,
1733 .dumpit = nfc_genl_dump_ses,
1734 .done = nfc_genl_dump_ses_done,
1735 .policy = nfc_genl_policy,
1738 .cmd = NFC_CMD_SE_IO,
1739 .doit = nfc_genl_se_io,
1740 .policy = nfc_genl_policy,
1743 .cmd = NFC_CMD_ACTIVATE_TARGET,
1744 .doit = nfc_genl_activate_target,
1745 .policy = nfc_genl_policy,
1748 .cmd = NFC_CMD_VENDOR,
1749 .doit = nfc_genl_vendor_cmd,
1750 .policy = nfc_genl_policy,
1754 static struct genl_family nfc_genl_family __ro_after_init = {
1756 .name = NFC_GENL_NAME,
1757 .version = NFC_GENL_VERSION,
1758 .maxattr = NFC_ATTR_MAX,
1759 .module = THIS_MODULE,
1760 .ops = nfc_genl_ops,
1761 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1762 .mcgrps = nfc_genl_mcgrps,
1763 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1767 struct urelease_work {
1768 struct work_struct w;
1772 static void nfc_urelease_event_work(struct work_struct *work)
1774 struct urelease_work *w = container_of(work, struct urelease_work, w);
1775 struct class_dev_iter iter;
1776 struct nfc_dev *dev;
1778 pr_debug("portid %d\n", w->portid);
1780 mutex_lock(&nfc_devlist_mutex);
1782 nfc_device_iter_init(&iter);
1783 dev = nfc_device_iter_next(&iter);
1786 mutex_lock(&dev->genl_data.genl_data_mutex);
1788 if (dev->genl_data.poll_req_portid == w->portid) {
1790 dev->genl_data.poll_req_portid = 0;
1793 mutex_unlock(&dev->genl_data.genl_data_mutex);
1795 dev = nfc_device_iter_next(&iter);
1798 nfc_device_iter_exit(&iter);
1800 mutex_unlock(&nfc_devlist_mutex);
1805 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1806 unsigned long event, void *ptr)
1808 struct netlink_notify *n = ptr;
1809 struct urelease_work *w;
1811 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1814 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1816 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1818 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1819 w->portid = n->portid;
1820 schedule_work((struct work_struct *) w);
1827 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1829 genl_data->poll_req_portid = 0;
1830 mutex_init(&genl_data->genl_data_mutex);
1833 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1835 mutex_destroy(&genl_data->genl_data_mutex);
1838 static struct notifier_block nl_notifier = {
1839 .notifier_call = nfc_genl_rcv_nl_event,
1843 * nfc_genl_init() - Initialize netlink interface
1845 * This initialization function registers the nfc netlink family.
1847 int __init nfc_genl_init(void)
1851 rc = genl_register_family(&nfc_genl_family);
1855 netlink_register_notifier(&nl_notifier);
1861 * nfc_genl_exit() - Deinitialize netlink interface
1863 * This exit function unregisters the nfc netlink family.
1865 void nfc_genl_exit(void)
1867 netlink_unregister_notifier(&nl_notifier);
1868 genl_unregister_family(&nfc_genl_family);