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 static struct genl_multicast_group nfc_genl_event_mcgrp
= {
33 .name
= NFC_GENL_MCAST_EVENT_NAME
,
36 static struct genl_family nfc_genl_family
= {
37 .id
= GENL_ID_GENERATE
,
39 .name
= NFC_GENL_NAME
,
40 .version
= NFC_GENL_VERSION
,
41 .maxattr
= NFC_ATTR_MAX
,
44 static const struct nla_policy nfc_genl_policy
[NFC_ATTR_MAX
+ 1] = {
45 [NFC_ATTR_DEVICE_INDEX
] = { .type
= NLA_U32
},
46 [NFC_ATTR_DEVICE_NAME
] = { .type
= NLA_STRING
,
47 .len
= NFC_DEVICE_NAME_MAXSIZE
},
48 [NFC_ATTR_PROTOCOLS
] = { .type
= NLA_U32
},
49 [NFC_ATTR_COMM_MODE
] = { .type
= NLA_U8
},
50 [NFC_ATTR_RF_MODE
] = { .type
= NLA_U8
},
51 [NFC_ATTR_DEVICE_POWERED
] = { .type
= NLA_U8
},
52 [NFC_ATTR_IM_PROTOCOLS
] = { .type
= NLA_U32
},
53 [NFC_ATTR_TM_PROTOCOLS
] = { .type
= NLA_U32
},
56 static int nfc_genl_send_target(struct sk_buff
*msg
, struct nfc_target
*target
,
57 struct netlink_callback
*cb
, int flags
)
61 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
62 &nfc_genl_family
, flags
, NFC_CMD_GET_TARGET
);
66 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
68 if (nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target
->idx
) ||
69 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, target
->supported_protocols
) ||
70 nla_put_u16(msg
, NFC_ATTR_TARGET_SENS_RES
, target
->sens_res
) ||
71 nla_put_u8(msg
, NFC_ATTR_TARGET_SEL_RES
, target
->sel_res
))
73 if (target
->nfcid1_len
> 0 &&
74 nla_put(msg
, NFC_ATTR_TARGET_NFCID1
, target
->nfcid1_len
,
77 if (target
->sensb_res_len
> 0 &&
78 nla_put(msg
, NFC_ATTR_TARGET_SENSB_RES
, target
->sensb_res_len
,
81 if (target
->sensf_res_len
> 0 &&
82 nla_put(msg
, NFC_ATTR_TARGET_SENSF_RES
, target
->sensf_res_len
,
86 return genlmsg_end(msg
, hdr
);
89 genlmsg_cancel(msg
, hdr
);
93 static struct nfc_dev
*__get_device_from_cb(struct netlink_callback
*cb
)
99 rc
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ nfc_genl_family
.hdrsize
,
100 nfc_genl_family
.attrbuf
,
101 nfc_genl_family
.maxattr
,
106 if (!nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
])
107 return ERR_PTR(-EINVAL
);
109 idx
= nla_get_u32(nfc_genl_family
.attrbuf
[NFC_ATTR_DEVICE_INDEX
]);
111 dev
= nfc_get_device(idx
);
113 return ERR_PTR(-ENODEV
);
118 static int nfc_genl_dump_targets(struct sk_buff
*skb
,
119 struct netlink_callback
*cb
)
122 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
126 dev
= __get_device_from_cb(cb
);
130 cb
->args
[1] = (long) dev
;
133 device_lock(&dev
->dev
);
135 cb
->seq
= dev
->targets_generation
;
137 while (i
< dev
->n_targets
) {
138 rc
= nfc_genl_send_target(skb
, &dev
->targets
[i
], cb
,
146 device_unlock(&dev
->dev
);
153 static int nfc_genl_dump_targets_done(struct netlink_callback
*cb
)
155 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
163 int nfc_genl_targets_found(struct nfc_dev
*dev
)
168 dev
->genl_data
.poll_req_pid
= 0;
170 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
174 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
175 NFC_EVENT_TARGETS_FOUND
);
179 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
180 goto nla_put_failure
;
182 genlmsg_end(msg
, hdr
);
184 return genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
187 genlmsg_cancel(msg
, hdr
);
193 int nfc_genl_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
198 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
202 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
203 NFC_EVENT_TARGET_LOST
);
207 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
208 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
209 goto nla_put_failure
;
211 genlmsg_end(msg
, hdr
);
213 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
218 genlmsg_cancel(msg
, hdr
);
224 int nfc_genl_tm_activated(struct nfc_dev
*dev
, u32 protocol
)
229 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
233 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
234 NFC_EVENT_TM_ACTIVATED
);
238 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
239 goto nla_put_failure
;
240 if (nla_put_u32(msg
, NFC_ATTR_TM_PROTOCOLS
, protocol
))
241 goto nla_put_failure
;
243 genlmsg_end(msg
, hdr
);
245 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
250 genlmsg_cancel(msg
, hdr
);
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(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
280 genlmsg_cancel(msg
, hdr
);
286 int nfc_genl_device_added(struct nfc_dev
*dev
)
291 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
295 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
296 NFC_EVENT_DEVICE_ADDED
);
300 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
301 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
302 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
303 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
304 goto nla_put_failure
;
306 genlmsg_end(msg
, hdr
);
308 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
313 genlmsg_cancel(msg
, hdr
);
319 int nfc_genl_device_removed(struct nfc_dev
*dev
)
324 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
328 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
329 NFC_EVENT_DEVICE_REMOVED
);
333 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
334 goto nla_put_failure
;
336 genlmsg_end(msg
, hdr
);
338 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
343 genlmsg_cancel(msg
, hdr
);
349 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
351 struct netlink_callback
*cb
,
356 hdr
= genlmsg_put(msg
, pid
, seq
, &nfc_genl_family
, flags
,
362 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
364 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
365 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
366 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
367 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
368 goto nla_put_failure
;
370 return genlmsg_end(msg
, hdr
);
373 genlmsg_cancel(msg
, hdr
);
377 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
378 struct netlink_callback
*cb
)
380 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
381 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
382 bool first_call
= false;
386 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
389 cb
->args
[0] = (long) iter
;
392 mutex_lock(&nfc_devlist_mutex
);
394 cb
->seq
= nfc_devlist_generation
;
397 nfc_device_iter_init(iter
);
398 dev
= nfc_device_iter_next(iter
);
404 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).pid
,
405 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
409 dev
= nfc_device_iter_next(iter
);
412 mutex_unlock(&nfc_devlist_mutex
);
414 cb
->args
[1] = (long) dev
;
419 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
421 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
423 nfc_device_iter_exit(iter
);
429 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
430 u8 comm_mode
, u8 rf_mode
)
435 pr_debug("DEP link is up\n");
437 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
441 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
445 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
446 goto nla_put_failure
;
447 if (rf_mode
== NFC_RF_INITIATOR
&&
448 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
449 goto nla_put_failure
;
450 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
451 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
452 goto nla_put_failure
;
454 genlmsg_end(msg
, hdr
);
456 dev
->dep_link_up
= true;
458 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
463 genlmsg_cancel(msg
, hdr
);
469 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
474 pr_debug("DEP link is down\n");
476 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
480 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
481 NFC_CMD_DEP_LINK_DOWN
);
485 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
486 goto nla_put_failure
;
488 genlmsg_end(msg
, hdr
);
490 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
495 genlmsg_cancel(msg
, hdr
);
501 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
508 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
511 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
513 dev
= nfc_get_device(idx
);
517 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
523 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_pid
, info
->snd_seq
,
530 return genlmsg_reply(msg
, info
);
539 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
545 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
548 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
550 dev
= nfc_get_device(idx
);
554 rc
= nfc_dev_up(dev
);
560 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
566 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
569 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
571 dev
= nfc_get_device(idx
);
575 rc
= nfc_dev_down(dev
);
581 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
586 u32 im_protocols
= 0, tm_protocols
= 0;
588 pr_debug("Poll start\n");
590 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
591 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
592 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
593 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
596 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
598 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
599 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
601 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
602 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
603 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
604 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
606 dev
= nfc_get_device(idx
);
610 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
612 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
614 dev
->genl_data
.poll_req_pid
= info
->snd_pid
;
616 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
622 static int nfc_genl_stop_poll(struct sk_buff
*skb
, struct genl_info
*info
)
628 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
631 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
633 dev
= nfc_get_device(idx
);
637 device_lock(&dev
->dev
);
640 device_unlock(&dev
->dev
);
644 device_unlock(&dev
->dev
);
646 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
648 if (dev
->genl_data
.poll_req_pid
!= info
->snd_pid
) {
653 rc
= nfc_stop_poll(dev
);
654 dev
->genl_data
.poll_req_pid
= 0;
657 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
662 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
669 pr_debug("DEP link up\n");
671 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
672 !info
->attrs
[NFC_ATTR_COMM_MODE
])
675 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
676 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
677 tgt_idx
= NFC_TARGET_IDX_ANY
;
679 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
681 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
683 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
686 dev
= nfc_get_device(idx
);
690 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
697 static int nfc_genl_dep_link_down(struct sk_buff
*skb
, struct genl_info
*info
)
703 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
706 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
708 dev
= nfc_get_device(idx
);
712 rc
= nfc_dep_link_down(dev
);
718 static struct genl_ops nfc_genl_ops
[] = {
720 .cmd
= NFC_CMD_GET_DEVICE
,
721 .doit
= nfc_genl_get_device
,
722 .dumpit
= nfc_genl_dump_devices
,
723 .done
= nfc_genl_dump_devices_done
,
724 .policy
= nfc_genl_policy
,
727 .cmd
= NFC_CMD_DEV_UP
,
728 .doit
= nfc_genl_dev_up
,
729 .policy
= nfc_genl_policy
,
732 .cmd
= NFC_CMD_DEV_DOWN
,
733 .doit
= nfc_genl_dev_down
,
734 .policy
= nfc_genl_policy
,
737 .cmd
= NFC_CMD_START_POLL
,
738 .doit
= nfc_genl_start_poll
,
739 .policy
= nfc_genl_policy
,
742 .cmd
= NFC_CMD_STOP_POLL
,
743 .doit
= nfc_genl_stop_poll
,
744 .policy
= nfc_genl_policy
,
747 .cmd
= NFC_CMD_DEP_LINK_UP
,
748 .doit
= nfc_genl_dep_link_up
,
749 .policy
= nfc_genl_policy
,
752 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
753 .doit
= nfc_genl_dep_link_down
,
754 .policy
= nfc_genl_policy
,
757 .cmd
= NFC_CMD_GET_TARGET
,
758 .dumpit
= nfc_genl_dump_targets
,
759 .done
= nfc_genl_dump_targets_done
,
760 .policy
= nfc_genl_policy
,
764 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
765 unsigned long event
, void *ptr
)
767 struct netlink_notify
*n
= ptr
;
768 struct class_dev_iter iter
;
771 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
774 pr_debug("NETLINK_URELEASE event from id %d\n", n
->pid
);
776 nfc_device_iter_init(&iter
);
777 dev
= nfc_device_iter_next(&iter
);
780 if (dev
->genl_data
.poll_req_pid
== n
->pid
) {
782 dev
->genl_data
.poll_req_pid
= 0;
784 dev
= nfc_device_iter_next(&iter
);
787 nfc_device_iter_exit(&iter
);
793 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
795 genl_data
->poll_req_pid
= 0;
796 mutex_init(&genl_data
->genl_data_mutex
);
799 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
801 mutex_destroy(&genl_data
->genl_data_mutex
);
804 static struct notifier_block nl_notifier
= {
805 .notifier_call
= nfc_genl_rcv_nl_event
,
809 * nfc_genl_init() - Initialize netlink interface
811 * This initialization function registers the nfc netlink family.
813 int __init
nfc_genl_init(void)
817 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
818 ARRAY_SIZE(nfc_genl_ops
));
822 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
824 netlink_register_notifier(&nl_notifier
);
830 * nfc_genl_exit() - Deinitialize netlink interface
832 * This exit function unregisters the nfc netlink family.
834 void nfc_genl_exit(void)
836 netlink_unregister_notifier(&nl_notifier
);
837 genl_unregister_family(&nfc_genl_family
);