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
},
58 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
59 struct netlink_callback
*cb
, int flags
)
63 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
64 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
68 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
70 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
71 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
72 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
73 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
75 if (target
->nfcid1_len
> 0 &&
76 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
79 if (target
->sensb_res_len
> 0 &&
80 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
83 if (target
->sensf_res_len
> 0 &&
84 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
88 return genlmsg_end(msg
, hdr
);
91 genlmsg_cancel(msg
, hdr
);
95 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
101 rc
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ nfc_genl_family
.hdrsize
,
102 nfc_genl_family
.attrbuf
,
103 nfc_genl_family
.maxattr
,
108 if (!nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
])
109 return ERR_PTR(-EINVAL
);
111 idx
= nla_get_u32(nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
]);
113 dev
= nfc_get_device(idx
);
115 return ERR_PTR(-ENODEV
);
120 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
121 struct netlink_callback
*cb
)
124 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
128 dev
= __get_device_from_cb(cb
);
132 cb
->args
[1] = (long) dev
;
135 device_lock(&dev
->dev
);
137 cb
->seq
= dev
->targets_generation
;
139 while (i
< dev
->n_targets
) {
140 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
148 device_unlock(&dev
->dev
);
155 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
157 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
165 int nfc_genl_targets_found(struct nfc_dev
*dev
)
170 dev
->genl_data
.poll_req_portid
= 0;
172 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
176 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
177 NFC_EVENT_TARGETS_FOUND
);
181 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
182 goto nla_put_failure
;
184 genlmsg_end(msg
, hdr
);
186 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
189 genlmsg_cancel(msg
, hdr
);
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(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
220 genlmsg_cancel(msg
, hdr
);
226 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
231 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
235 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
236 NFC_EVENT_TM_ACTIVATED
);
240 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
241 goto nla_put_failure
;
242 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
243 goto nla_put_failure
;
245 genlmsg_end(msg
, hdr
);
247 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
252 genlmsg_cancel(msg
, hdr
);
258 int nfc_genl_tm_deactivated(struct nfc_dev
*dev
)
263 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
267 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
268 NFC_EVENT_TM_DEACTIVATED
);
272 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
273 goto nla_put_failure
;
275 genlmsg_end(msg
, hdr
);
277 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
282 genlmsg_cancel(msg
, hdr
);
288 int nfc_genl_device_added(struct nfc_dev
*dev
)
293 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
297 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
298 NFC_EVENT_DEVICE_ADDED
);
302 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
303 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
304 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
305 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
306 goto nla_put_failure
;
308 genlmsg_end(msg
, hdr
);
310 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
315 genlmsg_cancel(msg
, hdr
);
321 int nfc_genl_device_removed(struct nfc_dev
*dev
)
326 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
330 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
331 NFC_EVENT_DEVICE_REMOVED
);
335 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
336 goto nla_put_failure
;
338 genlmsg_end(msg
, hdr
);
340 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
345 genlmsg_cancel(msg
, hdr
);
351 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
353 struct netlink_callback
*cb
,
358 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, flags
,
364 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
366 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
367 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
368 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
369 nla_put_u32(msg
, NFC_ATTR_SE
, dev
->supported_se
) ||
370 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
) ||
371 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, dev
->rf_mode
))
372 goto nla_put_failure
;
374 return genlmsg_end(msg
, hdr
);
377 genlmsg_cancel(msg
, hdr
);
381 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
382 struct netlink_callback
*cb
)
384 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
385 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
386 bool first_call
= false;
390 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
393 cb
->args
[0] = (long) iter
;
396 mutex_lock(&nfc_devlist_mutex
);
398 cb
->seq
= nfc_devlist_generation
;
401 nfc_device_iter_init(iter
);
402 dev
= nfc_device_iter_next(iter
);
408 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).portid
,
409 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
413 dev
= nfc_device_iter_next(iter
);
416 mutex_unlock(&nfc_devlist_mutex
);
418 cb
->args
[1] = (long) dev
;
423 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
425 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
427 nfc_device_iter_exit(iter
);
433 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
434 u8 comm_mode
, u8 rf_mode
)
439 pr_debug("DEP link is up\n");
441 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
445 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
449 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
450 goto nla_put_failure
;
451 if (rf_mode
== NFC_RF_INITIATOR
&&
452 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
453 goto nla_put_failure
;
454 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
455 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
456 goto nla_put_failure
;
458 genlmsg_end(msg
, hdr
);
460 dev
->dep_link_up
= true;
462 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
467 genlmsg_cancel(msg
, hdr
);
473 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
478 pr_debug("DEP link is down\n");
480 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
484 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
485 NFC_CMD_DEP_LINK_DOWN
);
489 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
490 goto nla_put_failure
;
492 genlmsg_end(msg
, hdr
);
494 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
499 genlmsg_cancel(msg
, hdr
);
505 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
512 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
515 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
517 dev
= nfc_get_device(idx
);
521 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
527 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_portid
, info
->snd_seq
,
534 return genlmsg_reply(msg
, info
);
543 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
549 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
552 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
554 dev
= nfc_get_device(idx
);
558 rc
= nfc_dev_up(dev
);
564 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
570 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
573 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
575 dev
= nfc_get_device(idx
);
579 rc
= nfc_dev_down(dev
);
585 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
590 u32 im_protocols
= 0, tm_protocols
= 0;
592 pr_debug("Poll start\n");
594 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
595 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
596 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
597 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
600 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
602 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
603 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
605 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
606 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
607 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
608 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
610 dev
= nfc_get_device(idx
);
614 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
616 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
618 dev
->genl_data
.poll_req_portid
= info
->snd_portid
;
620 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
626 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
632 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
635 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
637 dev
= nfc_get_device(idx
);
641 device_lock(&dev
->dev
);
644 device_unlock(&dev
->dev
);
648 device_unlock(&dev
->dev
);
650 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
652 if (dev
->genl_data
.poll_req_portid
!= info
->snd_portid
) {
657 rc
= nfc_stop_poll(dev
);
658 dev
->genl_data
.poll_req_portid
= 0;
661 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
666 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
673 pr_debug("DEP link up\n");
675 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
676 !info
->attrs
[NFC_ATTR_COMM_MODE
])
679 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
680 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
681 tgt_idx
= NFC_TARGET_IDX_ANY
;
683 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
685 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
687 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
690 dev
= nfc_get_device(idx
);
694 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
701 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
707 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
710 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
712 dev
= nfc_get_device(idx
);
716 rc
= nfc_dep_link_down(dev
);
722 static int nfc_genl_send_params(struct sk_buff
*msg
,
723 struct nfc_llcp_local
*local
,
728 hdr
= genlmsg_put(msg
, portid
, seq
, &nfc_genl_family
, 0,
729 NFC_CMD_LLC_GET_PARAMS
);
733 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, local
->dev
->idx
) ||
734 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_LTO
, local
->lto
) ||
735 nla_put_u8(msg
, NFC_ATTR_LLC_PARAM_RW
, local
->rw
) ||
736 nla_put_u16(msg
, NFC_ATTR_LLC_PARAM_MIUX
, be16_to_cpu(local
->miux
)))
737 goto nla_put_failure
;
739 return genlmsg_end(msg
, hdr
);
743 genlmsg_cancel(msg
, hdr
);
747 static int nfc_genl_llc_get_params(struct sk_buff
*skb
, struct genl_info
*info
)
750 struct nfc_llcp_local
*local
;
752 struct sk_buff
*msg
= NULL
;
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 device_lock(&dev
->dev
);
766 local
= nfc_llcp_find_local(dev
);
772 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
778 rc
= nfc_genl_send_params(msg
, local
, info
->snd_portid
, info
->snd_seq
);
781 device_unlock(&dev
->dev
);
792 return genlmsg_reply(msg
, info
);
795 static int nfc_genl_llc_set_params(struct sk_buff
*skb
, struct genl_info
*info
)
798 struct nfc_llcp_local
*local
;
804 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
805 (!info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
] &&
806 !info
->attrs
[NFC_ATTR_LLC_PARAM_RW
] &&
807 !info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]))
810 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]) {
811 rw
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_RW
]);
813 if (rw
> LLCP_MAX_RW
)
817 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]) {
818 miux
= nla_get_u16(info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
]);
820 if (miux
> LLCP_MAX_MIUX
)
824 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
826 dev
= nfc_get_device(idx
);
830 device_lock(&dev
->dev
);
832 local
= nfc_llcp_find_local(dev
);
839 if (info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]) {
840 if (dev
->dep_link_up
) {
845 local
->lto
= nla_get_u8(info
->attrs
[NFC_ATTR_LLC_PARAM_LTO
]);
848 if (info
->attrs
[NFC_ATTR_LLC_PARAM_RW
])
851 if (info
->attrs
[NFC_ATTR_LLC_PARAM_MIUX
])
852 local
->miux
= cpu_to_be16(miux
);
855 device_unlock(&dev
->dev
);
862 static struct genl_ops nfc_genl_ops
[] = {
864 .cmd
= NFC_CMD_GET_DEVICE
,
865 .doit
= nfc_genl_get_device
,
866 .dumpit
= nfc_genl_dump_devices
,
867 .done
= nfc_genl_dump_devices_done
,
868 .policy
= nfc_genl_policy
,
871 .cmd
= NFC_CMD_DEV_UP
,
872 .doit
= nfc_genl_dev_up
,
873 .policy
= nfc_genl_policy
,
876 .cmd
= NFC_CMD_DEV_DOWN
,
877 .doit
= nfc_genl_dev_down
,
878 .policy
= nfc_genl_policy
,
881 .cmd
= NFC_CMD_START_POLL
,
882 .doit
= nfc_genl_start_poll
,
883 .policy
= nfc_genl_policy
,
886 .cmd
= NFC_CMD_STOP_POLL
,
887 .doit
= nfc_genl_stop_poll
,
888 .policy
= nfc_genl_policy
,
891 .cmd
= NFC_CMD_DEP_LINK_UP
,
892 .doit
= nfc_genl_dep_link_up
,
893 .policy
= nfc_genl_policy
,
896 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
897 .doit
= nfc_genl_dep_link_down
,
898 .policy
= nfc_genl_policy
,
901 .cmd
= NFC_CMD_GET_TARGET
,
902 .dumpit
= nfc_genl_dump_targets
,
903 .done
= nfc_genl_dump_targets_done
,
904 .policy
= nfc_genl_policy
,
907 .cmd
= NFC_CMD_LLC_GET_PARAMS
,
908 .doit
= nfc_genl_llc_get_params
,
909 .policy
= nfc_genl_policy
,
912 .cmd
= NFC_CMD_LLC_SET_PARAMS
,
913 .doit
= nfc_genl_llc_set_params
,
914 .policy
= nfc_genl_policy
,
919 struct urelease_work
{
920 struct work_struct w
;
924 static void nfc_urelease_event_work(struct work_struct
*work
)
926 struct urelease_work
*w
= container_of(work
, struct urelease_work
, w
);
927 struct class_dev_iter iter
;
930 pr_debug("portid %d\n", w
->portid
);
932 mutex_lock(&nfc_devlist_mutex
);
934 nfc_device_iter_init(&iter
);
935 dev
= nfc_device_iter_next(&iter
);
938 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
940 if (dev
->genl_data
.poll_req_portid
== w
->portid
) {
942 dev
->genl_data
.poll_req_portid
= 0;
945 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
947 dev
= nfc_device_iter_next(&iter
);
950 nfc_device_iter_exit(&iter
);
952 mutex_unlock(&nfc_devlist_mutex
);
957 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
958 unsigned long event
, void *ptr
)
960 struct netlink_notify
*n
= ptr
;
961 struct urelease_work
*w
;
963 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
966 pr_debug("NETLINK_URELEASE event from id %d\n", n
->portid
);
968 w
= kmalloc(sizeof(*w
), GFP_ATOMIC
);
970 INIT_WORK((struct work_struct
*) w
, nfc_urelease_event_work
);
971 w
->portid
= n
->portid
;
972 schedule_work((struct work_struct
*) w
);
979 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
981 genl_data
->poll_req_portid
= 0;
982 mutex_init(&genl_data
->genl_data_mutex
);
985 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
987 mutex_destroy(&genl_data
->genl_data_mutex
);
990 static struct notifier_block nl_notifier
= {
991 .notifier_call
= nfc_genl_rcv_nl_event
,
995 * nfc_genl_init() - Initialize netlink interface
997 * This initialization function registers the nfc netlink family.
999 int __init
nfc_genl_init(void)
1003 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
1004 ARRAY_SIZE(nfc_genl_ops
));
1008 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
1010 netlink_register_notifier(&nl_notifier
);
1016 * nfc_genl_exit() - Deinitialize netlink interface
1018 * This exit function unregisters the nfc netlink family.
1020 void nfc_genl_exit(void)
1022 netlink_unregister_notifier(&nl_notifier
);
1023 genl_unregister_family(&nfc_genl_family
);