Merge tag 'regmap-v3.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6.git] / net / nfc / netlink.c
blobf0c4d61f37c0f623df3eb40a1222a87f9c875f69
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 },
61 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
62 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
63 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
66 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
67 struct netlink_callback *cb, int flags)
69 void *hdr;
71 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
72 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
73 if (!hdr)
74 return -EMSGSIZE;
76 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
79 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
80 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
81 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
82 goto nla_put_failure;
83 if (target->nfcid1_len > 0 &&
84 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
85 target->nfcid1))
86 goto nla_put_failure;
87 if (target->sensb_res_len > 0 &&
88 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
89 target->sensb_res))
90 goto nla_put_failure;
91 if (target->sensf_res_len > 0 &&
92 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
93 target->sensf_res))
94 goto nla_put_failure;
96 return genlmsg_end(msg, hdr);
98 nla_put_failure:
99 genlmsg_cancel(msg, hdr);
100 return -EMSGSIZE;
103 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
105 struct nfc_dev *dev;
106 int rc;
107 u32 idx;
109 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
110 nfc_genl_family.attrbuf,
111 nfc_genl_family.maxattr,
112 nfc_genl_policy);
113 if (rc < 0)
114 return ERR_PTR(rc);
116 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
117 return ERR_PTR(-EINVAL);
119 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
121 dev = nfc_get_device(idx);
122 if (!dev)
123 return ERR_PTR(-ENODEV);
125 return dev;
128 static int nfc_genl_dump_targets(struct sk_buff *skb,
129 struct netlink_callback *cb)
131 int i = cb->args[0];
132 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
133 int rc;
135 if (!dev) {
136 dev = __get_device_from_cb(cb);
137 if (IS_ERR(dev))
138 return PTR_ERR(dev);
140 cb->args[1] = (long) dev;
143 device_lock(&dev->dev);
145 cb->seq = dev->targets_generation;
147 while (i < dev->n_targets) {
148 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
149 NLM_F_MULTI);
150 if (rc < 0)
151 break;
153 i++;
156 device_unlock(&dev->dev);
158 cb->args[0] = i;
160 return skb->len;
163 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
165 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
167 if (dev)
168 nfc_put_device(dev);
170 return 0;
173 int nfc_genl_targets_found(struct nfc_dev *dev)
175 struct sk_buff *msg;
176 void *hdr;
178 dev->genl_data.poll_req_portid = 0;
180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
181 if (!msg)
182 return -ENOMEM;
184 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
185 NFC_EVENT_TARGETS_FOUND);
186 if (!hdr)
187 goto free_msg;
189 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
190 goto nla_put_failure;
192 genlmsg_end(msg, hdr);
194 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
196 nla_put_failure:
197 genlmsg_cancel(msg, hdr);
198 free_msg:
199 nlmsg_free(msg);
200 return -EMSGSIZE;
203 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
205 struct sk_buff *msg;
206 void *hdr;
208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
209 if (!msg)
210 return -ENOMEM;
212 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
213 NFC_EVENT_TARGET_LOST);
214 if (!hdr)
215 goto free_msg;
217 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
218 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
219 goto nla_put_failure;
221 genlmsg_end(msg, hdr);
223 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
225 return 0;
227 nla_put_failure:
228 genlmsg_cancel(msg, hdr);
229 free_msg:
230 nlmsg_free(msg);
231 return -EMSGSIZE;
234 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
236 struct sk_buff *msg;
237 void *hdr;
239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
240 if (!msg)
241 return -ENOMEM;
243 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
244 NFC_EVENT_TM_ACTIVATED);
245 if (!hdr)
246 goto free_msg;
248 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
249 goto nla_put_failure;
250 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
251 goto nla_put_failure;
253 genlmsg_end(msg, hdr);
255 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
257 return 0;
259 nla_put_failure:
260 genlmsg_cancel(msg, hdr);
261 free_msg:
262 nlmsg_free(msg);
263 return -EMSGSIZE;
266 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
268 struct sk_buff *msg;
269 void *hdr;
271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
272 if (!msg)
273 return -ENOMEM;
275 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
276 NFC_EVENT_TM_DEACTIVATED);
277 if (!hdr)
278 goto free_msg;
280 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
281 goto nla_put_failure;
283 genlmsg_end(msg, hdr);
285 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
287 return 0;
289 nla_put_failure:
290 genlmsg_cancel(msg, hdr);
291 free_msg:
292 nlmsg_free(msg);
293 return -EMSGSIZE;
296 int nfc_genl_device_added(struct nfc_dev *dev)
298 struct sk_buff *msg;
299 void *hdr;
301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
302 if (!msg)
303 return -ENOMEM;
305 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
306 NFC_EVENT_DEVICE_ADDED);
307 if (!hdr)
308 goto free_msg;
310 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
311 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
312 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
313 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
314 goto nla_put_failure;
316 genlmsg_end(msg, hdr);
318 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
320 return 0;
322 nla_put_failure:
323 genlmsg_cancel(msg, hdr);
324 free_msg:
325 nlmsg_free(msg);
326 return -EMSGSIZE;
329 int nfc_genl_device_removed(struct nfc_dev *dev)
331 struct sk_buff *msg;
332 void *hdr;
334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
335 if (!msg)
336 return -ENOMEM;
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
339 NFC_EVENT_DEVICE_REMOVED);
340 if (!hdr)
341 goto free_msg;
343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
344 goto nla_put_failure;
346 genlmsg_end(msg, hdr);
348 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
350 return 0;
352 nla_put_failure:
353 genlmsg_cancel(msg, hdr);
354 free_msg:
355 nlmsg_free(msg);
356 return -EMSGSIZE;
359 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
361 struct sk_buff *msg;
362 struct nlattr *sdp_attr, *uri_attr;
363 struct nfc_llcp_sdp_tlv *sdres;
364 struct hlist_node *n;
365 void *hdr;
366 int rc = -EMSGSIZE;
367 int i;
369 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
370 if (!msg)
371 return -ENOMEM;
373 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
374 NFC_EVENT_LLC_SDRES);
375 if (!hdr)
376 goto free_msg;
378 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
379 goto nla_put_failure;
381 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
382 if (sdp_attr == NULL) {
383 rc = -ENOMEM;
384 goto nla_put_failure;
387 i = 1;
388 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
389 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
391 uri_attr = nla_nest_start(msg, i++);
392 if (uri_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
397 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
398 goto nla_put_failure;
400 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
401 goto nla_put_failure;
403 nla_nest_end(msg, uri_attr);
405 hlist_del(&sdres->node);
407 nfc_llcp_free_sdp_tlv(sdres);
410 nla_nest_end(msg, sdp_attr);
412 genlmsg_end(msg, hdr);
414 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
416 nla_put_failure:
417 genlmsg_cancel(msg, hdr);
419 free_msg:
420 nlmsg_free(msg);
422 nfc_llcp_free_sdp_tlv_list(sdres_list);
424 return rc;
427 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
428 u32 portid, u32 seq,
429 struct netlink_callback *cb,
430 int flags)
432 void *hdr;
434 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
435 NFC_CMD_GET_DEVICE);
436 if (!hdr)
437 return -EMSGSIZE;
439 if (cb)
440 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
442 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
443 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
444 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
445 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
446 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
447 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
448 goto nla_put_failure;
450 return genlmsg_end(msg, hdr);
452 nla_put_failure:
453 genlmsg_cancel(msg, hdr);
454 return -EMSGSIZE;
457 static int nfc_genl_dump_devices(struct sk_buff *skb,
458 struct netlink_callback *cb)
460 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
461 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
462 bool first_call = false;
464 if (!iter) {
465 first_call = true;
466 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
467 if (!iter)
468 return -ENOMEM;
469 cb->args[0] = (long) iter;
472 mutex_lock(&nfc_devlist_mutex);
474 cb->seq = nfc_devlist_generation;
476 if (first_call) {
477 nfc_device_iter_init(iter);
478 dev = nfc_device_iter_next(iter);
481 while (dev) {
482 int rc;
484 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
485 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
486 if (rc < 0)
487 break;
489 dev = nfc_device_iter_next(iter);
492 mutex_unlock(&nfc_devlist_mutex);
494 cb->args[1] = (long) dev;
496 return skb->len;
499 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
501 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
503 nfc_device_iter_exit(iter);
504 kfree(iter);
506 return 0;
509 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
510 u8 comm_mode, u8 rf_mode)
512 struct sk_buff *msg;
513 void *hdr;
515 pr_debug("DEP link is up\n");
517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
518 if (!msg)
519 return -ENOMEM;
521 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
522 if (!hdr)
523 goto free_msg;
525 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
526 goto nla_put_failure;
527 if (rf_mode == NFC_RF_INITIATOR &&
528 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
529 goto nla_put_failure;
530 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
531 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
532 goto nla_put_failure;
534 genlmsg_end(msg, hdr);
536 dev->dep_link_up = true;
538 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
540 return 0;
542 nla_put_failure:
543 genlmsg_cancel(msg, hdr);
544 free_msg:
545 nlmsg_free(msg);
546 return -EMSGSIZE;
549 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
551 struct sk_buff *msg;
552 void *hdr;
554 pr_debug("DEP link is down\n");
556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
557 if (!msg)
558 return -ENOMEM;
560 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
561 NFC_CMD_DEP_LINK_DOWN);
562 if (!hdr)
563 goto free_msg;
565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
566 goto nla_put_failure;
568 genlmsg_end(msg, hdr);
570 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
572 return 0;
574 nla_put_failure:
575 genlmsg_cancel(msg, hdr);
576 free_msg:
577 nlmsg_free(msg);
578 return -EMSGSIZE;
581 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
583 struct sk_buff *msg;
584 struct nfc_dev *dev;
585 u32 idx;
586 int rc = -ENOBUFS;
588 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
589 return -EINVAL;
591 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
593 dev = nfc_get_device(idx);
594 if (!dev)
595 return -ENODEV;
597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
598 if (!msg) {
599 rc = -ENOMEM;
600 goto out_putdev;
603 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
604 NULL, 0);
605 if (rc < 0)
606 goto out_free;
608 nfc_put_device(dev);
610 return genlmsg_reply(msg, info);
612 out_free:
613 nlmsg_free(msg);
614 out_putdev:
615 nfc_put_device(dev);
616 return rc;
619 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
621 struct nfc_dev *dev;
622 int rc;
623 u32 idx;
625 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
626 return -EINVAL;
628 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
630 dev = nfc_get_device(idx);
631 if (!dev)
632 return -ENODEV;
634 rc = nfc_dev_up(dev);
636 nfc_put_device(dev);
637 return rc;
640 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
642 struct nfc_dev *dev;
643 int rc;
644 u32 idx;
646 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
647 return -EINVAL;
649 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
651 dev = nfc_get_device(idx);
652 if (!dev)
653 return -ENODEV;
655 rc = nfc_dev_down(dev);
657 nfc_put_device(dev);
658 return rc;
661 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
663 struct nfc_dev *dev;
664 int rc;
665 u32 idx;
666 u32 im_protocols = 0, tm_protocols = 0;
668 pr_debug("Poll start\n");
670 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
671 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
672 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
673 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
674 return -EINVAL;
676 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
678 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
679 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
681 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
682 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
683 else if (info->attrs[NFC_ATTR_PROTOCOLS])
684 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
686 dev = nfc_get_device(idx);
687 if (!dev)
688 return -ENODEV;
690 mutex_lock(&dev->genl_data.genl_data_mutex);
692 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
693 if (!rc)
694 dev->genl_data.poll_req_portid = info->snd_portid;
696 mutex_unlock(&dev->genl_data.genl_data_mutex);
698 nfc_put_device(dev);
699 return rc;
702 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
704 struct nfc_dev *dev;
705 int rc;
706 u32 idx;
708 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
709 return -EINVAL;
711 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713 dev = nfc_get_device(idx);
714 if (!dev)
715 return -ENODEV;
717 device_lock(&dev->dev);
719 if (!dev->polling) {
720 device_unlock(&dev->dev);
721 return -EINVAL;
724 device_unlock(&dev->dev);
726 mutex_lock(&dev->genl_data.genl_data_mutex);
728 if (dev->genl_data.poll_req_portid != info->snd_portid) {
729 rc = -EBUSY;
730 goto out;
733 rc = nfc_stop_poll(dev);
734 dev->genl_data.poll_req_portid = 0;
736 out:
737 mutex_unlock(&dev->genl_data.genl_data_mutex);
738 nfc_put_device(dev);
739 return rc;
742 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
744 struct nfc_dev *dev;
745 int rc, tgt_idx;
746 u32 idx;
747 u8 comm;
749 pr_debug("DEP link up\n");
751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
752 !info->attrs[NFC_ATTR_COMM_MODE])
753 return -EINVAL;
755 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
756 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
757 tgt_idx = NFC_TARGET_IDX_ANY;
758 else
759 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
761 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
763 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
764 return -EINVAL;
766 dev = nfc_get_device(idx);
767 if (!dev)
768 return -ENODEV;
770 rc = nfc_dep_link_up(dev, tgt_idx, comm);
772 nfc_put_device(dev);
774 return rc;
777 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
779 struct nfc_dev *dev;
780 int rc;
781 u32 idx;
783 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
784 return -EINVAL;
786 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
788 dev = nfc_get_device(idx);
789 if (!dev)
790 return -ENODEV;
792 rc = nfc_dep_link_down(dev);
794 nfc_put_device(dev);
795 return rc;
798 static int nfc_genl_send_params(struct sk_buff *msg,
799 struct nfc_llcp_local *local,
800 u32 portid, u32 seq)
802 void *hdr;
804 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
805 NFC_CMD_LLC_GET_PARAMS);
806 if (!hdr)
807 return -EMSGSIZE;
809 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
810 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
811 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
812 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
813 goto nla_put_failure;
815 return genlmsg_end(msg, hdr);
817 nla_put_failure:
819 genlmsg_cancel(msg, hdr);
820 return -EMSGSIZE;
823 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
825 struct nfc_dev *dev;
826 struct nfc_llcp_local *local;
827 int rc = 0;
828 struct sk_buff *msg = NULL;
829 u32 idx;
831 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
832 return -EINVAL;
834 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
836 dev = nfc_get_device(idx);
837 if (!dev)
838 return -ENODEV;
840 device_lock(&dev->dev);
842 local = nfc_llcp_find_local(dev);
843 if (!local) {
844 rc = -ENODEV;
845 goto exit;
848 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
849 if (!msg) {
850 rc = -ENOMEM;
851 goto exit;
854 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
856 exit:
857 device_unlock(&dev->dev);
859 nfc_put_device(dev);
861 if (rc < 0) {
862 if (msg)
863 nlmsg_free(msg);
865 return rc;
868 return genlmsg_reply(msg, info);
871 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
873 struct nfc_dev *dev;
874 struct nfc_llcp_local *local;
875 u8 rw = 0;
876 u16 miux = 0;
877 u32 idx;
878 int rc = 0;
880 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
881 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
882 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
883 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
884 return -EINVAL;
886 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
887 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
889 if (rw > LLCP_MAX_RW)
890 return -EINVAL;
893 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
894 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
896 if (miux > LLCP_MAX_MIUX)
897 return -EINVAL;
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
902 dev = nfc_get_device(idx);
903 if (!dev)
904 return -ENODEV;
906 device_lock(&dev->dev);
908 local = nfc_llcp_find_local(dev);
909 if (!local) {
910 nfc_put_device(dev);
911 rc = -ENODEV;
912 goto exit;
915 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
916 if (dev->dep_link_up) {
917 rc = -EINPROGRESS;
918 goto exit;
921 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
924 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
925 local->rw = rw;
927 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
928 local->miux = cpu_to_be16(miux);
930 exit:
931 device_unlock(&dev->dev);
933 nfc_put_device(dev);
935 return rc;
938 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
940 struct nfc_dev *dev;
941 struct nfc_llcp_local *local;
942 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
943 u32 idx;
944 u8 tid;
945 char *uri;
946 int rc = 0, rem;
947 size_t uri_len, tlvs_len;
948 struct hlist_head sdreq_list;
949 struct nfc_llcp_sdp_tlv *sdreq;
951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
952 !info->attrs[NFC_ATTR_LLC_SDP])
953 return -EINVAL;
955 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
957 dev = nfc_get_device(idx);
958 if (!dev) {
959 rc = -ENODEV;
960 goto exit;
963 device_lock(&dev->dev);
965 if (dev->dep_link_up == false) {
966 rc = -ENOLINK;
967 goto exit;
970 local = nfc_llcp_find_local(dev);
971 if (!local) {
972 nfc_put_device(dev);
973 rc = -ENODEV;
974 goto exit;
977 INIT_HLIST_HEAD(&sdreq_list);
979 tlvs_len = 0;
981 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
982 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
983 nfc_sdp_genl_policy);
985 if (rc != 0) {
986 rc = -EINVAL;
987 goto exit;
990 if (!sdp_attrs[NFC_SDP_ATTR_URI])
991 continue;
993 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
994 if (uri_len == 0)
995 continue;
997 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
998 if (uri == NULL || *uri == 0)
999 continue;
1001 tid = local->sdreq_next_tid++;
1003 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1004 if (sdreq == NULL) {
1005 rc = -ENOMEM;
1006 goto exit;
1009 tlvs_len += sdreq->tlv_len;
1011 hlist_add_head(&sdreq->node, &sdreq_list);
1014 if (hlist_empty(&sdreq_list)) {
1015 rc = -EINVAL;
1016 goto exit;
1019 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1020 exit:
1021 device_unlock(&dev->dev);
1023 nfc_put_device(dev);
1025 return rc;
1028 static struct genl_ops nfc_genl_ops[] = {
1030 .cmd = NFC_CMD_GET_DEVICE,
1031 .doit = nfc_genl_get_device,
1032 .dumpit = nfc_genl_dump_devices,
1033 .done = nfc_genl_dump_devices_done,
1034 .policy = nfc_genl_policy,
1037 .cmd = NFC_CMD_DEV_UP,
1038 .doit = nfc_genl_dev_up,
1039 .policy = nfc_genl_policy,
1042 .cmd = NFC_CMD_DEV_DOWN,
1043 .doit = nfc_genl_dev_down,
1044 .policy = nfc_genl_policy,
1047 .cmd = NFC_CMD_START_POLL,
1048 .doit = nfc_genl_start_poll,
1049 .policy = nfc_genl_policy,
1052 .cmd = NFC_CMD_STOP_POLL,
1053 .doit = nfc_genl_stop_poll,
1054 .policy = nfc_genl_policy,
1057 .cmd = NFC_CMD_DEP_LINK_UP,
1058 .doit = nfc_genl_dep_link_up,
1059 .policy = nfc_genl_policy,
1062 .cmd = NFC_CMD_DEP_LINK_DOWN,
1063 .doit = nfc_genl_dep_link_down,
1064 .policy = nfc_genl_policy,
1067 .cmd = NFC_CMD_GET_TARGET,
1068 .dumpit = nfc_genl_dump_targets,
1069 .done = nfc_genl_dump_targets_done,
1070 .policy = nfc_genl_policy,
1073 .cmd = NFC_CMD_LLC_GET_PARAMS,
1074 .doit = nfc_genl_llc_get_params,
1075 .policy = nfc_genl_policy,
1078 .cmd = NFC_CMD_LLC_SET_PARAMS,
1079 .doit = nfc_genl_llc_set_params,
1080 .policy = nfc_genl_policy,
1083 .cmd = NFC_CMD_LLC_SDREQ,
1084 .doit = nfc_genl_llc_sdreq,
1085 .policy = nfc_genl_policy,
1090 struct urelease_work {
1091 struct work_struct w;
1092 int portid;
1095 static void nfc_urelease_event_work(struct work_struct *work)
1097 struct urelease_work *w = container_of(work, struct urelease_work, w);
1098 struct class_dev_iter iter;
1099 struct nfc_dev *dev;
1101 pr_debug("portid %d\n", w->portid);
1103 mutex_lock(&nfc_devlist_mutex);
1105 nfc_device_iter_init(&iter);
1106 dev = nfc_device_iter_next(&iter);
1108 while (dev) {
1109 mutex_lock(&dev->genl_data.genl_data_mutex);
1111 if (dev->genl_data.poll_req_portid == w->portid) {
1112 nfc_stop_poll(dev);
1113 dev->genl_data.poll_req_portid = 0;
1116 mutex_unlock(&dev->genl_data.genl_data_mutex);
1118 dev = nfc_device_iter_next(&iter);
1121 nfc_device_iter_exit(&iter);
1123 mutex_unlock(&nfc_devlist_mutex);
1125 kfree(w);
1128 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1129 unsigned long event, void *ptr)
1131 struct netlink_notify *n = ptr;
1132 struct urelease_work *w;
1134 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1135 goto out;
1137 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1139 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1140 if (w) {
1141 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1142 w->portid = n->portid;
1143 schedule_work((struct work_struct *) w);
1146 out:
1147 return NOTIFY_DONE;
1150 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1152 genl_data->poll_req_portid = 0;
1153 mutex_init(&genl_data->genl_data_mutex);
1156 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1158 mutex_destroy(&genl_data->genl_data_mutex);
1161 static struct notifier_block nl_notifier = {
1162 .notifier_call = nfc_genl_rcv_nl_event,
1166 * nfc_genl_init() - Initialize netlink interface
1168 * This initialization function registers the nfc netlink family.
1170 int __init nfc_genl_init(void)
1172 int rc;
1174 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
1175 ARRAY_SIZE(nfc_genl_ops));
1176 if (rc)
1177 return rc;
1179 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1181 netlink_register_notifier(&nl_notifier);
1183 return rc;
1187 * nfc_genl_exit() - Deinitialize netlink interface
1189 * This exit function unregisters the nfc netlink family.
1191 void nfc_genl_exit(void)
1193 netlink_unregister_notifier(&nl_notifier);
1194 genl_unregister_family(&nfc_genl_family);