watchdog: dw: Enable OF support for DW watchdog timer
[linux-2.6.git] / net / nfc / netlink.c
blob84b7e3ea7b7ad7ce09e9256916589e4724d55969
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
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>
30 #include "nfc.h"
31 #include "llcp.h"
33 static struct genl_multicast_group nfc_genl_event_mcgrp = {
34 .name = NFC_GENL_MCAST_EVENT_NAME,
37 static struct genl_family nfc_genl_family = {
38 .id = GENL_ID_GENERATE,
39 .hdrsize = 0,
40 .name = NFC_GENL_NAME,
41 .version = NFC_GENL_VERSION,
42 .maxattr = NFC_ATTR_MAX,
45 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
48 .len = NFC_DEVICE_NAME_MAXSIZE },
49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
50 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
52 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
53 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
55 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
60 .len = NFC_FIRMWARE_NAME_MAXSIZE },
61 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
64 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
65 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
66 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
69 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
70 struct netlink_callback *cb, int flags)
72 void *hdr;
74 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
75 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
76 if (!hdr)
77 return -EMSGSIZE;
79 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
81 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
82 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
83 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
84 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
85 goto nla_put_failure;
86 if (target->nfcid1_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
88 target->nfcid1))
89 goto nla_put_failure;
90 if (target->sensb_res_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
92 target->sensb_res))
93 goto nla_put_failure;
94 if (target->sensf_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
96 target->sensf_res))
97 goto nla_put_failure;
99 return genlmsg_end(msg, hdr);
101 nla_put_failure:
102 genlmsg_cancel(msg, hdr);
103 return -EMSGSIZE;
106 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
108 struct nfc_dev *dev;
109 int rc;
110 u32 idx;
112 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
113 nfc_genl_family.attrbuf,
114 nfc_genl_family.maxattr,
115 nfc_genl_policy);
116 if (rc < 0)
117 return ERR_PTR(rc);
119 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
120 return ERR_PTR(-EINVAL);
122 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
124 dev = nfc_get_device(idx);
125 if (!dev)
126 return ERR_PTR(-ENODEV);
128 return dev;
131 static int nfc_genl_dump_targets(struct sk_buff *skb,
132 struct netlink_callback *cb)
134 int i = cb->args[0];
135 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
136 int rc;
138 if (!dev) {
139 dev = __get_device_from_cb(cb);
140 if (IS_ERR(dev))
141 return PTR_ERR(dev);
143 cb->args[1] = (long) dev;
146 device_lock(&dev->dev);
148 cb->seq = dev->targets_generation;
150 while (i < dev->n_targets) {
151 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
152 NLM_F_MULTI);
153 if (rc < 0)
154 break;
156 i++;
159 device_unlock(&dev->dev);
161 cb->args[0] = i;
163 return skb->len;
166 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
168 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
170 if (dev)
171 nfc_put_device(dev);
173 return 0;
176 int nfc_genl_targets_found(struct nfc_dev *dev)
178 struct sk_buff *msg;
179 void *hdr;
181 dev->genl_data.poll_req_portid = 0;
183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
184 if (!msg)
185 return -ENOMEM;
187 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
188 NFC_EVENT_TARGETS_FOUND);
189 if (!hdr)
190 goto free_msg;
192 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
193 goto nla_put_failure;
195 genlmsg_end(msg, hdr);
197 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
199 nla_put_failure:
200 genlmsg_cancel(msg, hdr);
201 free_msg:
202 nlmsg_free(msg);
203 return -EMSGSIZE;
206 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
208 struct sk_buff *msg;
209 void *hdr;
211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
212 if (!msg)
213 return -ENOMEM;
215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
216 NFC_EVENT_TARGET_LOST);
217 if (!hdr)
218 goto free_msg;
220 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
221 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
222 goto nla_put_failure;
224 genlmsg_end(msg, hdr);
226 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
228 return 0;
230 nla_put_failure:
231 genlmsg_cancel(msg, hdr);
232 free_msg:
233 nlmsg_free(msg);
234 return -EMSGSIZE;
237 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
239 struct sk_buff *msg;
240 void *hdr;
242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
243 if (!msg)
244 return -ENOMEM;
246 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
247 NFC_EVENT_TM_ACTIVATED);
248 if (!hdr)
249 goto free_msg;
251 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
252 goto nla_put_failure;
253 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
254 goto nla_put_failure;
256 genlmsg_end(msg, hdr);
258 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
260 return 0;
262 nla_put_failure:
263 genlmsg_cancel(msg, hdr);
264 free_msg:
265 nlmsg_free(msg);
266 return -EMSGSIZE;
269 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
271 struct sk_buff *msg;
272 void *hdr;
274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
275 if (!msg)
276 return -ENOMEM;
278 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
279 NFC_EVENT_TM_DEACTIVATED);
280 if (!hdr)
281 goto free_msg;
283 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
284 goto nla_put_failure;
286 genlmsg_end(msg, hdr);
288 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
290 return 0;
292 nla_put_failure:
293 genlmsg_cancel(msg, hdr);
294 free_msg:
295 nlmsg_free(msg);
296 return -EMSGSIZE;
299 int nfc_genl_device_added(struct nfc_dev *dev)
301 struct sk_buff *msg;
302 void *hdr;
304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
305 if (!msg)
306 return -ENOMEM;
308 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
309 NFC_EVENT_DEVICE_ADDED);
310 if (!hdr)
311 goto free_msg;
313 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
314 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
315 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
316 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
317 goto nla_put_failure;
319 genlmsg_end(msg, hdr);
321 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
323 return 0;
325 nla_put_failure:
326 genlmsg_cancel(msg, hdr);
327 free_msg:
328 nlmsg_free(msg);
329 return -EMSGSIZE;
332 int nfc_genl_device_removed(struct nfc_dev *dev)
334 struct sk_buff *msg;
335 void *hdr;
337 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
338 if (!msg)
339 return -ENOMEM;
341 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
342 NFC_EVENT_DEVICE_REMOVED);
343 if (!hdr)
344 goto free_msg;
346 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
347 goto nla_put_failure;
349 genlmsg_end(msg, hdr);
351 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
353 return 0;
355 nla_put_failure:
356 genlmsg_cancel(msg, hdr);
357 free_msg:
358 nlmsg_free(msg);
359 return -EMSGSIZE;
362 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
364 struct sk_buff *msg;
365 struct nlattr *sdp_attr, *uri_attr;
366 struct nfc_llcp_sdp_tlv *sdres;
367 struct hlist_node *n;
368 void *hdr;
369 int rc = -EMSGSIZE;
370 int i;
372 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
373 if (!msg)
374 return -ENOMEM;
376 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
377 NFC_EVENT_LLC_SDRES);
378 if (!hdr)
379 goto free_msg;
381 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
382 goto nla_put_failure;
384 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
385 if (sdp_attr == NULL) {
386 rc = -ENOMEM;
387 goto nla_put_failure;
390 i = 1;
391 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
392 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
394 uri_attr = nla_nest_start(msg, i++);
395 if (uri_attr == NULL) {
396 rc = -ENOMEM;
397 goto nla_put_failure;
400 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
401 goto nla_put_failure;
403 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
404 goto nla_put_failure;
406 nla_nest_end(msg, uri_attr);
408 hlist_del(&sdres->node);
410 nfc_llcp_free_sdp_tlv(sdres);
413 nla_nest_end(msg, sdp_attr);
415 genlmsg_end(msg, hdr);
417 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
419 nla_put_failure:
420 genlmsg_cancel(msg, hdr);
422 free_msg:
423 nlmsg_free(msg);
425 nfc_llcp_free_sdp_tlv_list(sdres_list);
427 return rc;
430 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
432 struct sk_buff *msg;
433 void *hdr;
435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
436 if (!msg)
437 return -ENOMEM;
439 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
440 NFC_EVENT_SE_ADDED);
441 if (!hdr)
442 goto free_msg;
444 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
445 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
446 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
447 goto nla_put_failure;
449 genlmsg_end(msg, hdr);
451 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
453 return 0;
455 nla_put_failure:
456 genlmsg_cancel(msg, hdr);
457 free_msg:
458 nlmsg_free(msg);
459 return -EMSGSIZE;
462 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
464 struct sk_buff *msg;
465 void *hdr;
467 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
468 if (!msg)
469 return -ENOMEM;
471 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
472 NFC_EVENT_SE_REMOVED);
473 if (!hdr)
474 goto free_msg;
476 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
477 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
478 goto nla_put_failure;
480 genlmsg_end(msg, hdr);
482 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
484 return 0;
486 nla_put_failure:
487 genlmsg_cancel(msg, hdr);
488 free_msg:
489 nlmsg_free(msg);
490 return -EMSGSIZE;
493 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
494 u32 portid, u32 seq,
495 struct netlink_callback *cb,
496 int flags)
498 void *hdr;
500 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
501 NFC_CMD_GET_DEVICE);
502 if (!hdr)
503 return -EMSGSIZE;
505 if (cb)
506 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
508 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
509 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
510 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
511 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
512 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
513 goto nla_put_failure;
515 return genlmsg_end(msg, hdr);
517 nla_put_failure:
518 genlmsg_cancel(msg, hdr);
519 return -EMSGSIZE;
522 static int nfc_genl_dump_devices(struct sk_buff *skb,
523 struct netlink_callback *cb)
525 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
526 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
527 bool first_call = false;
529 if (!iter) {
530 first_call = true;
531 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
532 if (!iter)
533 return -ENOMEM;
534 cb->args[0] = (long) iter;
537 mutex_lock(&nfc_devlist_mutex);
539 cb->seq = nfc_devlist_generation;
541 if (first_call) {
542 nfc_device_iter_init(iter);
543 dev = nfc_device_iter_next(iter);
546 while (dev) {
547 int rc;
549 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
550 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
551 if (rc < 0)
552 break;
554 dev = nfc_device_iter_next(iter);
557 mutex_unlock(&nfc_devlist_mutex);
559 cb->args[1] = (long) dev;
561 return skb->len;
564 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
566 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
568 nfc_device_iter_exit(iter);
569 kfree(iter);
571 return 0;
574 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
575 u8 comm_mode, u8 rf_mode)
577 struct sk_buff *msg;
578 void *hdr;
580 pr_debug("DEP link is up\n");
582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
583 if (!msg)
584 return -ENOMEM;
586 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
587 if (!hdr)
588 goto free_msg;
590 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
591 goto nla_put_failure;
592 if (rf_mode == NFC_RF_INITIATOR &&
593 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
594 goto nla_put_failure;
595 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
596 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
597 goto nla_put_failure;
599 genlmsg_end(msg, hdr);
601 dev->dep_link_up = true;
603 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
605 return 0;
607 nla_put_failure:
608 genlmsg_cancel(msg, hdr);
609 free_msg:
610 nlmsg_free(msg);
611 return -EMSGSIZE;
614 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
616 struct sk_buff *msg;
617 void *hdr;
619 pr_debug("DEP link is down\n");
621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
622 if (!msg)
623 return -ENOMEM;
625 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
626 NFC_CMD_DEP_LINK_DOWN);
627 if (!hdr)
628 goto free_msg;
630 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
631 goto nla_put_failure;
633 genlmsg_end(msg, hdr);
635 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
637 return 0;
639 nla_put_failure:
640 genlmsg_cancel(msg, hdr);
641 free_msg:
642 nlmsg_free(msg);
643 return -EMSGSIZE;
646 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
648 struct sk_buff *msg;
649 struct nfc_dev *dev;
650 u32 idx;
651 int rc = -ENOBUFS;
653 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
654 return -EINVAL;
656 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
658 dev = nfc_get_device(idx);
659 if (!dev)
660 return -ENODEV;
662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
663 if (!msg) {
664 rc = -ENOMEM;
665 goto out_putdev;
668 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
669 NULL, 0);
670 if (rc < 0)
671 goto out_free;
673 nfc_put_device(dev);
675 return genlmsg_reply(msg, info);
677 out_free:
678 nlmsg_free(msg);
679 out_putdev:
680 nfc_put_device(dev);
681 return rc;
684 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
686 struct nfc_dev *dev;
687 int rc;
688 u32 idx;
690 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
691 return -EINVAL;
693 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
695 dev = nfc_get_device(idx);
696 if (!dev)
697 return -ENODEV;
699 rc = nfc_dev_up(dev);
701 nfc_put_device(dev);
702 return rc;
705 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
707 struct nfc_dev *dev;
708 int rc;
709 u32 idx;
711 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
712 return -EINVAL;
714 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
716 dev = nfc_get_device(idx);
717 if (!dev)
718 return -ENODEV;
720 rc = nfc_dev_down(dev);
722 nfc_put_device(dev);
723 return rc;
726 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
728 struct nfc_dev *dev;
729 int rc;
730 u32 idx;
731 u32 im_protocols = 0, tm_protocols = 0;
733 pr_debug("Poll start\n");
735 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
736 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
737 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
738 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
739 return -EINVAL;
741 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
743 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
744 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
746 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
747 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
748 else if (info->attrs[NFC_ATTR_PROTOCOLS])
749 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
751 dev = nfc_get_device(idx);
752 if (!dev)
753 return -ENODEV;
755 mutex_lock(&dev->genl_data.genl_data_mutex);
757 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
758 if (!rc)
759 dev->genl_data.poll_req_portid = info->snd_portid;
761 mutex_unlock(&dev->genl_data.genl_data_mutex);
763 nfc_put_device(dev);
764 return rc;
767 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
769 struct nfc_dev *dev;
770 int rc;
771 u32 idx;
773 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
774 return -EINVAL;
776 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
778 dev = nfc_get_device(idx);
779 if (!dev)
780 return -ENODEV;
782 device_lock(&dev->dev);
784 if (!dev->polling) {
785 device_unlock(&dev->dev);
786 return -EINVAL;
789 device_unlock(&dev->dev);
791 mutex_lock(&dev->genl_data.genl_data_mutex);
793 if (dev->genl_data.poll_req_portid != info->snd_portid) {
794 rc = -EBUSY;
795 goto out;
798 rc = nfc_stop_poll(dev);
799 dev->genl_data.poll_req_portid = 0;
801 out:
802 mutex_unlock(&dev->genl_data.genl_data_mutex);
803 nfc_put_device(dev);
804 return rc;
807 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
809 struct nfc_dev *dev;
810 int rc, tgt_idx;
811 u32 idx;
812 u8 comm;
814 pr_debug("DEP link up\n");
816 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
817 !info->attrs[NFC_ATTR_COMM_MODE])
818 return -EINVAL;
820 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
821 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
822 tgt_idx = NFC_TARGET_IDX_ANY;
823 else
824 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
826 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
828 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
829 return -EINVAL;
831 dev = nfc_get_device(idx);
832 if (!dev)
833 return -ENODEV;
835 rc = nfc_dep_link_up(dev, tgt_idx, comm);
837 nfc_put_device(dev);
839 return rc;
842 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
844 struct nfc_dev *dev;
845 int rc;
846 u32 idx;
848 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
849 return -EINVAL;
851 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
853 dev = nfc_get_device(idx);
854 if (!dev)
855 return -ENODEV;
857 rc = nfc_dep_link_down(dev);
859 nfc_put_device(dev);
860 return rc;
863 static int nfc_genl_send_params(struct sk_buff *msg,
864 struct nfc_llcp_local *local,
865 u32 portid, u32 seq)
867 void *hdr;
869 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
870 NFC_CMD_LLC_GET_PARAMS);
871 if (!hdr)
872 return -EMSGSIZE;
874 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
875 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
876 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
877 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
878 goto nla_put_failure;
880 return genlmsg_end(msg, hdr);
882 nla_put_failure:
884 genlmsg_cancel(msg, hdr);
885 return -EMSGSIZE;
888 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
890 struct nfc_dev *dev;
891 struct nfc_llcp_local *local;
892 int rc = 0;
893 struct sk_buff *msg = NULL;
894 u32 idx;
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
897 return -EINVAL;
899 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901 dev = nfc_get_device(idx);
902 if (!dev)
903 return -ENODEV;
905 device_lock(&dev->dev);
907 local = nfc_llcp_find_local(dev);
908 if (!local) {
909 rc = -ENODEV;
910 goto exit;
913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
914 if (!msg) {
915 rc = -ENOMEM;
916 goto exit;
919 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
921 exit:
922 device_unlock(&dev->dev);
924 nfc_put_device(dev);
926 if (rc < 0) {
927 if (msg)
928 nlmsg_free(msg);
930 return rc;
933 return genlmsg_reply(msg, info);
936 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
938 struct nfc_dev *dev;
939 struct nfc_llcp_local *local;
940 u8 rw = 0;
941 u16 miux = 0;
942 u32 idx;
943 int rc = 0;
945 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
946 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
947 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
948 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
949 return -EINVAL;
951 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
952 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
954 if (rw > LLCP_MAX_RW)
955 return -EINVAL;
958 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
959 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
961 if (miux > LLCP_MAX_MIUX)
962 return -EINVAL;
965 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
967 dev = nfc_get_device(idx);
968 if (!dev)
969 return -ENODEV;
971 device_lock(&dev->dev);
973 local = nfc_llcp_find_local(dev);
974 if (!local) {
975 nfc_put_device(dev);
976 rc = -ENODEV;
977 goto exit;
980 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
981 if (dev->dep_link_up) {
982 rc = -EINPROGRESS;
983 goto exit;
986 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
989 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
990 local->rw = rw;
992 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
993 local->miux = cpu_to_be16(miux);
995 exit:
996 device_unlock(&dev->dev);
998 nfc_put_device(dev);
1000 return rc;
1003 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1005 struct nfc_dev *dev;
1006 struct nfc_llcp_local *local;
1007 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1008 u32 idx;
1009 u8 tid;
1010 char *uri;
1011 int rc = 0, rem;
1012 size_t uri_len, tlvs_len;
1013 struct hlist_head sdreq_list;
1014 struct nfc_llcp_sdp_tlv *sdreq;
1016 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1017 !info->attrs[NFC_ATTR_LLC_SDP])
1018 return -EINVAL;
1020 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1022 dev = nfc_get_device(idx);
1023 if (!dev) {
1024 rc = -ENODEV;
1025 goto exit;
1028 device_lock(&dev->dev);
1030 if (dev->dep_link_up == false) {
1031 rc = -ENOLINK;
1032 goto exit;
1035 local = nfc_llcp_find_local(dev);
1036 if (!local) {
1037 nfc_put_device(dev);
1038 rc = -ENODEV;
1039 goto exit;
1042 INIT_HLIST_HEAD(&sdreq_list);
1044 tlvs_len = 0;
1046 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1047 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1048 nfc_sdp_genl_policy);
1050 if (rc != 0) {
1051 rc = -EINVAL;
1052 goto exit;
1055 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1056 continue;
1058 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1059 if (uri_len == 0)
1060 continue;
1062 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1063 if (uri == NULL || *uri == 0)
1064 continue;
1066 tid = local->sdreq_next_tid++;
1068 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1069 if (sdreq == NULL) {
1070 rc = -ENOMEM;
1071 goto exit;
1074 tlvs_len += sdreq->tlv_len;
1076 hlist_add_head(&sdreq->node, &sdreq_list);
1079 if (hlist_empty(&sdreq_list)) {
1080 rc = -EINVAL;
1081 goto exit;
1084 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1085 exit:
1086 device_unlock(&dev->dev);
1088 nfc_put_device(dev);
1090 return rc;
1093 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1095 struct nfc_dev *dev;
1096 int rc;
1097 u32 idx;
1098 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1100 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1101 return -EINVAL;
1103 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1105 dev = nfc_get_device(idx);
1106 if (!dev)
1107 return -ENODEV;
1109 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1110 sizeof(firmware_name));
1112 rc = nfc_fw_download(dev, firmware_name);
1114 nfc_put_device(dev);
1115 return rc;
1118 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1119 u32 result)
1121 struct sk_buff *msg;
1122 void *hdr;
1124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1125 if (!msg)
1126 return -ENOMEM;
1128 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1129 NFC_CMD_FW_DOWNLOAD);
1130 if (!hdr)
1131 goto free_msg;
1133 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1134 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1135 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1136 goto nla_put_failure;
1138 genlmsg_end(msg, hdr);
1140 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1142 return 0;
1144 nla_put_failure:
1145 genlmsg_cancel(msg, hdr);
1146 free_msg:
1147 nlmsg_free(msg);
1148 return -EMSGSIZE;
1151 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1153 struct nfc_dev *dev;
1154 int rc;
1155 u32 idx, se_idx;
1157 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1158 !info->attrs[NFC_ATTR_SE_INDEX])
1159 return -EINVAL;
1161 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1162 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1164 dev = nfc_get_device(idx);
1165 if (!dev)
1166 return -ENODEV;
1168 rc = nfc_enable_se(dev, se_idx);
1170 nfc_put_device(dev);
1171 return rc;
1174 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1176 struct nfc_dev *dev;
1177 int rc;
1178 u32 idx, se_idx;
1180 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1181 !info->attrs[NFC_ATTR_SE_INDEX])
1182 return -EINVAL;
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1185 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1187 dev = nfc_get_device(idx);
1188 if (!dev)
1189 return -ENODEV;
1191 rc = nfc_disable_se(dev, se_idx);
1193 nfc_put_device(dev);
1194 return rc;
1197 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1198 u32 portid, u32 seq,
1199 struct netlink_callback *cb,
1200 int flags)
1202 void *hdr;
1203 struct nfc_se *se, *n;
1205 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1206 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1207 NFC_CMD_GET_SE);
1208 if (!hdr)
1209 goto nla_put_failure;
1211 if (cb)
1212 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1214 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1215 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1216 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1217 goto nla_put_failure;
1219 if (genlmsg_end(msg, hdr) < 0)
1220 goto nla_put_failure;
1223 return 0;
1225 nla_put_failure:
1226 genlmsg_cancel(msg, hdr);
1227 return -EMSGSIZE;
1230 static int nfc_genl_dump_ses(struct sk_buff *skb,
1231 struct netlink_callback *cb)
1233 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1234 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1235 bool first_call = false;
1237 if (!iter) {
1238 first_call = true;
1239 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1240 if (!iter)
1241 return -ENOMEM;
1242 cb->args[0] = (long) iter;
1245 mutex_lock(&nfc_devlist_mutex);
1247 cb->seq = nfc_devlist_generation;
1249 if (first_call) {
1250 nfc_device_iter_init(iter);
1251 dev = nfc_device_iter_next(iter);
1254 while (dev) {
1255 int rc;
1257 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1258 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1259 if (rc < 0)
1260 break;
1262 dev = nfc_device_iter_next(iter);
1265 mutex_unlock(&nfc_devlist_mutex);
1267 cb->args[1] = (long) dev;
1269 return skb->len;
1272 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1274 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1276 nfc_device_iter_exit(iter);
1277 kfree(iter);
1279 return 0;
1282 struct se_io_ctx {
1283 u32 dev_idx;
1284 u32 se_idx;
1287 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1289 struct se_io_ctx *ctx = context;
1290 struct sk_buff *msg;
1291 void *hdr;
1293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1294 if (!msg) {
1295 kfree(ctx);
1296 return;
1299 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1300 NFC_CMD_SE_IO);
1301 if (!hdr)
1302 goto free_msg;
1304 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1305 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1306 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1307 goto nla_put_failure;
1309 genlmsg_end(msg, hdr);
1311 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1313 kfree(ctx);
1315 return;
1317 nla_put_failure:
1318 genlmsg_cancel(msg, hdr);
1319 free_msg:
1320 nlmsg_free(msg);
1321 kfree(ctx);
1323 return;
1326 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1328 struct nfc_dev *dev;
1329 struct se_io_ctx *ctx;
1330 u32 dev_idx, se_idx;
1331 u8 *apdu;
1332 size_t apdu_len;
1334 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1335 !info->attrs[NFC_ATTR_SE_INDEX] ||
1336 !info->attrs[NFC_ATTR_SE_APDU])
1337 return -EINVAL;
1339 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1340 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1342 dev = nfc_get_device(dev_idx);
1343 if (!dev)
1344 return -ENODEV;
1346 if (!dev->ops || !dev->ops->se_io)
1347 return -ENOTSUPP;
1349 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1350 if (apdu_len == 0)
1351 return -EINVAL;
1353 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1354 if (!apdu)
1355 return -EINVAL;
1357 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1358 if (!ctx)
1359 return -ENOMEM;
1361 ctx->dev_idx = dev_idx;
1362 ctx->se_idx = se_idx;
1364 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1367 static struct genl_ops nfc_genl_ops[] = {
1369 .cmd = NFC_CMD_GET_DEVICE,
1370 .doit = nfc_genl_get_device,
1371 .dumpit = nfc_genl_dump_devices,
1372 .done = nfc_genl_dump_devices_done,
1373 .policy = nfc_genl_policy,
1376 .cmd = NFC_CMD_DEV_UP,
1377 .doit = nfc_genl_dev_up,
1378 .policy = nfc_genl_policy,
1381 .cmd = NFC_CMD_DEV_DOWN,
1382 .doit = nfc_genl_dev_down,
1383 .policy = nfc_genl_policy,
1386 .cmd = NFC_CMD_START_POLL,
1387 .doit = nfc_genl_start_poll,
1388 .policy = nfc_genl_policy,
1391 .cmd = NFC_CMD_STOP_POLL,
1392 .doit = nfc_genl_stop_poll,
1393 .policy = nfc_genl_policy,
1396 .cmd = NFC_CMD_DEP_LINK_UP,
1397 .doit = nfc_genl_dep_link_up,
1398 .policy = nfc_genl_policy,
1401 .cmd = NFC_CMD_DEP_LINK_DOWN,
1402 .doit = nfc_genl_dep_link_down,
1403 .policy = nfc_genl_policy,
1406 .cmd = NFC_CMD_GET_TARGET,
1407 .dumpit = nfc_genl_dump_targets,
1408 .done = nfc_genl_dump_targets_done,
1409 .policy = nfc_genl_policy,
1412 .cmd = NFC_CMD_LLC_GET_PARAMS,
1413 .doit = nfc_genl_llc_get_params,
1414 .policy = nfc_genl_policy,
1417 .cmd = NFC_CMD_LLC_SET_PARAMS,
1418 .doit = nfc_genl_llc_set_params,
1419 .policy = nfc_genl_policy,
1422 .cmd = NFC_CMD_LLC_SDREQ,
1423 .doit = nfc_genl_llc_sdreq,
1424 .policy = nfc_genl_policy,
1427 .cmd = NFC_CMD_FW_DOWNLOAD,
1428 .doit = nfc_genl_fw_download,
1429 .policy = nfc_genl_policy,
1432 .cmd = NFC_CMD_ENABLE_SE,
1433 .doit = nfc_genl_enable_se,
1434 .policy = nfc_genl_policy,
1437 .cmd = NFC_CMD_DISABLE_SE,
1438 .doit = nfc_genl_disable_se,
1439 .policy = nfc_genl_policy,
1442 .cmd = NFC_CMD_GET_SE,
1443 .dumpit = nfc_genl_dump_ses,
1444 .done = nfc_genl_dump_ses_done,
1445 .policy = nfc_genl_policy,
1448 .cmd = NFC_CMD_SE_IO,
1449 .doit = nfc_genl_se_io,
1450 .policy = nfc_genl_policy,
1455 struct urelease_work {
1456 struct work_struct w;
1457 int portid;
1460 static void nfc_urelease_event_work(struct work_struct *work)
1462 struct urelease_work *w = container_of(work, struct urelease_work, w);
1463 struct class_dev_iter iter;
1464 struct nfc_dev *dev;
1466 pr_debug("portid %d\n", w->portid);
1468 mutex_lock(&nfc_devlist_mutex);
1470 nfc_device_iter_init(&iter);
1471 dev = nfc_device_iter_next(&iter);
1473 while (dev) {
1474 mutex_lock(&dev->genl_data.genl_data_mutex);
1476 if (dev->genl_data.poll_req_portid == w->portid) {
1477 nfc_stop_poll(dev);
1478 dev->genl_data.poll_req_portid = 0;
1481 mutex_unlock(&dev->genl_data.genl_data_mutex);
1483 dev = nfc_device_iter_next(&iter);
1486 nfc_device_iter_exit(&iter);
1488 mutex_unlock(&nfc_devlist_mutex);
1490 kfree(w);
1493 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1494 unsigned long event, void *ptr)
1496 struct netlink_notify *n = ptr;
1497 struct urelease_work *w;
1499 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1500 goto out;
1502 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1504 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1505 if (w) {
1506 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1507 w->portid = n->portid;
1508 schedule_work((struct work_struct *) w);
1511 out:
1512 return NOTIFY_DONE;
1515 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1517 genl_data->poll_req_portid = 0;
1518 mutex_init(&genl_data->genl_data_mutex);
1521 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1523 mutex_destroy(&genl_data->genl_data_mutex);
1526 static struct notifier_block nl_notifier = {
1527 .notifier_call = nfc_genl_rcv_nl_event,
1531 * nfc_genl_init() - Initialize netlink interface
1533 * This initialization function registers the nfc netlink family.
1535 int __init nfc_genl_init(void)
1537 int rc;
1539 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
1540 ARRAY_SIZE(nfc_genl_ops));
1541 if (rc)
1542 return rc;
1544 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1546 netlink_register_notifier(&nl_notifier);
1548 return rc;
1552 * nfc_genl_exit() - Deinitialize netlink interface
1554 * This exit function unregisters the nfc netlink family.
1556 void nfc_genl_exit(void)
1558 netlink_unregister_notifier(&nl_notifier);
1559 genl_unregister_family(&nfc_genl_family);