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 ": " fmt
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 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 },
51 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
52 struct netlink_callback *cb, int flags)
58 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
59 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
63 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
65 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
66 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS,
67 target->supported_protocols);
68 NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
69 NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
71 return genlmsg_end(msg, hdr);
74 genlmsg_cancel(msg, hdr);
78 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
84 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
85 nfc_genl_family.attrbuf,
86 nfc_genl_family.maxattr,
91 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
92 return ERR_PTR(-EINVAL);
94 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
96 dev = nfc_get_device(idx);
98 return ERR_PTR(-ENODEV);
103 static int nfc_genl_dump_targets(struct sk_buff *skb,
104 struct netlink_callback *cb)
107 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
113 dev = __get_device_from_cb(cb);
117 cb->args[1] = (long) dev;
120 spin_lock_bh(&dev->targets_lock);
122 cb->seq = dev->targets_generation;
124 while (i < dev->n_targets) {
125 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
133 spin_unlock_bh(&dev->targets_lock);
140 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
152 int nfc_genl_targets_found(struct nfc_dev *dev)
159 dev->genl_data.poll_req_pid = 0;
161 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
165 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
166 NFC_EVENT_TARGETS_FOUND);
170 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
172 genlmsg_end(msg, hdr);
174 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
177 genlmsg_cancel(msg, hdr);
183 int nfc_genl_device_added(struct nfc_dev *dev)
190 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
195 NFC_EVENT_DEVICE_ADDED);
199 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
200 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
201 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
203 genlmsg_end(msg, hdr);
205 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
210 genlmsg_cancel(msg, hdr);
216 int nfc_genl_device_removed(struct nfc_dev *dev)
223 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
227 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
228 NFC_EVENT_DEVICE_REMOVED);
232 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
234 genlmsg_end(msg, hdr);
236 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
241 genlmsg_cancel(msg, hdr);
247 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
249 struct netlink_callback *cb,
256 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
262 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
264 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
265 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
266 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
268 return genlmsg_end(msg, hdr);
271 genlmsg_cancel(msg, hdr);
275 static int nfc_genl_dump_devices(struct sk_buff *skb,
276 struct netlink_callback *cb)
278 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
279 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
280 bool first_call = false;
286 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
289 cb->args[0] = (long) iter;
292 mutex_lock(&nfc_devlist_mutex);
294 cb->seq = nfc_devlist_generation;
297 nfc_device_iter_init(iter);
298 dev = nfc_device_iter_next(iter);
304 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
310 dev = nfc_device_iter_next(iter);
313 mutex_unlock(&nfc_devlist_mutex);
315 cb->args[1] = (long) dev;
320 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
322 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
326 nfc_device_iter_exit(iter);
332 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
341 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
344 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
346 dev = nfc_get_device(idx);
350 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
356 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
363 return genlmsg_reply(msg, info);
372 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
380 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
383 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
385 dev = nfc_get_device(idx);
389 rc = nfc_dev_up(dev);
395 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
403 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
406 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
408 dev = nfc_get_device(idx);
412 rc = nfc_dev_down(dev);
418 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
427 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
428 !info->attrs[NFC_ATTR_PROTOCOLS])
431 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
432 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
434 dev = nfc_get_device(idx);
438 mutex_lock(&dev->genl_data.genl_data_mutex);
440 rc = nfc_start_poll(dev, protocols);
442 dev->genl_data.poll_req_pid = info->snd_pid;
444 mutex_unlock(&dev->genl_data.genl_data_mutex);
450 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
458 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
461 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
463 dev = nfc_get_device(idx);
467 mutex_lock(&dev->genl_data.genl_data_mutex);
469 if (dev->genl_data.poll_req_pid != info->snd_pid) {
474 rc = nfc_stop_poll(dev);
475 dev->genl_data.poll_req_pid = 0;
478 mutex_unlock(&dev->genl_data.genl_data_mutex);
483 static struct genl_ops nfc_genl_ops[] = {
485 .cmd = NFC_CMD_GET_DEVICE,
486 .doit = nfc_genl_get_device,
487 .dumpit = nfc_genl_dump_devices,
488 .done = nfc_genl_dump_devices_done,
489 .policy = nfc_genl_policy,
492 .cmd = NFC_CMD_DEV_UP,
493 .doit = nfc_genl_dev_up,
494 .policy = nfc_genl_policy,
497 .cmd = NFC_CMD_DEV_DOWN,
498 .doit = nfc_genl_dev_down,
499 .policy = nfc_genl_policy,
502 .cmd = NFC_CMD_START_POLL,
503 .doit = nfc_genl_start_poll,
504 .policy = nfc_genl_policy,
507 .cmd = NFC_CMD_STOP_POLL,
508 .doit = nfc_genl_stop_poll,
509 .policy = nfc_genl_policy,
512 .cmd = NFC_CMD_GET_TARGET,
513 .dumpit = nfc_genl_dump_targets,
514 .done = nfc_genl_dump_targets_done,
515 .policy = nfc_genl_policy,
519 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
520 unsigned long event, void *ptr)
522 struct netlink_notify *n = ptr;
523 struct class_dev_iter iter;
526 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
529 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
531 nfc_device_iter_init(&iter);
532 dev = nfc_device_iter_next(&iter);
535 mutex_lock(&dev->genl_data.genl_data_mutex);
536 if (dev->genl_data.poll_req_pid == n->pid) {
538 dev->genl_data.poll_req_pid = 0;
540 mutex_unlock(&dev->genl_data.genl_data_mutex);
541 dev = nfc_device_iter_next(&iter);
544 nfc_device_iter_exit(&iter);
550 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
552 genl_data->poll_req_pid = 0;
553 mutex_init(&genl_data->genl_data_mutex);
556 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
558 mutex_destroy(&genl_data->genl_data_mutex);
561 static struct notifier_block nl_notifier = {
562 .notifier_call = nfc_genl_rcv_nl_event,
566 * nfc_genl_init() - Initialize netlink interface
568 * This initialization function registers the nfc netlink family.
570 int __init nfc_genl_init(void)
574 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
575 ARRAY_SIZE(nfc_genl_ops));
579 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
581 netlink_register_notifier(&nl_notifier);
587 * nfc_genl_exit() - Deinitialize netlink interface
589 * This exit function unregisters the nfc netlink family.
591 void nfc_genl_exit(void)
593 netlink_unregister_notifier(&nl_notifier);
594 genl_unregister_family(&nfc_genl_family);