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 * 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,
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 return 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 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
502 struct netlink_callback *cb,
507 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
513 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
515 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
516 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
517 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
518 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
519 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
520 goto nla_put_failure;
522 return genlmsg_end(msg, hdr);
525 genlmsg_cancel(msg, hdr);
529 static int nfc_genl_dump_devices(struct sk_buff *skb,
530 struct netlink_callback *cb)
532 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
533 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
534 bool first_call = false;
538 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
541 cb->args[0] = (long) iter;
544 mutex_lock(&nfc_devlist_mutex);
546 cb->seq = nfc_devlist_generation;
549 nfc_device_iter_init(iter);
550 dev = nfc_device_iter_next(iter);
556 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
557 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
561 dev = nfc_device_iter_next(iter);
564 mutex_unlock(&nfc_devlist_mutex);
566 cb->args[1] = (long) dev;
571 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
573 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
575 nfc_device_iter_exit(iter);
581 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
582 u8 comm_mode, u8 rf_mode)
587 pr_debug("DEP link is up\n");
589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
593 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
597 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
598 goto nla_put_failure;
599 if (rf_mode == NFC_RF_INITIATOR &&
600 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
601 goto nla_put_failure;
602 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
603 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
604 goto nla_put_failure;
606 genlmsg_end(msg, hdr);
608 dev->dep_link_up = true;
610 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
615 genlmsg_cancel(msg, hdr);
621 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
626 pr_debug("DEP link is down\n");
628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
632 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
633 NFC_CMD_DEP_LINK_DOWN);
637 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
638 goto nla_put_failure;
640 genlmsg_end(msg, hdr);
642 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
647 genlmsg_cancel(msg, hdr);
653 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
660 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
663 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
665 dev = nfc_get_device(idx);
669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
675 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
682 return genlmsg_reply(msg, info);
691 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
697 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
700 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
702 dev = nfc_get_device(idx);
706 rc = nfc_dev_up(dev);
712 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
718 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
721 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
723 dev = nfc_get_device(idx);
727 rc = nfc_dev_down(dev);
733 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
738 u32 im_protocols = 0, tm_protocols = 0;
740 pr_debug("Poll start\n");
742 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
743 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
744 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
745 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
748 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
750 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
751 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
753 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
754 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
755 else if (info->attrs[NFC_ATTR_PROTOCOLS])
756 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
758 dev = nfc_get_device(idx);
762 mutex_lock(&dev->genl_data.genl_data_mutex);
764 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
766 dev->genl_data.poll_req_portid = info->snd_portid;
768 mutex_unlock(&dev->genl_data.genl_data_mutex);
774 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
780 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
783 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
785 dev = nfc_get_device(idx);
789 device_lock(&dev->dev);
792 device_unlock(&dev->dev);
796 device_unlock(&dev->dev);
798 mutex_lock(&dev->genl_data.genl_data_mutex);
800 if (dev->genl_data.poll_req_portid != info->snd_portid) {
805 rc = nfc_stop_poll(dev);
806 dev->genl_data.poll_req_portid = 0;
809 mutex_unlock(&dev->genl_data.genl_data_mutex);
814 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
821 pr_debug("DEP link up\n");
823 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
824 !info->attrs[NFC_ATTR_COMM_MODE])
827 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
828 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
829 tgt_idx = NFC_TARGET_IDX_ANY;
831 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
833 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
835 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
838 dev = nfc_get_device(idx);
842 rc = nfc_dep_link_up(dev, tgt_idx, comm);
849 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
855 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
858 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
860 dev = nfc_get_device(idx);
864 rc = nfc_dep_link_down(dev);
870 static int nfc_genl_send_params(struct sk_buff *msg,
871 struct nfc_llcp_local *local,
876 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
877 NFC_CMD_LLC_GET_PARAMS);
881 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
882 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
883 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
884 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
885 goto nla_put_failure;
887 return genlmsg_end(msg, hdr);
891 genlmsg_cancel(msg, hdr);
895 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
898 struct nfc_llcp_local *local;
900 struct sk_buff *msg = NULL;
903 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
906 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
908 dev = nfc_get_device(idx);
912 device_lock(&dev->dev);
914 local = nfc_llcp_find_local(dev);
920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
926 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
929 device_unlock(&dev->dev);
940 return genlmsg_reply(msg, info);
943 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
946 struct nfc_llcp_local *local;
952 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
953 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
954 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
955 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
958 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
959 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
961 if (rw > LLCP_MAX_RW)
965 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
966 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
968 if (miux > LLCP_MAX_MIUX)
972 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
974 dev = nfc_get_device(idx);
978 device_lock(&dev->dev);
980 local = nfc_llcp_find_local(dev);
987 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
988 if (dev->dep_link_up) {
993 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
996 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
999 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1000 local->miux = cpu_to_be16(miux);
1003 device_unlock(&dev->dev);
1005 nfc_put_device(dev);
1010 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1012 struct nfc_dev *dev;
1013 struct nfc_llcp_local *local;
1014 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1019 size_t uri_len, tlvs_len;
1020 struct hlist_head sdreq_list;
1021 struct nfc_llcp_sdp_tlv *sdreq;
1023 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1024 !info->attrs[NFC_ATTR_LLC_SDP])
1027 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1029 dev = nfc_get_device(idx);
1035 device_lock(&dev->dev);
1037 if (dev->dep_link_up == false) {
1042 local = nfc_llcp_find_local(dev);
1044 nfc_put_device(dev);
1049 INIT_HLIST_HEAD(&sdreq_list);
1053 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1054 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1055 nfc_sdp_genl_policy);
1062 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1065 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1069 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1070 if (uri == NULL || *uri == 0)
1073 tid = local->sdreq_next_tid++;
1075 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1076 if (sdreq == NULL) {
1081 tlvs_len += sdreq->tlv_len;
1083 hlist_add_head(&sdreq->node, &sdreq_list);
1086 if (hlist_empty(&sdreq_list)) {
1091 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1093 device_unlock(&dev->dev);
1095 nfc_put_device(dev);
1100 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1102 struct nfc_dev *dev;
1105 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1107 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1110 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1112 dev = nfc_get_device(idx);
1116 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1117 sizeof(firmware_name));
1119 rc = nfc_fw_download(dev, firmware_name);
1121 nfc_put_device(dev);
1125 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1128 struct sk_buff *msg;
1131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1135 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1136 NFC_CMD_FW_DOWNLOAD);
1140 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1141 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1142 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1143 goto nla_put_failure;
1145 genlmsg_end(msg, hdr);
1147 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1152 genlmsg_cancel(msg, hdr);
1158 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1160 struct nfc_dev *dev;
1164 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1165 !info->attrs[NFC_ATTR_SE_INDEX])
1168 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1169 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1171 dev = nfc_get_device(idx);
1175 rc = nfc_enable_se(dev, se_idx);
1177 nfc_put_device(dev);
1181 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1183 struct nfc_dev *dev;
1187 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1188 !info->attrs[NFC_ATTR_SE_INDEX])
1191 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1192 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1194 dev = nfc_get_device(idx);
1198 rc = nfc_disable_se(dev, se_idx);
1200 nfc_put_device(dev);
1204 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1205 u32 portid, u32 seq,
1206 struct netlink_callback *cb,
1210 struct nfc_se *se, *n;
1212 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1213 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1216 goto nla_put_failure;
1219 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1221 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1222 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1223 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1224 goto nla_put_failure;
1226 if (genlmsg_end(msg, hdr) < 0)
1227 goto nla_put_failure;
1233 genlmsg_cancel(msg, hdr);
1237 static int nfc_genl_dump_ses(struct sk_buff *skb,
1238 struct netlink_callback *cb)
1240 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1241 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1242 bool first_call = false;
1246 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1249 cb->args[0] = (long) iter;
1252 mutex_lock(&nfc_devlist_mutex);
1254 cb->seq = nfc_devlist_generation;
1257 nfc_device_iter_init(iter);
1258 dev = nfc_device_iter_next(iter);
1264 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1265 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1269 dev = nfc_device_iter_next(iter);
1272 mutex_unlock(&nfc_devlist_mutex);
1274 cb->args[1] = (long) dev;
1279 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1281 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1283 nfc_device_iter_exit(iter);
1294 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1296 struct se_io_ctx *ctx = context;
1297 struct sk_buff *msg;
1300 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1306 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1311 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1312 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1313 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1314 goto nla_put_failure;
1316 genlmsg_end(msg, hdr);
1318 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1325 genlmsg_cancel(msg, hdr);
1333 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1335 struct nfc_dev *dev;
1336 struct se_io_ctx *ctx;
1337 u32 dev_idx, se_idx;
1341 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1342 !info->attrs[NFC_ATTR_SE_INDEX] ||
1343 !info->attrs[NFC_ATTR_SE_APDU])
1346 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1347 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1349 dev = nfc_get_device(dev_idx);
1353 if (!dev->ops || !dev->ops->se_io)
1356 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1360 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1364 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1368 ctx->dev_idx = dev_idx;
1369 ctx->se_idx = se_idx;
1371 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1374 static const struct genl_ops nfc_genl_ops[] = {
1376 .cmd = NFC_CMD_GET_DEVICE,
1377 .doit = nfc_genl_get_device,
1378 .dumpit = nfc_genl_dump_devices,
1379 .done = nfc_genl_dump_devices_done,
1380 .policy = nfc_genl_policy,
1383 .cmd = NFC_CMD_DEV_UP,
1384 .doit = nfc_genl_dev_up,
1385 .policy = nfc_genl_policy,
1388 .cmd = NFC_CMD_DEV_DOWN,
1389 .doit = nfc_genl_dev_down,
1390 .policy = nfc_genl_policy,
1393 .cmd = NFC_CMD_START_POLL,
1394 .doit = nfc_genl_start_poll,
1395 .policy = nfc_genl_policy,
1398 .cmd = NFC_CMD_STOP_POLL,
1399 .doit = nfc_genl_stop_poll,
1400 .policy = nfc_genl_policy,
1403 .cmd = NFC_CMD_DEP_LINK_UP,
1404 .doit = nfc_genl_dep_link_up,
1405 .policy = nfc_genl_policy,
1408 .cmd = NFC_CMD_DEP_LINK_DOWN,
1409 .doit = nfc_genl_dep_link_down,
1410 .policy = nfc_genl_policy,
1413 .cmd = NFC_CMD_GET_TARGET,
1414 .dumpit = nfc_genl_dump_targets,
1415 .done = nfc_genl_dump_targets_done,
1416 .policy = nfc_genl_policy,
1419 .cmd = NFC_CMD_LLC_GET_PARAMS,
1420 .doit = nfc_genl_llc_get_params,
1421 .policy = nfc_genl_policy,
1424 .cmd = NFC_CMD_LLC_SET_PARAMS,
1425 .doit = nfc_genl_llc_set_params,
1426 .policy = nfc_genl_policy,
1429 .cmd = NFC_CMD_LLC_SDREQ,
1430 .doit = nfc_genl_llc_sdreq,
1431 .policy = nfc_genl_policy,
1434 .cmd = NFC_CMD_FW_DOWNLOAD,
1435 .doit = nfc_genl_fw_download,
1436 .policy = nfc_genl_policy,
1439 .cmd = NFC_CMD_ENABLE_SE,
1440 .doit = nfc_genl_enable_se,
1441 .policy = nfc_genl_policy,
1444 .cmd = NFC_CMD_DISABLE_SE,
1445 .doit = nfc_genl_disable_se,
1446 .policy = nfc_genl_policy,
1449 .cmd = NFC_CMD_GET_SE,
1450 .dumpit = nfc_genl_dump_ses,
1451 .done = nfc_genl_dump_ses_done,
1452 .policy = nfc_genl_policy,
1455 .cmd = NFC_CMD_SE_IO,
1456 .doit = nfc_genl_se_io,
1457 .policy = nfc_genl_policy,
1462 struct urelease_work {
1463 struct work_struct w;
1467 static void nfc_urelease_event_work(struct work_struct *work)
1469 struct urelease_work *w = container_of(work, struct urelease_work, w);
1470 struct class_dev_iter iter;
1471 struct nfc_dev *dev;
1473 pr_debug("portid %d\n", w->portid);
1475 mutex_lock(&nfc_devlist_mutex);
1477 nfc_device_iter_init(&iter);
1478 dev = nfc_device_iter_next(&iter);
1481 mutex_lock(&dev->genl_data.genl_data_mutex);
1483 if (dev->genl_data.poll_req_portid == w->portid) {
1485 dev->genl_data.poll_req_portid = 0;
1488 mutex_unlock(&dev->genl_data.genl_data_mutex);
1490 dev = nfc_device_iter_next(&iter);
1493 nfc_device_iter_exit(&iter);
1495 mutex_unlock(&nfc_devlist_mutex);
1500 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1501 unsigned long event, void *ptr)
1503 struct netlink_notify *n = ptr;
1504 struct urelease_work *w;
1506 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1509 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1511 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1513 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1514 w->portid = n->portid;
1515 schedule_work((struct work_struct *) w);
1522 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1524 genl_data->poll_req_portid = 0;
1525 mutex_init(&genl_data->genl_data_mutex);
1528 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1530 mutex_destroy(&genl_data->genl_data_mutex);
1533 static struct notifier_block nl_notifier = {
1534 .notifier_call = nfc_genl_rcv_nl_event,
1538 * nfc_genl_init() - Initialize netlink interface
1540 * This initialization function registers the nfc netlink family.
1542 int __init nfc_genl_init(void)
1546 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1552 netlink_register_notifier(&nl_notifier);
1558 * nfc_genl_exit() - Deinitialize netlink interface
1560 * This exit function unregisters the nfc netlink family.
1562 void nfc_genl_exit(void)
1564 netlink_unregister_notifier(&nl_notifier);
1565 genl_unregister_family(&nfc_genl_family);