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 struct genl_multicast_group nfc_genl_event_mcgrp
= {
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
},
61 static const struct nla_policy nfc_sdp_genl_policy
[NFC_SDP_ATTR_MAX
+ 1] = {
62 [NFC_SDP_ATTR_URI
] = { .type
= NLA_STRING
},
63 [NFC_SDP_ATTR_SAP
] = { .type
= NLA_U8
},
66 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
67 struct netlink_callback
*cb
, int flags
)
71 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
72 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
76 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
78 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
79 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
80 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
81 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
83 if (target
->nfcid1_len
> 0 &&
84 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
87 if (target
->sensb_res_len
> 0 &&
88 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
91 if (target
->sensf_res_len
> 0 &&
92 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
96 return genlmsg_end(msg
, hdr
);
99 genlmsg_cancel(msg
, hdr
);
103 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
109 rc
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ nfc_genl_family
.hdrsize
,
110 nfc_genl_family
.attrbuf
,
111 nfc_genl_family
.maxattr
,
116 if (!nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
])
117 return ERR_PTR(-EINVAL
);
119 idx
= nla_get_u32(nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
]);
121 dev
= nfc_get_device(idx
);
123 return ERR_PTR(-ENODEV
);
128 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
129 struct netlink_callback
*cb
)
132 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
136 dev
= __get_device_from_cb(cb
);
140 cb
->args
[1] = (long) dev
;
143 device_lock(&dev
->dev
);
145 cb
->seq
= dev
->targets_generation
;
147 while (i
< dev
->n_targets
) {
148 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
156 device_unlock(&dev
->dev
);
163 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
165 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
173 int nfc_genl_targets_found(struct nfc_dev
*dev
)
178 dev
->genl_data
.poll_req_portid
= 0;
180 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
184 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
185 NFC_EVENT_TARGETS_FOUND
);
189 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
190 goto nla_put_failure
;
192 genlmsg_end(msg
, hdr
);
194 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
197 genlmsg_cancel(msg
, hdr
);
203 int nfc_genl_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
208 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
212 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
213 NFC_EVENT_TARGET_LOST
);
217 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
218 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
219 goto nla_put_failure
;
221 genlmsg_end(msg
, hdr
);
223 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
228 genlmsg_cancel(msg
, hdr
);
234 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
239 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
243 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
244 NFC_EVENT_TM_ACTIVATED
);
248 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
249 goto nla_put_failure
;
250 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
251 goto nla_put_failure
;
253 genlmsg_end(msg
, hdr
);
255 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
260 genlmsg_cancel(msg
, hdr
);
266 int nfc_genl_tm_deactivated(struct nfc_dev
*dev
)
271 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
275 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
276 NFC_EVENT_TM_DEACTIVATED
);
280 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
281 goto nla_put_failure
;
283 genlmsg_end(msg
, hdr
);
285 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
290 genlmsg_cancel(msg
, hdr
);
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 (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
311 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
312 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
313 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
314 goto nla_put_failure
;
316 genlmsg_end(msg
, hdr
);
318 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
323 genlmsg_cancel(msg
, hdr
);
329 int nfc_genl_device_removed(struct nfc_dev
*dev
)
334 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
338 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
339 NFC_EVENT_DEVICE_REMOVED
);
343 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
344 goto nla_put_failure
;
346 genlmsg_end(msg
, hdr
);
348 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
353 genlmsg_cancel(msg
, hdr
);
359 int nfc_genl_llc_send_sdres(struct nfc_dev
*dev
, struct hlist_head
*sdres_list
)
362 struct nlattr
*sdp_attr
, *uri_attr
;
363 struct nfc_llcp_sdp_tlv
*sdres
;
364 struct hlist_node
*n
;
369 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
373 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
374 NFC_EVENT_LLC_SDRES
);
378 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
379 goto nla_put_failure
;
381 sdp_attr
= nla_nest_start(msg
, NFC_ATTR_LLC_SDP
);
382 if (sdp_attr
== NULL
) {
384 goto nla_put_failure
;
388 hlist_for_each_entry_safe(sdres
, n
, sdres_list
, node
) {
389 pr_debug("uri: %s, sap: %d\n", sdres
->uri
, sdres
->sap
);
391 uri_attr
= nla_nest_start(msg
, i
++);
392 if (uri_attr
== NULL
) {
394 goto nla_put_failure
;
397 if (nla_put_u8(msg
, NFC_SDP_ATTR_SAP
, sdres
->sap
))
398 goto nla_put_failure
;
400 if (nla_put_string(msg
, NFC_SDP_ATTR_URI
, sdres
->uri
))
401 goto nla_put_failure
;
403 nla_nest_end(msg
, uri_attr
);
405 hlist_del(&sdres
->node
);
407 nfc_llcp_free_sdp_tlv(sdres
);
410 nla_nest_end(msg
, sdp_attr
);
412 genlmsg_end(msg
, hdr
);
414 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
417 genlmsg_cancel(msg
, hdr
);
422 nfc_llcp_free_sdp_tlv_list(sdres_list
);
427 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
429 struct netlink_callback
*cb
,
434 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
440 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
442 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
443 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
444 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
445 nla_put_u32(msg
, NFC_ATTR_SE
, dev
->supported_se
) ||
446 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
) ||
447 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, dev
->rf_mode
))
448 goto nla_put_failure
;
450 return genlmsg_end(msg
, hdr
);
453 genlmsg_cancel(msg
, hdr
);
457 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
458 struct netlink_callback
*cb
)
460 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
461 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
462 bool first_call
= false;
466 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
469 cb
->args
[0] = (long) iter
;
472 mutex_lock(&nfc_devlist_mutex
);
474 cb
->seq
= nfc_devlist_generation
;
477 nfc_device_iter_init(iter
);
478 dev
= nfc_device_iter_next(iter
);
484 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
485 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
489 dev
= nfc_device_iter_next(iter
);
492 mutex_unlock(&nfc_devlist_mutex
);
494 cb
->args
[1] = (long) dev
;
499 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
501 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
503 nfc_device_iter_exit(iter
);
509 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
510 u8 comm_mode
, u8 rf_mode
)
515 pr_debug("DEP link is up\n");
517 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
521 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
525 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
526 goto nla_put_failure
;
527 if (rf_mode
== NFC_RF_INITIATOR
&&
528 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
529 goto nla_put_failure
;
530 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
531 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
532 goto nla_put_failure
;
534 genlmsg_end(msg
, hdr
);
536 dev
->dep_link_up
= true;
538 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
543 genlmsg_cancel(msg
, hdr
);
549 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
554 pr_debug("DEP link is down\n");
556 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
560 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
561 NFC_CMD_DEP_LINK_DOWN
);
565 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
566 goto nla_put_failure
;
568 genlmsg_end(msg
, hdr
);
570 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
575 genlmsg_cancel(msg
, hdr
);
581 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
588 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
591 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
593 dev
= nfc_get_device(idx
);
597 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
603 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_portid
, info
->snd_seq
,
610 return genlmsg_reply(msg
, info
);
619 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
625 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
628 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
630 dev
= nfc_get_device(idx
);
634 rc
= nfc_dev_up(dev
);
640 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
646 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
649 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
651 dev
= nfc_get_device(idx
);
655 rc
= nfc_dev_down(dev
);
661 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
666 u32 im_protocols
= 0, tm_protocols
= 0;
668 pr_debug("Poll start\n");
670 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
671 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
672 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
673 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
676 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
678 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
679 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
681 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
682 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
683 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
684 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
686 dev
= nfc_get_device(idx
);
690 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
692 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
694 dev
->genl_data
.poll_req_portid
= info
->snd_portid
;
696 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
702 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
708 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
711 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
713 dev
= nfc_get_device(idx
);
717 device_lock(&dev
->dev
);
720 device_unlock(&dev
->dev
);
724 device_unlock(&dev
->dev
);
726 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
728 if (dev
->genl_data
.poll_req_portid
!= info
->snd_portid
) {
733 rc
= nfc_stop_poll(dev
);
734 dev
->genl_data
.poll_req_portid
= 0;
737 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
742 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
749 pr_debug("DEP link up\n");
751 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
752 !info
->attrs
[NFC_ATTR_COMM_MODE
])
755 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
756 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
757 tgt_idx
= NFC_TARGET_IDX_ANY
;
759 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
761 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
763 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
766 dev
= nfc_get_device(idx
);
770 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
777 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
783 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
786 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
788 dev
= nfc_get_device(idx
);
792 rc
= nfc_dep_link_down(dev
);
798 static int nfc_genl_send_params(struct sk_buff
*msg
,
799 struct nfc_llcp_local
*local
,
804 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, 0,
805 NFC_CMD_LLC_GET_PARAMS
);
809 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, local
->dev
->idx
) ||
810 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_LTO
, local
->lto
) ||
811 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_RW
, local
->rw
) ||
812 nla_put_u16(msg
, NFC_ATTR_LLC_PARAM_MIUX
, be16_to_cpu(local
->miux
)))
813 goto nla_put_failure
;
815 return genlmsg_end(msg
, hdr
);
819 genlmsg_cancel(msg
, hdr
);
823 static int nfc_genl_llc_get_params(struct sk_buff
*skb
, struct genl_info
*info
)
826 struct nfc_llcp_local
*local
;
828 struct sk_buff
*msg
= NULL
;
831 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
834 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
836 dev
= nfc_get_device(idx
);
840 device_lock(&dev
->dev
);
842 local
= nfc_llcp_find_local(dev
);
848 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
854 rc
= nfc_genl_send_params(msg
, local
, info
->snd_portid
, info
->snd_seq
);
857 device_unlock(&dev
->dev
);
868 return genlmsg_reply(msg
, info
);
871 static int nfc_genl_llc_set_params(struct sk_buff
*skb
, struct genl_info
*info
)
874 struct nfc_llcp_local
*local
;
880 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
881 (!info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
] &&
882 !info
->attrs
[NFC_ATTR_LLC_PARAM_RW
] &&
883 !info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]))
886 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]) {
887 rw
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]);
889 if (rw
> LLCP_MAX_RW
)
893 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]) {
894 miux
= nla_get_u16(info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]);
896 if (miux
> LLCP_MAX_MIUX
)
900 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
902 dev
= nfc_get_device(idx
);
906 device_lock(&dev
->dev
);
908 local
= nfc_llcp_find_local(dev
);
915 if (info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]) {
916 if (dev
->dep_link_up
) {
921 local
->lto
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]);
924 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
])
927 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
])
928 local
->miux
= cpu_to_be16(miux
);
931 device_unlock(&dev
->dev
);
938 static int nfc_genl_llc_sdreq(struct sk_buff
*skb
, struct genl_info
*info
)
941 struct nfc_llcp_local
*local
;
942 struct nlattr
*attr
, *sdp_attrs
[NFC_SDP_ATTR_MAX
+1];
947 size_t uri_len
, tlvs_len
;
948 struct hlist_head sdreq_list
;
949 struct nfc_llcp_sdp_tlv
*sdreq
;
951 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
952 !info
->attrs
[NFC_ATTR_LLC_SDP
])
955 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
957 dev
= nfc_get_device(idx
);
963 device_lock(&dev
->dev
);
965 if (dev
->dep_link_up
== false) {
970 local
= nfc_llcp_find_local(dev
);
977 INIT_HLIST_HEAD(&sdreq_list
);
981 nla_for_each_nested(attr
, info
->attrs
[NFC_ATTR_LLC_SDP
], rem
) {
982 rc
= nla_parse_nested(sdp_attrs
, NFC_SDP_ATTR_MAX
, attr
,
983 nfc_sdp_genl_policy
);
990 if (!sdp_attrs
[NFC_SDP_ATTR_URI
])
993 uri_len
= nla_len(sdp_attrs
[NFC_SDP_ATTR_URI
]);
997 uri
= nla_data(sdp_attrs
[NFC_SDP_ATTR_URI
]);
998 if (uri
== NULL
|| *uri
== 0)
1001 tid
= local
->sdreq_next_tid
++;
1003 sdreq
= nfc_llcp_build_sdreq_tlv(tid
, uri
, uri_len
);
1004 if (sdreq
== NULL
) {
1009 tlvs_len
+= sdreq
->tlv_len
;
1011 hlist_add_head(&sdreq
->node
, &sdreq_list
);
1014 if (hlist_empty(&sdreq_list
)) {
1019 rc
= nfc_llcp_send_snl_sdreq(local
, &sdreq_list
, tlvs_len
);
1021 device_unlock(&dev
->dev
);
1023 nfc_put_device(dev
);
1028 static struct genl_ops nfc_genl_ops
[] = {
1030 .cmd
= NFC_CMD_GET_DEVICE
,
1031 .doit
= nfc_genl_get_device
,
1032 .dumpit
= nfc_genl_dump_devices
,
1033 .done
= nfc_genl_dump_devices_done
,
1034 .policy
= nfc_genl_policy
,
1037 .cmd
= NFC_CMD_DEV_UP
,
1038 .doit
= nfc_genl_dev_up
,
1039 .policy
= nfc_genl_policy
,
1042 .cmd
= NFC_CMD_DEV_DOWN
,
1043 .doit
= nfc_genl_dev_down
,
1044 .policy
= nfc_genl_policy
,
1047 .cmd
= NFC_CMD_START_POLL
,
1048 .doit
= nfc_genl_start_poll
,
1049 .policy
= nfc_genl_policy
,
1052 .cmd
= NFC_CMD_STOP_POLL
,
1053 .doit
= nfc_genl_stop_poll
,
1054 .policy
= nfc_genl_policy
,
1057 .cmd
= NFC_CMD_DEP_LINK_UP
,
1058 .doit
= nfc_genl_dep_link_up
,
1059 .policy
= nfc_genl_policy
,
1062 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
1063 .doit
= nfc_genl_dep_link_down
,
1064 .policy
= nfc_genl_policy
,
1067 .cmd
= NFC_CMD_GET_TARGET
,
1068 .dumpit
= nfc_genl_dump_targets
,
1069 .done
= nfc_genl_dump_targets_done
,
1070 .policy
= nfc_genl_policy
,
1073 .cmd
= NFC_CMD_LLC_GET_PARAMS
,
1074 .doit
= nfc_genl_llc_get_params
,
1075 .policy
= nfc_genl_policy
,
1078 .cmd
= NFC_CMD_LLC_SET_PARAMS
,
1079 .doit
= nfc_genl_llc_set_params
,
1080 .policy
= nfc_genl_policy
,
1083 .cmd
= NFC_CMD_LLC_SDREQ
,
1084 .doit
= nfc_genl_llc_sdreq
,
1085 .policy
= nfc_genl_policy
,
1090 struct urelease_work
{
1091 struct work_struct w
;
1095 static void nfc_urelease_event_work(struct work_struct
*work
)
1097 struct urelease_work
*w
= container_of(work
, struct urelease_work
, w
);
1098 struct class_dev_iter iter
;
1099 struct nfc_dev
*dev
;
1101 pr_debug("portid %d\n", w
->portid
);
1103 mutex_lock(&nfc_devlist_mutex
);
1105 nfc_device_iter_init(&iter
);
1106 dev
= nfc_device_iter_next(&iter
);
1109 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
1111 if (dev
->genl_data
.poll_req_portid
== w
->portid
) {
1113 dev
->genl_data
.poll_req_portid
= 0;
1116 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
1118 dev
= nfc_device_iter_next(&iter
);
1121 nfc_device_iter_exit(&iter
);
1123 mutex_unlock(&nfc_devlist_mutex
);
1128 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
1129 unsigned long event
, void *ptr
)
1131 struct netlink_notify
*n
= ptr
;
1132 struct urelease_work
*w
;
1134 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
1137 pr_debug("NETLINK_URELEASE event from id %d\n", n
->portid
);
1139 w
= kmalloc(sizeof(*w
), GFP_ATOMIC
);
1141 INIT_WORK((struct work_struct
*) w
, nfc_urelease_event_work
);
1142 w
->portid
= n
->portid
;
1143 schedule_work((struct work_struct
*) w
);
1150 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
1152 genl_data
->poll_req_portid
= 0;
1153 mutex_init(&genl_data
->genl_data_mutex
);
1156 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
1158 mutex_destroy(&genl_data
->genl_data_mutex
);
1161 static struct notifier_block nl_notifier
= {
1162 .notifier_call
= nfc_genl_rcv_nl_event
,
1166 * nfc_genl_init() - Initialize netlink interface
1168 * This initialization function registers the nfc netlink family.
1170 int __init
nfc_genl_init(void)
1174 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
1175 ARRAY_SIZE(nfc_genl_ops
));
1179 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
1181 netlink_register_notifier(&nl_notifier
);
1187 * nfc_genl_exit() - Deinitialize netlink interface
1189 * This exit function unregisters the nfc netlink family.
1191 void nfc_genl_exit(void)
1193 netlink_unregister_notifier(&nl_notifier
);
1194 genl_unregister_family(&nfc_genl_family
);