ARM: 7681/1: hw_breakpoint: use warn_once to avoid spam from reset_ctrl_regs()
[linux-2.6/btrfs-unstable.git] / net / nfc / netlink.c
blob504b883439f1b535b565c8efdc79e965afe465d4
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"
32 #include "llcp/llcp.h"
34 static struct genl_multicast_group nfc_genl_event_mcgrp = {
35 .name = NFC_GENL_MCAST_EVENT_NAME,
38 static struct genl_family nfc_genl_family = {
39 .id = GENL_ID_GENERATE,
40 .hdrsize = 0,
41 .name = NFC_GENL_NAME,
42 .version = NFC_GENL_VERSION,
43 .maxattr = NFC_ATTR_MAX,
46 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
47 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
48 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
49 .len = NFC_DEVICE_NAME_MAXSIZE },
50 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
51 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
52 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
53 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
54 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
55 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
58 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
59 struct netlink_callback *cb, int flags)
61 void *hdr;
63 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
64 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
65 if (!hdr)
66 return -EMSGSIZE;
68 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
70 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
71 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
72 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
73 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
74 goto nla_put_failure;
75 if (target->nfcid1_len > 0 &&
76 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
77 target->nfcid1))
78 goto nla_put_failure;
79 if (target->sensb_res_len > 0 &&
80 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
81 target->sensb_res))
82 goto nla_put_failure;
83 if (target->sensf_res_len > 0 &&
84 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
85 target->sensf_res))
86 goto nla_put_failure;
88 return genlmsg_end(msg, hdr);
90 nla_put_failure:
91 genlmsg_cancel(msg, hdr);
92 return -EMSGSIZE;
95 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
97 struct nfc_dev *dev;
98 int rc;
99 u32 idx;
101 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
102 nfc_genl_family.attrbuf,
103 nfc_genl_family.maxattr,
104 nfc_genl_policy);
105 if (rc < 0)
106 return ERR_PTR(rc);
108 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
109 return ERR_PTR(-EINVAL);
111 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
113 dev = nfc_get_device(idx);
114 if (!dev)
115 return ERR_PTR(-ENODEV);
117 return dev;
120 static int nfc_genl_dump_targets(struct sk_buff *skb,
121 struct netlink_callback *cb)
123 int i = cb->args[0];
124 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
125 int rc;
127 if (!dev) {
128 dev = __get_device_from_cb(cb);
129 if (IS_ERR(dev))
130 return PTR_ERR(dev);
132 cb->args[1] = (long) dev;
135 device_lock(&dev->dev);
137 cb->seq = dev->targets_generation;
139 while (i < dev->n_targets) {
140 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
141 NLM_F_MULTI);
142 if (rc < 0)
143 break;
145 i++;
148 device_unlock(&dev->dev);
150 cb->args[0] = i;
152 return skb->len;
155 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
157 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
159 if (dev)
160 nfc_put_device(dev);
162 return 0;
165 int nfc_genl_targets_found(struct nfc_dev *dev)
167 struct sk_buff *msg;
168 void *hdr;
170 dev->genl_data.poll_req_portid = 0;
172 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
173 if (!msg)
174 return -ENOMEM;
176 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
177 NFC_EVENT_TARGETS_FOUND);
178 if (!hdr)
179 goto free_msg;
181 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
182 goto nla_put_failure;
184 genlmsg_end(msg, hdr);
186 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
188 nla_put_failure:
189 genlmsg_cancel(msg, hdr);
190 free_msg:
191 nlmsg_free(msg);
192 return -EMSGSIZE;
195 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
197 struct sk_buff *msg;
198 void *hdr;
200 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
201 if (!msg)
202 return -ENOMEM;
204 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
205 NFC_EVENT_TARGET_LOST);
206 if (!hdr)
207 goto free_msg;
209 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
210 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
211 goto nla_put_failure;
213 genlmsg_end(msg, hdr);
215 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
217 return 0;
219 nla_put_failure:
220 genlmsg_cancel(msg, hdr);
221 free_msg:
222 nlmsg_free(msg);
223 return -EMSGSIZE;
226 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
228 struct sk_buff *msg;
229 void *hdr;
231 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
232 if (!msg)
233 return -ENOMEM;
235 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
236 NFC_EVENT_TM_ACTIVATED);
237 if (!hdr)
238 goto free_msg;
240 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
241 goto nla_put_failure;
242 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
243 goto nla_put_failure;
245 genlmsg_end(msg, hdr);
247 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
249 return 0;
251 nla_put_failure:
252 genlmsg_cancel(msg, hdr);
253 free_msg:
254 nlmsg_free(msg);
255 return -EMSGSIZE;
258 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
260 struct sk_buff *msg;
261 void *hdr;
263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
264 if (!msg)
265 return -ENOMEM;
267 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
268 NFC_EVENT_TM_DEACTIVATED);
269 if (!hdr)
270 goto free_msg;
272 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
273 goto nla_put_failure;
275 genlmsg_end(msg, hdr);
277 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
279 return 0;
281 nla_put_failure:
282 genlmsg_cancel(msg, hdr);
283 free_msg:
284 nlmsg_free(msg);
285 return -EMSGSIZE;
288 int nfc_genl_device_added(struct nfc_dev *dev)
290 struct sk_buff *msg;
291 void *hdr;
293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
294 if (!msg)
295 return -ENOMEM;
297 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
298 NFC_EVENT_DEVICE_ADDED);
299 if (!hdr)
300 goto free_msg;
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 genlmsg_end(msg, hdr);
310 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
312 return 0;
314 nla_put_failure:
315 genlmsg_cancel(msg, hdr);
316 free_msg:
317 nlmsg_free(msg);
318 return -EMSGSIZE;
321 int nfc_genl_device_removed(struct nfc_dev *dev)
323 struct sk_buff *msg;
324 void *hdr;
326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
327 if (!msg)
328 return -ENOMEM;
330 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
331 NFC_EVENT_DEVICE_REMOVED);
332 if (!hdr)
333 goto free_msg;
335 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
336 goto nla_put_failure;
338 genlmsg_end(msg, hdr);
340 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
342 return 0;
344 nla_put_failure:
345 genlmsg_cancel(msg, hdr);
346 free_msg:
347 nlmsg_free(msg);
348 return -EMSGSIZE;
351 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
352 u32 portid, u32 seq,
353 struct netlink_callback *cb,
354 int flags)
356 void *hdr;
358 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
359 NFC_CMD_GET_DEVICE);
360 if (!hdr)
361 return -EMSGSIZE;
363 if (cb)
364 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
366 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
367 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
368 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
369 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
370 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
371 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
372 goto nla_put_failure;
374 return genlmsg_end(msg, hdr);
376 nla_put_failure:
377 genlmsg_cancel(msg, hdr);
378 return -EMSGSIZE;
381 static int nfc_genl_dump_devices(struct sk_buff *skb,
382 struct netlink_callback *cb)
384 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
385 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
386 bool first_call = false;
388 if (!iter) {
389 first_call = true;
390 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
391 if (!iter)
392 return -ENOMEM;
393 cb->args[0] = (long) iter;
396 mutex_lock(&nfc_devlist_mutex);
398 cb->seq = nfc_devlist_generation;
400 if (first_call) {
401 nfc_device_iter_init(iter);
402 dev = nfc_device_iter_next(iter);
405 while (dev) {
406 int rc;
408 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
409 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
410 if (rc < 0)
411 break;
413 dev = nfc_device_iter_next(iter);
416 mutex_unlock(&nfc_devlist_mutex);
418 cb->args[1] = (long) dev;
420 return skb->len;
423 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
425 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
427 nfc_device_iter_exit(iter);
428 kfree(iter);
430 return 0;
433 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
434 u8 comm_mode, u8 rf_mode)
436 struct sk_buff *msg;
437 void *hdr;
439 pr_debug("DEP link is up\n");
441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
442 if (!msg)
443 return -ENOMEM;
445 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
446 if (!hdr)
447 goto free_msg;
449 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
450 goto nla_put_failure;
451 if (rf_mode == NFC_RF_INITIATOR &&
452 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
453 goto nla_put_failure;
454 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
455 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
456 goto nla_put_failure;
458 genlmsg_end(msg, hdr);
460 dev->dep_link_up = true;
462 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
464 return 0;
466 nla_put_failure:
467 genlmsg_cancel(msg, hdr);
468 free_msg:
469 nlmsg_free(msg);
470 return -EMSGSIZE;
473 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
475 struct sk_buff *msg;
476 void *hdr;
478 pr_debug("DEP link is down\n");
480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
481 if (!msg)
482 return -ENOMEM;
484 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
485 NFC_CMD_DEP_LINK_DOWN);
486 if (!hdr)
487 goto free_msg;
489 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
490 goto nla_put_failure;
492 genlmsg_end(msg, hdr);
494 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
496 return 0;
498 nla_put_failure:
499 genlmsg_cancel(msg, hdr);
500 free_msg:
501 nlmsg_free(msg);
502 return -EMSGSIZE;
505 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
507 struct sk_buff *msg;
508 struct nfc_dev *dev;
509 u32 idx;
510 int rc = -ENOBUFS;
512 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
513 return -EINVAL;
515 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
517 dev = nfc_get_device(idx);
518 if (!dev)
519 return -ENODEV;
521 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
522 if (!msg) {
523 rc = -ENOMEM;
524 goto out_putdev;
527 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
528 NULL, 0);
529 if (rc < 0)
530 goto out_free;
532 nfc_put_device(dev);
534 return genlmsg_reply(msg, info);
536 out_free:
537 nlmsg_free(msg);
538 out_putdev:
539 nfc_put_device(dev);
540 return rc;
543 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
545 struct nfc_dev *dev;
546 int rc;
547 u32 idx;
549 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
550 return -EINVAL;
552 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
554 dev = nfc_get_device(idx);
555 if (!dev)
556 return -ENODEV;
558 rc = nfc_dev_up(dev);
560 nfc_put_device(dev);
561 return rc;
564 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
566 struct nfc_dev *dev;
567 int rc;
568 u32 idx;
570 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
571 return -EINVAL;
573 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
575 dev = nfc_get_device(idx);
576 if (!dev)
577 return -ENODEV;
579 rc = nfc_dev_down(dev);
581 nfc_put_device(dev);
582 return rc;
585 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
587 struct nfc_dev *dev;
588 int rc;
589 u32 idx;
590 u32 im_protocols = 0, tm_protocols = 0;
592 pr_debug("Poll start\n");
594 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
595 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
596 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
597 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
598 return -EINVAL;
600 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
602 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
603 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
605 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
606 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
607 else if (info->attrs[NFC_ATTR_PROTOCOLS])
608 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
610 dev = nfc_get_device(idx);
611 if (!dev)
612 return -ENODEV;
614 mutex_lock(&dev->genl_data.genl_data_mutex);
616 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
617 if (!rc)
618 dev->genl_data.poll_req_portid = info->snd_portid;
620 mutex_unlock(&dev->genl_data.genl_data_mutex);
622 nfc_put_device(dev);
623 return rc;
626 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
628 struct nfc_dev *dev;
629 int rc;
630 u32 idx;
632 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
633 return -EINVAL;
635 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
637 dev = nfc_get_device(idx);
638 if (!dev)
639 return -ENODEV;
641 device_lock(&dev->dev);
643 if (!dev->polling) {
644 device_unlock(&dev->dev);
645 return -EINVAL;
648 device_unlock(&dev->dev);
650 mutex_lock(&dev->genl_data.genl_data_mutex);
652 if (dev->genl_data.poll_req_portid != info->snd_portid) {
653 rc = -EBUSY;
654 goto out;
657 rc = nfc_stop_poll(dev);
658 dev->genl_data.poll_req_portid = 0;
660 out:
661 mutex_unlock(&dev->genl_data.genl_data_mutex);
662 nfc_put_device(dev);
663 return rc;
666 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
668 struct nfc_dev *dev;
669 int rc, tgt_idx;
670 u32 idx;
671 u8 comm;
673 pr_debug("DEP link up\n");
675 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
676 !info->attrs[NFC_ATTR_COMM_MODE])
677 return -EINVAL;
679 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
680 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
681 tgt_idx = NFC_TARGET_IDX_ANY;
682 else
683 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
685 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
687 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
688 return -EINVAL;
690 dev = nfc_get_device(idx);
691 if (!dev)
692 return -ENODEV;
694 rc = nfc_dep_link_up(dev, tgt_idx, comm);
696 nfc_put_device(dev);
698 return rc;
701 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
703 struct nfc_dev *dev;
704 int rc;
705 u32 idx;
707 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
708 return -EINVAL;
710 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
712 dev = nfc_get_device(idx);
713 if (!dev)
714 return -ENODEV;
716 rc = nfc_dep_link_down(dev);
718 nfc_put_device(dev);
719 return rc;
722 static int nfc_genl_send_params(struct sk_buff *msg,
723 struct nfc_llcp_local *local,
724 u32 portid, u32 seq)
726 void *hdr;
728 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
729 NFC_CMD_LLC_GET_PARAMS);
730 if (!hdr)
731 return -EMSGSIZE;
733 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
734 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
735 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
736 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
737 goto nla_put_failure;
739 return genlmsg_end(msg, hdr);
741 nla_put_failure:
743 genlmsg_cancel(msg, hdr);
744 return -EMSGSIZE;
747 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
749 struct nfc_dev *dev;
750 struct nfc_llcp_local *local;
751 int rc = 0;
752 struct sk_buff *msg = NULL;
753 u32 idx;
755 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
756 return -EINVAL;
758 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
760 dev = nfc_get_device(idx);
761 if (!dev)
762 return -ENODEV;
764 device_lock(&dev->dev);
766 local = nfc_llcp_find_local(dev);
767 if (!local) {
768 rc = -ENODEV;
769 goto exit;
772 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
773 if (!msg) {
774 rc = -ENOMEM;
775 goto exit;
778 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
780 exit:
781 device_unlock(&dev->dev);
783 nfc_put_device(dev);
785 if (rc < 0) {
786 if (msg)
787 nlmsg_free(msg);
789 return rc;
792 return genlmsg_reply(msg, info);
795 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
797 struct nfc_dev *dev;
798 struct nfc_llcp_local *local;
799 u8 rw = 0;
800 u16 miux = 0;
801 u32 idx;
802 int rc = 0;
804 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
805 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
806 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
807 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
808 return -EINVAL;
810 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
811 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
813 if (rw > LLCP_MAX_RW)
814 return -EINVAL;
817 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
818 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
820 if (miux > LLCP_MAX_MIUX)
821 return -EINVAL;
824 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
826 dev = nfc_get_device(idx);
827 if (!dev)
828 return -ENODEV;
830 device_lock(&dev->dev);
832 local = nfc_llcp_find_local(dev);
833 if (!local) {
834 nfc_put_device(dev);
835 rc = -ENODEV;
836 goto exit;
839 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
840 if (dev->dep_link_up) {
841 rc = -EINPROGRESS;
842 goto exit;
845 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
848 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
849 local->rw = rw;
851 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
852 local->miux = cpu_to_be16(miux);
854 exit:
855 device_unlock(&dev->dev);
857 nfc_put_device(dev);
859 return rc;
862 static struct genl_ops nfc_genl_ops[] = {
864 .cmd = NFC_CMD_GET_DEVICE,
865 .doit = nfc_genl_get_device,
866 .dumpit = nfc_genl_dump_devices,
867 .done = nfc_genl_dump_devices_done,
868 .policy = nfc_genl_policy,
871 .cmd = NFC_CMD_DEV_UP,
872 .doit = nfc_genl_dev_up,
873 .policy = nfc_genl_policy,
876 .cmd = NFC_CMD_DEV_DOWN,
877 .doit = nfc_genl_dev_down,
878 .policy = nfc_genl_policy,
881 .cmd = NFC_CMD_START_POLL,
882 .doit = nfc_genl_start_poll,
883 .policy = nfc_genl_policy,
886 .cmd = NFC_CMD_STOP_POLL,
887 .doit = nfc_genl_stop_poll,
888 .policy = nfc_genl_policy,
891 .cmd = NFC_CMD_DEP_LINK_UP,
892 .doit = nfc_genl_dep_link_up,
893 .policy = nfc_genl_policy,
896 .cmd = NFC_CMD_DEP_LINK_DOWN,
897 .doit = nfc_genl_dep_link_down,
898 .policy = nfc_genl_policy,
901 .cmd = NFC_CMD_GET_TARGET,
902 .dumpit = nfc_genl_dump_targets,
903 .done = nfc_genl_dump_targets_done,
904 .policy = nfc_genl_policy,
907 .cmd = NFC_CMD_LLC_GET_PARAMS,
908 .doit = nfc_genl_llc_get_params,
909 .policy = nfc_genl_policy,
912 .cmd = NFC_CMD_LLC_SET_PARAMS,
913 .doit = nfc_genl_llc_set_params,
914 .policy = nfc_genl_policy,
919 struct urelease_work {
920 struct work_struct w;
921 int portid;
924 static void nfc_urelease_event_work(struct work_struct *work)
926 struct urelease_work *w = container_of(work, struct urelease_work, w);
927 struct class_dev_iter iter;
928 struct nfc_dev *dev;
930 pr_debug("portid %d\n", w->portid);
932 mutex_lock(&nfc_devlist_mutex);
934 nfc_device_iter_init(&iter);
935 dev = nfc_device_iter_next(&iter);
937 while (dev) {
938 mutex_lock(&dev->genl_data.genl_data_mutex);
940 if (dev->genl_data.poll_req_portid == w->portid) {
941 nfc_stop_poll(dev);
942 dev->genl_data.poll_req_portid = 0;
945 mutex_unlock(&dev->genl_data.genl_data_mutex);
947 dev = nfc_device_iter_next(&iter);
950 nfc_device_iter_exit(&iter);
952 mutex_unlock(&nfc_devlist_mutex);
954 kfree(w);
957 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
958 unsigned long event, void *ptr)
960 struct netlink_notify *n = ptr;
961 struct urelease_work *w;
963 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
964 goto out;
966 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
968 w = kmalloc(sizeof(*w), GFP_ATOMIC);
969 if (w) {
970 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
971 w->portid = n->portid;
972 schedule_work((struct work_struct *) w);
975 out:
976 return NOTIFY_DONE;
979 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
981 genl_data->poll_req_portid = 0;
982 mutex_init(&genl_data->genl_data_mutex);
985 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
987 mutex_destroy(&genl_data->genl_data_mutex);
990 static struct notifier_block nl_notifier = {
991 .notifier_call = nfc_genl_rcv_nl_event,
995 * nfc_genl_init() - Initialize netlink interface
997 * This initialization function registers the nfc netlink family.
999 int __init nfc_genl_init(void)
1001 int rc;
1003 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
1004 ARRAY_SIZE(nfc_genl_ops));
1005 if (rc)
1006 return rc;
1008 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1010 netlink_register_notifier(&nl_notifier);
1012 return rc;
1016 * nfc_genl_exit() - Deinitialize netlink interface
1018 * This exit function unregisters the nfc netlink family.
1020 void nfc_genl_exit(void)
1022 netlink_unregister_notifier(&nl_notifier);
1023 genl_unregister_family(&nfc_genl_family);