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>
32 #include "llcp/llcp.h"
34 static struct genl_multicast_group nfc_genl_event_mcgrp
= {
35 .name
= NFC_GENL_MCAST_EVENT_NAME
,
38 static struct genl_family nfc_genl_family
= {
39 .id
= GENL_ID_GENERATE
,
41 .name
= NFC_GENL_NAME
,
42 .version
= NFC_GENL_VERSION
,
43 .maxattr
= NFC_ATTR_MAX
,
46 static const struct nla_policy nfc_genl_policy
[NFC_ATTR_MAX
+ 1] = {
47 [NFC_ATTR_DEVICE_INDEX
] = { .type
= NLA_U32
},
48 [NFC_ATTR_DEVICE_NAME
] = { .type
= NLA_STRING
,
49 .len
= NFC_DEVICE_NAME_MAXSIZE
},
50 [NFC_ATTR_PROTOCOLS
] = { .type
= NLA_U32
},
51 [NFC_ATTR_COMM_MODE
] = { .type
= NLA_U8
},
52 [NFC_ATTR_RF_MODE
] = { .type
= NLA_U8
},
53 [NFC_ATTR_DEVICE_POWERED
] = { .type
= NLA_U8
},
54 [NFC_ATTR_IM_PROTOCOLS
] = { .type
= NLA_U32
},
55 [NFC_ATTR_TM_PROTOCOLS
] = { .type
= NLA_U32
},
56 [NFC_ATTR_LLC_PARAM_LTO
] = { .type
= NLA_U8
},
57 [NFC_ATTR_LLC_PARAM_RW
] = { .type
= NLA_U8
},
58 [NFC_ATTR_LLC_PARAM_MIUX
] = { .type
= NLA_U16
},
59 [NFC_ATTR_LLC_SDP
] = { .type
= NLA_NESTED
},
62 static const struct nla_policy nfc_sdp_genl_policy
[NFC_SDP_ATTR_MAX
+ 1] = {
63 [NFC_SDP_ATTR_URI
] = { .type
= NLA_STRING
},
64 [NFC_SDP_ATTR_SAP
] = { .type
= NLA_U8
},
67 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
68 struct netlink_callback
*cb
, int flags
)
72 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
73 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
77 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
79 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
80 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
81 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
82 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
84 if (target
->nfcid1_len
> 0 &&
85 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
88 if (target
->sensb_res_len
> 0 &&
89 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
92 if (target
->sensf_res_len
> 0 &&
93 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
97 return genlmsg_end(msg
, hdr
);
100 genlmsg_cancel(msg
, hdr
);
104 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
110 rc
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ nfc_genl_family
.hdrsize
,
111 nfc_genl_family
.attrbuf
,
112 nfc_genl_family
.maxattr
,
117 if (!nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
])
118 return ERR_PTR(-EINVAL
);
120 idx
= nla_get_u32(nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
]);
122 dev
= nfc_get_device(idx
);
124 return ERR_PTR(-ENODEV
);
129 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
130 struct netlink_callback
*cb
)
133 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
137 dev
= __get_device_from_cb(cb
);
141 cb
->args
[1] = (long) dev
;
144 device_lock(&dev
->dev
);
146 cb
->seq
= dev
->targets_generation
;
148 while (i
< dev
->n_targets
) {
149 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
157 device_unlock(&dev
->dev
);
164 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
166 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
174 int nfc_genl_targets_found(struct nfc_dev
*dev
)
179 dev
->genl_data
.poll_req_portid
= 0;
181 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
185 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
186 NFC_EVENT_TARGETS_FOUND
);
190 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
191 goto nla_put_failure
;
193 genlmsg_end(msg
, hdr
);
195 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
198 genlmsg_cancel(msg
, hdr
);
204 int nfc_genl_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
209 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
213 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
214 NFC_EVENT_TARGET_LOST
);
218 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
219 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
220 goto nla_put_failure
;
222 genlmsg_end(msg
, hdr
);
224 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
229 genlmsg_cancel(msg
, hdr
);
235 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
240 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
244 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
245 NFC_EVENT_TM_ACTIVATED
);
249 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
250 goto nla_put_failure
;
251 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
252 goto nla_put_failure
;
254 genlmsg_end(msg
, hdr
);
256 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
261 genlmsg_cancel(msg
, hdr
);
267 int nfc_genl_tm_deactivated(struct nfc_dev
*dev
)
272 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
276 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
277 NFC_EVENT_TM_DEACTIVATED
);
281 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
282 goto nla_put_failure
;
284 genlmsg_end(msg
, hdr
);
286 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
291 genlmsg_cancel(msg
, hdr
);
297 int nfc_genl_device_added(struct nfc_dev
*dev
)
302 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
306 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
307 NFC_EVENT_DEVICE_ADDED
);
311 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
312 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
313 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
314 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
315 goto nla_put_failure
;
317 genlmsg_end(msg
, hdr
);
319 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
324 genlmsg_cancel(msg
, hdr
);
330 int nfc_genl_device_removed(struct nfc_dev
*dev
)
335 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
339 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
340 NFC_EVENT_DEVICE_REMOVED
);
344 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
345 goto nla_put_failure
;
347 genlmsg_end(msg
, hdr
);
349 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
354 genlmsg_cancel(msg
, hdr
);
360 int nfc_genl_llc_send_sdres(struct nfc_dev
*dev
, struct hlist_head
*sdres_list
)
363 struct nlattr
*sdp_attr
, *uri_attr
;
364 struct nfc_llcp_sdp_tlv
*sdres
;
365 struct hlist_node
*n
;
370 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
374 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
375 NFC_EVENT_LLC_SDRES
);
379 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
380 goto nla_put_failure
;
382 sdp_attr
= nla_nest_start(msg
, NFC_ATTR_LLC_SDP
);
383 if (sdp_attr
== NULL
) {
385 goto nla_put_failure
;
389 hlist_for_each_entry_safe(sdres
, n
, sdres_list
, node
) {
390 pr_debug("uri: %s, sap: %d\n", sdres
->uri
, sdres
->sap
);
392 uri_attr
= nla_nest_start(msg
, i
++);
393 if (uri_attr
== NULL
) {
395 goto nla_put_failure
;
398 if (nla_put_u8(msg
, NFC_SDP_ATTR_SAP
, sdres
->sap
))
399 goto nla_put_failure
;
401 if (nla_put_string(msg
, NFC_SDP_ATTR_URI
, sdres
->uri
))
402 goto nla_put_failure
;
404 nla_nest_end(msg
, uri_attr
);
406 hlist_del(&sdres
->node
);
408 nfc_llcp_free_sdp_tlv(sdres
);
411 nla_nest_end(msg
, sdp_attr
);
413 genlmsg_end(msg
, hdr
);
415 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
418 genlmsg_cancel(msg
, hdr
);
423 nfc_llcp_free_sdp_tlv_list(sdres_list
);
428 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
430 struct netlink_callback
*cb
,
435 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
441 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
443 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
444 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
445 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
446 nla_put_u32(msg
, NFC_ATTR_SE
, dev
->supported_se
) ||
447 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
) ||
448 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, dev
->rf_mode
))
449 goto nla_put_failure
;
451 return genlmsg_end(msg
, hdr
);
454 genlmsg_cancel(msg
, hdr
);
458 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
459 struct netlink_callback
*cb
)
461 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
462 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
463 bool first_call
= false;
467 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
470 cb
->args
[0] = (long) iter
;
473 mutex_lock(&nfc_devlist_mutex
);
475 cb
->seq
= nfc_devlist_generation
;
478 nfc_device_iter_init(iter
);
479 dev
= nfc_device_iter_next(iter
);
485 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
486 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
490 dev
= nfc_device_iter_next(iter
);
493 mutex_unlock(&nfc_devlist_mutex
);
495 cb
->args
[1] = (long) dev
;
500 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
502 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
504 nfc_device_iter_exit(iter
);
510 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
511 u8 comm_mode
, u8 rf_mode
)
516 pr_debug("DEP link is up\n");
518 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
522 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
526 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
527 goto nla_put_failure
;
528 if (rf_mode
== NFC_RF_INITIATOR
&&
529 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
530 goto nla_put_failure
;
531 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
532 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
533 goto nla_put_failure
;
535 genlmsg_end(msg
, hdr
);
537 dev
->dep_link_up
= true;
539 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
544 genlmsg_cancel(msg
, hdr
);
550 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
555 pr_debug("DEP link is down\n");
557 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
561 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
562 NFC_CMD_DEP_LINK_DOWN
);
566 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
567 goto nla_put_failure
;
569 genlmsg_end(msg
, hdr
);
571 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
576 genlmsg_cancel(msg
, hdr
);
582 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
589 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
592 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
594 dev
= nfc_get_device(idx
);
598 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
604 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_portid
, info
->snd_seq
,
611 return genlmsg_reply(msg
, info
);
620 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
626 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
629 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
631 dev
= nfc_get_device(idx
);
635 rc
= nfc_dev_up(dev
);
641 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
647 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
650 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
652 dev
= nfc_get_device(idx
);
656 rc
= nfc_dev_down(dev
);
662 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
667 u32 im_protocols
= 0, tm_protocols
= 0;
669 pr_debug("Poll start\n");
671 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
672 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
673 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
674 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
677 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
679 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
680 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
682 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
683 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
684 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
685 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
687 dev
= nfc_get_device(idx
);
691 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
693 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
695 dev
->genl_data
.poll_req_portid
= info
->snd_portid
;
697 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
703 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
709 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
712 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
714 dev
= nfc_get_device(idx
);
718 device_lock(&dev
->dev
);
721 device_unlock(&dev
->dev
);
725 device_unlock(&dev
->dev
);
727 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
729 if (dev
->genl_data
.poll_req_portid
!= info
->snd_portid
) {
734 rc
= nfc_stop_poll(dev
);
735 dev
->genl_data
.poll_req_portid
= 0;
738 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
743 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
750 pr_debug("DEP link up\n");
752 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
753 !info
->attrs
[NFC_ATTR_COMM_MODE
])
756 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
757 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
758 tgt_idx
= NFC_TARGET_IDX_ANY
;
760 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
762 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
764 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
767 dev
= nfc_get_device(idx
);
771 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
778 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
784 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
787 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
789 dev
= nfc_get_device(idx
);
793 rc
= nfc_dep_link_down(dev
);
799 static int nfc_genl_send_params(struct sk_buff
*msg
,
800 struct nfc_llcp_local
*local
,
805 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, 0,
806 NFC_CMD_LLC_GET_PARAMS
);
810 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, local
->dev
->idx
) ||
811 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_LTO
, local
->lto
) ||
812 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_RW
, local
->rw
) ||
813 nla_put_u16(msg
, NFC_ATTR_LLC_PARAM_MIUX
, be16_to_cpu(local
->miux
)))
814 goto nla_put_failure
;
816 return genlmsg_end(msg
, hdr
);
820 genlmsg_cancel(msg
, hdr
);
824 static int nfc_genl_llc_get_params(struct sk_buff
*skb
, struct genl_info
*info
)
827 struct nfc_llcp_local
*local
;
829 struct sk_buff
*msg
= NULL
;
832 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
835 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
837 dev
= nfc_get_device(idx
);
841 device_lock(&dev
->dev
);
843 local
= nfc_llcp_find_local(dev
);
849 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
855 rc
= nfc_genl_send_params(msg
, local
, info
->snd_portid
, info
->snd_seq
);
858 device_unlock(&dev
->dev
);
869 return genlmsg_reply(msg
, info
);
872 static int nfc_genl_llc_set_params(struct sk_buff
*skb
, struct genl_info
*info
)
875 struct nfc_llcp_local
*local
;
881 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
882 (!info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
] &&
883 !info
->attrs
[NFC_ATTR_LLC_PARAM_RW
] &&
884 !info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]))
887 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]) {
888 rw
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]);
890 if (rw
> LLCP_MAX_RW
)
894 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]) {
895 miux
= nla_get_u16(info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]);
897 if (miux
> LLCP_MAX_MIUX
)
901 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
903 dev
= nfc_get_device(idx
);
907 device_lock(&dev
->dev
);
909 local
= nfc_llcp_find_local(dev
);
916 if (info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]) {
917 if (dev
->dep_link_up
) {
922 local
->lto
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]);
925 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
])
928 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
])
929 local
->miux
= cpu_to_be16(miux
);
932 device_unlock(&dev
->dev
);
939 static int nfc_genl_llc_sdreq(struct sk_buff
*skb
, struct genl_info
*info
)
942 struct nfc_llcp_local
*local
;
943 struct nlattr
*attr
, *sdp_attrs
[NFC_SDP_ATTR_MAX
+1];
948 size_t uri_len
, tlvs_len
;
949 struct hlist_head sdreq_list
;
950 struct nfc_llcp_sdp_tlv
*sdreq
;
952 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
953 !info
->attrs
[NFC_ATTR_LLC_SDP
])
956 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
958 dev
= nfc_get_device(idx
);
964 device_lock(&dev
->dev
);
966 if (dev
->dep_link_up
== false) {
971 local
= nfc_llcp_find_local(dev
);
978 INIT_HLIST_HEAD(&sdreq_list
);
982 nla_for_each_nested(attr
, info
->attrs
[NFC_ATTR_LLC_SDP
], rem
) {
983 rc
= nla_parse_nested(sdp_attrs
, NFC_SDP_ATTR_MAX
, attr
,
984 nfc_sdp_genl_policy
);
991 if (!sdp_attrs
[NFC_SDP_ATTR_URI
])
994 uri_len
= nla_len(sdp_attrs
[NFC_SDP_ATTR_URI
]);
998 uri
= nla_data(sdp_attrs
[NFC_SDP_ATTR_URI
]);
999 if (uri
== NULL
|| *uri
== 0)
1002 tid
= local
->sdreq_next_tid
++;
1004 sdreq
= nfc_llcp_build_sdreq_tlv(tid
, uri
, uri_len
);
1005 if (sdreq
== NULL
) {
1010 tlvs_len
+= sdreq
->tlv_len
;
1012 hlist_add_head(&sdreq
->node
, &sdreq_list
);
1015 if (hlist_empty(&sdreq_list
)) {
1020 rc
= nfc_llcp_send_snl_sdreq(local
, &sdreq_list
, tlvs_len
);
1022 device_unlock(&dev
->dev
);
1024 nfc_put_device(dev
);
1029 static struct genl_ops nfc_genl_ops
[] = {
1031 .cmd
= NFC_CMD_GET_DEVICE
,
1032 .doit
= nfc_genl_get_device
,
1033 .dumpit
= nfc_genl_dump_devices
,
1034 .done
= nfc_genl_dump_devices_done
,
1035 .policy
= nfc_genl_policy
,
1038 .cmd
= NFC_CMD_DEV_UP
,
1039 .doit
= nfc_genl_dev_up
,
1040 .policy
= nfc_genl_policy
,
1043 .cmd
= NFC_CMD_DEV_DOWN
,
1044 .doit
= nfc_genl_dev_down
,
1045 .policy
= nfc_genl_policy
,
1048 .cmd
= NFC_CMD_START_POLL
,
1049 .doit
= nfc_genl_start_poll
,
1050 .policy
= nfc_genl_policy
,
1053 .cmd
= NFC_CMD_STOP_POLL
,
1054 .doit
= nfc_genl_stop_poll
,
1055 .policy
= nfc_genl_policy
,
1058 .cmd
= NFC_CMD_DEP_LINK_UP
,
1059 .doit
= nfc_genl_dep_link_up
,
1060 .policy
= nfc_genl_policy
,
1063 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
1064 .doit
= nfc_genl_dep_link_down
,
1065 .policy
= nfc_genl_policy
,
1068 .cmd
= NFC_CMD_GET_TARGET
,
1069 .dumpit
= nfc_genl_dump_targets
,
1070 .done
= nfc_genl_dump_targets_done
,
1071 .policy
= nfc_genl_policy
,
1074 .cmd
= NFC_CMD_LLC_GET_PARAMS
,
1075 .doit
= nfc_genl_llc_get_params
,
1076 .policy
= nfc_genl_policy
,
1079 .cmd
= NFC_CMD_LLC_SET_PARAMS
,
1080 .doit
= nfc_genl_llc_set_params
,
1081 .policy
= nfc_genl_policy
,
1084 .cmd
= NFC_CMD_LLC_SDREQ
,
1085 .doit
= nfc_genl_llc_sdreq
,
1086 .policy
= nfc_genl_policy
,
1091 struct urelease_work
{
1092 struct work_struct w
;
1096 static void nfc_urelease_event_work(struct work_struct
*work
)
1098 struct urelease_work
*w
= container_of(work
, struct urelease_work
, w
);
1099 struct class_dev_iter iter
;
1100 struct nfc_dev
*dev
;
1102 pr_debug("portid %d\n", w
->portid
);
1104 mutex_lock(&nfc_devlist_mutex
);
1106 nfc_device_iter_init(&iter
);
1107 dev
= nfc_device_iter_next(&iter
);
1110 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
1112 if (dev
->genl_data
.poll_req_portid
== w
->portid
) {
1114 dev
->genl_data
.poll_req_portid
= 0;
1117 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
1119 dev
= nfc_device_iter_next(&iter
);
1122 nfc_device_iter_exit(&iter
);
1124 mutex_unlock(&nfc_devlist_mutex
);
1129 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
1130 unsigned long event
, void *ptr
)
1132 struct netlink_notify
*n
= ptr
;
1133 struct urelease_work
*w
;
1135 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
1138 pr_debug("NETLINK_URELEASE event from id %d\n", n
->portid
);
1140 w
= kmalloc(sizeof(*w
), GFP_ATOMIC
);
1142 INIT_WORK((struct work_struct
*) w
, nfc_urelease_event_work
);
1143 w
->portid
= n
->portid
;
1144 schedule_work((struct work_struct
*) w
);
1151 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
1153 genl_data
->poll_req_portid
= 0;
1154 mutex_init(&genl_data
->genl_data_mutex
);
1157 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
1159 mutex_destroy(&genl_data
->genl_data_mutex
);
1162 static struct notifier_block nl_notifier
= {
1163 .notifier_call
= nfc_genl_rcv_nl_event
,
1167 * nfc_genl_init() - Initialize netlink interface
1169 * This initialization function registers the nfc netlink family.
1171 int __init
nfc_genl_init(void)
1175 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
1176 ARRAY_SIZE(nfc_genl_ops
));
1180 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
1182 netlink_register_notifier(&nl_notifier
);
1188 * nfc_genl_exit() - Deinitialize netlink interface
1190 * This exit function unregisters the nfc netlink family.
1192 void nfc_genl_exit(void)
1194 netlink_unregister_notifier(&nl_notifier
);
1195 genl_unregister_family(&nfc_genl_family
);