2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
24 #include <net/genetlink.h>
25 #include <linux/nfc.h>
26 #include <linux/slab.h>
31 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
35 static struct genl_family nfc_genl_family = {
36 .id = GENL_ID_GENERATE,
38 .name = NFC_GENL_NAME,
39 .version = NFC_GENL_VERSION,
40 .maxattr = NFC_ATTR_MAX,
43 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
44 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
45 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
46 .len = NFC_DEVICE_NAME_MAXSIZE },
47 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
48 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
51 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
53 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
55 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
56 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
57 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 .len = NFC_FIRMWARE_NAME_MAXSIZE },
59 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
62 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
63 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
64 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
67 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
68 struct netlink_callback *cb, int flags)
72 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
73 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
79 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
80 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
81 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
82 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
84 if (target->nfcid1_len > 0 &&
85 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
88 if (target->sensb_res_len > 0 &&
89 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
92 if (target->sensf_res_len > 0 &&
93 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
97 if (target->is_iso15693) {
98 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
99 target->iso15693_dsfid) ||
100 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
101 sizeof(target->iso15693_uid), target->iso15693_uid))
102 goto nla_put_failure;
105 genlmsg_end(msg, hdr);
109 genlmsg_cancel(msg, hdr);
113 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
119 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
120 nfc_genl_family.attrbuf,
121 nfc_genl_family.maxattr,
126 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
127 return ERR_PTR(-EINVAL);
129 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
131 dev = nfc_get_device(idx);
133 return ERR_PTR(-ENODEV);
138 static int nfc_genl_dump_targets(struct sk_buff *skb,
139 struct netlink_callback *cb)
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
146 dev = __get_device_from_cb(cb);
150 cb->args[1] = (long) dev;
153 device_lock(&dev->dev);
155 cb->seq = dev->targets_generation;
157 while (i < dev->n_targets) {
158 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
166 device_unlock(&dev->dev);
173 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
175 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
183 int nfc_genl_targets_found(struct nfc_dev *dev)
188 dev->genl_data.poll_req_portid = 0;
190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
195 NFC_EVENT_TARGETS_FOUND);
199 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
200 goto nla_put_failure;
202 genlmsg_end(msg, hdr);
204 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
207 genlmsg_cancel(msg, hdr);
213 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
222 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
223 NFC_EVENT_TARGET_LOST);
227 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
228 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
229 goto nla_put_failure;
231 genlmsg_end(msg, hdr);
233 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
238 genlmsg_cancel(msg, hdr);
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);
270 genlmsg_cancel(msg, hdr);
276 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
285 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
286 NFC_EVENT_TM_DEACTIVATED);
290 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
291 goto nla_put_failure;
293 genlmsg_end(msg, hdr);
295 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
300 genlmsg_cancel(msg, hdr);
306 int nfc_genl_device_added(struct nfc_dev *dev)
311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
315 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
316 NFC_EVENT_DEVICE_ADDED);
320 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
321 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
322 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
323 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
324 goto nla_put_failure;
326 genlmsg_end(msg, hdr);
328 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
333 genlmsg_cancel(msg, hdr);
339 int nfc_genl_device_removed(struct nfc_dev *dev)
344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
348 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
349 NFC_EVENT_DEVICE_REMOVED);
353 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
354 goto nla_put_failure;
356 genlmsg_end(msg, hdr);
358 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
363 genlmsg_cancel(msg, hdr);
369 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
372 struct nlattr *sdp_attr, *uri_attr;
373 struct nfc_llcp_sdp_tlv *sdres;
374 struct hlist_node *n;
379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
383 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
384 NFC_EVENT_LLC_SDRES);
388 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
389 goto nla_put_failure;
391 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
392 if (sdp_attr == NULL) {
394 goto nla_put_failure;
398 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
399 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
401 uri_attr = nla_nest_start(msg, i++);
402 if (uri_attr == NULL) {
404 goto nla_put_failure;
407 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
408 goto nla_put_failure;
410 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
411 goto nla_put_failure;
413 nla_nest_end(msg, uri_attr);
415 hlist_del(&sdres->node);
417 nfc_llcp_free_sdp_tlv(sdres);
420 nla_nest_end(msg, sdp_attr);
422 genlmsg_end(msg, hdr);
424 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
427 genlmsg_cancel(msg, hdr);
432 nfc_llcp_free_sdp_tlv_list(sdres_list);
437 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
446 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
451 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
452 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
453 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
454 goto nla_put_failure;
456 genlmsg_end(msg, hdr);
458 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
463 genlmsg_cancel(msg, hdr);
469 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
478 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
479 NFC_EVENT_SE_REMOVED);
483 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
484 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
485 goto nla_put_failure;
487 genlmsg_end(msg, hdr);
489 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
494 genlmsg_cancel(msg, hdr);
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);
539 genlmsg_cancel(msg, hdr);
541 /* evt_transaction is no more used */
542 devm_kfree(&dev->dev, evt_transaction);
547 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
549 struct netlink_callback *cb,
554 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
560 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
562 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
563 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
564 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
565 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
566 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
567 goto nla_put_failure;
569 genlmsg_end(msg, hdr);
573 genlmsg_cancel(msg, hdr);
577 static int nfc_genl_dump_devices(struct sk_buff *skb,
578 struct netlink_callback *cb)
580 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
581 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
582 bool first_call = false;
586 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
589 cb->args[0] = (long) iter;
592 mutex_lock(&nfc_devlist_mutex);
594 cb->seq = nfc_devlist_generation;
597 nfc_device_iter_init(iter);
598 dev = nfc_device_iter_next(iter);
604 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
605 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
609 dev = nfc_device_iter_next(iter);
612 mutex_unlock(&nfc_devlist_mutex);
614 cb->args[1] = (long) dev;
619 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
621 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
623 nfc_device_iter_exit(iter);
629 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
630 u8 comm_mode, u8 rf_mode)
635 pr_debug("DEP link is up\n");
637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
641 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
645 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
646 goto nla_put_failure;
647 if (rf_mode == NFC_RF_INITIATOR &&
648 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
649 goto nla_put_failure;
650 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
651 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
652 goto nla_put_failure;
654 genlmsg_end(msg, hdr);
656 dev->dep_link_up = true;
658 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
663 genlmsg_cancel(msg, hdr);
669 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
674 pr_debug("DEP link is down\n");
676 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
680 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
681 NFC_CMD_DEP_LINK_DOWN);
685 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
686 goto nla_put_failure;
688 genlmsg_end(msg, hdr);
690 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
695 genlmsg_cancel(msg, hdr);
701 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
708 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
711 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713 dev = nfc_get_device(idx);
717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
723 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
730 return genlmsg_reply(msg, info);
739 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
745 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
748 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
750 dev = nfc_get_device(idx);
754 rc = nfc_dev_up(dev);
760 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
766 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
769 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
771 dev = nfc_get_device(idx);
775 rc = nfc_dev_down(dev);
781 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
786 u32 im_protocols = 0, tm_protocols = 0;
788 pr_debug("Poll start\n");
790 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
791 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
792 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
793 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
796 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
798 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
799 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
801 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
802 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
803 else if (info->attrs[NFC_ATTR_PROTOCOLS])
804 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
806 dev = nfc_get_device(idx);
810 mutex_lock(&dev->genl_data.genl_data_mutex);
812 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
814 dev->genl_data.poll_req_portid = info->snd_portid;
816 mutex_unlock(&dev->genl_data.genl_data_mutex);
822 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
828 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
831 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
833 dev = nfc_get_device(idx);
837 device_lock(&dev->dev);
840 device_unlock(&dev->dev);
844 device_unlock(&dev->dev);
846 mutex_lock(&dev->genl_data.genl_data_mutex);
848 if (dev->genl_data.poll_req_portid != info->snd_portid) {
853 rc = nfc_stop_poll(dev);
854 dev->genl_data.poll_req_portid = 0;
857 mutex_unlock(&dev->genl_data.genl_data_mutex);
862 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
865 u32 device_idx, target_idx, protocol;
868 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
871 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
873 dev = nfc_get_device(device_idx);
877 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
878 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
880 nfc_deactivate_target(dev, target_idx);
881 rc = nfc_activate_target(dev, target_idx, protocol);
887 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
894 pr_debug("DEP link up\n");
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
897 !info->attrs[NFC_ATTR_COMM_MODE])
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
902 tgt_idx = NFC_TARGET_IDX_ANY;
904 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
906 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
908 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
911 dev = nfc_get_device(idx);
915 rc = nfc_dep_link_up(dev, tgt_idx, comm);
922 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
928 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
931 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
933 dev = nfc_get_device(idx);
937 rc = nfc_dep_link_down(dev);
943 static int nfc_genl_send_params(struct sk_buff *msg,
944 struct nfc_llcp_local *local,
949 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
950 NFC_CMD_LLC_GET_PARAMS);
954 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
955 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
956 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
957 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
958 goto nla_put_failure;
960 genlmsg_end(msg, hdr);
965 genlmsg_cancel(msg, hdr);
969 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
972 struct nfc_llcp_local *local;
974 struct sk_buff *msg = NULL;
977 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
980 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
982 dev = nfc_get_device(idx);
986 device_lock(&dev->dev);
988 local = nfc_llcp_find_local(dev);
994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1000 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1003 device_unlock(&dev->dev);
1005 nfc_put_device(dev);
1014 return genlmsg_reply(msg, info);
1017 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1019 struct nfc_dev *dev;
1020 struct nfc_llcp_local *local;
1026 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1027 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1028 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1029 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1032 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1033 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1035 if (rw > LLCP_MAX_RW)
1039 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1040 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1042 if (miux > LLCP_MAX_MIUX)
1046 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1048 dev = nfc_get_device(idx);
1052 device_lock(&dev->dev);
1054 local = nfc_llcp_find_local(dev);
1056 nfc_put_device(dev);
1061 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1062 if (dev->dep_link_up) {
1067 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1073 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1074 local->miux = cpu_to_be16(miux);
1077 device_unlock(&dev->dev);
1079 nfc_put_device(dev);
1084 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1086 struct nfc_dev *dev;
1087 struct nfc_llcp_local *local;
1088 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1093 size_t uri_len, tlvs_len;
1094 struct hlist_head sdreq_list;
1095 struct nfc_llcp_sdp_tlv *sdreq;
1097 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1098 !info->attrs[NFC_ATTR_LLC_SDP])
1101 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1103 dev = nfc_get_device(idx);
1109 device_lock(&dev->dev);
1111 if (dev->dep_link_up == false) {
1116 local = nfc_llcp_find_local(dev);
1118 nfc_put_device(dev);
1123 INIT_HLIST_HEAD(&sdreq_list);
1127 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1128 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1129 nfc_sdp_genl_policy);
1136 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1139 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1143 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1144 if (uri == NULL || *uri == 0)
1147 tid = local->sdreq_next_tid++;
1149 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1150 if (sdreq == NULL) {
1155 tlvs_len += sdreq->tlv_len;
1157 hlist_add_head(&sdreq->node, &sdreq_list);
1160 if (hlist_empty(&sdreq_list)) {
1165 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1167 device_unlock(&dev->dev);
1169 nfc_put_device(dev);
1174 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1176 struct nfc_dev *dev;
1179 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1181 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1186 dev = nfc_get_device(idx);
1190 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1191 sizeof(firmware_name));
1193 rc = nfc_fw_download(dev, firmware_name);
1195 nfc_put_device(dev);
1199 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1202 struct sk_buff *msg;
1205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1209 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1210 NFC_CMD_FW_DOWNLOAD);
1214 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1215 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1216 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1217 goto nla_put_failure;
1219 genlmsg_end(msg, hdr);
1221 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1226 genlmsg_cancel(msg, hdr);
1232 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1234 struct nfc_dev *dev;
1238 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1239 !info->attrs[NFC_ATTR_SE_INDEX])
1242 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1243 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1245 dev = nfc_get_device(idx);
1249 rc = nfc_enable_se(dev, se_idx);
1251 nfc_put_device(dev);
1255 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1257 struct nfc_dev *dev;
1261 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1262 !info->attrs[NFC_ATTR_SE_INDEX])
1265 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1266 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1268 dev = nfc_get_device(idx);
1272 rc = nfc_disable_se(dev, se_idx);
1274 nfc_put_device(dev);
1278 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1279 u32 portid, u32 seq,
1280 struct netlink_callback *cb,
1284 struct nfc_se *se, *n;
1286 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1287 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1290 goto nla_put_failure;
1293 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1295 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1296 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1297 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1298 goto nla_put_failure;
1300 genlmsg_end(msg, hdr);
1306 genlmsg_cancel(msg, hdr);
1310 static int nfc_genl_dump_ses(struct sk_buff *skb,
1311 struct netlink_callback *cb)
1313 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1314 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1315 bool first_call = false;
1319 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1322 cb->args[0] = (long) iter;
1325 mutex_lock(&nfc_devlist_mutex);
1327 cb->seq = nfc_devlist_generation;
1330 nfc_device_iter_init(iter);
1331 dev = nfc_device_iter_next(iter);
1337 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1338 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1342 dev = nfc_device_iter_next(iter);
1345 mutex_unlock(&nfc_devlist_mutex);
1347 cb->args[1] = (long) dev;
1352 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1354 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1356 nfc_device_iter_exit(iter);
1362 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1363 u8 *apdu, size_t apdu_length,
1364 se_io_cb_t cb, void *cb_context)
1369 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1371 device_lock(&dev->dev);
1373 if (!device_is_registered(&dev->dev)) {
1383 if (!dev->ops->se_io) {
1388 se = nfc_find_se(dev, se_idx);
1394 if (se->state != NFC_SE_ENABLED) {
1399 rc = dev->ops->se_io(dev, se_idx, apdu,
1400 apdu_length, cb, cb_context);
1403 device_unlock(&dev->dev);
1412 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1414 struct se_io_ctx *ctx = context;
1415 struct sk_buff *msg;
1418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1424 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1429 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1430 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1431 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1432 goto nla_put_failure;
1434 genlmsg_end(msg, hdr);
1436 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1443 genlmsg_cancel(msg, hdr);
1451 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1453 struct nfc_dev *dev;
1454 struct se_io_ctx *ctx;
1455 u32 dev_idx, se_idx;
1459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1460 !info->attrs[NFC_ATTR_SE_INDEX] ||
1461 !info->attrs[NFC_ATTR_SE_APDU])
1464 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1465 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1467 dev = nfc_get_device(dev_idx);
1471 if (!dev->ops || !dev->ops->se_io)
1474 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1478 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1482 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1486 ctx->dev_idx = dev_idx;
1487 ctx->se_idx = se_idx;
1489 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1492 static const struct genl_ops nfc_genl_ops[] = {
1494 .cmd = NFC_CMD_GET_DEVICE,
1495 .doit = nfc_genl_get_device,
1496 .dumpit = nfc_genl_dump_devices,
1497 .done = nfc_genl_dump_devices_done,
1498 .policy = nfc_genl_policy,
1501 .cmd = NFC_CMD_DEV_UP,
1502 .doit = nfc_genl_dev_up,
1503 .policy = nfc_genl_policy,
1506 .cmd = NFC_CMD_DEV_DOWN,
1507 .doit = nfc_genl_dev_down,
1508 .policy = nfc_genl_policy,
1511 .cmd = NFC_CMD_START_POLL,
1512 .doit = nfc_genl_start_poll,
1513 .policy = nfc_genl_policy,
1516 .cmd = NFC_CMD_STOP_POLL,
1517 .doit = nfc_genl_stop_poll,
1518 .policy = nfc_genl_policy,
1521 .cmd = NFC_CMD_DEP_LINK_UP,
1522 .doit = nfc_genl_dep_link_up,
1523 .policy = nfc_genl_policy,
1526 .cmd = NFC_CMD_DEP_LINK_DOWN,
1527 .doit = nfc_genl_dep_link_down,
1528 .policy = nfc_genl_policy,
1531 .cmd = NFC_CMD_GET_TARGET,
1532 .dumpit = nfc_genl_dump_targets,
1533 .done = nfc_genl_dump_targets_done,
1534 .policy = nfc_genl_policy,
1537 .cmd = NFC_CMD_LLC_GET_PARAMS,
1538 .doit = nfc_genl_llc_get_params,
1539 .policy = nfc_genl_policy,
1542 .cmd = NFC_CMD_LLC_SET_PARAMS,
1543 .doit = nfc_genl_llc_set_params,
1544 .policy = nfc_genl_policy,
1547 .cmd = NFC_CMD_LLC_SDREQ,
1548 .doit = nfc_genl_llc_sdreq,
1549 .policy = nfc_genl_policy,
1552 .cmd = NFC_CMD_FW_DOWNLOAD,
1553 .doit = nfc_genl_fw_download,
1554 .policy = nfc_genl_policy,
1557 .cmd = NFC_CMD_ENABLE_SE,
1558 .doit = nfc_genl_enable_se,
1559 .policy = nfc_genl_policy,
1562 .cmd = NFC_CMD_DISABLE_SE,
1563 .doit = nfc_genl_disable_se,
1564 .policy = nfc_genl_policy,
1567 .cmd = NFC_CMD_GET_SE,
1568 .dumpit = nfc_genl_dump_ses,
1569 .done = nfc_genl_dump_ses_done,
1570 .policy = nfc_genl_policy,
1573 .cmd = NFC_CMD_SE_IO,
1574 .doit = nfc_genl_se_io,
1575 .policy = nfc_genl_policy,
1578 .cmd = NFC_CMD_ACTIVATE_TARGET,
1579 .doit = nfc_genl_activate_target,
1580 .policy = nfc_genl_policy,
1585 struct urelease_work {
1586 struct work_struct w;
1590 static void nfc_urelease_event_work(struct work_struct *work)
1592 struct urelease_work *w = container_of(work, struct urelease_work, w);
1593 struct class_dev_iter iter;
1594 struct nfc_dev *dev;
1596 pr_debug("portid %d\n", w->portid);
1598 mutex_lock(&nfc_devlist_mutex);
1600 nfc_device_iter_init(&iter);
1601 dev = nfc_device_iter_next(&iter);
1604 mutex_lock(&dev->genl_data.genl_data_mutex);
1606 if (dev->genl_data.poll_req_portid == w->portid) {
1608 dev->genl_data.poll_req_portid = 0;
1611 mutex_unlock(&dev->genl_data.genl_data_mutex);
1613 dev = nfc_device_iter_next(&iter);
1616 nfc_device_iter_exit(&iter);
1618 mutex_unlock(&nfc_devlist_mutex);
1623 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1624 unsigned long event, void *ptr)
1626 struct netlink_notify *n = ptr;
1627 struct urelease_work *w;
1629 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1632 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1634 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1636 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1637 w->portid = n->portid;
1638 schedule_work((struct work_struct *) w);
1645 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1647 genl_data->poll_req_portid = 0;
1648 mutex_init(&genl_data->genl_data_mutex);
1651 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1653 mutex_destroy(&genl_data->genl_data_mutex);
1656 static struct notifier_block nl_notifier = {
1657 .notifier_call = nfc_genl_rcv_nl_event,
1661 * nfc_genl_init() - Initialize netlink interface
1663 * This initialization function registers the nfc netlink family.
1665 int __init nfc_genl_init(void)
1669 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1675 netlink_register_notifier(&nl_notifier);
1681 * nfc_genl_exit() - Deinitialize netlink interface
1683 * This exit function unregisters the nfc netlink family.
1685 void nfc_genl_exit(void)
1687 netlink_unregister_notifier(&nl_notifier);
1688 genl_unregister_family(&nfc_genl_family);