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 return genlmsg_end(msg, hdr);
108 genlmsg_cancel(msg, hdr);
112 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
118 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
119 nfc_genl_family.attrbuf,
120 nfc_genl_family.maxattr,
125 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
126 return ERR_PTR(-EINVAL);
128 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
130 dev = nfc_get_device(idx);
132 return ERR_PTR(-ENODEV);
137 static int nfc_genl_dump_targets(struct sk_buff *skb,
138 struct netlink_callback *cb)
141 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
145 dev = __get_device_from_cb(cb);
149 cb->args[1] = (long) dev;
152 device_lock(&dev->dev);
154 cb->seq = dev->targets_generation;
156 while (i < dev->n_targets) {
157 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
165 device_unlock(&dev->dev);
172 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
174 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
182 int nfc_genl_targets_found(struct nfc_dev *dev)
187 dev->genl_data.poll_req_portid = 0;
189 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
193 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
194 NFC_EVENT_TARGETS_FOUND);
198 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
199 goto nla_put_failure;
201 genlmsg_end(msg, hdr);
203 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
206 genlmsg_cancel(msg, hdr);
212 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
217 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
221 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
222 NFC_EVENT_TARGET_LOST);
226 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
227 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
228 goto nla_put_failure;
230 genlmsg_end(msg, hdr);
232 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
237 genlmsg_cancel(msg, hdr);
243 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
248 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
252 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
253 NFC_EVENT_TM_ACTIVATED);
257 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
258 goto nla_put_failure;
259 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
260 goto nla_put_failure;
262 genlmsg_end(msg, hdr);
264 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
269 genlmsg_cancel(msg, hdr);
275 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
284 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
285 NFC_EVENT_TM_DEACTIVATED);
289 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
290 goto nla_put_failure;
292 genlmsg_end(msg, hdr);
294 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
299 genlmsg_cancel(msg, hdr);
305 int nfc_genl_device_added(struct nfc_dev *dev)
310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
314 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
315 NFC_EVENT_DEVICE_ADDED);
319 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
320 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
321 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
322 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
323 goto nla_put_failure;
325 genlmsg_end(msg, hdr);
327 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
332 genlmsg_cancel(msg, hdr);
338 int nfc_genl_device_removed(struct nfc_dev *dev)
343 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
347 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
348 NFC_EVENT_DEVICE_REMOVED);
352 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
353 goto nla_put_failure;
355 genlmsg_end(msg, hdr);
357 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
362 genlmsg_cancel(msg, hdr);
368 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
371 struct nlattr *sdp_attr, *uri_attr;
372 struct nfc_llcp_sdp_tlv *sdres;
373 struct hlist_node *n;
378 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
382 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
383 NFC_EVENT_LLC_SDRES);
387 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
388 goto nla_put_failure;
390 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
391 if (sdp_attr == NULL) {
393 goto nla_put_failure;
397 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
398 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
400 uri_attr = nla_nest_start(msg, i++);
401 if (uri_attr == NULL) {
403 goto nla_put_failure;
406 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
407 goto nla_put_failure;
409 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
410 goto nla_put_failure;
412 nla_nest_end(msg, uri_attr);
414 hlist_del(&sdres->node);
416 nfc_llcp_free_sdp_tlv(sdres);
419 nla_nest_end(msg, sdp_attr);
421 genlmsg_end(msg, hdr);
423 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
426 genlmsg_cancel(msg, hdr);
431 nfc_llcp_free_sdp_tlv_list(sdres_list);
436 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
445 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
450 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
451 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
452 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
453 goto nla_put_failure;
455 genlmsg_end(msg, hdr);
457 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
462 genlmsg_cancel(msg, hdr);
468 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
473 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
477 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
478 NFC_EVENT_SE_REMOVED);
482 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
483 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
484 goto nla_put_failure;
486 genlmsg_end(msg, hdr);
488 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
493 genlmsg_cancel(msg, hdr);
499 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
501 struct netlink_callback *cb,
506 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
512 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
514 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
515 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
516 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
517 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
518 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
519 goto nla_put_failure;
521 return genlmsg_end(msg, hdr);
524 genlmsg_cancel(msg, hdr);
528 static int nfc_genl_dump_devices(struct sk_buff *skb,
529 struct netlink_callback *cb)
531 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
532 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
533 bool first_call = false;
537 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
540 cb->args[0] = (long) iter;
543 mutex_lock(&nfc_devlist_mutex);
545 cb->seq = nfc_devlist_generation;
548 nfc_device_iter_init(iter);
549 dev = nfc_device_iter_next(iter);
555 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
556 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
560 dev = nfc_device_iter_next(iter);
563 mutex_unlock(&nfc_devlist_mutex);
565 cb->args[1] = (long) dev;
570 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
572 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
574 nfc_device_iter_exit(iter);
580 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
581 u8 comm_mode, u8 rf_mode)
586 pr_debug("DEP link is up\n");
588 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
592 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
596 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
597 goto nla_put_failure;
598 if (rf_mode == NFC_RF_INITIATOR &&
599 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
600 goto nla_put_failure;
601 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
602 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
603 goto nla_put_failure;
605 genlmsg_end(msg, hdr);
607 dev->dep_link_up = true;
609 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
614 genlmsg_cancel(msg, hdr);
620 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
625 pr_debug("DEP link is down\n");
627 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
631 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
632 NFC_CMD_DEP_LINK_DOWN);
636 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
637 goto nla_put_failure;
639 genlmsg_end(msg, hdr);
641 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
646 genlmsg_cancel(msg, hdr);
652 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
659 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
662 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
664 dev = nfc_get_device(idx);
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
674 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
681 return genlmsg_reply(msg, info);
690 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
696 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
699 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
701 dev = nfc_get_device(idx);
705 rc = nfc_dev_up(dev);
711 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
717 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
720 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
722 dev = nfc_get_device(idx);
726 rc = nfc_dev_down(dev);
732 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
737 u32 im_protocols = 0, tm_protocols = 0;
739 pr_debug("Poll start\n");
741 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
742 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
743 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
744 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
747 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
749 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
750 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
752 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
753 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
754 else if (info->attrs[NFC_ATTR_PROTOCOLS])
755 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
757 dev = nfc_get_device(idx);
761 mutex_lock(&dev->genl_data.genl_data_mutex);
763 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
765 dev->genl_data.poll_req_portid = info->snd_portid;
767 mutex_unlock(&dev->genl_data.genl_data_mutex);
773 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
779 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
782 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
784 dev = nfc_get_device(idx);
788 device_lock(&dev->dev);
791 device_unlock(&dev->dev);
795 device_unlock(&dev->dev);
797 mutex_lock(&dev->genl_data.genl_data_mutex);
799 if (dev->genl_data.poll_req_portid != info->snd_portid) {
804 rc = nfc_stop_poll(dev);
805 dev->genl_data.poll_req_portid = 0;
808 mutex_unlock(&dev->genl_data.genl_data_mutex);
813 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
820 pr_debug("DEP link up\n");
822 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
823 !info->attrs[NFC_ATTR_COMM_MODE])
826 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
827 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
828 tgt_idx = NFC_TARGET_IDX_ANY;
830 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
832 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
834 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
837 dev = nfc_get_device(idx);
841 rc = nfc_dep_link_up(dev, tgt_idx, comm);
848 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
854 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
857 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
859 dev = nfc_get_device(idx);
863 rc = nfc_dep_link_down(dev);
869 static int nfc_genl_send_params(struct sk_buff *msg,
870 struct nfc_llcp_local *local,
875 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
876 NFC_CMD_LLC_GET_PARAMS);
880 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
881 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
882 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
883 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
884 goto nla_put_failure;
886 return genlmsg_end(msg, hdr);
890 genlmsg_cancel(msg, hdr);
894 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
897 struct nfc_llcp_local *local;
899 struct sk_buff *msg = NULL;
902 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
905 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
907 dev = nfc_get_device(idx);
911 device_lock(&dev->dev);
913 local = nfc_llcp_find_local(dev);
919 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
925 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
928 device_unlock(&dev->dev);
939 return genlmsg_reply(msg, info);
942 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
945 struct nfc_llcp_local *local;
951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
952 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
953 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
954 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
957 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
958 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
960 if (rw > LLCP_MAX_RW)
964 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
965 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
967 if (miux > LLCP_MAX_MIUX)
971 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
973 dev = nfc_get_device(idx);
977 device_lock(&dev->dev);
979 local = nfc_llcp_find_local(dev);
986 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
987 if (dev->dep_link_up) {
992 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
995 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
998 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
999 local->miux = cpu_to_be16(miux);
1002 device_unlock(&dev->dev);
1004 nfc_put_device(dev);
1009 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1011 struct nfc_dev *dev;
1012 struct nfc_llcp_local *local;
1013 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1018 size_t uri_len, tlvs_len;
1019 struct hlist_head sdreq_list;
1020 struct nfc_llcp_sdp_tlv *sdreq;
1022 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1023 !info->attrs[NFC_ATTR_LLC_SDP])
1026 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1028 dev = nfc_get_device(idx);
1034 device_lock(&dev->dev);
1036 if (dev->dep_link_up == false) {
1041 local = nfc_llcp_find_local(dev);
1043 nfc_put_device(dev);
1048 INIT_HLIST_HEAD(&sdreq_list);
1052 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1053 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1054 nfc_sdp_genl_policy);
1061 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1064 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1068 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1069 if (uri == NULL || *uri == 0)
1072 tid = local->sdreq_next_tid++;
1074 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1075 if (sdreq == NULL) {
1080 tlvs_len += sdreq->tlv_len;
1082 hlist_add_head(&sdreq->node, &sdreq_list);
1085 if (hlist_empty(&sdreq_list)) {
1090 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1092 device_unlock(&dev->dev);
1094 nfc_put_device(dev);
1099 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1101 struct nfc_dev *dev;
1104 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1106 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1109 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1111 dev = nfc_get_device(idx);
1115 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1116 sizeof(firmware_name));
1118 rc = nfc_fw_download(dev, firmware_name);
1120 nfc_put_device(dev);
1124 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1127 struct sk_buff *msg;
1130 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1134 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1135 NFC_CMD_FW_DOWNLOAD);
1139 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1140 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1141 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1142 goto nla_put_failure;
1144 genlmsg_end(msg, hdr);
1146 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1151 genlmsg_cancel(msg, hdr);
1157 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1159 struct nfc_dev *dev;
1163 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1164 !info->attrs[NFC_ATTR_SE_INDEX])
1167 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1168 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1170 dev = nfc_get_device(idx);
1174 rc = nfc_enable_se(dev, se_idx);
1176 nfc_put_device(dev);
1180 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1182 struct nfc_dev *dev;
1186 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1187 !info->attrs[NFC_ATTR_SE_INDEX])
1190 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1191 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1193 dev = nfc_get_device(idx);
1197 rc = nfc_disable_se(dev, se_idx);
1199 nfc_put_device(dev);
1203 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1204 u32 portid, u32 seq,
1205 struct netlink_callback *cb,
1209 struct nfc_se *se, *n;
1211 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1212 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1215 goto nla_put_failure;
1218 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1220 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1221 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1222 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1223 goto nla_put_failure;
1225 if (genlmsg_end(msg, hdr) < 0)
1226 goto nla_put_failure;
1232 genlmsg_cancel(msg, hdr);
1236 static int nfc_genl_dump_ses(struct sk_buff *skb,
1237 struct netlink_callback *cb)
1239 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1240 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1241 bool first_call = false;
1245 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1248 cb->args[0] = (long) iter;
1251 mutex_lock(&nfc_devlist_mutex);
1253 cb->seq = nfc_devlist_generation;
1256 nfc_device_iter_init(iter);
1257 dev = nfc_device_iter_next(iter);
1263 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1264 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1268 dev = nfc_device_iter_next(iter);
1271 mutex_unlock(&nfc_devlist_mutex);
1273 cb->args[1] = (long) dev;
1278 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1280 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1282 nfc_device_iter_exit(iter);
1293 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1295 struct se_io_ctx *ctx = context;
1296 struct sk_buff *msg;
1299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1305 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1310 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1311 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1312 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1313 goto nla_put_failure;
1315 genlmsg_end(msg, hdr);
1317 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1324 genlmsg_cancel(msg, hdr);
1332 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1334 struct nfc_dev *dev;
1335 struct se_io_ctx *ctx;
1336 u32 dev_idx, se_idx;
1340 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1341 !info->attrs[NFC_ATTR_SE_INDEX] ||
1342 !info->attrs[NFC_ATTR_SE_APDU])
1345 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1346 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1348 dev = nfc_get_device(dev_idx);
1352 if (!dev->ops || !dev->ops->se_io)
1355 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1359 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1363 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1367 ctx->dev_idx = dev_idx;
1368 ctx->se_idx = se_idx;
1370 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1373 static const struct genl_ops nfc_genl_ops[] = {
1375 .cmd = NFC_CMD_GET_DEVICE,
1376 .doit = nfc_genl_get_device,
1377 .dumpit = nfc_genl_dump_devices,
1378 .done = nfc_genl_dump_devices_done,
1379 .policy = nfc_genl_policy,
1382 .cmd = NFC_CMD_DEV_UP,
1383 .doit = nfc_genl_dev_up,
1384 .policy = nfc_genl_policy,
1387 .cmd = NFC_CMD_DEV_DOWN,
1388 .doit = nfc_genl_dev_down,
1389 .policy = nfc_genl_policy,
1392 .cmd = NFC_CMD_START_POLL,
1393 .doit = nfc_genl_start_poll,
1394 .policy = nfc_genl_policy,
1397 .cmd = NFC_CMD_STOP_POLL,
1398 .doit = nfc_genl_stop_poll,
1399 .policy = nfc_genl_policy,
1402 .cmd = NFC_CMD_DEP_LINK_UP,
1403 .doit = nfc_genl_dep_link_up,
1404 .policy = nfc_genl_policy,
1407 .cmd = NFC_CMD_DEP_LINK_DOWN,
1408 .doit = nfc_genl_dep_link_down,
1409 .policy = nfc_genl_policy,
1412 .cmd = NFC_CMD_GET_TARGET,
1413 .dumpit = nfc_genl_dump_targets,
1414 .done = nfc_genl_dump_targets_done,
1415 .policy = nfc_genl_policy,
1418 .cmd = NFC_CMD_LLC_GET_PARAMS,
1419 .doit = nfc_genl_llc_get_params,
1420 .policy = nfc_genl_policy,
1423 .cmd = NFC_CMD_LLC_SET_PARAMS,
1424 .doit = nfc_genl_llc_set_params,
1425 .policy = nfc_genl_policy,
1428 .cmd = NFC_CMD_LLC_SDREQ,
1429 .doit = nfc_genl_llc_sdreq,
1430 .policy = nfc_genl_policy,
1433 .cmd = NFC_CMD_FW_DOWNLOAD,
1434 .doit = nfc_genl_fw_download,
1435 .policy = nfc_genl_policy,
1438 .cmd = NFC_CMD_ENABLE_SE,
1439 .doit = nfc_genl_enable_se,
1440 .policy = nfc_genl_policy,
1443 .cmd = NFC_CMD_DISABLE_SE,
1444 .doit = nfc_genl_disable_se,
1445 .policy = nfc_genl_policy,
1448 .cmd = NFC_CMD_GET_SE,
1449 .dumpit = nfc_genl_dump_ses,
1450 .done = nfc_genl_dump_ses_done,
1451 .policy = nfc_genl_policy,
1454 .cmd = NFC_CMD_SE_IO,
1455 .doit = nfc_genl_se_io,
1456 .policy = nfc_genl_policy,
1461 struct urelease_work {
1462 struct work_struct w;
1466 static void nfc_urelease_event_work(struct work_struct *work)
1468 struct urelease_work *w = container_of(work, struct urelease_work, w);
1469 struct class_dev_iter iter;
1470 struct nfc_dev *dev;
1472 pr_debug("portid %d\n", w->portid);
1474 mutex_lock(&nfc_devlist_mutex);
1476 nfc_device_iter_init(&iter);
1477 dev = nfc_device_iter_next(&iter);
1480 mutex_lock(&dev->genl_data.genl_data_mutex);
1482 if (dev->genl_data.poll_req_portid == w->portid) {
1484 dev->genl_data.poll_req_portid = 0;
1487 mutex_unlock(&dev->genl_data.genl_data_mutex);
1489 dev = nfc_device_iter_next(&iter);
1492 nfc_device_iter_exit(&iter);
1494 mutex_unlock(&nfc_devlist_mutex);
1499 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1500 unsigned long event, void *ptr)
1502 struct netlink_notify *n = ptr;
1503 struct urelease_work *w;
1505 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1508 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1510 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1512 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1513 w->portid = n->portid;
1514 schedule_work((struct work_struct *) w);
1521 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1523 genl_data->poll_req_portid = 0;
1524 mutex_init(&genl_data->genl_data_mutex);
1527 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1529 mutex_destroy(&genl_data->genl_data_mutex);
1532 static struct notifier_block nl_notifier = {
1533 .notifier_call = nfc_genl_rcv_nl_event,
1537 * nfc_genl_init() - Initialize netlink interface
1539 * This initialization function registers the nfc netlink family.
1541 int __init nfc_genl_init(void)
1545 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1551 netlink_register_notifier(&nl_notifier);
1557 * nfc_genl_exit() - Deinitialize netlink interface
1559 * This exit function unregisters the nfc netlink family.
1561 void nfc_genl_exit(void)
1563 netlink_unregister_notifier(&nl_notifier);
1564 genl_unregister_family(&nfc_genl_family);