powerpc: Add new kconfig CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
[linux/fpc-iii.git] / net / nfc / netlink.c
blobc0b83dc9d99305850a61b647fe362b6ed52c50aa
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 * Vendor commands implementation based on net/wireless/nl80211.c
9 * which is:
11 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
12 * Copyright 2013-2014 Intel Mobile Communications GmbH
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
30 #include <net/genetlink.h>
31 #include <linux/nfc.h>
32 #include <linux/slab.h>
34 #include "nfc.h"
35 #include "llcp.h"
37 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
38 { .name = NFC_GENL_MCAST_EVENT_NAME, },
41 static struct genl_family nfc_genl_family;
42 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
43 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
44 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
45 .len = NFC_DEVICE_NAME_MAXSIZE },
46 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
47 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
48 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
50 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
51 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
53 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
55 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
56 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
57 .len = NFC_FIRMWARE_NAME_MAXSIZE },
58 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
59 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
63 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
64 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
65 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
68 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
69 struct netlink_callback *cb, int flags)
71 void *hdr;
73 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
74 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
75 if (!hdr)
76 return -EMSGSIZE;
78 genl_dump_check_consistent(cb, hdr);
80 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
81 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
82 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
83 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
84 goto nla_put_failure;
85 if (target->nfcid1_len > 0 &&
86 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
87 target->nfcid1))
88 goto nla_put_failure;
89 if (target->sensb_res_len > 0 &&
90 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
91 target->sensb_res))
92 goto nla_put_failure;
93 if (target->sensf_res_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
95 target->sensf_res))
96 goto nla_put_failure;
98 if (target->is_iso15693) {
99 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
100 target->iso15693_dsfid) ||
101 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
102 sizeof(target->iso15693_uid), target->iso15693_uid))
103 goto nla_put_failure;
106 genlmsg_end(msg, hdr);
107 return 0;
109 nla_put_failure:
110 genlmsg_cancel(msg, hdr);
111 return -EMSGSIZE;
114 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
116 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
117 struct nfc_dev *dev;
118 int rc;
119 u32 idx;
121 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
122 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
123 NULL);
124 if (rc < 0)
125 return ERR_PTR(rc);
127 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
128 return ERR_PTR(-EINVAL);
130 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
132 dev = nfc_get_device(idx);
133 if (!dev)
134 return ERR_PTR(-ENODEV);
136 return dev;
139 static int nfc_genl_dump_targets(struct sk_buff *skb,
140 struct netlink_callback *cb)
142 int i = cb->args[0];
143 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
144 int rc;
146 if (!dev) {
147 dev = __get_device_from_cb(cb);
148 if (IS_ERR(dev))
149 return PTR_ERR(dev);
151 cb->args[1] = (long) dev;
154 device_lock(&dev->dev);
156 cb->seq = dev->targets_generation;
158 while (i < dev->n_targets) {
159 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
160 NLM_F_MULTI);
161 if (rc < 0)
162 break;
164 i++;
167 device_unlock(&dev->dev);
169 cb->args[0] = i;
171 return skb->len;
174 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
176 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
178 if (dev)
179 nfc_put_device(dev);
181 return 0;
184 int nfc_genl_targets_found(struct nfc_dev *dev)
186 struct sk_buff *msg;
187 void *hdr;
189 dev->genl_data.poll_req_portid = 0;
191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
192 if (!msg)
193 return -ENOMEM;
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 NFC_EVENT_TARGETS_FOUND);
197 if (!hdr)
198 goto free_msg;
200 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
201 goto nla_put_failure;
203 genlmsg_end(msg, hdr);
205 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
207 nla_put_failure:
208 genlmsg_cancel(msg, hdr);
209 free_msg:
210 nlmsg_free(msg);
211 return -EMSGSIZE;
214 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
216 struct sk_buff *msg;
217 void *hdr;
219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
220 if (!msg)
221 return -ENOMEM;
223 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
224 NFC_EVENT_TARGET_LOST);
225 if (!hdr)
226 goto free_msg;
228 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
229 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
230 goto nla_put_failure;
232 genlmsg_end(msg, hdr);
234 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
236 return 0;
238 nla_put_failure:
239 genlmsg_cancel(msg, hdr);
240 free_msg:
241 nlmsg_free(msg);
242 return -EMSGSIZE;
245 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
247 struct sk_buff *msg;
248 void *hdr;
250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
251 if (!msg)
252 return -ENOMEM;
254 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
255 NFC_EVENT_TM_ACTIVATED);
256 if (!hdr)
257 goto free_msg;
259 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
260 goto nla_put_failure;
261 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
262 goto nla_put_failure;
264 genlmsg_end(msg, hdr);
266 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
268 return 0;
270 nla_put_failure:
271 genlmsg_cancel(msg, hdr);
272 free_msg:
273 nlmsg_free(msg);
274 return -EMSGSIZE;
277 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
279 struct sk_buff *msg;
280 void *hdr;
282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
283 if (!msg)
284 return -ENOMEM;
286 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
287 NFC_EVENT_TM_DEACTIVATED);
288 if (!hdr)
289 goto free_msg;
291 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
292 goto nla_put_failure;
294 genlmsg_end(msg, hdr);
296 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
298 return 0;
300 nla_put_failure:
301 genlmsg_cancel(msg, hdr);
302 free_msg:
303 nlmsg_free(msg);
304 return -EMSGSIZE;
307 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
309 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
310 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
311 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
312 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
313 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
314 return -1;
315 return 0;
318 int nfc_genl_device_added(struct nfc_dev *dev)
320 struct sk_buff *msg;
321 void *hdr;
323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
324 if (!msg)
325 return -ENOMEM;
327 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
328 NFC_EVENT_DEVICE_ADDED);
329 if (!hdr)
330 goto free_msg;
332 if (nfc_genl_setup_device_added(dev, msg))
333 goto nla_put_failure;
335 genlmsg_end(msg, hdr);
337 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
339 return 0;
341 nla_put_failure:
342 genlmsg_cancel(msg, hdr);
343 free_msg:
344 nlmsg_free(msg);
345 return -EMSGSIZE;
348 int nfc_genl_device_removed(struct nfc_dev *dev)
350 struct sk_buff *msg;
351 void *hdr;
353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
354 if (!msg)
355 return -ENOMEM;
357 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
358 NFC_EVENT_DEVICE_REMOVED);
359 if (!hdr)
360 goto free_msg;
362 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
363 goto nla_put_failure;
365 genlmsg_end(msg, hdr);
367 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
369 return 0;
371 nla_put_failure:
372 genlmsg_cancel(msg, hdr);
373 free_msg:
374 nlmsg_free(msg);
375 return -EMSGSIZE;
378 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
380 struct sk_buff *msg;
381 struct nlattr *sdp_attr, *uri_attr;
382 struct nfc_llcp_sdp_tlv *sdres;
383 struct hlist_node *n;
384 void *hdr;
385 int rc = -EMSGSIZE;
386 int i;
388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
389 if (!msg)
390 return -ENOMEM;
392 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
393 NFC_EVENT_LLC_SDRES);
394 if (!hdr)
395 goto free_msg;
397 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
398 goto nla_put_failure;
400 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
401 if (sdp_attr == NULL) {
402 rc = -ENOMEM;
403 goto nla_put_failure;
406 i = 1;
407 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
408 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
410 uri_attr = nla_nest_start(msg, i++);
411 if (uri_attr == NULL) {
412 rc = -ENOMEM;
413 goto nla_put_failure;
416 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
417 goto nla_put_failure;
419 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
420 goto nla_put_failure;
422 nla_nest_end(msg, uri_attr);
424 hlist_del(&sdres->node);
426 nfc_llcp_free_sdp_tlv(sdres);
429 nla_nest_end(msg, sdp_attr);
431 genlmsg_end(msg, hdr);
433 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
435 nla_put_failure:
436 genlmsg_cancel(msg, hdr);
438 free_msg:
439 nlmsg_free(msg);
441 nfc_llcp_free_sdp_tlv_list(sdres_list);
443 return rc;
446 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
448 struct sk_buff *msg;
449 void *hdr;
451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
452 if (!msg)
453 return -ENOMEM;
455 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
456 NFC_EVENT_SE_ADDED);
457 if (!hdr)
458 goto free_msg;
460 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
461 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
462 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
463 goto nla_put_failure;
465 genlmsg_end(msg, hdr);
467 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
469 return 0;
471 nla_put_failure:
472 genlmsg_cancel(msg, hdr);
473 free_msg:
474 nlmsg_free(msg);
475 return -EMSGSIZE;
478 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
480 struct sk_buff *msg;
481 void *hdr;
483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
484 if (!msg)
485 return -ENOMEM;
487 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
488 NFC_EVENT_SE_REMOVED);
489 if (!hdr)
490 goto free_msg;
492 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
493 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
494 goto nla_put_failure;
496 genlmsg_end(msg, hdr);
498 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
500 return 0;
502 nla_put_failure:
503 genlmsg_cancel(msg, hdr);
504 free_msg:
505 nlmsg_free(msg);
506 return -EMSGSIZE;
509 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
510 struct nfc_evt_transaction *evt_transaction)
512 struct nfc_se *se;
513 struct sk_buff *msg;
514 void *hdr;
516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
517 if (!msg)
518 return -ENOMEM;
520 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
521 NFC_EVENT_SE_TRANSACTION);
522 if (!hdr)
523 goto free_msg;
525 se = nfc_find_se(dev, se_idx);
526 if (!se)
527 goto free_msg;
529 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
530 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
531 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
532 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
533 evt_transaction->aid) ||
534 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
535 evt_transaction->params))
536 goto nla_put_failure;
538 /* evt_transaction is no more used */
539 devm_kfree(&dev->dev, evt_transaction);
541 genlmsg_end(msg, hdr);
543 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
545 return 0;
547 nla_put_failure:
548 genlmsg_cancel(msg, hdr);
549 free_msg:
550 /* evt_transaction is no more used */
551 devm_kfree(&dev->dev, evt_transaction);
552 nlmsg_free(msg);
553 return -EMSGSIZE;
556 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
558 struct nfc_se *se;
559 struct sk_buff *msg;
560 void *hdr;
562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
563 if (!msg)
564 return -ENOMEM;
566 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
567 NFC_EVENT_SE_CONNECTIVITY);
568 if (!hdr)
569 goto free_msg;
571 se = nfc_find_se(dev, se_idx);
572 if (!se)
573 goto free_msg;
575 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
576 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
577 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
578 goto nla_put_failure;
580 genlmsg_end(msg, hdr);
582 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
584 return 0;
586 nla_put_failure:
587 genlmsg_cancel(msg, hdr);
588 free_msg:
589 nlmsg_free(msg);
590 return -EMSGSIZE;
593 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
594 u32 portid, u32 seq,
595 struct netlink_callback *cb,
596 int flags)
598 void *hdr;
600 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
601 NFC_CMD_GET_DEVICE);
602 if (!hdr)
603 return -EMSGSIZE;
605 if (cb)
606 genl_dump_check_consistent(cb, hdr);
608 if (nfc_genl_setup_device_added(dev, msg))
609 goto nla_put_failure;
611 genlmsg_end(msg, hdr);
612 return 0;
614 nla_put_failure:
615 genlmsg_cancel(msg, hdr);
616 return -EMSGSIZE;
619 static int nfc_genl_dump_devices(struct sk_buff *skb,
620 struct netlink_callback *cb)
622 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
623 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
624 bool first_call = false;
626 if (!iter) {
627 first_call = true;
628 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
629 if (!iter)
630 return -ENOMEM;
631 cb->args[0] = (long) iter;
634 mutex_lock(&nfc_devlist_mutex);
636 cb->seq = nfc_devlist_generation;
638 if (first_call) {
639 nfc_device_iter_init(iter);
640 dev = nfc_device_iter_next(iter);
643 while (dev) {
644 int rc;
646 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
647 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
648 if (rc < 0)
649 break;
651 dev = nfc_device_iter_next(iter);
654 mutex_unlock(&nfc_devlist_mutex);
656 cb->args[1] = (long) dev;
658 return skb->len;
661 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
663 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
665 nfc_device_iter_exit(iter);
666 kfree(iter);
668 return 0;
671 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
672 u8 comm_mode, u8 rf_mode)
674 struct sk_buff *msg;
675 void *hdr;
677 pr_debug("DEP link is up\n");
679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
680 if (!msg)
681 return -ENOMEM;
683 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
684 if (!hdr)
685 goto free_msg;
687 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
688 goto nla_put_failure;
689 if (rf_mode == NFC_RF_INITIATOR &&
690 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
691 goto nla_put_failure;
692 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
693 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
694 goto nla_put_failure;
696 genlmsg_end(msg, hdr);
698 dev->dep_link_up = true;
700 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
702 return 0;
704 nla_put_failure:
705 genlmsg_cancel(msg, hdr);
706 free_msg:
707 nlmsg_free(msg);
708 return -EMSGSIZE;
711 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
713 struct sk_buff *msg;
714 void *hdr;
716 pr_debug("DEP link is down\n");
718 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
719 if (!msg)
720 return -ENOMEM;
722 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
723 NFC_CMD_DEP_LINK_DOWN);
724 if (!hdr)
725 goto free_msg;
727 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
728 goto nla_put_failure;
730 genlmsg_end(msg, hdr);
732 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
734 return 0;
736 nla_put_failure:
737 genlmsg_cancel(msg, hdr);
738 free_msg:
739 nlmsg_free(msg);
740 return -EMSGSIZE;
743 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
745 struct sk_buff *msg;
746 struct nfc_dev *dev;
747 u32 idx;
748 int rc = -ENOBUFS;
750 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
751 return -EINVAL;
753 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
755 dev = nfc_get_device(idx);
756 if (!dev)
757 return -ENODEV;
759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
760 if (!msg) {
761 rc = -ENOMEM;
762 goto out_putdev;
765 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
766 NULL, 0);
767 if (rc < 0)
768 goto out_free;
770 nfc_put_device(dev);
772 return genlmsg_reply(msg, info);
774 out_free:
775 nlmsg_free(msg);
776 out_putdev:
777 nfc_put_device(dev);
778 return rc;
781 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
783 struct nfc_dev *dev;
784 int rc;
785 u32 idx;
787 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
788 return -EINVAL;
790 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
792 dev = nfc_get_device(idx);
793 if (!dev)
794 return -ENODEV;
796 rc = nfc_dev_up(dev);
798 nfc_put_device(dev);
799 return rc;
802 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
804 struct nfc_dev *dev;
805 int rc;
806 u32 idx;
808 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
809 return -EINVAL;
811 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
813 dev = nfc_get_device(idx);
814 if (!dev)
815 return -ENODEV;
817 rc = nfc_dev_down(dev);
819 nfc_put_device(dev);
820 return rc;
823 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
825 struct nfc_dev *dev;
826 int rc;
827 u32 idx;
828 u32 im_protocols = 0, tm_protocols = 0;
830 pr_debug("Poll start\n");
832 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
833 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
834 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
835 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
836 return -EINVAL;
838 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
840 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
841 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
843 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
844 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
845 else if (info->attrs[NFC_ATTR_PROTOCOLS])
846 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
848 dev = nfc_get_device(idx);
849 if (!dev)
850 return -ENODEV;
852 mutex_lock(&dev->genl_data.genl_data_mutex);
854 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
855 if (!rc)
856 dev->genl_data.poll_req_portid = info->snd_portid;
858 mutex_unlock(&dev->genl_data.genl_data_mutex);
860 nfc_put_device(dev);
861 return rc;
864 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
866 struct nfc_dev *dev;
867 int rc;
868 u32 idx;
870 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
871 return -EINVAL;
873 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
875 dev = nfc_get_device(idx);
876 if (!dev)
877 return -ENODEV;
879 device_lock(&dev->dev);
881 if (!dev->polling) {
882 device_unlock(&dev->dev);
883 return -EINVAL;
886 device_unlock(&dev->dev);
888 mutex_lock(&dev->genl_data.genl_data_mutex);
890 if (dev->genl_data.poll_req_portid != info->snd_portid) {
891 rc = -EBUSY;
892 goto out;
895 rc = nfc_stop_poll(dev);
896 dev->genl_data.poll_req_portid = 0;
898 out:
899 mutex_unlock(&dev->genl_data.genl_data_mutex);
900 nfc_put_device(dev);
901 return rc;
904 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
906 struct nfc_dev *dev;
907 u32 device_idx, target_idx, protocol;
908 int rc;
910 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
911 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
912 !info->attrs[NFC_ATTR_PROTOCOLS])
913 return -EINVAL;
915 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
917 dev = nfc_get_device(device_idx);
918 if (!dev)
919 return -ENODEV;
921 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
922 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
924 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
925 rc = nfc_activate_target(dev, target_idx, protocol);
927 nfc_put_device(dev);
928 return rc;
931 static int nfc_genl_deactivate_target(struct sk_buff *skb,
932 struct genl_info *info)
934 struct nfc_dev *dev;
935 u32 device_idx, target_idx;
936 int rc;
938 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
939 return -EINVAL;
941 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
943 dev = nfc_get_device(device_idx);
944 if (!dev)
945 return -ENODEV;
947 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
949 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
951 nfc_put_device(dev);
952 return rc;
955 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
957 struct nfc_dev *dev;
958 int rc, tgt_idx;
959 u32 idx;
960 u8 comm;
962 pr_debug("DEP link up\n");
964 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
965 !info->attrs[NFC_ATTR_COMM_MODE])
966 return -EINVAL;
968 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
969 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
970 tgt_idx = NFC_TARGET_IDX_ANY;
971 else
972 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
974 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
976 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
977 return -EINVAL;
979 dev = nfc_get_device(idx);
980 if (!dev)
981 return -ENODEV;
983 rc = nfc_dep_link_up(dev, tgt_idx, comm);
985 nfc_put_device(dev);
987 return rc;
990 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
992 struct nfc_dev *dev;
993 int rc;
994 u32 idx;
996 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
997 return -EINVAL;
999 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1001 dev = nfc_get_device(idx);
1002 if (!dev)
1003 return -ENODEV;
1005 rc = nfc_dep_link_down(dev);
1007 nfc_put_device(dev);
1008 return rc;
1011 static int nfc_genl_send_params(struct sk_buff *msg,
1012 struct nfc_llcp_local *local,
1013 u32 portid, u32 seq)
1015 void *hdr;
1017 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
1018 NFC_CMD_LLC_GET_PARAMS);
1019 if (!hdr)
1020 return -EMSGSIZE;
1022 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1023 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1024 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1025 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1026 goto nla_put_failure;
1028 genlmsg_end(msg, hdr);
1029 return 0;
1031 nla_put_failure:
1033 genlmsg_cancel(msg, hdr);
1034 return -EMSGSIZE;
1037 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1039 struct nfc_dev *dev;
1040 struct nfc_llcp_local *local;
1041 int rc = 0;
1042 struct sk_buff *msg = NULL;
1043 u32 idx;
1045 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1046 return -EINVAL;
1048 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1050 dev = nfc_get_device(idx);
1051 if (!dev)
1052 return -ENODEV;
1054 device_lock(&dev->dev);
1056 local = nfc_llcp_find_local(dev);
1057 if (!local) {
1058 rc = -ENODEV;
1059 goto exit;
1062 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1063 if (!msg) {
1064 rc = -ENOMEM;
1065 goto exit;
1068 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1070 exit:
1071 device_unlock(&dev->dev);
1073 nfc_put_device(dev);
1075 if (rc < 0) {
1076 if (msg)
1077 nlmsg_free(msg);
1079 return rc;
1082 return genlmsg_reply(msg, info);
1085 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1087 struct nfc_dev *dev;
1088 struct nfc_llcp_local *local;
1089 u8 rw = 0;
1090 u16 miux = 0;
1091 u32 idx;
1092 int rc = 0;
1094 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1095 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1096 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1097 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1098 return -EINVAL;
1100 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1101 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1103 if (rw > LLCP_MAX_RW)
1104 return -EINVAL;
1107 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1108 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1110 if (miux > LLCP_MAX_MIUX)
1111 return -EINVAL;
1114 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1116 dev = nfc_get_device(idx);
1117 if (!dev)
1118 return -ENODEV;
1120 device_lock(&dev->dev);
1122 local = nfc_llcp_find_local(dev);
1123 if (!local) {
1124 nfc_put_device(dev);
1125 rc = -ENODEV;
1126 goto exit;
1129 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1130 if (dev->dep_link_up) {
1131 rc = -EINPROGRESS;
1132 goto exit;
1135 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1138 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1139 local->rw = rw;
1141 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1142 local->miux = cpu_to_be16(miux);
1144 exit:
1145 device_unlock(&dev->dev);
1147 nfc_put_device(dev);
1149 return rc;
1152 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1154 struct nfc_dev *dev;
1155 struct nfc_llcp_local *local;
1156 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1157 u32 idx;
1158 u8 tid;
1159 char *uri;
1160 int rc = 0, rem;
1161 size_t uri_len, tlvs_len;
1162 struct hlist_head sdreq_list;
1163 struct nfc_llcp_sdp_tlv *sdreq;
1165 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1166 !info->attrs[NFC_ATTR_LLC_SDP])
1167 return -EINVAL;
1169 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1171 dev = nfc_get_device(idx);
1172 if (!dev)
1173 return -ENODEV;
1175 device_lock(&dev->dev);
1177 if (dev->dep_link_up == false) {
1178 rc = -ENOLINK;
1179 goto exit;
1182 local = nfc_llcp_find_local(dev);
1183 if (!local) {
1184 nfc_put_device(dev);
1185 rc = -ENODEV;
1186 goto exit;
1189 INIT_HLIST_HEAD(&sdreq_list);
1191 tlvs_len = 0;
1193 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1194 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1195 nfc_sdp_genl_policy, info->extack);
1197 if (rc != 0) {
1198 rc = -EINVAL;
1199 goto exit;
1202 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1203 continue;
1205 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1206 if (uri_len == 0)
1207 continue;
1209 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1210 if (uri == NULL || *uri == 0)
1211 continue;
1213 tid = local->sdreq_next_tid++;
1215 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1216 if (sdreq == NULL) {
1217 rc = -ENOMEM;
1218 goto exit;
1221 tlvs_len += sdreq->tlv_len;
1223 hlist_add_head(&sdreq->node, &sdreq_list);
1226 if (hlist_empty(&sdreq_list)) {
1227 rc = -EINVAL;
1228 goto exit;
1231 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1232 exit:
1233 device_unlock(&dev->dev);
1235 nfc_put_device(dev);
1237 return rc;
1240 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1242 struct nfc_dev *dev;
1243 int rc;
1244 u32 idx;
1245 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1247 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1248 return -EINVAL;
1250 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1252 dev = nfc_get_device(idx);
1253 if (!dev)
1254 return -ENODEV;
1256 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1257 sizeof(firmware_name));
1259 rc = nfc_fw_download(dev, firmware_name);
1261 nfc_put_device(dev);
1262 return rc;
1265 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1266 u32 result)
1268 struct sk_buff *msg;
1269 void *hdr;
1271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1272 if (!msg)
1273 return -ENOMEM;
1275 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1276 NFC_CMD_FW_DOWNLOAD);
1277 if (!hdr)
1278 goto free_msg;
1280 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1281 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1282 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1283 goto nla_put_failure;
1285 genlmsg_end(msg, hdr);
1287 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1289 return 0;
1291 nla_put_failure:
1292 genlmsg_cancel(msg, hdr);
1293 free_msg:
1294 nlmsg_free(msg);
1295 return -EMSGSIZE;
1298 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1300 struct nfc_dev *dev;
1301 int rc;
1302 u32 idx, se_idx;
1304 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1305 !info->attrs[NFC_ATTR_SE_INDEX])
1306 return -EINVAL;
1308 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1309 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1311 dev = nfc_get_device(idx);
1312 if (!dev)
1313 return -ENODEV;
1315 rc = nfc_enable_se(dev, se_idx);
1317 nfc_put_device(dev);
1318 return rc;
1321 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1323 struct nfc_dev *dev;
1324 int rc;
1325 u32 idx, se_idx;
1327 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1328 !info->attrs[NFC_ATTR_SE_INDEX])
1329 return -EINVAL;
1331 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1332 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1334 dev = nfc_get_device(idx);
1335 if (!dev)
1336 return -ENODEV;
1338 rc = nfc_disable_se(dev, se_idx);
1340 nfc_put_device(dev);
1341 return rc;
1344 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1345 u32 portid, u32 seq,
1346 struct netlink_callback *cb,
1347 int flags)
1349 void *hdr;
1350 struct nfc_se *se, *n;
1352 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1353 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1354 NFC_CMD_GET_SE);
1355 if (!hdr)
1356 goto nla_put_failure;
1358 if (cb)
1359 genl_dump_check_consistent(cb, hdr);
1361 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1362 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1363 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1364 goto nla_put_failure;
1366 genlmsg_end(msg, hdr);
1369 return 0;
1371 nla_put_failure:
1372 genlmsg_cancel(msg, hdr);
1373 return -EMSGSIZE;
1376 static int nfc_genl_dump_ses(struct sk_buff *skb,
1377 struct netlink_callback *cb)
1379 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1380 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1381 bool first_call = false;
1383 if (!iter) {
1384 first_call = true;
1385 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1386 if (!iter)
1387 return -ENOMEM;
1388 cb->args[0] = (long) iter;
1391 mutex_lock(&nfc_devlist_mutex);
1393 cb->seq = nfc_devlist_generation;
1395 if (first_call) {
1396 nfc_device_iter_init(iter);
1397 dev = nfc_device_iter_next(iter);
1400 while (dev) {
1401 int rc;
1403 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1404 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1405 if (rc < 0)
1406 break;
1408 dev = nfc_device_iter_next(iter);
1411 mutex_unlock(&nfc_devlist_mutex);
1413 cb->args[1] = (long) dev;
1415 return skb->len;
1418 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1420 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1422 nfc_device_iter_exit(iter);
1423 kfree(iter);
1425 return 0;
1428 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1429 u8 *apdu, size_t apdu_length,
1430 se_io_cb_t cb, void *cb_context)
1432 struct nfc_se *se;
1433 int rc;
1435 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1437 device_lock(&dev->dev);
1439 if (!device_is_registered(&dev->dev)) {
1440 rc = -ENODEV;
1441 goto error;
1444 if (!dev->dev_up) {
1445 rc = -ENODEV;
1446 goto error;
1449 if (!dev->ops->se_io) {
1450 rc = -EOPNOTSUPP;
1451 goto error;
1454 se = nfc_find_se(dev, se_idx);
1455 if (!se) {
1456 rc = -EINVAL;
1457 goto error;
1460 if (se->state != NFC_SE_ENABLED) {
1461 rc = -ENODEV;
1462 goto error;
1465 rc = dev->ops->se_io(dev, se_idx, apdu,
1466 apdu_length, cb, cb_context);
1468 error:
1469 device_unlock(&dev->dev);
1470 return rc;
1473 struct se_io_ctx {
1474 u32 dev_idx;
1475 u32 se_idx;
1478 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1480 struct se_io_ctx *ctx = context;
1481 struct sk_buff *msg;
1482 void *hdr;
1484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1485 if (!msg) {
1486 kfree(ctx);
1487 return;
1490 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1491 NFC_CMD_SE_IO);
1492 if (!hdr)
1493 goto free_msg;
1495 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1496 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1497 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1498 goto nla_put_failure;
1500 genlmsg_end(msg, hdr);
1502 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1504 kfree(ctx);
1506 return;
1508 nla_put_failure:
1509 genlmsg_cancel(msg, hdr);
1510 free_msg:
1511 nlmsg_free(msg);
1512 kfree(ctx);
1514 return;
1517 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1519 struct nfc_dev *dev;
1520 struct se_io_ctx *ctx;
1521 u32 dev_idx, se_idx;
1522 u8 *apdu;
1523 size_t apdu_len;
1525 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1526 !info->attrs[NFC_ATTR_SE_INDEX] ||
1527 !info->attrs[NFC_ATTR_SE_APDU])
1528 return -EINVAL;
1530 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1531 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1533 dev = nfc_get_device(dev_idx);
1534 if (!dev)
1535 return -ENODEV;
1537 if (!dev->ops || !dev->ops->se_io)
1538 return -ENOTSUPP;
1540 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1541 if (apdu_len == 0)
1542 return -EINVAL;
1544 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1545 if (!apdu)
1546 return -EINVAL;
1548 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1549 if (!ctx)
1550 return -ENOMEM;
1552 ctx->dev_idx = dev_idx;
1553 ctx->se_idx = se_idx;
1555 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1558 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1559 struct genl_info *info)
1561 struct nfc_dev *dev;
1562 struct nfc_vendor_cmd *cmd;
1563 u32 dev_idx, vid, subcmd;
1564 u8 *data;
1565 size_t data_len;
1566 int i, err;
1568 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1569 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1570 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1571 return -EINVAL;
1573 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1574 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1575 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1577 dev = nfc_get_device(dev_idx);
1578 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1579 return -ENODEV;
1581 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1582 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1583 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1584 if (data_len == 0)
1585 return -EINVAL;
1586 } else {
1587 data = NULL;
1588 data_len = 0;
1591 for (i = 0; i < dev->n_vendor_cmds; i++) {
1592 cmd = &dev->vendor_cmds[i];
1594 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1595 continue;
1597 dev->cur_cmd_info = info;
1598 err = cmd->doit(dev, data, data_len);
1599 dev->cur_cmd_info = NULL;
1600 return err;
1603 return -EOPNOTSUPP;
1606 /* message building helper */
1607 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1608 int flags, u8 cmd)
1610 /* since there is no private header just add the generic one */
1611 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1614 static struct sk_buff *
1615 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1616 u32 portid, u32 seq,
1617 enum nfc_attrs attr,
1618 u32 oui, u32 subcmd, gfp_t gfp)
1620 struct sk_buff *skb;
1621 void *hdr;
1623 skb = nlmsg_new(approxlen + 100, gfp);
1624 if (!skb)
1625 return NULL;
1627 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1628 if (!hdr) {
1629 kfree_skb(skb);
1630 return NULL;
1633 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1634 goto nla_put_failure;
1635 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1636 goto nla_put_failure;
1637 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1638 goto nla_put_failure;
1640 ((void **)skb->cb)[0] = dev;
1641 ((void **)skb->cb)[1] = hdr;
1643 return skb;
1645 nla_put_failure:
1646 kfree_skb(skb);
1647 return NULL;
1650 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1651 enum nfc_attrs attr,
1652 u32 oui, u32 subcmd,
1653 int approxlen)
1655 if (WARN_ON(!dev->cur_cmd_info))
1656 return NULL;
1658 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1659 dev->cur_cmd_info->snd_portid,
1660 dev->cur_cmd_info->snd_seq, attr,
1661 oui, subcmd, GFP_KERNEL);
1663 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1665 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1667 struct nfc_dev *dev = ((void **)skb->cb)[0];
1668 void *hdr = ((void **)skb->cb)[1];
1670 /* clear CB data for netlink core to own from now on */
1671 memset(skb->cb, 0, sizeof(skb->cb));
1673 if (WARN_ON(!dev->cur_cmd_info)) {
1674 kfree_skb(skb);
1675 return -EINVAL;
1678 genlmsg_end(skb, hdr);
1679 return genlmsg_reply(skb, dev->cur_cmd_info);
1681 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1683 static const struct genl_ops nfc_genl_ops[] = {
1685 .cmd = NFC_CMD_GET_DEVICE,
1686 .doit = nfc_genl_get_device,
1687 .dumpit = nfc_genl_dump_devices,
1688 .done = nfc_genl_dump_devices_done,
1689 .policy = nfc_genl_policy,
1692 .cmd = NFC_CMD_DEV_UP,
1693 .doit = nfc_genl_dev_up,
1694 .policy = nfc_genl_policy,
1697 .cmd = NFC_CMD_DEV_DOWN,
1698 .doit = nfc_genl_dev_down,
1699 .policy = nfc_genl_policy,
1702 .cmd = NFC_CMD_START_POLL,
1703 .doit = nfc_genl_start_poll,
1704 .policy = nfc_genl_policy,
1707 .cmd = NFC_CMD_STOP_POLL,
1708 .doit = nfc_genl_stop_poll,
1709 .policy = nfc_genl_policy,
1712 .cmd = NFC_CMD_DEP_LINK_UP,
1713 .doit = nfc_genl_dep_link_up,
1714 .policy = nfc_genl_policy,
1717 .cmd = NFC_CMD_DEP_LINK_DOWN,
1718 .doit = nfc_genl_dep_link_down,
1719 .policy = nfc_genl_policy,
1722 .cmd = NFC_CMD_GET_TARGET,
1723 .dumpit = nfc_genl_dump_targets,
1724 .done = nfc_genl_dump_targets_done,
1725 .policy = nfc_genl_policy,
1728 .cmd = NFC_CMD_LLC_GET_PARAMS,
1729 .doit = nfc_genl_llc_get_params,
1730 .policy = nfc_genl_policy,
1733 .cmd = NFC_CMD_LLC_SET_PARAMS,
1734 .doit = nfc_genl_llc_set_params,
1735 .policy = nfc_genl_policy,
1738 .cmd = NFC_CMD_LLC_SDREQ,
1739 .doit = nfc_genl_llc_sdreq,
1740 .policy = nfc_genl_policy,
1743 .cmd = NFC_CMD_FW_DOWNLOAD,
1744 .doit = nfc_genl_fw_download,
1745 .policy = nfc_genl_policy,
1748 .cmd = NFC_CMD_ENABLE_SE,
1749 .doit = nfc_genl_enable_se,
1750 .policy = nfc_genl_policy,
1753 .cmd = NFC_CMD_DISABLE_SE,
1754 .doit = nfc_genl_disable_se,
1755 .policy = nfc_genl_policy,
1758 .cmd = NFC_CMD_GET_SE,
1759 .dumpit = nfc_genl_dump_ses,
1760 .done = nfc_genl_dump_ses_done,
1761 .policy = nfc_genl_policy,
1764 .cmd = NFC_CMD_SE_IO,
1765 .doit = nfc_genl_se_io,
1766 .policy = nfc_genl_policy,
1769 .cmd = NFC_CMD_ACTIVATE_TARGET,
1770 .doit = nfc_genl_activate_target,
1771 .policy = nfc_genl_policy,
1774 .cmd = NFC_CMD_VENDOR,
1775 .doit = nfc_genl_vendor_cmd,
1776 .policy = nfc_genl_policy,
1779 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1780 .doit = nfc_genl_deactivate_target,
1781 .policy = nfc_genl_policy,
1785 static struct genl_family nfc_genl_family __ro_after_init = {
1786 .hdrsize = 0,
1787 .name = NFC_GENL_NAME,
1788 .version = NFC_GENL_VERSION,
1789 .maxattr = NFC_ATTR_MAX,
1790 .module = THIS_MODULE,
1791 .ops = nfc_genl_ops,
1792 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1793 .mcgrps = nfc_genl_mcgrps,
1794 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1798 struct urelease_work {
1799 struct work_struct w;
1800 u32 portid;
1803 static void nfc_urelease_event_work(struct work_struct *work)
1805 struct urelease_work *w = container_of(work, struct urelease_work, w);
1806 struct class_dev_iter iter;
1807 struct nfc_dev *dev;
1809 pr_debug("portid %d\n", w->portid);
1811 mutex_lock(&nfc_devlist_mutex);
1813 nfc_device_iter_init(&iter);
1814 dev = nfc_device_iter_next(&iter);
1816 while (dev) {
1817 mutex_lock(&dev->genl_data.genl_data_mutex);
1819 if (dev->genl_data.poll_req_portid == w->portid) {
1820 nfc_stop_poll(dev);
1821 dev->genl_data.poll_req_portid = 0;
1824 mutex_unlock(&dev->genl_data.genl_data_mutex);
1826 dev = nfc_device_iter_next(&iter);
1829 nfc_device_iter_exit(&iter);
1831 mutex_unlock(&nfc_devlist_mutex);
1833 kfree(w);
1836 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1837 unsigned long event, void *ptr)
1839 struct netlink_notify *n = ptr;
1840 struct urelease_work *w;
1842 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1843 goto out;
1845 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1847 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1848 if (w) {
1849 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1850 w->portid = n->portid;
1851 schedule_work((struct work_struct *) w);
1854 out:
1855 return NOTIFY_DONE;
1858 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1860 genl_data->poll_req_portid = 0;
1861 mutex_init(&genl_data->genl_data_mutex);
1864 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1866 mutex_destroy(&genl_data->genl_data_mutex);
1869 static struct notifier_block nl_notifier = {
1870 .notifier_call = nfc_genl_rcv_nl_event,
1874 * nfc_genl_init() - Initialize netlink interface
1876 * This initialization function registers the nfc netlink family.
1878 int __init nfc_genl_init(void)
1880 int rc;
1882 rc = genl_register_family(&nfc_genl_family);
1883 if (rc)
1884 return rc;
1886 netlink_register_notifier(&nl_notifier);
1888 return 0;
1892 * nfc_genl_exit() - Deinitialize netlink interface
1894 * This exit function unregisters the nfc netlink family.
1896 void nfc_genl_exit(void)
1898 netlink_unregister_notifier(&nl_notifier);
1899 genl_unregister_family(&nfc_genl_family);