1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
9 * Vendor commands implementation based on net/wireless/nl80211.c
12 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
13 * Copyright 2013-2014 Intel Mobile Communications GmbH
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
18 #include <net/genetlink.h>
19 #include <linux/nfc.h>
20 #include <linux/slab.h>
25 static const struct genl_multicast_group nfc_genl_mcgrps
[] = {
26 { .name
= NFC_GENL_MCAST_EVENT_NAME
, },
29 static struct genl_family nfc_genl_family
;
30 static const struct nla_policy nfc_genl_policy
[NFC_ATTR_MAX
+ 1] = {
31 [NFC_ATTR_DEVICE_INDEX
] = { .type
= NLA_U32
},
32 [NFC_ATTR_DEVICE_NAME
] = { .type
= NLA_STRING
,
33 .len
= NFC_DEVICE_NAME_MAXSIZE
},
34 [NFC_ATTR_PROTOCOLS
] = { .type
= NLA_U32
},
35 [NFC_ATTR_COMM_MODE
] = { .type
= NLA_U8
},
36 [NFC_ATTR_RF_MODE
] = { .type
= NLA_U8
},
37 [NFC_ATTR_DEVICE_POWERED
] = { .type
= NLA_U8
},
38 [NFC_ATTR_IM_PROTOCOLS
] = { .type
= NLA_U32
},
39 [NFC_ATTR_TM_PROTOCOLS
] = { .type
= NLA_U32
},
40 [NFC_ATTR_LLC_PARAM_LTO
] = { .type
= NLA_U8
},
41 [NFC_ATTR_LLC_PARAM_RW
] = { .type
= NLA_U8
},
42 [NFC_ATTR_LLC_PARAM_MIUX
] = { .type
= NLA_U16
},
43 [NFC_ATTR_LLC_SDP
] = { .type
= NLA_NESTED
},
44 [NFC_ATTR_FIRMWARE_NAME
] = { .type
= NLA_STRING
,
45 .len
= NFC_FIRMWARE_NAME_MAXSIZE
},
46 [NFC_ATTR_SE_APDU
] = { .type
= NLA_BINARY
},
47 [NFC_ATTR_VENDOR_DATA
] = { .type
= NLA_BINARY
},
51 static const struct nla_policy nfc_sdp_genl_policy
[NFC_SDP_ATTR_MAX
+ 1] = {
52 [NFC_SDP_ATTR_URI
] = { .type
= NLA_STRING
,
54 [NFC_SDP_ATTR_SAP
] = { .type
= NLA_U8
},
57 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
58 struct netlink_callback
*cb
, int flags
)
62 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
63 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
67 genl_dump_check_consistent(cb
, hdr
);
69 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
70 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
71 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
72 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
74 if (target
->nfcid1_len
> 0 &&
75 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
78 if (target
->sensb_res_len
> 0 &&
79 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
82 if (target
->sensf_res_len
> 0 &&
83 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
87 if (target
->is_iso15693
) {
88 if (nla_put_u8(msg
, NFC_ATTR_TARGET_ISO15693_DSFID
,
89 target
->iso15693_dsfid
) ||
90 nla_put(msg
, NFC_ATTR_TARGET_ISO15693_UID
,
91 sizeof(target
->iso15693_uid
), target
->iso15693_uid
))
95 genlmsg_end(msg
, hdr
);
99 genlmsg_cancel(msg
, hdr
);
103 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
105 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
109 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
110 return ERR_PTR(-EINVAL
);
112 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
114 dev
= nfc_get_device(idx
);
116 return ERR_PTR(-ENODEV
);
121 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
122 struct netlink_callback
*cb
)
125 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
129 dev
= __get_device_from_cb(cb
);
133 cb
->args
[1] = (long) dev
;
136 device_lock(&dev
->dev
);
138 cb
->seq
= dev
->targets_generation
;
140 while (i
< dev
->n_targets
) {
141 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
149 device_unlock(&dev
->dev
);
156 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
158 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
166 int nfc_genl_targets_found(struct nfc_dev
*dev
)
171 dev
->genl_data
.poll_req_portid
= 0;
173 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
177 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
178 NFC_EVENT_TARGETS_FOUND
);
182 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
183 goto nla_put_failure
;
185 genlmsg_end(msg
, hdr
);
187 return genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_ATOMIC
);
195 int nfc_genl_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
200 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
204 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
205 NFC_EVENT_TARGET_LOST
);
209 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
210 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
211 goto nla_put_failure
;
213 genlmsg_end(msg
, hdr
);
215 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
225 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
230 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
234 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
235 NFC_EVENT_TM_ACTIVATED
);
239 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
240 goto nla_put_failure
;
241 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
242 goto nla_put_failure
;
244 genlmsg_end(msg
, hdr
);
246 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
256 int nfc_genl_tm_deactivated(struct nfc_dev
*dev
)
261 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
265 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
266 NFC_EVENT_TM_DEACTIVATED
);
270 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
271 goto nla_put_failure
;
273 genlmsg_end(msg
, hdr
);
275 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
285 static int nfc_genl_setup_device_added(struct nfc_dev
*dev
, struct sk_buff
*msg
)
287 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
288 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
289 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
290 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
) ||
291 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, dev
->rf_mode
))
296 int nfc_genl_device_added(struct nfc_dev
*dev
)
301 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
305 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
306 NFC_EVENT_DEVICE_ADDED
);
310 if (nfc_genl_setup_device_added(dev
, msg
))
311 goto nla_put_failure
;
313 genlmsg_end(msg
, hdr
);
315 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
325 int nfc_genl_device_removed(struct nfc_dev
*dev
)
330 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
334 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
335 NFC_EVENT_DEVICE_REMOVED
);
339 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
340 goto nla_put_failure
;
342 genlmsg_end(msg
, hdr
);
344 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
354 int nfc_genl_llc_send_sdres(struct nfc_dev
*dev
, struct hlist_head
*sdres_list
)
357 struct nlattr
*sdp_attr
, *uri_attr
;
358 struct nfc_llcp_sdp_tlv
*sdres
;
359 struct hlist_node
*n
;
364 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
368 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
369 NFC_EVENT_LLC_SDRES
);
373 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
374 goto nla_put_failure
;
376 sdp_attr
= nla_nest_start_noflag(msg
, NFC_ATTR_LLC_SDP
);
377 if (sdp_attr
== NULL
) {
379 goto nla_put_failure
;
383 hlist_for_each_entry_safe(sdres
, n
, sdres_list
, node
) {
384 pr_debug("uri: %s, sap: %d\n", sdres
->uri
, sdres
->sap
);
386 uri_attr
= nla_nest_start_noflag(msg
, i
++);
387 if (uri_attr
== NULL
) {
389 goto nla_put_failure
;
392 if (nla_put_u8(msg
, NFC_SDP_ATTR_SAP
, sdres
->sap
))
393 goto nla_put_failure
;
395 if (nla_put_string(msg
, NFC_SDP_ATTR_URI
, sdres
->uri
))
396 goto nla_put_failure
;
398 nla_nest_end(msg
, uri_attr
);
400 hlist_del(&sdres
->node
);
402 nfc_llcp_free_sdp_tlv(sdres
);
405 nla_nest_end(msg
, sdp_attr
);
407 genlmsg_end(msg
, hdr
);
409 return genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_ATOMIC
);
415 nfc_llcp_free_sdp_tlv_list(sdres_list
);
420 int nfc_genl_se_added(struct nfc_dev
*dev
, u32 se_idx
, u16 type
)
425 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
429 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
434 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
435 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
) ||
436 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, type
))
437 goto nla_put_failure
;
439 genlmsg_end(msg
, hdr
);
441 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
451 int nfc_genl_se_removed(struct nfc_dev
*dev
, u32 se_idx
)
456 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
460 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
461 NFC_EVENT_SE_REMOVED
);
465 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
466 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
))
467 goto nla_put_failure
;
469 genlmsg_end(msg
, hdr
);
471 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
481 int nfc_genl_se_transaction(struct nfc_dev
*dev
, u8 se_idx
,
482 struct nfc_evt_transaction
*evt_transaction
)
488 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
492 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
493 NFC_EVENT_SE_TRANSACTION
);
497 se
= nfc_find_se(dev
, se_idx
);
501 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
502 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
) ||
503 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, se
->type
) ||
504 nla_put(msg
, NFC_ATTR_SE_AID
, evt_transaction
->aid_len
,
505 evt_transaction
->aid
) ||
506 nla_put(msg
, NFC_ATTR_SE_PARAMS
, evt_transaction
->params_len
,
507 evt_transaction
->params
))
508 goto nla_put_failure
;
510 /* evt_transaction is no more used */
511 devm_kfree(&dev
->dev
, evt_transaction
);
513 genlmsg_end(msg
, hdr
);
515 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
521 /* evt_transaction is no more used */
522 devm_kfree(&dev
->dev
, evt_transaction
);
527 int nfc_genl_se_connectivity(struct nfc_dev
*dev
, u8 se_idx
)
533 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
537 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
538 NFC_EVENT_SE_CONNECTIVITY
);
542 se
= nfc_find_se(dev
, se_idx
);
546 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
547 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
) ||
548 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, se
->type
))
549 goto nla_put_failure
;
551 genlmsg_end(msg
, hdr
);
553 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
563 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
565 struct netlink_callback
*cb
,
570 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
576 genl_dump_check_consistent(cb
, hdr
);
578 if (nfc_genl_setup_device_added(dev
, msg
))
579 goto nla_put_failure
;
581 genlmsg_end(msg
, hdr
);
585 genlmsg_cancel(msg
, hdr
);
589 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
590 struct netlink_callback
*cb
)
592 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
593 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
594 bool first_call
= false;
598 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
601 cb
->args
[0] = (long) iter
;
604 mutex_lock(&nfc_devlist_mutex
);
606 cb
->seq
= nfc_devlist_generation
;
609 nfc_device_iter_init(iter
);
610 dev
= nfc_device_iter_next(iter
);
616 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
617 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
621 dev
= nfc_device_iter_next(iter
);
624 mutex_unlock(&nfc_devlist_mutex
);
626 cb
->args
[1] = (long) dev
;
631 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
633 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
635 nfc_device_iter_exit(iter
);
641 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
642 u8 comm_mode
, u8 rf_mode
)
647 pr_debug("DEP link is up\n");
649 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
653 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
657 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
658 goto nla_put_failure
;
659 if (rf_mode
== NFC_RF_INITIATOR
&&
660 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
661 goto nla_put_failure
;
662 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
663 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
664 goto nla_put_failure
;
666 genlmsg_end(msg
, hdr
);
668 dev
->dep_link_up
= true;
670 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_ATOMIC
);
680 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
685 pr_debug("DEP link is down\n");
687 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
691 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
692 NFC_CMD_DEP_LINK_DOWN
);
696 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
697 goto nla_put_failure
;
699 genlmsg_end(msg
, hdr
);
701 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_ATOMIC
);
711 static int nfc_genl_get_device(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 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
733 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_portid
, info
->snd_seq
,
740 return genlmsg_reply(msg
, info
);
749 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
755 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
758 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
760 dev
= nfc_get_device(idx
);
764 rc
= nfc_dev_up(dev
);
770 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
776 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
779 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
781 dev
= nfc_get_device(idx
);
785 rc
= nfc_dev_down(dev
);
791 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
796 u32 im_protocols
= 0, tm_protocols
= 0;
798 pr_debug("Poll start\n");
800 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
801 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
802 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
803 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
806 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
808 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
809 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
811 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
812 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
813 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
814 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
816 dev
= nfc_get_device(idx
);
820 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
822 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
824 dev
->genl_data
.poll_req_portid
= info
->snd_portid
;
826 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
832 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
838 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
841 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
843 dev
= nfc_get_device(idx
);
847 device_lock(&dev
->dev
);
850 device_unlock(&dev
->dev
);
854 device_unlock(&dev
->dev
);
856 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
858 if (dev
->genl_data
.poll_req_portid
!= info
->snd_portid
) {
863 rc
= nfc_stop_poll(dev
);
864 dev
->genl_data
.poll_req_portid
= 0;
867 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
872 static int nfc_genl_activate_target(struct sk_buff
*skb
, struct genl_info
*info
)
875 u32 device_idx
, target_idx
, protocol
;
878 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
879 !info
->attrs
[NFC_ATTR_TARGET_INDEX
] ||
880 !info
->attrs
[NFC_ATTR_PROTOCOLS
])
883 device_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
885 dev
= nfc_get_device(device_idx
);
889 target_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
890 protocol
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
892 nfc_deactivate_target(dev
, target_idx
, NFC_TARGET_MODE_SLEEP
);
893 rc
= nfc_activate_target(dev
, target_idx
, protocol
);
899 static int nfc_genl_deactivate_target(struct sk_buff
*skb
,
900 struct genl_info
*info
)
903 u32 device_idx
, target_idx
;
906 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
907 !info
->attrs
[NFC_ATTR_TARGET_INDEX
])
910 device_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
912 dev
= nfc_get_device(device_idx
);
916 target_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
918 rc
= nfc_deactivate_target(dev
, target_idx
, NFC_TARGET_MODE_SLEEP
);
924 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
931 pr_debug("DEP link up\n");
933 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
934 !info
->attrs
[NFC_ATTR_COMM_MODE
])
937 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
938 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
939 tgt_idx
= NFC_TARGET_IDX_ANY
;
941 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
943 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
945 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
948 dev
= nfc_get_device(idx
);
952 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
959 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
965 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
966 !info
->attrs
[NFC_ATTR_TARGET_INDEX
])
969 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
971 dev
= nfc_get_device(idx
);
975 rc
= nfc_dep_link_down(dev
);
981 static int nfc_genl_send_params(struct sk_buff
*msg
,
982 struct nfc_llcp_local
*local
,
987 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, 0,
988 NFC_CMD_LLC_GET_PARAMS
);
992 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, local
->dev
->idx
) ||
993 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_LTO
, local
->lto
) ||
994 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_RW
, local
->rw
) ||
995 nla_put_u16(msg
, NFC_ATTR_LLC_PARAM_MIUX
, be16_to_cpu(local
->miux
)))
996 goto nla_put_failure
;
998 genlmsg_end(msg
, hdr
);
1002 genlmsg_cancel(msg
, hdr
);
1006 static int nfc_genl_llc_get_params(struct sk_buff
*skb
, struct genl_info
*info
)
1008 struct nfc_dev
*dev
;
1009 struct nfc_llcp_local
*local
;
1011 struct sk_buff
*msg
= NULL
;
1014 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1015 !info
->attrs
[NFC_ATTR_FIRMWARE_NAME
])
1018 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1020 dev
= nfc_get_device(idx
);
1024 device_lock(&dev
->dev
);
1026 local
= nfc_llcp_find_local(dev
);
1032 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1038 rc
= nfc_genl_send_params(msg
, local
, info
->snd_portid
, info
->snd_seq
);
1041 device_unlock(&dev
->dev
);
1043 nfc_put_device(dev
);
1052 return genlmsg_reply(msg
, info
);
1055 static int nfc_genl_llc_set_params(struct sk_buff
*skb
, struct genl_info
*info
)
1057 struct nfc_dev
*dev
;
1058 struct nfc_llcp_local
*local
;
1064 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1065 (!info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
] &&
1066 !info
->attrs
[NFC_ATTR_LLC_PARAM_RW
] &&
1067 !info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]))
1070 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]) {
1071 rw
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]);
1073 if (rw
> LLCP_MAX_RW
)
1077 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]) {
1078 miux
= nla_get_u16(info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]);
1080 if (miux
> LLCP_MAX_MIUX
)
1084 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1086 dev
= nfc_get_device(idx
);
1090 device_lock(&dev
->dev
);
1092 local
= nfc_llcp_find_local(dev
);
1098 if (info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]) {
1099 if (dev
->dep_link_up
) {
1104 local
->lto
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]);
1107 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
])
1110 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
])
1111 local
->miux
= cpu_to_be16(miux
);
1114 device_unlock(&dev
->dev
);
1116 nfc_put_device(dev
);
1121 static int nfc_genl_llc_sdreq(struct sk_buff
*skb
, struct genl_info
*info
)
1123 struct nfc_dev
*dev
;
1124 struct nfc_llcp_local
*local
;
1125 struct nlattr
*attr
, *sdp_attrs
[NFC_SDP_ATTR_MAX
+1];
1130 size_t uri_len
, tlvs_len
;
1131 struct hlist_head sdreq_list
;
1132 struct nfc_llcp_sdp_tlv
*sdreq
;
1134 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1135 !info
->attrs
[NFC_ATTR_LLC_SDP
])
1138 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1140 dev
= nfc_get_device(idx
);
1144 device_lock(&dev
->dev
);
1146 if (dev
->dep_link_up
== false) {
1151 local
= nfc_llcp_find_local(dev
);
1157 INIT_HLIST_HEAD(&sdreq_list
);
1161 nla_for_each_nested(attr
, info
->attrs
[NFC_ATTR_LLC_SDP
], rem
) {
1162 rc
= nla_parse_nested_deprecated(sdp_attrs
, NFC_SDP_ATTR_MAX
,
1163 attr
, nfc_sdp_genl_policy
,
1171 if (!sdp_attrs
[NFC_SDP_ATTR_URI
])
1174 uri_len
= nla_len(sdp_attrs
[NFC_SDP_ATTR_URI
]);
1178 uri
= nla_data(sdp_attrs
[NFC_SDP_ATTR_URI
]);
1179 if (uri
== NULL
|| *uri
== 0)
1182 tid
= local
->sdreq_next_tid
++;
1184 sdreq
= nfc_llcp_build_sdreq_tlv(tid
, uri
, uri_len
);
1185 if (sdreq
== NULL
) {
1190 tlvs_len
+= sdreq
->tlv_len
;
1192 hlist_add_head(&sdreq
->node
, &sdreq_list
);
1195 if (hlist_empty(&sdreq_list
)) {
1200 rc
= nfc_llcp_send_snl_sdreq(local
, &sdreq_list
, tlvs_len
);
1202 device_unlock(&dev
->dev
);
1204 nfc_put_device(dev
);
1209 static int nfc_genl_fw_download(struct sk_buff
*skb
, struct genl_info
*info
)
1211 struct nfc_dev
*dev
;
1214 char firmware_name
[NFC_FIRMWARE_NAME_MAXSIZE
+ 1];
1216 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
1219 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1221 dev
= nfc_get_device(idx
);
1225 nla_strlcpy(firmware_name
, info
->attrs
[NFC_ATTR_FIRMWARE_NAME
],
1226 sizeof(firmware_name
));
1228 rc
= nfc_fw_download(dev
, firmware_name
);
1230 nfc_put_device(dev
);
1234 int nfc_genl_fw_download_done(struct nfc_dev
*dev
, const char *firmware_name
,
1237 struct sk_buff
*msg
;
1240 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1244 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
1245 NFC_CMD_FW_DOWNLOAD
);
1249 if (nla_put_string(msg
, NFC_ATTR_FIRMWARE_NAME
, firmware_name
) ||
1250 nla_put_u32(msg
, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS
, result
) ||
1251 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
1252 goto nla_put_failure
;
1254 genlmsg_end(msg
, hdr
);
1256 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
1266 static int nfc_genl_enable_se(struct sk_buff
*skb
, struct genl_info
*info
)
1268 struct nfc_dev
*dev
;
1272 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1273 !info
->attrs
[NFC_ATTR_SE_INDEX
])
1276 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1277 se_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_SE_INDEX
]);
1279 dev
= nfc_get_device(idx
);
1283 rc
= nfc_enable_se(dev
, se_idx
);
1285 nfc_put_device(dev
);
1289 static int nfc_genl_disable_se(struct sk_buff
*skb
, struct genl_info
*info
)
1291 struct nfc_dev
*dev
;
1295 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1296 !info
->attrs
[NFC_ATTR_SE_INDEX
])
1299 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1300 se_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_SE_INDEX
]);
1302 dev
= nfc_get_device(idx
);
1306 rc
= nfc_disable_se(dev
, se_idx
);
1308 nfc_put_device(dev
);
1312 static int nfc_genl_send_se(struct sk_buff
*msg
, struct nfc_dev
*dev
,
1313 u32 portid
, u32 seq
,
1314 struct netlink_callback
*cb
,
1318 struct nfc_se
*se
, *n
;
1320 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
1321 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
1324 goto nla_put_failure
;
1327 genl_dump_check_consistent(cb
, hdr
);
1329 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
1330 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se
->idx
) ||
1331 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, se
->type
))
1332 goto nla_put_failure
;
1334 genlmsg_end(msg
, hdr
);
1340 genlmsg_cancel(msg
, hdr
);
1344 static int nfc_genl_dump_ses(struct sk_buff
*skb
,
1345 struct netlink_callback
*cb
)
1347 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
1348 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
1349 bool first_call
= false;
1353 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
1356 cb
->args
[0] = (long) iter
;
1359 mutex_lock(&nfc_devlist_mutex
);
1361 cb
->seq
= nfc_devlist_generation
;
1364 nfc_device_iter_init(iter
);
1365 dev
= nfc_device_iter_next(iter
);
1371 rc
= nfc_genl_send_se(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
1372 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
1376 dev
= nfc_device_iter_next(iter
);
1379 mutex_unlock(&nfc_devlist_mutex
);
1381 cb
->args
[1] = (long) dev
;
1386 static int nfc_genl_dump_ses_done(struct netlink_callback
*cb
)
1388 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
1390 nfc_device_iter_exit(iter
);
1396 static int nfc_se_io(struct nfc_dev
*dev
, u32 se_idx
,
1397 u8
*apdu
, size_t apdu_length
,
1398 se_io_cb_t cb
, void *cb_context
)
1403 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
1405 device_lock(&dev
->dev
);
1407 if (!device_is_registered(&dev
->dev
)) {
1417 if (!dev
->ops
->se_io
) {
1422 se
= nfc_find_se(dev
, se_idx
);
1428 if (se
->state
!= NFC_SE_ENABLED
) {
1433 rc
= dev
->ops
->se_io(dev
, se_idx
, apdu
,
1434 apdu_length
, cb
, cb_context
);
1437 device_unlock(&dev
->dev
);
1446 static void se_io_cb(void *context
, u8
*apdu
, size_t apdu_len
, int err
)
1448 struct se_io_ctx
*ctx
= context
;
1449 struct sk_buff
*msg
;
1452 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1458 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
1463 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, ctx
->dev_idx
) ||
1464 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, ctx
->se_idx
) ||
1465 nla_put(msg
, NFC_ATTR_SE_APDU
, apdu_len
, apdu
))
1466 goto nla_put_failure
;
1468 genlmsg_end(msg
, hdr
);
1470 genlmsg_multicast(&nfc_genl_family
, msg
, 0, 0, GFP_KERNEL
);
1484 static int nfc_genl_se_io(struct sk_buff
*skb
, struct genl_info
*info
)
1486 struct nfc_dev
*dev
;
1487 struct se_io_ctx
*ctx
;
1488 u32 dev_idx
, se_idx
;
1492 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1493 !info
->attrs
[NFC_ATTR_SE_INDEX
] ||
1494 !info
->attrs
[NFC_ATTR_SE_APDU
])
1497 dev_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1498 se_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_SE_INDEX
]);
1500 dev
= nfc_get_device(dev_idx
);
1504 if (!dev
->ops
|| !dev
->ops
->se_io
)
1507 apdu_len
= nla_len(info
->attrs
[NFC_ATTR_SE_APDU
]);
1511 apdu
= nla_data(info
->attrs
[NFC_ATTR_SE_APDU
]);
1515 ctx
= kzalloc(sizeof(struct se_io_ctx
), GFP_KERNEL
);
1519 ctx
->dev_idx
= dev_idx
;
1520 ctx
->se_idx
= se_idx
;
1522 return nfc_se_io(dev
, se_idx
, apdu
, apdu_len
, se_io_cb
, ctx
);
1525 static int nfc_genl_vendor_cmd(struct sk_buff
*skb
,
1526 struct genl_info
*info
)
1528 struct nfc_dev
*dev
;
1529 struct nfc_vendor_cmd
*cmd
;
1530 u32 dev_idx
, vid
, subcmd
;
1535 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1536 !info
->attrs
[NFC_ATTR_VENDOR_ID
] ||
1537 !info
->attrs
[NFC_ATTR_VENDOR_SUBCMD
])
1540 dev_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1541 vid
= nla_get_u32(info
->attrs
[NFC_ATTR_VENDOR_ID
]);
1542 subcmd
= nla_get_u32(info
->attrs
[NFC_ATTR_VENDOR_SUBCMD
]);
1544 dev
= nfc_get_device(dev_idx
);
1545 if (!dev
|| !dev
->vendor_cmds
|| !dev
->n_vendor_cmds
)
1548 if (info
->attrs
[NFC_ATTR_VENDOR_DATA
]) {
1549 data
= nla_data(info
->attrs
[NFC_ATTR_VENDOR_DATA
]);
1550 data_len
= nla_len(info
->attrs
[NFC_ATTR_VENDOR_DATA
]);
1558 for (i
= 0; i
< dev
->n_vendor_cmds
; i
++) {
1559 cmd
= &dev
->vendor_cmds
[i
];
1561 if (cmd
->vendor_id
!= vid
|| cmd
->subcmd
!= subcmd
)
1564 dev
->cur_cmd_info
= info
;
1565 err
= cmd
->doit(dev
, data
, data_len
);
1566 dev
->cur_cmd_info
= NULL
;
1573 /* message building helper */
1574 static inline void *nfc_hdr_put(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1577 /* since there is no private header just add the generic one */
1578 return genlmsg_put(skb
, portid
, seq
, &nfc_genl_family
, flags
, cmd
);
1581 static struct sk_buff
*
1582 __nfc_alloc_vendor_cmd_skb(struct nfc_dev
*dev
, int approxlen
,
1583 u32 portid
, u32 seq
,
1584 enum nfc_attrs attr
,
1585 u32 oui
, u32 subcmd
, gfp_t gfp
)
1587 struct sk_buff
*skb
;
1590 skb
= nlmsg_new(approxlen
+ 100, gfp
);
1594 hdr
= nfc_hdr_put(skb
, portid
, seq
, 0, NFC_CMD_VENDOR
);
1600 if (nla_put_u32(skb
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
1601 goto nla_put_failure
;
1602 if (nla_put_u32(skb
, NFC_ATTR_VENDOR_ID
, oui
))
1603 goto nla_put_failure
;
1604 if (nla_put_u32(skb
, NFC_ATTR_VENDOR_SUBCMD
, subcmd
))
1605 goto nla_put_failure
;
1607 ((void **)skb
->cb
)[0] = dev
;
1608 ((void **)skb
->cb
)[1] = hdr
;
1617 struct sk_buff
*__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev
*dev
,
1618 enum nfc_attrs attr
,
1619 u32 oui
, u32 subcmd
,
1622 if (WARN_ON(!dev
->cur_cmd_info
))
1625 return __nfc_alloc_vendor_cmd_skb(dev
, approxlen
,
1626 dev
->cur_cmd_info
->snd_portid
,
1627 dev
->cur_cmd_info
->snd_seq
, attr
,
1628 oui
, subcmd
, GFP_KERNEL
);
1630 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb
);
1632 int nfc_vendor_cmd_reply(struct sk_buff
*skb
)
1634 struct nfc_dev
*dev
= ((void **)skb
->cb
)[0];
1635 void *hdr
= ((void **)skb
->cb
)[1];
1637 /* clear CB data for netlink core to own from now on */
1638 memset(skb
->cb
, 0, sizeof(skb
->cb
));
1640 if (WARN_ON(!dev
->cur_cmd_info
)) {
1645 genlmsg_end(skb
, hdr
);
1646 return genlmsg_reply(skb
, dev
->cur_cmd_info
);
1648 EXPORT_SYMBOL(nfc_vendor_cmd_reply
);
1650 static const struct genl_ops nfc_genl_ops
[] = {
1652 .cmd
= NFC_CMD_GET_DEVICE
,
1653 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1654 .doit
= nfc_genl_get_device
,
1655 .dumpit
= nfc_genl_dump_devices
,
1656 .done
= nfc_genl_dump_devices_done
,
1659 .cmd
= NFC_CMD_DEV_UP
,
1660 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1661 .doit
= nfc_genl_dev_up
,
1664 .cmd
= NFC_CMD_DEV_DOWN
,
1665 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1666 .doit
= nfc_genl_dev_down
,
1669 .cmd
= NFC_CMD_START_POLL
,
1670 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1671 .doit
= nfc_genl_start_poll
,
1674 .cmd
= NFC_CMD_STOP_POLL
,
1675 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1676 .doit
= nfc_genl_stop_poll
,
1679 .cmd
= NFC_CMD_DEP_LINK_UP
,
1680 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1681 .doit
= nfc_genl_dep_link_up
,
1684 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
1685 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1686 .doit
= nfc_genl_dep_link_down
,
1689 .cmd
= NFC_CMD_GET_TARGET
,
1690 .validate
= GENL_DONT_VALIDATE_STRICT
|
1691 GENL_DONT_VALIDATE_DUMP_STRICT
,
1692 .dumpit
= nfc_genl_dump_targets
,
1693 .done
= nfc_genl_dump_targets_done
,
1696 .cmd
= NFC_CMD_LLC_GET_PARAMS
,
1697 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1698 .doit
= nfc_genl_llc_get_params
,
1701 .cmd
= NFC_CMD_LLC_SET_PARAMS
,
1702 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1703 .doit
= nfc_genl_llc_set_params
,
1706 .cmd
= NFC_CMD_LLC_SDREQ
,
1707 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1708 .doit
= nfc_genl_llc_sdreq
,
1711 .cmd
= NFC_CMD_FW_DOWNLOAD
,
1712 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1713 .doit
= nfc_genl_fw_download
,
1716 .cmd
= NFC_CMD_ENABLE_SE
,
1717 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1718 .doit
= nfc_genl_enable_se
,
1721 .cmd
= NFC_CMD_DISABLE_SE
,
1722 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1723 .doit
= nfc_genl_disable_se
,
1726 .cmd
= NFC_CMD_GET_SE
,
1727 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1728 .dumpit
= nfc_genl_dump_ses
,
1729 .done
= nfc_genl_dump_ses_done
,
1732 .cmd
= NFC_CMD_SE_IO
,
1733 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1734 .doit
= nfc_genl_se_io
,
1737 .cmd
= NFC_CMD_ACTIVATE_TARGET
,
1738 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1739 .doit
= nfc_genl_activate_target
,
1742 .cmd
= NFC_CMD_VENDOR
,
1743 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1744 .doit
= nfc_genl_vendor_cmd
,
1747 .cmd
= NFC_CMD_DEACTIVATE_TARGET
,
1748 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1749 .doit
= nfc_genl_deactivate_target
,
1753 static struct genl_family nfc_genl_family __ro_after_init
= {
1755 .name
= NFC_GENL_NAME
,
1756 .version
= NFC_GENL_VERSION
,
1757 .maxattr
= NFC_ATTR_MAX
,
1758 .policy
= nfc_genl_policy
,
1759 .module
= THIS_MODULE
,
1760 .ops
= nfc_genl_ops
,
1761 .n_ops
= ARRAY_SIZE(nfc_genl_ops
),
1762 .mcgrps
= nfc_genl_mcgrps
,
1763 .n_mcgrps
= ARRAY_SIZE(nfc_genl_mcgrps
),
1767 struct urelease_work
{
1768 struct work_struct w
;
1772 static void nfc_urelease_event_work(struct work_struct
*work
)
1774 struct urelease_work
*w
= container_of(work
, struct urelease_work
, w
);
1775 struct class_dev_iter iter
;
1776 struct nfc_dev
*dev
;
1778 pr_debug("portid %d\n", w
->portid
);
1780 mutex_lock(&nfc_devlist_mutex
);
1782 nfc_device_iter_init(&iter
);
1783 dev
= nfc_device_iter_next(&iter
);
1786 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
1788 if (dev
->genl_data
.poll_req_portid
== w
->portid
) {
1790 dev
->genl_data
.poll_req_portid
= 0;
1793 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
1795 dev
= nfc_device_iter_next(&iter
);
1798 nfc_device_iter_exit(&iter
);
1800 mutex_unlock(&nfc_devlist_mutex
);
1805 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
1806 unsigned long event
, void *ptr
)
1808 struct netlink_notify
*n
= ptr
;
1809 struct urelease_work
*w
;
1811 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
1814 pr_debug("NETLINK_URELEASE event from id %d\n", n
->portid
);
1816 w
= kmalloc(sizeof(*w
), GFP_ATOMIC
);
1818 INIT_WORK((struct work_struct
*) w
, nfc_urelease_event_work
);
1819 w
->portid
= n
->portid
;
1820 schedule_work((struct work_struct
*) w
);
1827 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
1829 genl_data
->poll_req_portid
= 0;
1830 mutex_init(&genl_data
->genl_data_mutex
);
1833 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
1835 mutex_destroy(&genl_data
->genl_data_mutex
);
1838 static struct notifier_block nl_notifier
= {
1839 .notifier_call
= nfc_genl_rcv_nl_event
,
1843 * nfc_genl_init() - Initialize netlink interface
1845 * This initialization function registers the nfc netlink family.
1847 int __init
nfc_genl_init(void)
1851 rc
= genl_register_family(&nfc_genl_family
);
1855 netlink_register_notifier(&nl_notifier
);
1861 * nfc_genl_exit() - Deinitialize netlink interface
1863 * This exit function unregisters the nfc netlink family.
1865 void nfc_genl_exit(void)
1867 netlink_unregister_notifier(&nl_notifier
);
1868 genl_unregister_family(&nfc_genl_family
);