ARM: dma-api: fix max_pfn off-by-one error in __dma_supported()
[linux/fpc-iii.git] / net / nfc / netlink.c
blobeee0dddb7749e0c9dec121ea29a047226b9e4473
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Authors:
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
9 * Vendor commands implementation based on net/wireless/nl80211.c
10 * which is:
12 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
13 * Copyright 2013-2014 Intel Mobile Communications GmbH
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
18 #include <net/genetlink.h>
19 #include <linux/nfc.h>
20 #include <linux/slab.h>
22 #include "nfc.h"
23 #include "llcp.h"
25 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
26 { .name = NFC_GENL_MCAST_EVENT_NAME, },
29 static struct genl_family nfc_genl_family;
30 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
31 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
32 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
33 .len = NFC_DEVICE_NAME_MAXSIZE },
34 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
35 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
36 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
37 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
38 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
39 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
40 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
41 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
42 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
43 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
44 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
45 .len = NFC_FIRMWARE_NAME_MAXSIZE },
46 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
47 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
51 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
52 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
53 .len = U8_MAX - 4 },
54 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
57 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
58 struct netlink_callback *cb, int flags)
60 void *hdr;
62 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
63 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
64 if (!hdr)
65 return -EMSGSIZE;
67 genl_dump_check_consistent(cb, hdr);
69 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
70 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
71 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
72 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
73 goto nla_put_failure;
74 if (target->nfcid1_len > 0 &&
75 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
76 target->nfcid1))
77 goto nla_put_failure;
78 if (target->sensb_res_len > 0 &&
79 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
80 target->sensb_res))
81 goto nla_put_failure;
82 if (target->sensf_res_len > 0 &&
83 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
84 target->sensf_res))
85 goto nla_put_failure;
87 if (target->is_iso15693) {
88 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
89 target->iso15693_dsfid) ||
90 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
91 sizeof(target->iso15693_uid), target->iso15693_uid))
92 goto nla_put_failure;
95 genlmsg_end(msg, hdr);
96 return 0;
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 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
106 struct nfc_dev *dev;
107 u32 idx;
109 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
110 return ERR_PTR(-EINVAL);
112 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
114 dev = nfc_get_device(idx);
115 if (!dev)
116 return ERR_PTR(-ENODEV);
118 return dev;
121 static int nfc_genl_dump_targets(struct sk_buff *skb,
122 struct netlink_callback *cb)
124 int i = cb->args[0];
125 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
126 int rc;
128 if (!dev) {
129 dev = __get_device_from_cb(cb);
130 if (IS_ERR(dev))
131 return PTR_ERR(dev);
133 cb->args[1] = (long) dev;
136 device_lock(&dev->dev);
138 cb->seq = dev->targets_generation;
140 while (i < dev->n_targets) {
141 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
142 NLM_F_MULTI);
143 if (rc < 0)
144 break;
146 i++;
149 device_unlock(&dev->dev);
151 cb->args[0] = i;
153 return skb->len;
156 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
158 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
160 if (dev)
161 nfc_put_device(dev);
163 return 0;
166 int nfc_genl_targets_found(struct nfc_dev *dev)
168 struct sk_buff *msg;
169 void *hdr;
171 dev->genl_data.poll_req_portid = 0;
173 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
174 if (!msg)
175 return -ENOMEM;
177 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
178 NFC_EVENT_TARGETS_FOUND);
179 if (!hdr)
180 goto free_msg;
182 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
183 goto nla_put_failure;
185 genlmsg_end(msg, hdr);
187 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
189 nla_put_failure:
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(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
217 return 0;
219 nla_put_failure:
220 free_msg:
221 nlmsg_free(msg);
222 return -EMSGSIZE;
225 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
227 struct sk_buff *msg;
228 void *hdr;
230 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
231 if (!msg)
232 return -ENOMEM;
234 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
235 NFC_EVENT_TM_ACTIVATED);
236 if (!hdr)
237 goto free_msg;
239 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
240 goto nla_put_failure;
241 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
242 goto nla_put_failure;
244 genlmsg_end(msg, hdr);
246 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
248 return 0;
250 nla_put_failure:
251 free_msg:
252 nlmsg_free(msg);
253 return -EMSGSIZE;
256 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
258 struct sk_buff *msg;
259 void *hdr;
261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
262 if (!msg)
263 return -ENOMEM;
265 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
266 NFC_EVENT_TM_DEACTIVATED);
267 if (!hdr)
268 goto free_msg;
270 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
271 goto nla_put_failure;
273 genlmsg_end(msg, hdr);
275 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
277 return 0;
279 nla_put_failure:
280 free_msg:
281 nlmsg_free(msg);
282 return -EMSGSIZE;
285 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
287 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
288 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
289 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
290 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
291 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
292 return -1;
293 return 0;
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 (nfc_genl_setup_device_added(dev, msg))
311 goto nla_put_failure;
313 genlmsg_end(msg, hdr);
315 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
317 return 0;
319 nla_put_failure:
320 free_msg:
321 nlmsg_free(msg);
322 return -EMSGSIZE;
325 int nfc_genl_device_removed(struct nfc_dev *dev)
327 struct sk_buff *msg;
328 void *hdr;
330 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
331 if (!msg)
332 return -ENOMEM;
334 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
335 NFC_EVENT_DEVICE_REMOVED);
336 if (!hdr)
337 goto free_msg;
339 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
340 goto nla_put_failure;
342 genlmsg_end(msg, hdr);
344 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
346 return 0;
348 nla_put_failure:
349 free_msg:
350 nlmsg_free(msg);
351 return -EMSGSIZE;
354 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
356 struct sk_buff *msg;
357 struct nlattr *sdp_attr, *uri_attr;
358 struct nfc_llcp_sdp_tlv *sdres;
359 struct hlist_node *n;
360 void *hdr;
361 int rc = -EMSGSIZE;
362 int i;
364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
365 if (!msg)
366 return -ENOMEM;
368 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
369 NFC_EVENT_LLC_SDRES);
370 if (!hdr)
371 goto free_msg;
373 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
374 goto nla_put_failure;
376 sdp_attr = nla_nest_start_noflag(msg, NFC_ATTR_LLC_SDP);
377 if (sdp_attr == NULL) {
378 rc = -ENOMEM;
379 goto nla_put_failure;
382 i = 1;
383 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
384 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
386 uri_attr = nla_nest_start_noflag(msg, i++);
387 if (uri_attr == NULL) {
388 rc = -ENOMEM;
389 goto nla_put_failure;
392 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
393 goto nla_put_failure;
395 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
396 goto nla_put_failure;
398 nla_nest_end(msg, uri_attr);
400 hlist_del(&sdres->node);
402 nfc_llcp_free_sdp_tlv(sdres);
405 nla_nest_end(msg, sdp_attr);
407 genlmsg_end(msg, hdr);
409 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
411 nla_put_failure:
412 free_msg:
413 nlmsg_free(msg);
415 nfc_llcp_free_sdp_tlv_list(sdres_list);
417 return rc;
420 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
422 struct sk_buff *msg;
423 void *hdr;
425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
426 if (!msg)
427 return -ENOMEM;
429 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
430 NFC_EVENT_SE_ADDED);
431 if (!hdr)
432 goto free_msg;
434 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
435 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
436 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
437 goto nla_put_failure;
439 genlmsg_end(msg, hdr);
441 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
443 return 0;
445 nla_put_failure:
446 free_msg:
447 nlmsg_free(msg);
448 return -EMSGSIZE;
451 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
453 struct sk_buff *msg;
454 void *hdr;
456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
457 if (!msg)
458 return -ENOMEM;
460 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
461 NFC_EVENT_SE_REMOVED);
462 if (!hdr)
463 goto free_msg;
465 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
466 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
467 goto nla_put_failure;
469 genlmsg_end(msg, hdr);
471 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
473 return 0;
475 nla_put_failure:
476 free_msg:
477 nlmsg_free(msg);
478 return -EMSGSIZE;
481 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
482 struct nfc_evt_transaction *evt_transaction)
484 struct nfc_se *se;
485 struct sk_buff *msg;
486 void *hdr;
488 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
489 if (!msg)
490 return -ENOMEM;
492 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
493 NFC_EVENT_SE_TRANSACTION);
494 if (!hdr)
495 goto free_msg;
497 se = nfc_find_se(dev, se_idx);
498 if (!se)
499 goto free_msg;
501 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
502 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
503 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
504 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
505 evt_transaction->aid) ||
506 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
507 evt_transaction->params))
508 goto nla_put_failure;
510 /* evt_transaction is no more used */
511 devm_kfree(&dev->dev, evt_transaction);
513 genlmsg_end(msg, hdr);
515 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
517 return 0;
519 nla_put_failure:
520 free_msg:
521 /* evt_transaction is no more used */
522 devm_kfree(&dev->dev, evt_transaction);
523 nlmsg_free(msg);
524 return -EMSGSIZE;
527 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
529 struct nfc_se *se;
530 struct sk_buff *msg;
531 void *hdr;
533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
534 if (!msg)
535 return -ENOMEM;
537 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
538 NFC_EVENT_SE_CONNECTIVITY);
539 if (!hdr)
540 goto free_msg;
542 se = nfc_find_se(dev, se_idx);
543 if (!se)
544 goto free_msg;
546 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
547 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
548 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
549 goto nla_put_failure;
551 genlmsg_end(msg, hdr);
553 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
555 return 0;
557 nla_put_failure:
558 free_msg:
559 nlmsg_free(msg);
560 return -EMSGSIZE;
563 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
564 u32 portid, u32 seq,
565 struct netlink_callback *cb,
566 int flags)
568 void *hdr;
570 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
571 NFC_CMD_GET_DEVICE);
572 if (!hdr)
573 return -EMSGSIZE;
575 if (cb)
576 genl_dump_check_consistent(cb, hdr);
578 if (nfc_genl_setup_device_added(dev, msg))
579 goto nla_put_failure;
581 genlmsg_end(msg, hdr);
582 return 0;
584 nla_put_failure:
585 genlmsg_cancel(msg, hdr);
586 return -EMSGSIZE;
589 static int nfc_genl_dump_devices(struct sk_buff *skb,
590 struct netlink_callback *cb)
592 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
593 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
594 bool first_call = false;
596 if (!iter) {
597 first_call = true;
598 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
599 if (!iter)
600 return -ENOMEM;
601 cb->args[0] = (long) iter;
604 mutex_lock(&nfc_devlist_mutex);
606 cb->seq = nfc_devlist_generation;
608 if (first_call) {
609 nfc_device_iter_init(iter);
610 dev = nfc_device_iter_next(iter);
613 while (dev) {
614 int rc;
616 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
617 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
618 if (rc < 0)
619 break;
621 dev = nfc_device_iter_next(iter);
624 mutex_unlock(&nfc_devlist_mutex);
626 cb->args[1] = (long) dev;
628 return skb->len;
631 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
633 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
635 nfc_device_iter_exit(iter);
636 kfree(iter);
638 return 0;
641 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
642 u8 comm_mode, u8 rf_mode)
644 struct sk_buff *msg;
645 void *hdr;
647 pr_debug("DEP link is up\n");
649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
650 if (!msg)
651 return -ENOMEM;
653 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
654 if (!hdr)
655 goto free_msg;
657 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
658 goto nla_put_failure;
659 if (rf_mode == NFC_RF_INITIATOR &&
660 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
661 goto nla_put_failure;
662 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
663 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
664 goto nla_put_failure;
666 genlmsg_end(msg, hdr);
668 dev->dep_link_up = true;
670 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
672 return 0;
674 nla_put_failure:
675 free_msg:
676 nlmsg_free(msg);
677 return -EMSGSIZE;
680 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
682 struct sk_buff *msg;
683 void *hdr;
685 pr_debug("DEP link is down\n");
687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
688 if (!msg)
689 return -ENOMEM;
691 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
692 NFC_CMD_DEP_LINK_DOWN);
693 if (!hdr)
694 goto free_msg;
696 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
697 goto nla_put_failure;
699 genlmsg_end(msg, hdr);
701 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
703 return 0;
705 nla_put_failure:
706 free_msg:
707 nlmsg_free(msg);
708 return -EMSGSIZE;
711 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
713 struct sk_buff *msg;
714 struct nfc_dev *dev;
715 u32 idx;
716 int rc = -ENOBUFS;
718 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
719 return -EINVAL;
721 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
723 dev = nfc_get_device(idx);
724 if (!dev)
725 return -ENODEV;
727 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
728 if (!msg) {
729 rc = -ENOMEM;
730 goto out_putdev;
733 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
734 NULL, 0);
735 if (rc < 0)
736 goto out_free;
738 nfc_put_device(dev);
740 return genlmsg_reply(msg, info);
742 out_free:
743 nlmsg_free(msg);
744 out_putdev:
745 nfc_put_device(dev);
746 return rc;
749 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
751 struct nfc_dev *dev;
752 int rc;
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 rc = nfc_dev_up(dev);
766 nfc_put_device(dev);
767 return rc;
770 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
772 struct nfc_dev *dev;
773 int rc;
774 u32 idx;
776 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
777 return -EINVAL;
779 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
781 dev = nfc_get_device(idx);
782 if (!dev)
783 return -ENODEV;
785 rc = nfc_dev_down(dev);
787 nfc_put_device(dev);
788 return rc;
791 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
793 struct nfc_dev *dev;
794 int rc;
795 u32 idx;
796 u32 im_protocols = 0, tm_protocols = 0;
798 pr_debug("Poll start\n");
800 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
801 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
802 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
803 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
804 return -EINVAL;
806 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
808 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
809 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
811 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
812 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
813 else if (info->attrs[NFC_ATTR_PROTOCOLS])
814 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
816 dev = nfc_get_device(idx);
817 if (!dev)
818 return -ENODEV;
820 mutex_lock(&dev->genl_data.genl_data_mutex);
822 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
823 if (!rc)
824 dev->genl_data.poll_req_portid = info->snd_portid;
826 mutex_unlock(&dev->genl_data.genl_data_mutex);
828 nfc_put_device(dev);
829 return rc;
832 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
834 struct nfc_dev *dev;
835 int rc;
836 u32 idx;
838 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
839 return -EINVAL;
841 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
843 dev = nfc_get_device(idx);
844 if (!dev)
845 return -ENODEV;
847 device_lock(&dev->dev);
849 if (!dev->polling) {
850 device_unlock(&dev->dev);
851 return -EINVAL;
854 device_unlock(&dev->dev);
856 mutex_lock(&dev->genl_data.genl_data_mutex);
858 if (dev->genl_data.poll_req_portid != info->snd_portid) {
859 rc = -EBUSY;
860 goto out;
863 rc = nfc_stop_poll(dev);
864 dev->genl_data.poll_req_portid = 0;
866 out:
867 mutex_unlock(&dev->genl_data.genl_data_mutex);
868 nfc_put_device(dev);
869 return rc;
872 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
874 struct nfc_dev *dev;
875 u32 device_idx, target_idx, protocol;
876 int rc;
878 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
879 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
880 !info->attrs[NFC_ATTR_PROTOCOLS])
881 return -EINVAL;
883 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
885 dev = nfc_get_device(device_idx);
886 if (!dev)
887 return -ENODEV;
889 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
890 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
892 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
893 rc = nfc_activate_target(dev, target_idx, protocol);
895 nfc_put_device(dev);
896 return rc;
899 static int nfc_genl_deactivate_target(struct sk_buff *skb,
900 struct genl_info *info)
902 struct nfc_dev *dev;
903 u32 device_idx, target_idx;
904 int rc;
906 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
907 !info->attrs[NFC_ATTR_TARGET_INDEX])
908 return -EINVAL;
910 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
912 dev = nfc_get_device(device_idx);
913 if (!dev)
914 return -ENODEV;
916 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
918 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
920 nfc_put_device(dev);
921 return rc;
924 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
926 struct nfc_dev *dev;
927 int rc, tgt_idx;
928 u32 idx;
929 u8 comm;
931 pr_debug("DEP link up\n");
933 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
934 !info->attrs[NFC_ATTR_COMM_MODE])
935 return -EINVAL;
937 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
938 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
939 tgt_idx = NFC_TARGET_IDX_ANY;
940 else
941 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
943 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
945 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
946 return -EINVAL;
948 dev = nfc_get_device(idx);
949 if (!dev)
950 return -ENODEV;
952 rc = nfc_dep_link_up(dev, tgt_idx, comm);
954 nfc_put_device(dev);
956 return rc;
959 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
961 struct nfc_dev *dev;
962 int rc;
963 u32 idx;
965 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
966 !info->attrs[NFC_ATTR_TARGET_INDEX])
967 return -EINVAL;
969 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
971 dev = nfc_get_device(idx);
972 if (!dev)
973 return -ENODEV;
975 rc = nfc_dep_link_down(dev);
977 nfc_put_device(dev);
978 return rc;
981 static int nfc_genl_send_params(struct sk_buff *msg,
982 struct nfc_llcp_local *local,
983 u32 portid, u32 seq)
985 void *hdr;
987 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
988 NFC_CMD_LLC_GET_PARAMS);
989 if (!hdr)
990 return -EMSGSIZE;
992 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
993 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
994 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
995 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
996 goto nla_put_failure;
998 genlmsg_end(msg, hdr);
999 return 0;
1001 nla_put_failure:
1002 genlmsg_cancel(msg, hdr);
1003 return -EMSGSIZE;
1006 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1008 struct nfc_dev *dev;
1009 struct nfc_llcp_local *local;
1010 int rc = 0;
1011 struct sk_buff *msg = NULL;
1012 u32 idx;
1014 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1015 !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1016 return -EINVAL;
1018 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1020 dev = nfc_get_device(idx);
1021 if (!dev)
1022 return -ENODEV;
1024 device_lock(&dev->dev);
1026 local = nfc_llcp_find_local(dev);
1027 if (!local) {
1028 rc = -ENODEV;
1029 goto exit;
1032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1033 if (!msg) {
1034 rc = -ENOMEM;
1035 goto exit;
1038 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1040 exit:
1041 device_unlock(&dev->dev);
1043 nfc_put_device(dev);
1045 if (rc < 0) {
1046 if (msg)
1047 nlmsg_free(msg);
1049 return rc;
1052 return genlmsg_reply(msg, info);
1055 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1057 struct nfc_dev *dev;
1058 struct nfc_llcp_local *local;
1059 u8 rw = 0;
1060 u16 miux = 0;
1061 u32 idx;
1062 int rc = 0;
1064 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1065 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1066 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1067 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1068 return -EINVAL;
1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1071 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1073 if (rw > LLCP_MAX_RW)
1074 return -EINVAL;
1077 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1078 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1080 if (miux > LLCP_MAX_MIUX)
1081 return -EINVAL;
1084 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1086 dev = nfc_get_device(idx);
1087 if (!dev)
1088 return -ENODEV;
1090 device_lock(&dev->dev);
1092 local = nfc_llcp_find_local(dev);
1093 if (!local) {
1094 rc = -ENODEV;
1095 goto exit;
1098 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1099 if (dev->dep_link_up) {
1100 rc = -EINPROGRESS;
1101 goto exit;
1104 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1107 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1108 local->rw = rw;
1110 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1111 local->miux = cpu_to_be16(miux);
1113 exit:
1114 device_unlock(&dev->dev);
1116 nfc_put_device(dev);
1118 return rc;
1121 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1123 struct nfc_dev *dev;
1124 struct nfc_llcp_local *local;
1125 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1126 u32 idx;
1127 u8 tid;
1128 char *uri;
1129 int rc = 0, rem;
1130 size_t uri_len, tlvs_len;
1131 struct hlist_head sdreq_list;
1132 struct nfc_llcp_sdp_tlv *sdreq;
1134 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1135 !info->attrs[NFC_ATTR_LLC_SDP])
1136 return -EINVAL;
1138 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1140 dev = nfc_get_device(idx);
1141 if (!dev)
1142 return -ENODEV;
1144 device_lock(&dev->dev);
1146 if (dev->dep_link_up == false) {
1147 rc = -ENOLINK;
1148 goto exit;
1151 local = nfc_llcp_find_local(dev);
1152 if (!local) {
1153 rc = -ENODEV;
1154 goto exit;
1157 INIT_HLIST_HEAD(&sdreq_list);
1159 tlvs_len = 0;
1161 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1162 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX,
1163 attr, nfc_sdp_genl_policy,
1164 info->extack);
1166 if (rc != 0) {
1167 rc = -EINVAL;
1168 goto exit;
1171 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1172 continue;
1174 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1175 if (uri_len == 0)
1176 continue;
1178 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1179 if (uri == NULL || *uri == 0)
1180 continue;
1182 tid = local->sdreq_next_tid++;
1184 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1185 if (sdreq == NULL) {
1186 rc = -ENOMEM;
1187 goto exit;
1190 tlvs_len += sdreq->tlv_len;
1192 hlist_add_head(&sdreq->node, &sdreq_list);
1195 if (hlist_empty(&sdreq_list)) {
1196 rc = -EINVAL;
1197 goto exit;
1200 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1201 exit:
1202 device_unlock(&dev->dev);
1204 nfc_put_device(dev);
1206 return rc;
1209 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1211 struct nfc_dev *dev;
1212 int rc;
1213 u32 idx;
1214 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1216 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1217 return -EINVAL;
1219 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1221 dev = nfc_get_device(idx);
1222 if (!dev)
1223 return -ENODEV;
1225 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1226 sizeof(firmware_name));
1228 rc = nfc_fw_download(dev, firmware_name);
1230 nfc_put_device(dev);
1231 return rc;
1234 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1235 u32 result)
1237 struct sk_buff *msg;
1238 void *hdr;
1240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1241 if (!msg)
1242 return -ENOMEM;
1244 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1245 NFC_CMD_FW_DOWNLOAD);
1246 if (!hdr)
1247 goto free_msg;
1249 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1250 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1251 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1252 goto nla_put_failure;
1254 genlmsg_end(msg, hdr);
1256 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1258 return 0;
1260 nla_put_failure:
1261 free_msg:
1262 nlmsg_free(msg);
1263 return -EMSGSIZE;
1266 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1268 struct nfc_dev *dev;
1269 int rc;
1270 u32 idx, se_idx;
1272 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1273 !info->attrs[NFC_ATTR_SE_INDEX])
1274 return -EINVAL;
1276 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1277 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1279 dev = nfc_get_device(idx);
1280 if (!dev)
1281 return -ENODEV;
1283 rc = nfc_enable_se(dev, se_idx);
1285 nfc_put_device(dev);
1286 return rc;
1289 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1291 struct nfc_dev *dev;
1292 int rc;
1293 u32 idx, se_idx;
1295 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1296 !info->attrs[NFC_ATTR_SE_INDEX])
1297 return -EINVAL;
1299 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1300 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1302 dev = nfc_get_device(idx);
1303 if (!dev)
1304 return -ENODEV;
1306 rc = nfc_disable_se(dev, se_idx);
1308 nfc_put_device(dev);
1309 return rc;
1312 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1313 u32 portid, u32 seq,
1314 struct netlink_callback *cb,
1315 int flags)
1317 void *hdr;
1318 struct nfc_se *se, *n;
1320 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1321 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1322 NFC_CMD_GET_SE);
1323 if (!hdr)
1324 goto nla_put_failure;
1326 if (cb)
1327 genl_dump_check_consistent(cb, hdr);
1329 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1330 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1331 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1332 goto nla_put_failure;
1334 genlmsg_end(msg, hdr);
1337 return 0;
1339 nla_put_failure:
1340 genlmsg_cancel(msg, hdr);
1341 return -EMSGSIZE;
1344 static int nfc_genl_dump_ses(struct sk_buff *skb,
1345 struct netlink_callback *cb)
1347 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1348 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1349 bool first_call = false;
1351 if (!iter) {
1352 first_call = true;
1353 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1354 if (!iter)
1355 return -ENOMEM;
1356 cb->args[0] = (long) iter;
1359 mutex_lock(&nfc_devlist_mutex);
1361 cb->seq = nfc_devlist_generation;
1363 if (first_call) {
1364 nfc_device_iter_init(iter);
1365 dev = nfc_device_iter_next(iter);
1368 while (dev) {
1369 int rc;
1371 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1372 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1373 if (rc < 0)
1374 break;
1376 dev = nfc_device_iter_next(iter);
1379 mutex_unlock(&nfc_devlist_mutex);
1381 cb->args[1] = (long) dev;
1383 return skb->len;
1386 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1388 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1390 nfc_device_iter_exit(iter);
1391 kfree(iter);
1393 return 0;
1396 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1397 u8 *apdu, size_t apdu_length,
1398 se_io_cb_t cb, void *cb_context)
1400 struct nfc_se *se;
1401 int rc;
1403 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1405 device_lock(&dev->dev);
1407 if (!device_is_registered(&dev->dev)) {
1408 rc = -ENODEV;
1409 goto error;
1412 if (!dev->dev_up) {
1413 rc = -ENODEV;
1414 goto error;
1417 if (!dev->ops->se_io) {
1418 rc = -EOPNOTSUPP;
1419 goto error;
1422 se = nfc_find_se(dev, se_idx);
1423 if (!se) {
1424 rc = -EINVAL;
1425 goto error;
1428 if (se->state != NFC_SE_ENABLED) {
1429 rc = -ENODEV;
1430 goto error;
1433 rc = dev->ops->se_io(dev, se_idx, apdu,
1434 apdu_length, cb, cb_context);
1436 error:
1437 device_unlock(&dev->dev);
1438 return rc;
1441 struct se_io_ctx {
1442 u32 dev_idx;
1443 u32 se_idx;
1446 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1448 struct se_io_ctx *ctx = context;
1449 struct sk_buff *msg;
1450 void *hdr;
1452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1453 if (!msg) {
1454 kfree(ctx);
1455 return;
1458 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1459 NFC_CMD_SE_IO);
1460 if (!hdr)
1461 goto free_msg;
1463 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1464 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1465 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1466 goto nla_put_failure;
1468 genlmsg_end(msg, hdr);
1470 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1472 kfree(ctx);
1474 return;
1476 nla_put_failure:
1477 free_msg:
1478 nlmsg_free(msg);
1479 kfree(ctx);
1481 return;
1484 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1486 struct nfc_dev *dev;
1487 struct se_io_ctx *ctx;
1488 u32 dev_idx, se_idx;
1489 u8 *apdu;
1490 size_t apdu_len;
1492 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1493 !info->attrs[NFC_ATTR_SE_INDEX] ||
1494 !info->attrs[NFC_ATTR_SE_APDU])
1495 return -EINVAL;
1497 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1498 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1500 dev = nfc_get_device(dev_idx);
1501 if (!dev)
1502 return -ENODEV;
1504 if (!dev->ops || !dev->ops->se_io)
1505 return -ENOTSUPP;
1507 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1508 if (apdu_len == 0)
1509 return -EINVAL;
1511 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1512 if (!apdu)
1513 return -EINVAL;
1515 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1516 if (!ctx)
1517 return -ENOMEM;
1519 ctx->dev_idx = dev_idx;
1520 ctx->se_idx = se_idx;
1522 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1525 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1526 struct genl_info *info)
1528 struct nfc_dev *dev;
1529 struct nfc_vendor_cmd *cmd;
1530 u32 dev_idx, vid, subcmd;
1531 u8 *data;
1532 size_t data_len;
1533 int i, err;
1535 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1536 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1537 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1538 return -EINVAL;
1540 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1541 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1542 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1544 dev = nfc_get_device(dev_idx);
1545 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1546 return -ENODEV;
1548 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1549 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1550 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1551 if (data_len == 0)
1552 return -EINVAL;
1553 } else {
1554 data = NULL;
1555 data_len = 0;
1558 for (i = 0; i < dev->n_vendor_cmds; i++) {
1559 cmd = &dev->vendor_cmds[i];
1561 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1562 continue;
1564 dev->cur_cmd_info = info;
1565 err = cmd->doit(dev, data, data_len);
1566 dev->cur_cmd_info = NULL;
1567 return err;
1570 return -EOPNOTSUPP;
1573 /* message building helper */
1574 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1575 int flags, u8 cmd)
1577 /* since there is no private header just add the generic one */
1578 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1581 static struct sk_buff *
1582 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1583 u32 portid, u32 seq,
1584 enum nfc_attrs attr,
1585 u32 oui, u32 subcmd, gfp_t gfp)
1587 struct sk_buff *skb;
1588 void *hdr;
1590 skb = nlmsg_new(approxlen + 100, gfp);
1591 if (!skb)
1592 return NULL;
1594 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1595 if (!hdr) {
1596 kfree_skb(skb);
1597 return NULL;
1600 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1601 goto nla_put_failure;
1602 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1603 goto nla_put_failure;
1604 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1605 goto nla_put_failure;
1607 ((void **)skb->cb)[0] = dev;
1608 ((void **)skb->cb)[1] = hdr;
1610 return skb;
1612 nla_put_failure:
1613 kfree_skb(skb);
1614 return NULL;
1617 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1618 enum nfc_attrs attr,
1619 u32 oui, u32 subcmd,
1620 int approxlen)
1622 if (WARN_ON(!dev->cur_cmd_info))
1623 return NULL;
1625 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1626 dev->cur_cmd_info->snd_portid,
1627 dev->cur_cmd_info->snd_seq, attr,
1628 oui, subcmd, GFP_KERNEL);
1630 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1632 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1634 struct nfc_dev *dev = ((void **)skb->cb)[0];
1635 void *hdr = ((void **)skb->cb)[1];
1637 /* clear CB data for netlink core to own from now on */
1638 memset(skb->cb, 0, sizeof(skb->cb));
1640 if (WARN_ON(!dev->cur_cmd_info)) {
1641 kfree_skb(skb);
1642 return -EINVAL;
1645 genlmsg_end(skb, hdr);
1646 return genlmsg_reply(skb, dev->cur_cmd_info);
1648 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1650 static const struct genl_ops nfc_genl_ops[] = {
1652 .cmd = NFC_CMD_GET_DEVICE,
1653 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1654 .doit = nfc_genl_get_device,
1655 .dumpit = nfc_genl_dump_devices,
1656 .done = nfc_genl_dump_devices_done,
1659 .cmd = NFC_CMD_DEV_UP,
1660 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1661 .doit = nfc_genl_dev_up,
1664 .cmd = NFC_CMD_DEV_DOWN,
1665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1666 .doit = nfc_genl_dev_down,
1669 .cmd = NFC_CMD_START_POLL,
1670 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1671 .doit = nfc_genl_start_poll,
1674 .cmd = NFC_CMD_STOP_POLL,
1675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1676 .doit = nfc_genl_stop_poll,
1679 .cmd = NFC_CMD_DEP_LINK_UP,
1680 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1681 .doit = nfc_genl_dep_link_up,
1684 .cmd = NFC_CMD_DEP_LINK_DOWN,
1685 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1686 .doit = nfc_genl_dep_link_down,
1689 .cmd = NFC_CMD_GET_TARGET,
1690 .validate = GENL_DONT_VALIDATE_STRICT |
1691 GENL_DONT_VALIDATE_DUMP_STRICT,
1692 .dumpit = nfc_genl_dump_targets,
1693 .done = nfc_genl_dump_targets_done,
1696 .cmd = NFC_CMD_LLC_GET_PARAMS,
1697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1698 .doit = nfc_genl_llc_get_params,
1701 .cmd = NFC_CMD_LLC_SET_PARAMS,
1702 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1703 .doit = nfc_genl_llc_set_params,
1706 .cmd = NFC_CMD_LLC_SDREQ,
1707 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1708 .doit = nfc_genl_llc_sdreq,
1711 .cmd = NFC_CMD_FW_DOWNLOAD,
1712 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1713 .doit = nfc_genl_fw_download,
1716 .cmd = NFC_CMD_ENABLE_SE,
1717 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1718 .doit = nfc_genl_enable_se,
1721 .cmd = NFC_CMD_DISABLE_SE,
1722 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1723 .doit = nfc_genl_disable_se,
1726 .cmd = NFC_CMD_GET_SE,
1727 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1728 .dumpit = nfc_genl_dump_ses,
1729 .done = nfc_genl_dump_ses_done,
1732 .cmd = NFC_CMD_SE_IO,
1733 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1734 .doit = nfc_genl_se_io,
1737 .cmd = NFC_CMD_ACTIVATE_TARGET,
1738 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1739 .doit = nfc_genl_activate_target,
1742 .cmd = NFC_CMD_VENDOR,
1743 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1744 .doit = nfc_genl_vendor_cmd,
1747 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1748 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1749 .doit = nfc_genl_deactivate_target,
1753 static struct genl_family nfc_genl_family __ro_after_init = {
1754 .hdrsize = 0,
1755 .name = NFC_GENL_NAME,
1756 .version = NFC_GENL_VERSION,
1757 .maxattr = NFC_ATTR_MAX,
1758 .policy = nfc_genl_policy,
1759 .module = THIS_MODULE,
1760 .ops = nfc_genl_ops,
1761 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1762 .mcgrps = nfc_genl_mcgrps,
1763 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1767 struct urelease_work {
1768 struct work_struct w;
1769 u32 portid;
1772 static void nfc_urelease_event_work(struct work_struct *work)
1774 struct urelease_work *w = container_of(work, struct urelease_work, w);
1775 struct class_dev_iter iter;
1776 struct nfc_dev *dev;
1778 pr_debug("portid %d\n", w->portid);
1780 mutex_lock(&nfc_devlist_mutex);
1782 nfc_device_iter_init(&iter);
1783 dev = nfc_device_iter_next(&iter);
1785 while (dev) {
1786 mutex_lock(&dev->genl_data.genl_data_mutex);
1788 if (dev->genl_data.poll_req_portid == w->portid) {
1789 nfc_stop_poll(dev);
1790 dev->genl_data.poll_req_portid = 0;
1793 mutex_unlock(&dev->genl_data.genl_data_mutex);
1795 dev = nfc_device_iter_next(&iter);
1798 nfc_device_iter_exit(&iter);
1800 mutex_unlock(&nfc_devlist_mutex);
1802 kfree(w);
1805 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1806 unsigned long event, void *ptr)
1808 struct netlink_notify *n = ptr;
1809 struct urelease_work *w;
1811 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1812 goto out;
1814 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1816 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1817 if (w) {
1818 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1819 w->portid = n->portid;
1820 schedule_work((struct work_struct *) w);
1823 out:
1824 return NOTIFY_DONE;
1827 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1829 genl_data->poll_req_portid = 0;
1830 mutex_init(&genl_data->genl_data_mutex);
1833 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1835 mutex_destroy(&genl_data->genl_data_mutex);
1838 static struct notifier_block nl_notifier = {
1839 .notifier_call = nfc_genl_rcv_nl_event,
1843 * nfc_genl_init() - Initialize netlink interface
1845 * This initialization function registers the nfc netlink family.
1847 int __init nfc_genl_init(void)
1849 int rc;
1851 rc = genl_register_family(&nfc_genl_family);
1852 if (rc)
1853 return rc;
1855 netlink_register_notifier(&nl_notifier);
1857 return 0;
1861 * nfc_genl_exit() - Deinitialize netlink interface
1863 * This exit function unregisters the nfc netlink family.
1865 void nfc_genl_exit(void)
1867 netlink_unregister_notifier(&nl_notifier);
1868 genl_unregister_family(&nfc_genl_family);