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 #include <net/genetlink.h>
25 #include <linux/nfc.h>
26 #include <linux/slab.h>
30 static struct genl_multicast_group nfc_genl_event_mcgrp = {
31 .name = NFC_GENL_MCAST_EVENT_NAME,
34 struct genl_family nfc_genl_family = {
35 .id = GENL_ID_GENERATE,
37 .name = NFC_GENL_NAME,
38 .version = NFC_GENL_VERSION,
39 .maxattr = NFC_ATTR_MAX,
42 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
43 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
44 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
45 .len = NFC_DEVICE_NAME_MAXSIZE },
46 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
49 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
50 struct netlink_callback *cb, int flags)
56 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
57 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
61 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
63 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
64 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS,
65 target->supported_protocols);
66 NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
67 NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
69 return genlmsg_end(msg, hdr);
72 genlmsg_cancel(msg, hdr);
76 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
82 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
83 nfc_genl_family.attrbuf,
84 nfc_genl_family.maxattr,
89 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
90 return ERR_PTR(-EINVAL);
92 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
94 dev = nfc_get_device(idx);
96 return ERR_PTR(-ENODEV);
101 static int nfc_genl_dump_targets(struct sk_buff *skb,
102 struct netlink_callback *cb)
105 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
111 dev = __get_device_from_cb(cb);
115 cb->args[1] = (long) dev;
118 spin_lock_bh(&dev->targets_lock);
120 cb->seq = dev->targets_generation;
122 while (i < dev->n_targets) {
123 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
131 spin_unlock_bh(&dev->targets_lock);
138 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
140 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
150 int nfc_genl_targets_found(struct nfc_dev *dev)
157 dev->genl_data.poll_req_pid = 0;
159 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
163 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
164 NFC_EVENT_TARGETS_FOUND);
168 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
170 genlmsg_end(msg, hdr);
172 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
175 genlmsg_cancel(msg, hdr);
181 int nfc_genl_device_added(struct nfc_dev *dev)
188 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
192 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
193 NFC_EVENT_DEVICE_ADDED);
197 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
198 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
199 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
201 genlmsg_end(msg, hdr);
203 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
208 genlmsg_cancel(msg, hdr);
214 int nfc_genl_device_removed(struct nfc_dev *dev)
221 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
225 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
226 NFC_EVENT_DEVICE_REMOVED);
230 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
232 genlmsg_end(msg, hdr);
234 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
239 genlmsg_cancel(msg, hdr);
245 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
247 struct netlink_callback *cb,
254 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
260 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
262 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
263 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
264 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
266 return genlmsg_end(msg, hdr);
269 genlmsg_cancel(msg, hdr);
273 static int nfc_genl_dump_devices(struct sk_buff *skb,
274 struct netlink_callback *cb)
276 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
277 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
278 bool first_call = false;
284 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
287 cb->args[0] = (long) iter;
290 mutex_lock(&nfc_devlist_mutex);
292 cb->seq = nfc_devlist_generation;
295 nfc_device_iter_init(iter);
296 dev = nfc_device_iter_next(iter);
302 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
308 dev = nfc_device_iter_next(iter);
311 mutex_unlock(&nfc_devlist_mutex);
313 cb->args[1] = (long) dev;
318 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
320 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
324 nfc_device_iter_exit(iter);
330 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
339 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
342 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
344 dev = nfc_get_device(idx);
348 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
354 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
361 return genlmsg_reply(msg, info);
370 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
378 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
381 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
383 dev = nfc_get_device(idx);
387 rc = nfc_dev_up(dev);
393 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
401 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
404 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
406 dev = nfc_get_device(idx);
410 rc = nfc_dev_down(dev);
416 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
425 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
426 !info->attrs[NFC_ATTR_PROTOCOLS])
429 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
430 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
432 dev = nfc_get_device(idx);
436 mutex_lock(&dev->genl_data.genl_data_mutex);
438 rc = nfc_start_poll(dev, protocols);
440 dev->genl_data.poll_req_pid = info->snd_pid;
442 mutex_unlock(&dev->genl_data.genl_data_mutex);
448 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
456 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
459 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
461 dev = nfc_get_device(idx);
465 mutex_lock(&dev->genl_data.genl_data_mutex);
467 if (dev->genl_data.poll_req_pid != info->snd_pid) {
472 rc = nfc_stop_poll(dev);
473 dev->genl_data.poll_req_pid = 0;
476 mutex_unlock(&dev->genl_data.genl_data_mutex);
481 static struct genl_ops nfc_genl_ops[] = {
483 .cmd = NFC_CMD_GET_DEVICE,
484 .doit = nfc_genl_get_device,
485 .dumpit = nfc_genl_dump_devices,
486 .done = nfc_genl_dump_devices_done,
487 .policy = nfc_genl_policy,
490 .cmd = NFC_CMD_DEV_UP,
491 .doit = nfc_genl_dev_up,
492 .policy = nfc_genl_policy,
495 .cmd = NFC_CMD_DEV_DOWN,
496 .doit = nfc_genl_dev_down,
497 .policy = nfc_genl_policy,
500 .cmd = NFC_CMD_START_POLL,
501 .doit = nfc_genl_start_poll,
502 .policy = nfc_genl_policy,
505 .cmd = NFC_CMD_STOP_POLL,
506 .doit = nfc_genl_stop_poll,
507 .policy = nfc_genl_policy,
510 .cmd = NFC_CMD_GET_TARGET,
511 .dumpit = nfc_genl_dump_targets,
512 .done = nfc_genl_dump_targets_done,
513 .policy = nfc_genl_policy,
517 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
518 unsigned long event, void *ptr)
520 struct netlink_notify *n = ptr;
521 struct class_dev_iter iter;
524 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
527 nfc_dbg("NETLINK_URELEASE event from id %d", n->pid);
529 nfc_device_iter_init(&iter);
530 dev = nfc_device_iter_next(&iter);
533 mutex_lock(&dev->genl_data.genl_data_mutex);
534 if (dev->genl_data.poll_req_pid == n->pid) {
536 dev->genl_data.poll_req_pid = 0;
538 mutex_unlock(&dev->genl_data.genl_data_mutex);
539 dev = nfc_device_iter_next(&iter);
542 nfc_device_iter_exit(&iter);
548 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
550 genl_data->poll_req_pid = 0;
551 mutex_init(&genl_data->genl_data_mutex);
554 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
556 mutex_destroy(&genl_data->genl_data_mutex);
559 static struct notifier_block nl_notifier = {
560 .notifier_call = nfc_genl_rcv_nl_event,
564 * nfc_genl_init() - Initialize netlink interface
566 * This initialization function registers the nfc netlink family.
568 int __init nfc_genl_init(void)
572 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
573 ARRAY_SIZE(nfc_genl_ops));
577 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
579 netlink_register_notifier(&nl_notifier);
585 * nfc_genl_exit() - Deinitialize netlink interface
587 * This exit function unregisters the nfc netlink family.
589 void nfc_genl_exit(void)
591 netlink_unregister_notifier(&nl_notifier);
592 genl_unregister_family(&nfc_genl_family);