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, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
26 #include <net/genetlink.h>
27 #include <linux/nfc.h>
28 #include <linux/slab.h>
33 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
34 { .name = NFC_GENL_MCAST_EVENT_NAME, },
37 static struct genl_family nfc_genl_family = {
38 .id = GENL_ID_GENERATE,
40 .name = NFC_GENL_NAME,
41 .version = NFC_GENL_VERSION,
42 .maxattr = NFC_ATTR_MAX,
45 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
48 .len = NFC_DEVICE_NAME_MAXSIZE },
49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
50 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
52 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
53 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
55 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
60 .len = NFC_FIRMWARE_NAME_MAXSIZE },
61 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
64 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
65 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
66 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
69 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
70 struct netlink_callback *cb, int flags)
74 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
75 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
79 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
81 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
82 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
83 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
84 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
86 if (target->nfcid1_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
90 if (target->sensb_res_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
94 if (target->sensf_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
99 return genlmsg_end(msg, hdr);
102 genlmsg_cancel(msg, hdr);
106 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
112 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
113 nfc_genl_family.attrbuf,
114 nfc_genl_family.maxattr,
119 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
120 return ERR_PTR(-EINVAL);
122 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
124 dev = nfc_get_device(idx);
126 return ERR_PTR(-ENODEV);
131 static int nfc_genl_dump_targets(struct sk_buff *skb,
132 struct netlink_callback *cb)
135 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
139 dev = __get_device_from_cb(cb);
143 cb->args[1] = (long) dev;
146 device_lock(&dev->dev);
148 cb->seq = dev->targets_generation;
150 while (i < dev->n_targets) {
151 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
159 device_unlock(&dev->dev);
166 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
168 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
176 int nfc_genl_targets_found(struct nfc_dev *dev)
181 dev->genl_data.poll_req_portid = 0;
183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
187 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
188 NFC_EVENT_TARGETS_FOUND);
192 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
193 goto nla_put_failure;
195 genlmsg_end(msg, hdr);
197 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
200 genlmsg_cancel(msg, hdr);
206 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
216 NFC_EVENT_TARGET_LOST);
220 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
221 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
222 goto nla_put_failure;
224 genlmsg_end(msg, hdr);
226 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
231 genlmsg_cancel(msg, hdr);
237 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
246 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
247 NFC_EVENT_TM_ACTIVATED);
251 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
252 goto nla_put_failure;
253 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
254 goto nla_put_failure;
256 genlmsg_end(msg, hdr);
258 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
263 genlmsg_cancel(msg, hdr);
269 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
278 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
279 NFC_EVENT_TM_DEACTIVATED);
283 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
284 goto nla_put_failure;
286 genlmsg_end(msg, hdr);
288 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
293 genlmsg_cancel(msg, hdr);
299 int nfc_genl_device_added(struct nfc_dev *dev)
304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
308 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
309 NFC_EVENT_DEVICE_ADDED);
313 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
314 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
315 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
316 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
317 goto nla_put_failure;
319 genlmsg_end(msg, hdr);
321 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
326 genlmsg_cancel(msg, hdr);
332 int nfc_genl_device_removed(struct nfc_dev *dev)
337 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
341 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
342 NFC_EVENT_DEVICE_REMOVED);
346 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
347 goto nla_put_failure;
349 genlmsg_end(msg, hdr);
351 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
356 genlmsg_cancel(msg, hdr);
362 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
365 struct nlattr *sdp_attr, *uri_attr;
366 struct nfc_llcp_sdp_tlv *sdres;
367 struct hlist_node *n;
372 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
376 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
377 NFC_EVENT_LLC_SDRES);
381 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
382 goto nla_put_failure;
384 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
385 if (sdp_attr == NULL) {
387 goto nla_put_failure;
391 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
392 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
394 uri_attr = nla_nest_start(msg, i++);
395 if (uri_attr == NULL) {
397 goto nla_put_failure;
400 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
401 goto nla_put_failure;
403 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
404 goto nla_put_failure;
406 nla_nest_end(msg, uri_attr);
408 hlist_del(&sdres->node);
410 nfc_llcp_free_sdp_tlv(sdres);
413 nla_nest_end(msg, sdp_attr);
415 genlmsg_end(msg, hdr);
417 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
420 genlmsg_cancel(msg, hdr);
425 nfc_llcp_free_sdp_tlv_list(sdres_list);
430 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
439 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
444 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
445 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
446 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
447 goto nla_put_failure;
449 genlmsg_end(msg, hdr);
451 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
456 genlmsg_cancel(msg, hdr);
462 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
467 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
471 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
472 NFC_EVENT_SE_REMOVED);
476 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
477 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
478 goto nla_put_failure;
480 genlmsg_end(msg, hdr);
482 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
487 genlmsg_cancel(msg, hdr);
493 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
495 struct netlink_callback *cb,
500 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
506 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
508 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
509 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
510 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
511 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
512 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
513 goto nla_put_failure;
515 return genlmsg_end(msg, hdr);
518 genlmsg_cancel(msg, hdr);
522 static int nfc_genl_dump_devices(struct sk_buff *skb,
523 struct netlink_callback *cb)
525 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
526 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
527 bool first_call = false;
531 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
534 cb->args[0] = (long) iter;
537 mutex_lock(&nfc_devlist_mutex);
539 cb->seq = nfc_devlist_generation;
542 nfc_device_iter_init(iter);
543 dev = nfc_device_iter_next(iter);
549 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
550 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
554 dev = nfc_device_iter_next(iter);
557 mutex_unlock(&nfc_devlist_mutex);
559 cb->args[1] = (long) dev;
564 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
566 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
568 nfc_device_iter_exit(iter);
574 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
575 u8 comm_mode, u8 rf_mode)
580 pr_debug("DEP link is up\n");
582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
586 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
590 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
591 goto nla_put_failure;
592 if (rf_mode == NFC_RF_INITIATOR &&
593 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
594 goto nla_put_failure;
595 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
596 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
597 goto nla_put_failure;
599 genlmsg_end(msg, hdr);
601 dev->dep_link_up = true;
603 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
608 genlmsg_cancel(msg, hdr);
614 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
619 pr_debug("DEP link is down\n");
621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
625 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
626 NFC_CMD_DEP_LINK_DOWN);
630 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
631 goto nla_put_failure;
633 genlmsg_end(msg, hdr);
635 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
640 genlmsg_cancel(msg, hdr);
646 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
653 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
656 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
658 dev = nfc_get_device(idx);
662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
668 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
675 return genlmsg_reply(msg, info);
684 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
690 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
693 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
695 dev = nfc_get_device(idx);
699 rc = nfc_dev_up(dev);
705 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
711 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
714 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
716 dev = nfc_get_device(idx);
720 rc = nfc_dev_down(dev);
726 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
731 u32 im_protocols = 0, tm_protocols = 0;
733 pr_debug("Poll start\n");
735 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
736 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
737 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
738 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
741 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
743 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
744 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
746 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
747 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
748 else if (info->attrs[NFC_ATTR_PROTOCOLS])
749 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
751 dev = nfc_get_device(idx);
755 mutex_lock(&dev->genl_data.genl_data_mutex);
757 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
759 dev->genl_data.poll_req_portid = info->snd_portid;
761 mutex_unlock(&dev->genl_data.genl_data_mutex);
767 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
773 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
776 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
778 dev = nfc_get_device(idx);
782 device_lock(&dev->dev);
785 device_unlock(&dev->dev);
789 device_unlock(&dev->dev);
791 mutex_lock(&dev->genl_data.genl_data_mutex);
793 if (dev->genl_data.poll_req_portid != info->snd_portid) {
798 rc = nfc_stop_poll(dev);
799 dev->genl_data.poll_req_portid = 0;
802 mutex_unlock(&dev->genl_data.genl_data_mutex);
807 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
814 pr_debug("DEP link up\n");
816 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
817 !info->attrs[NFC_ATTR_COMM_MODE])
820 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
821 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
822 tgt_idx = NFC_TARGET_IDX_ANY;
824 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
826 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
828 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
831 dev = nfc_get_device(idx);
835 rc = nfc_dep_link_up(dev, tgt_idx, comm);
842 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
848 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
851 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
853 dev = nfc_get_device(idx);
857 rc = nfc_dep_link_down(dev);
863 static int nfc_genl_send_params(struct sk_buff *msg,
864 struct nfc_llcp_local *local,
869 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
870 NFC_CMD_LLC_GET_PARAMS);
874 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
875 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
876 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
877 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
878 goto nla_put_failure;
880 return genlmsg_end(msg, hdr);
884 genlmsg_cancel(msg, hdr);
888 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
891 struct nfc_llcp_local *local;
893 struct sk_buff *msg = NULL;
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
899 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901 dev = nfc_get_device(idx);
905 device_lock(&dev->dev);
907 local = nfc_llcp_find_local(dev);
913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
919 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
922 device_unlock(&dev->dev);
933 return genlmsg_reply(msg, info);
936 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
939 struct nfc_llcp_local *local;
945 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
946 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
947 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
948 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
951 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
952 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
954 if (rw > LLCP_MAX_RW)
958 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
959 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
961 if (miux > LLCP_MAX_MIUX)
965 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
967 dev = nfc_get_device(idx);
971 device_lock(&dev->dev);
973 local = nfc_llcp_find_local(dev);
980 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
981 if (dev->dep_link_up) {
986 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
989 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
992 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
993 local->miux = cpu_to_be16(miux);
996 device_unlock(&dev->dev);
1003 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1005 struct nfc_dev *dev;
1006 struct nfc_llcp_local *local;
1007 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1012 size_t uri_len, tlvs_len;
1013 struct hlist_head sdreq_list;
1014 struct nfc_llcp_sdp_tlv *sdreq;
1016 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1017 !info->attrs[NFC_ATTR_LLC_SDP])
1020 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1022 dev = nfc_get_device(idx);
1028 device_lock(&dev->dev);
1030 if (dev->dep_link_up == false) {
1035 local = nfc_llcp_find_local(dev);
1037 nfc_put_device(dev);
1042 INIT_HLIST_HEAD(&sdreq_list);
1046 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1047 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1048 nfc_sdp_genl_policy);
1055 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1058 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1062 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1063 if (uri == NULL || *uri == 0)
1066 tid = local->sdreq_next_tid++;
1068 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1069 if (sdreq == NULL) {
1074 tlvs_len += sdreq->tlv_len;
1076 hlist_add_head(&sdreq->node, &sdreq_list);
1079 if (hlist_empty(&sdreq_list)) {
1084 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1086 device_unlock(&dev->dev);
1088 nfc_put_device(dev);
1093 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1095 struct nfc_dev *dev;
1098 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1100 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1103 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1105 dev = nfc_get_device(idx);
1109 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1110 sizeof(firmware_name));
1112 rc = nfc_fw_download(dev, firmware_name);
1114 nfc_put_device(dev);
1118 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1121 struct sk_buff *msg;
1124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1128 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1129 NFC_CMD_FW_DOWNLOAD);
1133 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1134 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1135 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1136 goto nla_put_failure;
1138 genlmsg_end(msg, hdr);
1140 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1145 genlmsg_cancel(msg, hdr);
1151 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1153 struct nfc_dev *dev;
1157 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1158 !info->attrs[NFC_ATTR_SE_INDEX])
1161 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1162 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1164 dev = nfc_get_device(idx);
1168 rc = nfc_enable_se(dev, se_idx);
1170 nfc_put_device(dev);
1174 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1176 struct nfc_dev *dev;
1180 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1181 !info->attrs[NFC_ATTR_SE_INDEX])
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1185 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1187 dev = nfc_get_device(idx);
1191 rc = nfc_disable_se(dev, se_idx);
1193 nfc_put_device(dev);
1197 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1198 u32 portid, u32 seq,
1199 struct netlink_callback *cb,
1203 struct nfc_se *se, *n;
1205 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1206 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1209 goto nla_put_failure;
1212 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1214 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1215 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1216 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1217 goto nla_put_failure;
1219 if (genlmsg_end(msg, hdr) < 0)
1220 goto nla_put_failure;
1226 genlmsg_cancel(msg, hdr);
1230 static int nfc_genl_dump_ses(struct sk_buff *skb,
1231 struct netlink_callback *cb)
1233 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1234 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1235 bool first_call = false;
1239 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1242 cb->args[0] = (long) iter;
1245 mutex_lock(&nfc_devlist_mutex);
1247 cb->seq = nfc_devlist_generation;
1250 nfc_device_iter_init(iter);
1251 dev = nfc_device_iter_next(iter);
1257 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1258 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1262 dev = nfc_device_iter_next(iter);
1265 mutex_unlock(&nfc_devlist_mutex);
1267 cb->args[1] = (long) dev;
1272 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1274 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1276 nfc_device_iter_exit(iter);
1287 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1289 struct se_io_ctx *ctx = context;
1290 struct sk_buff *msg;
1293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1299 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1304 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1305 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1306 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1307 goto nla_put_failure;
1309 genlmsg_end(msg, hdr);
1311 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1318 genlmsg_cancel(msg, hdr);
1326 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1328 struct nfc_dev *dev;
1329 struct se_io_ctx *ctx;
1330 u32 dev_idx, se_idx;
1334 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1335 !info->attrs[NFC_ATTR_SE_INDEX] ||
1336 !info->attrs[NFC_ATTR_SE_APDU])
1339 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1340 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1342 dev = nfc_get_device(dev_idx);
1346 if (!dev->ops || !dev->ops->se_io)
1349 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1353 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1357 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1361 ctx->dev_idx = dev_idx;
1362 ctx->se_idx = se_idx;
1364 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1367 static const struct genl_ops nfc_genl_ops[] = {
1369 .cmd = NFC_CMD_GET_DEVICE,
1370 .doit = nfc_genl_get_device,
1371 .dumpit = nfc_genl_dump_devices,
1372 .done = nfc_genl_dump_devices_done,
1373 .policy = nfc_genl_policy,
1376 .cmd = NFC_CMD_DEV_UP,
1377 .doit = nfc_genl_dev_up,
1378 .policy = nfc_genl_policy,
1381 .cmd = NFC_CMD_DEV_DOWN,
1382 .doit = nfc_genl_dev_down,
1383 .policy = nfc_genl_policy,
1386 .cmd = NFC_CMD_START_POLL,
1387 .doit = nfc_genl_start_poll,
1388 .policy = nfc_genl_policy,
1391 .cmd = NFC_CMD_STOP_POLL,
1392 .doit = nfc_genl_stop_poll,
1393 .policy = nfc_genl_policy,
1396 .cmd = NFC_CMD_DEP_LINK_UP,
1397 .doit = nfc_genl_dep_link_up,
1398 .policy = nfc_genl_policy,
1401 .cmd = NFC_CMD_DEP_LINK_DOWN,
1402 .doit = nfc_genl_dep_link_down,
1403 .policy = nfc_genl_policy,
1406 .cmd = NFC_CMD_GET_TARGET,
1407 .dumpit = nfc_genl_dump_targets,
1408 .done = nfc_genl_dump_targets_done,
1409 .policy = nfc_genl_policy,
1412 .cmd = NFC_CMD_LLC_GET_PARAMS,
1413 .doit = nfc_genl_llc_get_params,
1414 .policy = nfc_genl_policy,
1417 .cmd = NFC_CMD_LLC_SET_PARAMS,
1418 .doit = nfc_genl_llc_set_params,
1419 .policy = nfc_genl_policy,
1422 .cmd = NFC_CMD_LLC_SDREQ,
1423 .doit = nfc_genl_llc_sdreq,
1424 .policy = nfc_genl_policy,
1427 .cmd = NFC_CMD_FW_DOWNLOAD,
1428 .doit = nfc_genl_fw_download,
1429 .policy = nfc_genl_policy,
1432 .cmd = NFC_CMD_ENABLE_SE,
1433 .doit = nfc_genl_enable_se,
1434 .policy = nfc_genl_policy,
1437 .cmd = NFC_CMD_DISABLE_SE,
1438 .doit = nfc_genl_disable_se,
1439 .policy = nfc_genl_policy,
1442 .cmd = NFC_CMD_GET_SE,
1443 .dumpit = nfc_genl_dump_ses,
1444 .done = nfc_genl_dump_ses_done,
1445 .policy = nfc_genl_policy,
1448 .cmd = NFC_CMD_SE_IO,
1449 .doit = nfc_genl_se_io,
1450 .policy = nfc_genl_policy,
1455 struct urelease_work {
1456 struct work_struct w;
1460 static void nfc_urelease_event_work(struct work_struct *work)
1462 struct urelease_work *w = container_of(work, struct urelease_work, w);
1463 struct class_dev_iter iter;
1464 struct nfc_dev *dev;
1466 pr_debug("portid %d\n", w->portid);
1468 mutex_lock(&nfc_devlist_mutex);
1470 nfc_device_iter_init(&iter);
1471 dev = nfc_device_iter_next(&iter);
1474 mutex_lock(&dev->genl_data.genl_data_mutex);
1476 if (dev->genl_data.poll_req_portid == w->portid) {
1478 dev->genl_data.poll_req_portid = 0;
1481 mutex_unlock(&dev->genl_data.genl_data_mutex);
1483 dev = nfc_device_iter_next(&iter);
1486 nfc_device_iter_exit(&iter);
1488 mutex_unlock(&nfc_devlist_mutex);
1493 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1494 unsigned long event, void *ptr)
1496 struct netlink_notify *n = ptr;
1497 struct urelease_work *w;
1499 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1502 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1504 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1506 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1507 w->portid = n->portid;
1508 schedule_work((struct work_struct *) w);
1515 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1517 genl_data->poll_req_portid = 0;
1518 mutex_init(&genl_data->genl_data_mutex);
1521 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1523 mutex_destroy(&genl_data->genl_data_mutex);
1526 static struct notifier_block nl_notifier = {
1527 .notifier_call = nfc_genl_rcv_nl_event,
1531 * nfc_genl_init() - Initialize netlink interface
1533 * This initialization function registers the nfc netlink family.
1535 int __init nfc_genl_init(void)
1539 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1545 netlink_register_notifier(&nl_notifier);
1551 * nfc_genl_exit() - Deinitialize netlink interface
1553 * This exit function unregisters the nfc netlink family.
1555 void nfc_genl_exit(void)
1557 netlink_unregister_notifier(&nl_notifier);
1558 genl_unregister_family(&nfc_genl_family);