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
},
59 [NFC_ATTR_FIRMWARE_NAME
] = { .type
= NLA_STRING
,
60 .len
= NFC_FIRMWARE_NAME_MAXSIZE
},
63 static const struct nla_policy nfc_sdp_genl_policy
[NFC_SDP_ATTR_MAX
+ 1] = {
64 [NFC_SDP_ATTR_URI
] = { .type
= NLA_STRING
},
65 [NFC_SDP_ATTR_SAP
] = { .type
= NLA_U8
},
68 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
69 struct netlink_callback
*cb
, int flags
)
73 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
74 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
78 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
80 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
81 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
82 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
83 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
85 if (target
->nfcid1_len
> 0 &&
86 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
89 if (target
->sensb_res_len
> 0 &&
90 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
93 if (target
->sensf_res_len
> 0 &&
94 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
98 return genlmsg_end(msg
, hdr
);
101 genlmsg_cancel(msg
, hdr
);
105 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
111 rc
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ nfc_genl_family
.hdrsize
,
112 nfc_genl_family
.attrbuf
,
113 nfc_genl_family
.maxattr
,
118 if (!nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
])
119 return ERR_PTR(-EINVAL
);
121 idx
= nla_get_u32(nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
]);
123 dev
= nfc_get_device(idx
);
125 return ERR_PTR(-ENODEV
);
130 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
131 struct netlink_callback
*cb
)
134 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
138 dev
= __get_device_from_cb(cb
);
142 cb
->args
[1] = (long) dev
;
145 device_lock(&dev
->dev
);
147 cb
->seq
= dev
->targets_generation
;
149 while (i
< dev
->n_targets
) {
150 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
158 device_unlock(&dev
->dev
);
165 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
167 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
175 int nfc_genl_targets_found(struct nfc_dev
*dev
)
180 dev
->genl_data
.poll_req_portid
= 0;
182 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
186 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
187 NFC_EVENT_TARGETS_FOUND
);
191 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
192 goto nla_put_failure
;
194 genlmsg_end(msg
, hdr
);
196 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
199 genlmsg_cancel(msg
, hdr
);
205 int nfc_genl_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
210 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
214 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
215 NFC_EVENT_TARGET_LOST
);
219 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
220 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
221 goto nla_put_failure
;
223 genlmsg_end(msg
, hdr
);
225 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
230 genlmsg_cancel(msg
, hdr
);
236 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
241 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
245 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
246 NFC_EVENT_TM_ACTIVATED
);
250 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
251 goto nla_put_failure
;
252 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
253 goto nla_put_failure
;
255 genlmsg_end(msg
, hdr
);
257 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
262 genlmsg_cancel(msg
, hdr
);
268 int nfc_genl_tm_deactivated(struct nfc_dev
*dev
)
273 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
277 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
278 NFC_EVENT_TM_DEACTIVATED
);
282 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
283 goto nla_put_failure
;
285 genlmsg_end(msg
, hdr
);
287 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
292 genlmsg_cancel(msg
, hdr
);
298 int nfc_genl_device_added(struct nfc_dev
*dev
)
303 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
307 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
308 NFC_EVENT_DEVICE_ADDED
);
312 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
313 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
314 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
315 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
316 goto nla_put_failure
;
318 genlmsg_end(msg
, hdr
);
320 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
325 genlmsg_cancel(msg
, hdr
);
331 int nfc_genl_device_removed(struct nfc_dev
*dev
)
336 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
340 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
341 NFC_EVENT_DEVICE_REMOVED
);
345 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
346 goto nla_put_failure
;
348 genlmsg_end(msg
, hdr
);
350 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
355 genlmsg_cancel(msg
, hdr
);
361 int nfc_genl_llc_send_sdres(struct nfc_dev
*dev
, struct hlist_head
*sdres_list
)
364 struct nlattr
*sdp_attr
, *uri_attr
;
365 struct nfc_llcp_sdp_tlv
*sdres
;
366 struct hlist_node
*n
;
371 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
375 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
376 NFC_EVENT_LLC_SDRES
);
380 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
381 goto nla_put_failure
;
383 sdp_attr
= nla_nest_start(msg
, NFC_ATTR_LLC_SDP
);
384 if (sdp_attr
== NULL
) {
386 goto nla_put_failure
;
390 hlist_for_each_entry_safe(sdres
, n
, sdres_list
, node
) {
391 pr_debug("uri: %s, sap: %d\n", sdres
->uri
, sdres
->sap
);
393 uri_attr
= nla_nest_start(msg
, i
++);
394 if (uri_attr
== NULL
) {
396 goto nla_put_failure
;
399 if (nla_put_u8(msg
, NFC_SDP_ATTR_SAP
, sdres
->sap
))
400 goto nla_put_failure
;
402 if (nla_put_string(msg
, NFC_SDP_ATTR_URI
, sdres
->uri
))
403 goto nla_put_failure
;
405 nla_nest_end(msg
, uri_attr
);
407 hlist_del(&sdres
->node
);
409 nfc_llcp_free_sdp_tlv(sdres
);
412 nla_nest_end(msg
, sdp_attr
);
414 genlmsg_end(msg
, hdr
);
416 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
419 genlmsg_cancel(msg
, hdr
);
424 nfc_llcp_free_sdp_tlv_list(sdres_list
);
429 int nfc_genl_se_added(struct nfc_dev
*dev
, u32 se_idx
, u16 type
)
434 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
438 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
443 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
444 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
) ||
445 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, type
))
446 goto nla_put_failure
;
448 genlmsg_end(msg
, hdr
);
450 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
455 genlmsg_cancel(msg
, hdr
);
461 int nfc_genl_se_removed(struct nfc_dev
*dev
, u32 se_idx
)
466 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
470 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
471 NFC_EVENT_SE_REMOVED
);
475 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
476 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se_idx
))
477 goto nla_put_failure
;
479 genlmsg_end(msg
, hdr
);
481 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
486 genlmsg_cancel(msg
, hdr
);
492 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
494 struct netlink_callback
*cb
,
499 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
505 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
507 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
508 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
509 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
510 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
) ||
511 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, dev
->rf_mode
))
512 goto nla_put_failure
;
514 return genlmsg_end(msg
, hdr
);
517 genlmsg_cancel(msg
, hdr
);
521 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
522 struct netlink_callback
*cb
)
524 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
525 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
526 bool first_call
= false;
530 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
533 cb
->args
[0] = (long) iter
;
536 mutex_lock(&nfc_devlist_mutex
);
538 cb
->seq
= nfc_devlist_generation
;
541 nfc_device_iter_init(iter
);
542 dev
= nfc_device_iter_next(iter
);
548 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
549 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
553 dev
= nfc_device_iter_next(iter
);
556 mutex_unlock(&nfc_devlist_mutex
);
558 cb
->args
[1] = (long) dev
;
563 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
565 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
567 nfc_device_iter_exit(iter
);
573 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
574 u8 comm_mode
, u8 rf_mode
)
579 pr_debug("DEP link is up\n");
581 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
585 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
589 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
590 goto nla_put_failure
;
591 if (rf_mode
== NFC_RF_INITIATOR
&&
592 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
593 goto nla_put_failure
;
594 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
595 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
596 goto nla_put_failure
;
598 genlmsg_end(msg
, hdr
);
600 dev
->dep_link_up
= true;
602 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
607 genlmsg_cancel(msg
, hdr
);
613 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
618 pr_debug("DEP link is down\n");
620 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
624 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
625 NFC_CMD_DEP_LINK_DOWN
);
629 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
630 goto nla_put_failure
;
632 genlmsg_end(msg
, hdr
);
634 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
639 genlmsg_cancel(msg
, hdr
);
645 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
652 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
655 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
657 dev
= nfc_get_device(idx
);
661 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
667 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_portid
, info
->snd_seq
,
674 return genlmsg_reply(msg
, info
);
683 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
689 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
692 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
694 dev
= nfc_get_device(idx
);
698 rc
= nfc_dev_up(dev
);
704 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
710 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
713 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
715 dev
= nfc_get_device(idx
);
719 rc
= nfc_dev_down(dev
);
725 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
730 u32 im_protocols
= 0, tm_protocols
= 0;
732 pr_debug("Poll start\n");
734 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
735 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
736 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
737 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
740 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
742 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
743 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
745 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
746 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
747 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
748 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
750 dev
= nfc_get_device(idx
);
754 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
756 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
758 dev
->genl_data
.poll_req_portid
= info
->snd_portid
;
760 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
766 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
772 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
775 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
777 dev
= nfc_get_device(idx
);
781 device_lock(&dev
->dev
);
784 device_unlock(&dev
->dev
);
788 device_unlock(&dev
->dev
);
790 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
792 if (dev
->genl_data
.poll_req_portid
!= info
->snd_portid
) {
797 rc
= nfc_stop_poll(dev
);
798 dev
->genl_data
.poll_req_portid
= 0;
801 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
806 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
813 pr_debug("DEP link up\n");
815 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
816 !info
->attrs
[NFC_ATTR_COMM_MODE
])
819 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
820 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
821 tgt_idx
= NFC_TARGET_IDX_ANY
;
823 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
825 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
827 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
830 dev
= nfc_get_device(idx
);
834 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
841 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
847 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
850 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
852 dev
= nfc_get_device(idx
);
856 rc
= nfc_dep_link_down(dev
);
862 static int nfc_genl_send_params(struct sk_buff
*msg
,
863 struct nfc_llcp_local
*local
,
868 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, 0,
869 NFC_CMD_LLC_GET_PARAMS
);
873 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, local
->dev
->idx
) ||
874 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_LTO
, local
->lto
) ||
875 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_RW
, local
->rw
) ||
876 nla_put_u16(msg
, NFC_ATTR_LLC_PARAM_MIUX
, be16_to_cpu(local
->miux
)))
877 goto nla_put_failure
;
879 return genlmsg_end(msg
, hdr
);
883 genlmsg_cancel(msg
, hdr
);
887 static int nfc_genl_llc_get_params(struct sk_buff
*skb
, struct genl_info
*info
)
890 struct nfc_llcp_local
*local
;
892 struct sk_buff
*msg
= NULL
;
895 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
898 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
900 dev
= nfc_get_device(idx
);
904 device_lock(&dev
->dev
);
906 local
= nfc_llcp_find_local(dev
);
912 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
918 rc
= nfc_genl_send_params(msg
, local
, info
->snd_portid
, info
->snd_seq
);
921 device_unlock(&dev
->dev
);
932 return genlmsg_reply(msg
, info
);
935 static int nfc_genl_llc_set_params(struct sk_buff
*skb
, struct genl_info
*info
)
938 struct nfc_llcp_local
*local
;
944 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
945 (!info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
] &&
946 !info
->attrs
[NFC_ATTR_LLC_PARAM_RW
] &&
947 !info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]))
950 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]) {
951 rw
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]);
953 if (rw
> LLCP_MAX_RW
)
957 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]) {
958 miux
= nla_get_u16(info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]);
960 if (miux
> LLCP_MAX_MIUX
)
964 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
966 dev
= nfc_get_device(idx
);
970 device_lock(&dev
->dev
);
972 local
= nfc_llcp_find_local(dev
);
979 if (info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]) {
980 if (dev
->dep_link_up
) {
985 local
->lto
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]);
988 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
])
991 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
])
992 local
->miux
= cpu_to_be16(miux
);
995 device_unlock(&dev
->dev
);
1002 static int nfc_genl_llc_sdreq(struct sk_buff
*skb
, struct genl_info
*info
)
1004 struct nfc_dev
*dev
;
1005 struct nfc_llcp_local
*local
;
1006 struct nlattr
*attr
, *sdp_attrs
[NFC_SDP_ATTR_MAX
+1];
1011 size_t uri_len
, tlvs_len
;
1012 struct hlist_head sdreq_list
;
1013 struct nfc_llcp_sdp_tlv
*sdreq
;
1015 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1016 !info
->attrs
[NFC_ATTR_LLC_SDP
])
1019 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1021 dev
= nfc_get_device(idx
);
1027 device_lock(&dev
->dev
);
1029 if (dev
->dep_link_up
== false) {
1034 local
= nfc_llcp_find_local(dev
);
1036 nfc_put_device(dev
);
1041 INIT_HLIST_HEAD(&sdreq_list
);
1045 nla_for_each_nested(attr
, info
->attrs
[NFC_ATTR_LLC_SDP
], rem
) {
1046 rc
= nla_parse_nested(sdp_attrs
, NFC_SDP_ATTR_MAX
, attr
,
1047 nfc_sdp_genl_policy
);
1054 if (!sdp_attrs
[NFC_SDP_ATTR_URI
])
1057 uri_len
= nla_len(sdp_attrs
[NFC_SDP_ATTR_URI
]);
1061 uri
= nla_data(sdp_attrs
[NFC_SDP_ATTR_URI
]);
1062 if (uri
== NULL
|| *uri
== 0)
1065 tid
= local
->sdreq_next_tid
++;
1067 sdreq
= nfc_llcp_build_sdreq_tlv(tid
, uri
, uri_len
);
1068 if (sdreq
== NULL
) {
1073 tlvs_len
+= sdreq
->tlv_len
;
1075 hlist_add_head(&sdreq
->node
, &sdreq_list
);
1078 if (hlist_empty(&sdreq_list
)) {
1083 rc
= nfc_llcp_send_snl_sdreq(local
, &sdreq_list
, tlvs_len
);
1085 device_unlock(&dev
->dev
);
1087 nfc_put_device(dev
);
1092 static int nfc_genl_fw_download(struct sk_buff
*skb
, struct genl_info
*info
)
1094 struct nfc_dev
*dev
;
1097 char firmware_name
[NFC_FIRMWARE_NAME_MAXSIZE
+ 1];
1099 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
1102 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1104 dev
= nfc_get_device(idx
);
1108 nla_strlcpy(firmware_name
, info
->attrs
[NFC_ATTR_FIRMWARE_NAME
],
1109 sizeof(firmware_name
));
1111 rc
= nfc_fw_download(dev
, firmware_name
);
1113 nfc_put_device(dev
);
1117 int nfc_genl_fw_download_done(struct nfc_dev
*dev
, const char *firmware_name
,
1120 struct sk_buff
*msg
;
1123 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1127 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
1128 NFC_CMD_FW_DOWNLOAD
);
1132 if (nla_put_string(msg
, NFC_ATTR_FIRMWARE_NAME
, firmware_name
) ||
1133 nla_put_u32(msg
, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS
, result
) ||
1134 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
1135 goto nla_put_failure
;
1137 genlmsg_end(msg
, hdr
);
1139 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
1144 genlmsg_cancel(msg
, hdr
);
1150 static int nfc_genl_enable_se(struct sk_buff
*skb
, struct genl_info
*info
)
1152 struct nfc_dev
*dev
;
1156 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1157 !info
->attrs
[NFC_ATTR_SE_INDEX
])
1160 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1161 se_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_SE_INDEX
]);
1163 dev
= nfc_get_device(idx
);
1167 rc
= nfc_enable_se(dev
, se_idx
);
1169 nfc_put_device(dev
);
1173 static int nfc_genl_disable_se(struct sk_buff
*skb
, struct genl_info
*info
)
1175 struct nfc_dev
*dev
;
1179 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
1180 !info
->attrs
[NFC_ATTR_SE_INDEX
])
1183 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
1184 se_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_SE_INDEX
]);
1186 dev
= nfc_get_device(idx
);
1190 rc
= nfc_disable_se(dev
, se_idx
);
1192 nfc_put_device(dev
);
1196 static int nfc_genl_send_se(struct sk_buff
*msg
, struct nfc_dev
*dev
,
1197 u32 portid
, u32 seq
,
1198 struct netlink_callback
*cb
,
1202 struct nfc_se
*se
, *n
;
1204 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
1205 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
1208 goto nla_put_failure
;
1211 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
1213 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
1214 nla_put_u32(msg
, NFC_ATTR_SE_INDEX
, se
->idx
) ||
1215 nla_put_u8(msg
, NFC_ATTR_SE_TYPE
, se
->type
))
1216 goto nla_put_failure
;
1218 if (genlmsg_end(msg
, hdr
) < 0)
1219 goto nla_put_failure
;
1225 genlmsg_cancel(msg
, hdr
);
1229 static int nfc_genl_dump_ses(struct sk_buff
*skb
,
1230 struct netlink_callback
*cb
)
1232 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
1233 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
1234 bool first_call
= false;
1238 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
1241 cb
->args
[0] = (long) iter
;
1244 mutex_lock(&nfc_devlist_mutex
);
1246 cb
->seq
= nfc_devlist_generation
;
1249 nfc_device_iter_init(iter
);
1250 dev
= nfc_device_iter_next(iter
);
1256 rc
= nfc_genl_send_se(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
1257 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
1261 dev
= nfc_device_iter_next(iter
);
1264 mutex_unlock(&nfc_devlist_mutex
);
1266 cb
->args
[1] = (long) dev
;
1271 static int nfc_genl_dump_ses_done(struct netlink_callback
*cb
)
1273 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
1275 nfc_device_iter_exit(iter
);
1281 static struct genl_ops nfc_genl_ops
[] = {
1283 .cmd
= NFC_CMD_GET_DEVICE
,
1284 .doit
= nfc_genl_get_device
,
1285 .dumpit
= nfc_genl_dump_devices
,
1286 .done
= nfc_genl_dump_devices_done
,
1287 .policy
= nfc_genl_policy
,
1290 .cmd
= NFC_CMD_DEV_UP
,
1291 .doit
= nfc_genl_dev_up
,
1292 .policy
= nfc_genl_policy
,
1295 .cmd
= NFC_CMD_DEV_DOWN
,
1296 .doit
= nfc_genl_dev_down
,
1297 .policy
= nfc_genl_policy
,
1300 .cmd
= NFC_CMD_START_POLL
,
1301 .doit
= nfc_genl_start_poll
,
1302 .policy
= nfc_genl_policy
,
1305 .cmd
= NFC_CMD_STOP_POLL
,
1306 .doit
= nfc_genl_stop_poll
,
1307 .policy
= nfc_genl_policy
,
1310 .cmd
= NFC_CMD_DEP_LINK_UP
,
1311 .doit
= nfc_genl_dep_link_up
,
1312 .policy
= nfc_genl_policy
,
1315 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
1316 .doit
= nfc_genl_dep_link_down
,
1317 .policy
= nfc_genl_policy
,
1320 .cmd
= NFC_CMD_GET_TARGET
,
1321 .dumpit
= nfc_genl_dump_targets
,
1322 .done
= nfc_genl_dump_targets_done
,
1323 .policy
= nfc_genl_policy
,
1326 .cmd
= NFC_CMD_LLC_GET_PARAMS
,
1327 .doit
= nfc_genl_llc_get_params
,
1328 .policy
= nfc_genl_policy
,
1331 .cmd
= NFC_CMD_LLC_SET_PARAMS
,
1332 .doit
= nfc_genl_llc_set_params
,
1333 .policy
= nfc_genl_policy
,
1336 .cmd
= NFC_CMD_LLC_SDREQ
,
1337 .doit
= nfc_genl_llc_sdreq
,
1338 .policy
= nfc_genl_policy
,
1341 .cmd
= NFC_CMD_FW_DOWNLOAD
,
1342 .doit
= nfc_genl_fw_download
,
1343 .policy
= nfc_genl_policy
,
1346 .cmd
= NFC_CMD_ENABLE_SE
,
1347 .doit
= nfc_genl_enable_se
,
1348 .policy
= nfc_genl_policy
,
1351 .cmd
= NFC_CMD_DISABLE_SE
,
1352 .doit
= nfc_genl_disable_se
,
1353 .policy
= nfc_genl_policy
,
1356 .cmd
= NFC_CMD_GET_SE
,
1357 .dumpit
= nfc_genl_dump_ses
,
1358 .done
= nfc_genl_dump_ses_done
,
1359 .policy
= nfc_genl_policy
,
1364 struct urelease_work
{
1365 struct work_struct w
;
1369 static void nfc_urelease_event_work(struct work_struct
*work
)
1371 struct urelease_work
*w
= container_of(work
, struct urelease_work
, w
);
1372 struct class_dev_iter iter
;
1373 struct nfc_dev
*dev
;
1375 pr_debug("portid %d\n", w
->portid
);
1377 mutex_lock(&nfc_devlist_mutex
);
1379 nfc_device_iter_init(&iter
);
1380 dev
= nfc_device_iter_next(&iter
);
1383 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
1385 if (dev
->genl_data
.poll_req_portid
== w
->portid
) {
1387 dev
->genl_data
.poll_req_portid
= 0;
1390 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
1392 dev
= nfc_device_iter_next(&iter
);
1395 nfc_device_iter_exit(&iter
);
1397 mutex_unlock(&nfc_devlist_mutex
);
1402 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
1403 unsigned long event
, void *ptr
)
1405 struct netlink_notify
*n
= ptr
;
1406 struct urelease_work
*w
;
1408 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
1411 pr_debug("NETLINK_URELEASE event from id %d\n", n
->portid
);
1413 w
= kmalloc(sizeof(*w
), GFP_ATOMIC
);
1415 INIT_WORK((struct work_struct
*) w
, nfc_urelease_event_work
);
1416 w
->portid
= n
->portid
;
1417 schedule_work((struct work_struct
*) w
);
1424 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
1426 genl_data
->poll_req_portid
= 0;
1427 mutex_init(&genl_data
->genl_data_mutex
);
1430 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
1432 mutex_destroy(&genl_data
->genl_data_mutex
);
1435 static struct notifier_block nl_notifier
= {
1436 .notifier_call
= nfc_genl_rcv_nl_event
,
1440 * nfc_genl_init() - Initialize netlink interface
1442 * This initialization function registers the nfc netlink family.
1444 int __init
nfc_genl_init(void)
1448 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
1449 ARRAY_SIZE(nfc_genl_ops
));
1453 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
1455 netlink_register_notifier(&nl_notifier
);
1461 * nfc_genl_exit() - Deinitialize netlink interface
1463 * This exit function unregisters the nfc netlink family.
1465 void nfc_genl_exit(void)
1467 netlink_unregister_notifier(&nl_notifier
);
1468 genl_unregister_family(&nfc_genl_family
);