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_APDU] = { .type = NLA_BINARY },
68 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
69 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
70 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
73 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
74 struct netlink_callback *cb, int flags)
78 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
79 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
83 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
85 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
86 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
87 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
88 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
90 if (target->nfcid1_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
94 if (target->sensb_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
98 if (target->sensf_res_len > 0 &&
99 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
101 goto nla_put_failure;
103 if (target->is_iso15693) {
104 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
105 target->iso15693_dsfid) ||
106 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
107 sizeof(target->iso15693_uid), target->iso15693_uid))
108 goto nla_put_failure;
111 genlmsg_end(msg, hdr);
115 genlmsg_cancel(msg, hdr);
119 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
125 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
126 nfc_genl_family.attrbuf,
127 nfc_genl_family.maxattr,
132 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
133 return ERR_PTR(-EINVAL);
135 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
137 dev = nfc_get_device(idx);
139 return ERR_PTR(-ENODEV);
144 static int nfc_genl_dump_targets(struct sk_buff *skb,
145 struct netlink_callback *cb)
148 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
152 dev = __get_device_from_cb(cb);
156 cb->args[1] = (long) dev;
159 device_lock(&dev->dev);
161 cb->seq = dev->targets_generation;
163 while (i < dev->n_targets) {
164 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
172 device_unlock(&dev->dev);
179 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
181 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
189 int nfc_genl_targets_found(struct nfc_dev *dev)
194 dev->genl_data.poll_req_portid = 0;
196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
200 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
201 NFC_EVENT_TARGETS_FOUND);
205 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
206 goto nla_put_failure;
208 genlmsg_end(msg, hdr);
210 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
213 genlmsg_cancel(msg, hdr);
219 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
228 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
229 NFC_EVENT_TARGET_LOST);
233 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
234 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
235 goto nla_put_failure;
237 genlmsg_end(msg, hdr);
239 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
244 genlmsg_cancel(msg, hdr);
250 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
255 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
259 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
260 NFC_EVENT_TM_ACTIVATED);
264 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
265 goto nla_put_failure;
266 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
267 goto nla_put_failure;
269 genlmsg_end(msg, hdr);
271 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
276 genlmsg_cancel(msg, hdr);
282 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
291 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
292 NFC_EVENT_TM_DEACTIVATED);
296 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
297 goto nla_put_failure;
299 genlmsg_end(msg, hdr);
301 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
306 genlmsg_cancel(msg, hdr);
312 int nfc_genl_device_added(struct nfc_dev *dev)
317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
321 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
322 NFC_EVENT_DEVICE_ADDED);
326 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
327 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
328 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
329 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
330 goto nla_put_failure;
332 genlmsg_end(msg, hdr);
334 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
339 genlmsg_cancel(msg, hdr);
345 int nfc_genl_device_removed(struct nfc_dev *dev)
350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
354 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
355 NFC_EVENT_DEVICE_REMOVED);
359 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
360 goto nla_put_failure;
362 genlmsg_end(msg, hdr);
364 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
369 genlmsg_cancel(msg, hdr);
375 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
378 struct nlattr *sdp_attr, *uri_attr;
379 struct nfc_llcp_sdp_tlv *sdres;
380 struct hlist_node *n;
385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
389 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
390 NFC_EVENT_LLC_SDRES);
394 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
395 goto nla_put_failure;
397 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
398 if (sdp_attr == NULL) {
400 goto nla_put_failure;
404 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
405 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
407 uri_attr = nla_nest_start(msg, i++);
408 if (uri_attr == NULL) {
410 goto nla_put_failure;
413 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
414 goto nla_put_failure;
416 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
417 goto nla_put_failure;
419 nla_nest_end(msg, uri_attr);
421 hlist_del(&sdres->node);
423 nfc_llcp_free_sdp_tlv(sdres);
426 nla_nest_end(msg, sdp_attr);
428 genlmsg_end(msg, hdr);
430 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
433 genlmsg_cancel(msg, hdr);
438 nfc_llcp_free_sdp_tlv_list(sdres_list);
443 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
452 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
457 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
458 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
459 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
460 goto nla_put_failure;
462 genlmsg_end(msg, hdr);
464 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
469 genlmsg_cancel(msg, hdr);
475 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
484 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
485 NFC_EVENT_SE_REMOVED);
489 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
490 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
491 goto nla_put_failure;
493 genlmsg_end(msg, hdr);
495 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
500 genlmsg_cancel(msg, hdr);
506 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
507 struct nfc_evt_transaction *evt_transaction)
513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
517 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
518 NFC_EVENT_SE_TRANSACTION);
522 se = nfc_find_se(dev, se_idx);
526 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
527 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
528 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
529 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
530 evt_transaction->aid) ||
531 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
532 evt_transaction->params))
533 goto nla_put_failure;
535 /* evt_transaction is no more used */
536 devm_kfree(&dev->dev, evt_transaction);
538 genlmsg_end(msg, hdr);
540 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
545 genlmsg_cancel(msg, hdr);
547 /* evt_transaction is no more used */
548 devm_kfree(&dev->dev, evt_transaction);
553 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
555 struct netlink_callback *cb,
560 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
566 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
568 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
569 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
570 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
571 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
572 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
573 goto nla_put_failure;
575 genlmsg_end(msg, hdr);
579 genlmsg_cancel(msg, hdr);
583 static int nfc_genl_dump_devices(struct sk_buff *skb,
584 struct netlink_callback *cb)
586 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
587 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
588 bool first_call = false;
592 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
595 cb->args[0] = (long) iter;
598 mutex_lock(&nfc_devlist_mutex);
600 cb->seq = nfc_devlist_generation;
603 nfc_device_iter_init(iter);
604 dev = nfc_device_iter_next(iter);
610 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
611 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
615 dev = nfc_device_iter_next(iter);
618 mutex_unlock(&nfc_devlist_mutex);
620 cb->args[1] = (long) dev;
625 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
627 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
629 nfc_device_iter_exit(iter);
635 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
636 u8 comm_mode, u8 rf_mode)
641 pr_debug("DEP link is up\n");
643 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
647 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
651 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
652 goto nla_put_failure;
653 if (rf_mode == NFC_RF_INITIATOR &&
654 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
655 goto nla_put_failure;
656 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
657 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
658 goto nla_put_failure;
660 genlmsg_end(msg, hdr);
662 dev->dep_link_up = true;
664 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
669 genlmsg_cancel(msg, hdr);
675 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
680 pr_debug("DEP link is down\n");
682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
686 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
687 NFC_CMD_DEP_LINK_DOWN);
691 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
692 goto nla_put_failure;
694 genlmsg_end(msg, hdr);
696 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
701 genlmsg_cancel(msg, hdr);
707 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
714 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
717 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
719 dev = nfc_get_device(idx);
723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
729 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
736 return genlmsg_reply(msg, info);
745 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
754 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
756 dev = nfc_get_device(idx);
760 rc = nfc_dev_up(dev);
766 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
772 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
775 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
777 dev = nfc_get_device(idx);
781 rc = nfc_dev_down(dev);
787 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
792 u32 im_protocols = 0, tm_protocols = 0;
794 pr_debug("Poll start\n");
796 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
797 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
798 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
799 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
802 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
804 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
805 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
807 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
808 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
809 else if (info->attrs[NFC_ATTR_PROTOCOLS])
810 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
812 dev = nfc_get_device(idx);
816 mutex_lock(&dev->genl_data.genl_data_mutex);
818 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
820 dev->genl_data.poll_req_portid = info->snd_portid;
822 mutex_unlock(&dev->genl_data.genl_data_mutex);
828 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
834 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
837 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
839 dev = nfc_get_device(idx);
843 device_lock(&dev->dev);
846 device_unlock(&dev->dev);
850 device_unlock(&dev->dev);
852 mutex_lock(&dev->genl_data.genl_data_mutex);
854 if (dev->genl_data.poll_req_portid != info->snd_portid) {
859 rc = nfc_stop_poll(dev);
860 dev->genl_data.poll_req_portid = 0;
863 mutex_unlock(&dev->genl_data.genl_data_mutex);
868 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
871 u32 device_idx, target_idx, protocol;
874 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
877 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
879 dev = nfc_get_device(device_idx);
883 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
884 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
886 nfc_deactivate_target(dev, target_idx);
887 rc = nfc_activate_target(dev, target_idx, protocol);
893 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
900 pr_debug("DEP link up\n");
902 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
903 !info->attrs[NFC_ATTR_COMM_MODE])
906 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
907 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
908 tgt_idx = NFC_TARGET_IDX_ANY;
910 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
912 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
914 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
917 dev = nfc_get_device(idx);
921 rc = nfc_dep_link_up(dev, tgt_idx, comm);
928 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
934 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
937 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
939 dev = nfc_get_device(idx);
943 rc = nfc_dep_link_down(dev);
949 static int nfc_genl_send_params(struct sk_buff *msg,
950 struct nfc_llcp_local *local,
955 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
956 NFC_CMD_LLC_GET_PARAMS);
960 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
961 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
962 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
963 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
964 goto nla_put_failure;
966 genlmsg_end(msg, hdr);
971 genlmsg_cancel(msg, hdr);
975 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
978 struct nfc_llcp_local *local;
980 struct sk_buff *msg = NULL;
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 device_lock(&dev->dev);
994 local = nfc_llcp_find_local(dev);
1000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1006 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1009 device_unlock(&dev->dev);
1011 nfc_put_device(dev);
1020 return genlmsg_reply(msg, info);
1023 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1025 struct nfc_dev *dev;
1026 struct nfc_llcp_local *local;
1032 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1033 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1034 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1035 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1038 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1039 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1041 if (rw > LLCP_MAX_RW)
1045 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1046 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1048 if (miux > LLCP_MAX_MIUX)
1052 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1054 dev = nfc_get_device(idx);
1058 device_lock(&dev->dev);
1060 local = nfc_llcp_find_local(dev);
1062 nfc_put_device(dev);
1067 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1068 if (dev->dep_link_up) {
1073 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1076 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1079 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1080 local->miux = cpu_to_be16(miux);
1083 device_unlock(&dev->dev);
1085 nfc_put_device(dev);
1090 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1092 struct nfc_dev *dev;
1093 struct nfc_llcp_local *local;
1094 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1099 size_t uri_len, tlvs_len;
1100 struct hlist_head sdreq_list;
1101 struct nfc_llcp_sdp_tlv *sdreq;
1103 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1104 !info->attrs[NFC_ATTR_LLC_SDP])
1107 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1109 dev = nfc_get_device(idx);
1115 device_lock(&dev->dev);
1117 if (dev->dep_link_up == false) {
1122 local = nfc_llcp_find_local(dev);
1124 nfc_put_device(dev);
1129 INIT_HLIST_HEAD(&sdreq_list);
1133 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1134 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1135 nfc_sdp_genl_policy);
1142 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1145 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1149 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1150 if (uri == NULL || *uri == 0)
1153 tid = local->sdreq_next_tid++;
1155 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1156 if (sdreq == NULL) {
1161 tlvs_len += sdreq->tlv_len;
1163 hlist_add_head(&sdreq->node, &sdreq_list);
1166 if (hlist_empty(&sdreq_list)) {
1171 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1173 device_unlock(&dev->dev);
1175 nfc_put_device(dev);
1180 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1182 struct nfc_dev *dev;
1185 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1187 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1190 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1192 dev = nfc_get_device(idx);
1196 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1197 sizeof(firmware_name));
1199 rc = nfc_fw_download(dev, firmware_name);
1201 nfc_put_device(dev);
1205 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1208 struct sk_buff *msg;
1211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1216 NFC_CMD_FW_DOWNLOAD);
1220 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1221 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1222 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1223 goto nla_put_failure;
1225 genlmsg_end(msg, hdr);
1227 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1232 genlmsg_cancel(msg, hdr);
1238 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1240 struct nfc_dev *dev;
1244 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1245 !info->attrs[NFC_ATTR_SE_INDEX])
1248 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1249 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1251 dev = nfc_get_device(idx);
1255 rc = nfc_enable_se(dev, se_idx);
1257 nfc_put_device(dev);
1261 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1263 struct nfc_dev *dev;
1267 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1268 !info->attrs[NFC_ATTR_SE_INDEX])
1271 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1272 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1274 dev = nfc_get_device(idx);
1278 rc = nfc_disable_se(dev, se_idx);
1280 nfc_put_device(dev);
1284 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1285 u32 portid, u32 seq,
1286 struct netlink_callback *cb,
1290 struct nfc_se *se, *n;
1292 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1293 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1296 goto nla_put_failure;
1299 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1301 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1302 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1303 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1304 goto nla_put_failure;
1306 genlmsg_end(msg, hdr);
1312 genlmsg_cancel(msg, hdr);
1316 static int nfc_genl_dump_ses(struct sk_buff *skb,
1317 struct netlink_callback *cb)
1319 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1320 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1321 bool first_call = false;
1325 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1328 cb->args[0] = (long) iter;
1331 mutex_lock(&nfc_devlist_mutex);
1333 cb->seq = nfc_devlist_generation;
1336 nfc_device_iter_init(iter);
1337 dev = nfc_device_iter_next(iter);
1343 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1344 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1348 dev = nfc_device_iter_next(iter);
1351 mutex_unlock(&nfc_devlist_mutex);
1353 cb->args[1] = (long) dev;
1358 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1360 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1362 nfc_device_iter_exit(iter);
1368 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1369 u8 *apdu, size_t apdu_length,
1370 se_io_cb_t cb, void *cb_context)
1375 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1377 device_lock(&dev->dev);
1379 if (!device_is_registered(&dev->dev)) {
1389 if (!dev->ops->se_io) {
1394 se = nfc_find_se(dev, se_idx);
1400 if (se->state != NFC_SE_ENABLED) {
1405 rc = dev->ops->se_io(dev, se_idx, apdu,
1406 apdu_length, cb, cb_context);
1409 device_unlock(&dev->dev);
1418 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1420 struct se_io_ctx *ctx = context;
1421 struct sk_buff *msg;
1424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1430 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1435 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1436 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1437 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1438 goto nla_put_failure;
1440 genlmsg_end(msg, hdr);
1442 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1449 genlmsg_cancel(msg, hdr);
1457 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1459 struct nfc_dev *dev;
1460 struct se_io_ctx *ctx;
1461 u32 dev_idx, se_idx;
1465 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1466 !info->attrs[NFC_ATTR_SE_INDEX] ||
1467 !info->attrs[NFC_ATTR_SE_APDU])
1470 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1471 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1473 dev = nfc_get_device(dev_idx);
1477 if (!dev->ops || !dev->ops->se_io)
1480 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1484 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1488 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1492 ctx->dev_idx = dev_idx;
1493 ctx->se_idx = se_idx;
1495 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1498 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1499 struct genl_info *info)
1501 struct nfc_dev *dev;
1502 struct nfc_vendor_cmd *cmd;
1503 u32 dev_idx, vid, subcmd;
1508 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1509 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1510 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1513 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1514 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1515 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1517 dev = nfc_get_device(dev_idx);
1518 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1521 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1523 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1530 for (i = 0; i < dev->n_vendor_cmds; i++) {
1531 cmd = &dev->vendor_cmds[i];
1533 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1536 return cmd->doit(dev, data, data_len);
1542 static const struct genl_ops nfc_genl_ops[] = {
1544 .cmd = NFC_CMD_GET_DEVICE,
1545 .doit = nfc_genl_get_device,
1546 .dumpit = nfc_genl_dump_devices,
1547 .done = nfc_genl_dump_devices_done,
1548 .policy = nfc_genl_policy,
1551 .cmd = NFC_CMD_DEV_UP,
1552 .doit = nfc_genl_dev_up,
1553 .policy = nfc_genl_policy,
1556 .cmd = NFC_CMD_DEV_DOWN,
1557 .doit = nfc_genl_dev_down,
1558 .policy = nfc_genl_policy,
1561 .cmd = NFC_CMD_START_POLL,
1562 .doit = nfc_genl_start_poll,
1563 .policy = nfc_genl_policy,
1566 .cmd = NFC_CMD_STOP_POLL,
1567 .doit = nfc_genl_stop_poll,
1568 .policy = nfc_genl_policy,
1571 .cmd = NFC_CMD_DEP_LINK_UP,
1572 .doit = nfc_genl_dep_link_up,
1573 .policy = nfc_genl_policy,
1576 .cmd = NFC_CMD_DEP_LINK_DOWN,
1577 .doit = nfc_genl_dep_link_down,
1578 .policy = nfc_genl_policy,
1581 .cmd = NFC_CMD_GET_TARGET,
1582 .dumpit = nfc_genl_dump_targets,
1583 .done = nfc_genl_dump_targets_done,
1584 .policy = nfc_genl_policy,
1587 .cmd = NFC_CMD_LLC_GET_PARAMS,
1588 .doit = nfc_genl_llc_get_params,
1589 .policy = nfc_genl_policy,
1592 .cmd = NFC_CMD_LLC_SET_PARAMS,
1593 .doit = nfc_genl_llc_set_params,
1594 .policy = nfc_genl_policy,
1597 .cmd = NFC_CMD_LLC_SDREQ,
1598 .doit = nfc_genl_llc_sdreq,
1599 .policy = nfc_genl_policy,
1602 .cmd = NFC_CMD_FW_DOWNLOAD,
1603 .doit = nfc_genl_fw_download,
1604 .policy = nfc_genl_policy,
1607 .cmd = NFC_CMD_ENABLE_SE,
1608 .doit = nfc_genl_enable_se,
1609 .policy = nfc_genl_policy,
1612 .cmd = NFC_CMD_DISABLE_SE,
1613 .doit = nfc_genl_disable_se,
1614 .policy = nfc_genl_policy,
1617 .cmd = NFC_CMD_GET_SE,
1618 .dumpit = nfc_genl_dump_ses,
1619 .done = nfc_genl_dump_ses_done,
1620 .policy = nfc_genl_policy,
1623 .cmd = NFC_CMD_SE_IO,
1624 .doit = nfc_genl_se_io,
1625 .policy = nfc_genl_policy,
1628 .cmd = NFC_CMD_ACTIVATE_TARGET,
1629 .doit = nfc_genl_activate_target,
1630 .policy = nfc_genl_policy,
1633 .cmd = NFC_CMD_VENDOR,
1634 .doit = nfc_genl_vendor_cmd,
1635 .policy = nfc_genl_policy,
1640 struct urelease_work {
1641 struct work_struct w;
1645 static void nfc_urelease_event_work(struct work_struct *work)
1647 struct urelease_work *w = container_of(work, struct urelease_work, w);
1648 struct class_dev_iter iter;
1649 struct nfc_dev *dev;
1651 pr_debug("portid %d\n", w->portid);
1653 mutex_lock(&nfc_devlist_mutex);
1655 nfc_device_iter_init(&iter);
1656 dev = nfc_device_iter_next(&iter);
1659 mutex_lock(&dev->genl_data.genl_data_mutex);
1661 if (dev->genl_data.poll_req_portid == w->portid) {
1663 dev->genl_data.poll_req_portid = 0;
1666 mutex_unlock(&dev->genl_data.genl_data_mutex);
1668 dev = nfc_device_iter_next(&iter);
1671 nfc_device_iter_exit(&iter);
1673 mutex_unlock(&nfc_devlist_mutex);
1678 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1679 unsigned long event, void *ptr)
1681 struct netlink_notify *n = ptr;
1682 struct urelease_work *w;
1684 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1687 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1689 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1691 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1692 w->portid = n->portid;
1693 schedule_work((struct work_struct *) w);
1700 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1702 genl_data->poll_req_portid = 0;
1703 mutex_init(&genl_data->genl_data_mutex);
1706 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1708 mutex_destroy(&genl_data->genl_data_mutex);
1711 static struct notifier_block nl_notifier = {
1712 .notifier_call = nfc_genl_rcv_nl_event,
1716 * nfc_genl_init() - Initialize netlink interface
1718 * This initialization function registers the nfc netlink family.
1720 int __init nfc_genl_init(void)
1724 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1730 netlink_register_notifier(&nl_notifier);
1736 * nfc_genl_exit() - Deinitialize netlink interface
1738 * This exit function unregisters the nfc netlink family.
1740 void nfc_genl_exit(void)
1742 netlink_unregister_notifier(&nl_notifier);
1743 genl_unregister_family(&nfc_genl_family);