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_GOODSIZE
, 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_GOODSIZE
, 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_device_added(struct nfc_dev
*dev
)
229 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
233 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
234 NFC_EVENT_DEVICE_ADDED
);
238 if (nla_put_string(msg
, NFC_ATTR_DEVICE_NAME
, nfc_device_name(dev
)) ||
239 nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
) ||
240 nla_put_u32(msg
, NFC_ATTR_PROTOCOLS
, dev
->supported_protocols
) ||
241 nla_put_u8(msg
, NFC_ATTR_DEVICE_POWERED
, dev
->dev_up
))
242 goto nla_put_failure
;
244 genlmsg_end(msg
, hdr
);
246 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
251 genlmsg_cancel(msg
, hdr
);
257 int nfc_genl_device_removed(struct nfc_dev
*dev
)
262 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
266 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
267 NFC_EVENT_DEVICE_REMOVED
);
271 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
272 goto nla_put_failure
;
274 genlmsg_end(msg
, hdr
);
276 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_KERNEL
);
281 genlmsg_cancel(msg
, hdr
);
287 static int nfc_genl_send_device(struct sk_buff
*msg
, struct nfc_dev
*dev
,
289 struct netlink_callback
*cb
,
294 hdr
= genlmsg_put(msg
, pid
, seq
, &nfc_genl_family
, flags
,
300 genl_dump_check_consistent(cb
, hdr
, &nfc_genl_family
);
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 return genlmsg_end(msg
, hdr
);
311 genlmsg_cancel(msg
, hdr
);
315 static int nfc_genl_dump_devices(struct sk_buff
*skb
,
316 struct netlink_callback
*cb
)
318 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
319 struct nfc_dev
*dev
= (struct nfc_dev
*) cb
->args
[1];
320 bool first_call
= false;
324 iter
= kmalloc(sizeof(struct class_dev_iter
), GFP_KERNEL
);
327 cb
->args
[0] = (long) iter
;
330 mutex_lock(&nfc_devlist_mutex
);
332 cb
->seq
= nfc_devlist_generation
;
335 nfc_device_iter_init(iter
);
336 dev
= nfc_device_iter_next(iter
);
342 rc
= nfc_genl_send_device(skb
, dev
, NETLINK_CB(cb
->skb
).pid
,
343 cb
->nlh
->nlmsg_seq
, cb
, NLM_F_MULTI
);
347 dev
= nfc_device_iter_next(iter
);
350 mutex_unlock(&nfc_devlist_mutex
);
352 cb
->args
[1] = (long) dev
;
357 static int nfc_genl_dump_devices_done(struct netlink_callback
*cb
)
359 struct class_dev_iter
*iter
= (struct class_dev_iter
*) cb
->args
[0];
361 nfc_device_iter_exit(iter
);
367 int nfc_genl_dep_link_up_event(struct nfc_dev
*dev
, u32 target_idx
,
368 u8 comm_mode
, u8 rf_mode
)
373 pr_debug("DEP link is up\n");
375 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
379 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0, NFC_CMD_DEP_LINK_UP
);
383 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
384 goto nla_put_failure
;
385 if (rf_mode
== NFC_RF_INITIATOR
&&
386 nla_put_u32(msg
, NFC_ATTR_TARGET_INDEX
, target_idx
))
387 goto nla_put_failure
;
388 if (nla_put_u8(msg
, NFC_ATTR_COMM_MODE
, comm_mode
) ||
389 nla_put_u8(msg
, NFC_ATTR_RF_MODE
, rf_mode
))
390 goto nla_put_failure
;
392 genlmsg_end(msg
, hdr
);
394 dev
->dep_link_up
= true;
396 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
401 genlmsg_cancel(msg
, hdr
);
407 int nfc_genl_dep_link_down_event(struct nfc_dev
*dev
)
412 pr_debug("DEP link is down\n");
414 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
418 hdr
= genlmsg_put(msg
, 0, 0, &nfc_genl_family
, 0,
419 NFC_CMD_DEP_LINK_DOWN
);
423 if (nla_put_u32(msg
, NFC_ATTR_DEVICE_INDEX
, dev
->idx
))
424 goto nla_put_failure
;
426 genlmsg_end(msg
, hdr
);
428 genlmsg_multicast(msg
, 0, nfc_genl_event_mcgrp
.id
, GFP_ATOMIC
);
433 genlmsg_cancel(msg
, hdr
);
439 static int nfc_genl_get_device(struct sk_buff
*skb
, struct genl_info
*info
)
446 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
449 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
451 dev
= nfc_get_device(idx
);
455 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
461 rc
= nfc_genl_send_device(msg
, dev
, info
->snd_pid
, info
->snd_seq
,
468 return genlmsg_reply(msg
, info
);
477 static int nfc_genl_dev_up(struct sk_buff
*skb
, struct genl_info
*info
)
483 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
486 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
488 dev
= nfc_get_device(idx
);
492 rc
= nfc_dev_up(dev
);
498 static int nfc_genl_dev_down(struct sk_buff
*skb
, struct genl_info
*info
)
504 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
])
507 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
509 dev
= nfc_get_device(idx
);
513 rc
= nfc_dev_down(dev
);
519 static int nfc_genl_start_poll(struct sk_buff
*skb
, struct genl_info
*info
)
524 u32 im_protocols
= 0, tm_protocols
= 0;
526 pr_debug("Poll start\n");
528 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
529 ((!info
->attrs
[NFC_ATTR_IM_PROTOCOLS
] &&
530 !info
->attrs
[NFC_ATTR_PROTOCOLS
]) &&
531 !info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]))
534 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
536 if (info
->attrs
[NFC_ATTR_TM_PROTOCOLS
])
537 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_TM_PROTOCOLS
]);
538 else if (info
->attrs
[NFC_ATTR_PROTOCOLS
])
539 tm_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_PROTOCOLS
]);
541 if (info
->attrs
[NFC_ATTR_IM_PROTOCOLS
])
542 im_protocols
= nla_get_u32(info
->attrs
[NFC_ATTR_IM_PROTOCOLS
]);
544 dev
= nfc_get_device(idx
);
548 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
550 rc
= nfc_start_poll(dev
, im_protocols
, tm_protocols
);
552 dev
->genl_data
.poll_req_pid
= info
->snd_pid
;
554 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
560 static int nfc_genl_stop_poll(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 mutex_lock(&dev
->genl_data
.genl_data_mutex
);
577 if (dev
->genl_data
.poll_req_pid
!= info
->snd_pid
) {
582 rc
= nfc_stop_poll(dev
);
583 dev
->genl_data
.poll_req_pid
= 0;
586 mutex_unlock(&dev
->genl_data
.genl_data_mutex
);
591 static int nfc_genl_dep_link_up(struct sk_buff
*skb
, struct genl_info
*info
)
598 pr_debug("DEP link up\n");
600 if (!info
->attrs
[NFC_ATTR_DEVICE_INDEX
] ||
601 !info
->attrs
[NFC_ATTR_COMM_MODE
])
604 idx
= nla_get_u32(info
->attrs
[NFC_ATTR_DEVICE_INDEX
]);
605 if (!info
->attrs
[NFC_ATTR_TARGET_INDEX
])
606 tgt_idx
= NFC_TARGET_IDX_ANY
;
608 tgt_idx
= nla_get_u32(info
->attrs
[NFC_ATTR_TARGET_INDEX
]);
610 comm
= nla_get_u8(info
->attrs
[NFC_ATTR_COMM_MODE
]);
612 if (comm
!= NFC_COMM_ACTIVE
&& comm
!= NFC_COMM_PASSIVE
)
615 dev
= nfc_get_device(idx
);
619 rc
= nfc_dep_link_up(dev
, tgt_idx
, comm
);
626 static int nfc_genl_dep_link_down(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 rc
= nfc_dep_link_down(dev
);
647 static struct genl_ops nfc_genl_ops
[] = {
649 .cmd
= NFC_CMD_GET_DEVICE
,
650 .doit
= nfc_genl_get_device
,
651 .dumpit
= nfc_genl_dump_devices
,
652 .done
= nfc_genl_dump_devices_done
,
653 .policy
= nfc_genl_policy
,
656 .cmd
= NFC_CMD_DEV_UP
,
657 .doit
= nfc_genl_dev_up
,
658 .policy
= nfc_genl_policy
,
661 .cmd
= NFC_CMD_DEV_DOWN
,
662 .doit
= nfc_genl_dev_down
,
663 .policy
= nfc_genl_policy
,
666 .cmd
= NFC_CMD_START_POLL
,
667 .doit
= nfc_genl_start_poll
,
668 .policy
= nfc_genl_policy
,
671 .cmd
= NFC_CMD_STOP_POLL
,
672 .doit
= nfc_genl_stop_poll
,
673 .policy
= nfc_genl_policy
,
676 .cmd
= NFC_CMD_DEP_LINK_UP
,
677 .doit
= nfc_genl_dep_link_up
,
678 .policy
= nfc_genl_policy
,
681 .cmd
= NFC_CMD_DEP_LINK_DOWN
,
682 .doit
= nfc_genl_dep_link_down
,
683 .policy
= nfc_genl_policy
,
686 .cmd
= NFC_CMD_GET_TARGET
,
687 .dumpit
= nfc_genl_dump_targets
,
688 .done
= nfc_genl_dump_targets_done
,
689 .policy
= nfc_genl_policy
,
693 static int nfc_genl_rcv_nl_event(struct notifier_block
*this,
694 unsigned long event
, void *ptr
)
696 struct netlink_notify
*n
= ptr
;
697 struct class_dev_iter iter
;
700 if (event
!= NETLINK_URELEASE
|| n
->protocol
!= NETLINK_GENERIC
)
703 pr_debug("NETLINK_URELEASE event from id %d\n", n
->pid
);
705 nfc_device_iter_init(&iter
);
706 dev
= nfc_device_iter_next(&iter
);
709 if (dev
->genl_data
.poll_req_pid
== n
->pid
) {
711 dev
->genl_data
.poll_req_pid
= 0;
713 dev
= nfc_device_iter_next(&iter
);
716 nfc_device_iter_exit(&iter
);
722 void nfc_genl_data_init(struct nfc_genl_data
*genl_data
)
724 genl_data
->poll_req_pid
= 0;
725 mutex_init(&genl_data
->genl_data_mutex
);
728 void nfc_genl_data_exit(struct nfc_genl_data
*genl_data
)
730 mutex_destroy(&genl_data
->genl_data_mutex
);
733 static struct notifier_block nl_notifier
= {
734 .notifier_call
= nfc_genl_rcv_nl_event
,
738 * nfc_genl_init() - Initialize netlink interface
740 * This initialization function registers the nfc netlink family.
742 int __init
nfc_genl_init(void)
746 rc
= genl_register_family_with_ops(&nfc_genl_family
, nfc_genl_ops
,
747 ARRAY_SIZE(nfc_genl_ops
));
751 rc
= genl_register_mc_group(&nfc_genl_family
, &nfc_genl_event_mcgrp
);
753 netlink_register_notifier(&nl_notifier
);
759 * nfc_genl_exit() - Deinitialize netlink interface
761 * This exit function unregisters the nfc netlink family.
763 void nfc_genl_exit(void)
765 netlink_unregister_notifier(&nl_notifier
);
766 genl_unregister_family(&nfc_genl_family
);