x86/mm: Add TLB purge to free pmd/pte page interfaces
[linux/fpc-iii.git] / net / nfc / netlink.c
blobdbf74afe82fb6ea08564971b646df81a1730ccb1
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 .id = GENL_ID_GENERATE,
43 .hdrsize = 0,
44 .name = NFC_GENL_NAME,
45 .version = NFC_GENL_VERSION,
46 .maxattr = NFC_ATTR_MAX,
49 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
50 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
51 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
52 .len = NFC_DEVICE_NAME_MAXSIZE },
53 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
55 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
56 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
57 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
58 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
59 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
60 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
61 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
62 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
63 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
64 .len = NFC_FIRMWARE_NAME_MAXSIZE },
65 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
66 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
70 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
71 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
72 .len = U8_MAX - 4 },
73 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
76 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
77 struct netlink_callback *cb, int flags)
79 void *hdr;
81 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
82 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
83 if (!hdr)
84 return -EMSGSIZE;
86 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
88 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
89 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
90 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
91 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
92 goto nla_put_failure;
93 if (target->nfcid1_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
95 target->nfcid1))
96 goto nla_put_failure;
97 if (target->sensb_res_len > 0 &&
98 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
99 target->sensb_res))
100 goto nla_put_failure;
101 if (target->sensf_res_len > 0 &&
102 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
103 target->sensf_res))
104 goto nla_put_failure;
106 if (target->is_iso15693) {
107 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
108 target->iso15693_dsfid) ||
109 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
110 sizeof(target->iso15693_uid), target->iso15693_uid))
111 goto nla_put_failure;
114 genlmsg_end(msg, hdr);
115 return 0;
117 nla_put_failure:
118 genlmsg_cancel(msg, hdr);
119 return -EMSGSIZE;
122 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
124 struct nfc_dev *dev;
125 int rc;
126 u32 idx;
128 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
129 nfc_genl_family.attrbuf,
130 nfc_genl_family.maxattr,
131 nfc_genl_policy);
132 if (rc < 0)
133 return ERR_PTR(rc);
135 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
136 return ERR_PTR(-EINVAL);
138 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
140 dev = nfc_get_device(idx);
141 if (!dev)
142 return ERR_PTR(-ENODEV);
144 return dev;
147 static int nfc_genl_dump_targets(struct sk_buff *skb,
148 struct netlink_callback *cb)
150 int i = cb->args[0];
151 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
152 int rc;
154 if (!dev) {
155 dev = __get_device_from_cb(cb);
156 if (IS_ERR(dev))
157 return PTR_ERR(dev);
159 cb->args[1] = (long) dev;
162 device_lock(&dev->dev);
164 cb->seq = dev->targets_generation;
166 while (i < dev->n_targets) {
167 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
168 NLM_F_MULTI);
169 if (rc < 0)
170 break;
172 i++;
175 device_unlock(&dev->dev);
177 cb->args[0] = i;
179 return skb->len;
182 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
184 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
186 if (dev)
187 nfc_put_device(dev);
189 return 0;
192 int nfc_genl_targets_found(struct nfc_dev *dev)
194 struct sk_buff *msg;
195 void *hdr;
197 dev->genl_data.poll_req_portid = 0;
199 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
200 if (!msg)
201 return -ENOMEM;
203 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
204 NFC_EVENT_TARGETS_FOUND);
205 if (!hdr)
206 goto free_msg;
208 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
209 goto nla_put_failure;
211 genlmsg_end(msg, hdr);
213 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
215 nla_put_failure:
216 genlmsg_cancel(msg, hdr);
217 free_msg:
218 nlmsg_free(msg);
219 return -EMSGSIZE;
222 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
224 struct sk_buff *msg;
225 void *hdr;
227 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
228 if (!msg)
229 return -ENOMEM;
231 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
232 NFC_EVENT_TARGET_LOST);
233 if (!hdr)
234 goto free_msg;
236 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
237 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
238 goto nla_put_failure;
240 genlmsg_end(msg, hdr);
242 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
244 return 0;
246 nla_put_failure:
247 genlmsg_cancel(msg, hdr);
248 free_msg:
249 nlmsg_free(msg);
250 return -EMSGSIZE;
253 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
255 struct sk_buff *msg;
256 void *hdr;
258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
259 if (!msg)
260 return -ENOMEM;
262 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
263 NFC_EVENT_TM_ACTIVATED);
264 if (!hdr)
265 goto free_msg;
267 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
268 goto nla_put_failure;
269 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
270 goto nla_put_failure;
272 genlmsg_end(msg, hdr);
274 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
276 return 0;
278 nla_put_failure:
279 genlmsg_cancel(msg, hdr);
280 free_msg:
281 nlmsg_free(msg);
282 return -EMSGSIZE;
285 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
287 struct sk_buff *msg;
288 void *hdr;
290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
291 if (!msg)
292 return -ENOMEM;
294 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
295 NFC_EVENT_TM_DEACTIVATED);
296 if (!hdr)
297 goto free_msg;
299 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
300 goto nla_put_failure;
302 genlmsg_end(msg, hdr);
304 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
306 return 0;
308 nla_put_failure:
309 genlmsg_cancel(msg, hdr);
310 free_msg:
311 nlmsg_free(msg);
312 return -EMSGSIZE;
315 int nfc_genl_device_added(struct nfc_dev *dev)
317 struct sk_buff *msg;
318 void *hdr;
320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
321 if (!msg)
322 return -ENOMEM;
324 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
325 NFC_EVENT_DEVICE_ADDED);
326 if (!hdr)
327 goto free_msg;
329 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
330 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
331 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
332 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
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, &nfc_genl_family);
608 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
609 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
610 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
611 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
612 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
613 goto nla_put_failure;
615 genlmsg_end(msg, hdr);
616 return 0;
618 nla_put_failure:
619 genlmsg_cancel(msg, hdr);
620 return -EMSGSIZE;
623 static int nfc_genl_dump_devices(struct sk_buff *skb,
624 struct netlink_callback *cb)
626 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
627 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
628 bool first_call = false;
630 if (!iter) {
631 first_call = true;
632 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
633 if (!iter)
634 return -ENOMEM;
635 cb->args[0] = (long) iter;
638 mutex_lock(&nfc_devlist_mutex);
640 cb->seq = nfc_devlist_generation;
642 if (first_call) {
643 nfc_device_iter_init(iter);
644 dev = nfc_device_iter_next(iter);
647 while (dev) {
648 int rc;
650 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
651 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
652 if (rc < 0)
653 break;
655 dev = nfc_device_iter_next(iter);
658 mutex_unlock(&nfc_devlist_mutex);
660 cb->args[1] = (long) dev;
662 return skb->len;
665 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
667 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
669 nfc_device_iter_exit(iter);
670 kfree(iter);
672 return 0;
675 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
676 u8 comm_mode, u8 rf_mode)
678 struct sk_buff *msg;
679 void *hdr;
681 pr_debug("DEP link is up\n");
683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
684 if (!msg)
685 return -ENOMEM;
687 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
688 if (!hdr)
689 goto free_msg;
691 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
692 goto nla_put_failure;
693 if (rf_mode == NFC_RF_INITIATOR &&
694 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
695 goto nla_put_failure;
696 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
697 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
698 goto nla_put_failure;
700 genlmsg_end(msg, hdr);
702 dev->dep_link_up = true;
704 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
706 return 0;
708 nla_put_failure:
709 genlmsg_cancel(msg, hdr);
710 free_msg:
711 nlmsg_free(msg);
712 return -EMSGSIZE;
715 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
717 struct sk_buff *msg;
718 void *hdr;
720 pr_debug("DEP link is down\n");
722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
723 if (!msg)
724 return -ENOMEM;
726 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
727 NFC_CMD_DEP_LINK_DOWN);
728 if (!hdr)
729 goto free_msg;
731 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
732 goto nla_put_failure;
734 genlmsg_end(msg, hdr);
736 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
738 return 0;
740 nla_put_failure:
741 genlmsg_cancel(msg, hdr);
742 free_msg:
743 nlmsg_free(msg);
744 return -EMSGSIZE;
747 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
749 struct sk_buff *msg;
750 struct nfc_dev *dev;
751 u32 idx;
752 int rc = -ENOBUFS;
754 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
755 return -EINVAL;
757 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
759 dev = nfc_get_device(idx);
760 if (!dev)
761 return -ENODEV;
763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
764 if (!msg) {
765 rc = -ENOMEM;
766 goto out_putdev;
769 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
770 NULL, 0);
771 if (rc < 0)
772 goto out_free;
774 nfc_put_device(dev);
776 return genlmsg_reply(msg, info);
778 out_free:
779 nlmsg_free(msg);
780 out_putdev:
781 nfc_put_device(dev);
782 return rc;
785 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
787 struct nfc_dev *dev;
788 int rc;
789 u32 idx;
791 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
792 return -EINVAL;
794 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
796 dev = nfc_get_device(idx);
797 if (!dev)
798 return -ENODEV;
800 rc = nfc_dev_up(dev);
802 nfc_put_device(dev);
803 return rc;
806 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
808 struct nfc_dev *dev;
809 int rc;
810 u32 idx;
812 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
813 return -EINVAL;
815 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
817 dev = nfc_get_device(idx);
818 if (!dev)
819 return -ENODEV;
821 rc = nfc_dev_down(dev);
823 nfc_put_device(dev);
824 return rc;
827 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
829 struct nfc_dev *dev;
830 int rc;
831 u32 idx;
832 u32 im_protocols = 0, tm_protocols = 0;
834 pr_debug("Poll start\n");
836 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
837 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
838 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
839 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
840 return -EINVAL;
842 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
844 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
845 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
847 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
848 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
849 else if (info->attrs[NFC_ATTR_PROTOCOLS])
850 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
852 dev = nfc_get_device(idx);
853 if (!dev)
854 return -ENODEV;
856 mutex_lock(&dev->genl_data.genl_data_mutex);
858 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
859 if (!rc)
860 dev->genl_data.poll_req_portid = info->snd_portid;
862 mutex_unlock(&dev->genl_data.genl_data_mutex);
864 nfc_put_device(dev);
865 return rc;
868 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
870 struct nfc_dev *dev;
871 int rc;
872 u32 idx;
874 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
875 return -EINVAL;
877 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
879 dev = nfc_get_device(idx);
880 if (!dev)
881 return -ENODEV;
883 device_lock(&dev->dev);
885 if (!dev->polling) {
886 device_unlock(&dev->dev);
887 return -EINVAL;
890 device_unlock(&dev->dev);
892 mutex_lock(&dev->genl_data.genl_data_mutex);
894 if (dev->genl_data.poll_req_portid != info->snd_portid) {
895 rc = -EBUSY;
896 goto out;
899 rc = nfc_stop_poll(dev);
900 dev->genl_data.poll_req_portid = 0;
902 out:
903 mutex_unlock(&dev->genl_data.genl_data_mutex);
904 nfc_put_device(dev);
905 return rc;
908 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
910 struct nfc_dev *dev;
911 u32 device_idx, target_idx, protocol;
912 int rc;
914 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
915 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
916 !info->attrs[NFC_ATTR_PROTOCOLS])
917 return -EINVAL;
919 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
921 dev = nfc_get_device(device_idx);
922 if (!dev)
923 return -ENODEV;
925 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
926 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
928 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
929 rc = nfc_activate_target(dev, target_idx, protocol);
931 nfc_put_device(dev);
932 return 0;
935 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
937 struct nfc_dev *dev;
938 int rc, tgt_idx;
939 u32 idx;
940 u8 comm;
942 pr_debug("DEP link up\n");
944 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
945 !info->attrs[NFC_ATTR_COMM_MODE])
946 return -EINVAL;
948 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
949 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
950 tgt_idx = NFC_TARGET_IDX_ANY;
951 else
952 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
954 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
956 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
957 return -EINVAL;
959 dev = nfc_get_device(idx);
960 if (!dev)
961 return -ENODEV;
963 rc = nfc_dep_link_up(dev, tgt_idx, comm);
965 nfc_put_device(dev);
967 return rc;
970 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
972 struct nfc_dev *dev;
973 int rc;
974 u32 idx;
976 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
977 return -EINVAL;
979 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
981 dev = nfc_get_device(idx);
982 if (!dev)
983 return -ENODEV;
985 rc = nfc_dep_link_down(dev);
987 nfc_put_device(dev);
988 return rc;
991 static int nfc_genl_send_params(struct sk_buff *msg,
992 struct nfc_llcp_local *local,
993 u32 portid, u32 seq)
995 void *hdr;
997 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
998 NFC_CMD_LLC_GET_PARAMS);
999 if (!hdr)
1000 return -EMSGSIZE;
1002 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1003 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1004 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1005 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1006 goto nla_put_failure;
1008 genlmsg_end(msg, hdr);
1009 return 0;
1011 nla_put_failure:
1013 genlmsg_cancel(msg, hdr);
1014 return -EMSGSIZE;
1017 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1019 struct nfc_dev *dev;
1020 struct nfc_llcp_local *local;
1021 int rc = 0;
1022 struct sk_buff *msg = NULL;
1023 u32 idx;
1025 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1026 return -EINVAL;
1028 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1030 dev = nfc_get_device(idx);
1031 if (!dev)
1032 return -ENODEV;
1034 device_lock(&dev->dev);
1036 local = nfc_llcp_find_local(dev);
1037 if (!local) {
1038 rc = -ENODEV;
1039 goto exit;
1042 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1043 if (!msg) {
1044 rc = -ENOMEM;
1045 goto exit;
1048 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1050 exit:
1051 device_unlock(&dev->dev);
1053 nfc_put_device(dev);
1055 if (rc < 0) {
1056 if (msg)
1057 nlmsg_free(msg);
1059 return rc;
1062 return genlmsg_reply(msg, info);
1065 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1067 struct nfc_dev *dev;
1068 struct nfc_llcp_local *local;
1069 u8 rw = 0;
1070 u16 miux = 0;
1071 u32 idx;
1072 int rc = 0;
1074 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1075 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1076 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1077 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1078 return -EINVAL;
1080 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1081 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1083 if (rw > LLCP_MAX_RW)
1084 return -EINVAL;
1087 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1088 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1090 if (miux > LLCP_MAX_MIUX)
1091 return -EINVAL;
1094 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1096 dev = nfc_get_device(idx);
1097 if (!dev)
1098 return -ENODEV;
1100 device_lock(&dev->dev);
1102 local = nfc_llcp_find_local(dev);
1103 if (!local) {
1104 nfc_put_device(dev);
1105 rc = -ENODEV;
1106 goto exit;
1109 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1110 if (dev->dep_link_up) {
1111 rc = -EINPROGRESS;
1112 goto exit;
1115 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1118 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1119 local->rw = rw;
1121 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1122 local->miux = cpu_to_be16(miux);
1124 exit:
1125 device_unlock(&dev->dev);
1127 nfc_put_device(dev);
1129 return rc;
1132 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1134 struct nfc_dev *dev;
1135 struct nfc_llcp_local *local;
1136 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1137 u32 idx;
1138 u8 tid;
1139 char *uri;
1140 int rc = 0, rem;
1141 size_t uri_len, tlvs_len;
1142 struct hlist_head sdreq_list;
1143 struct nfc_llcp_sdp_tlv *sdreq;
1145 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1146 !info->attrs[NFC_ATTR_LLC_SDP])
1147 return -EINVAL;
1149 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1151 dev = nfc_get_device(idx);
1152 if (!dev)
1153 return -ENODEV;
1155 device_lock(&dev->dev);
1157 if (dev->dep_link_up == false) {
1158 rc = -ENOLINK;
1159 goto exit;
1162 local = nfc_llcp_find_local(dev);
1163 if (!local) {
1164 nfc_put_device(dev);
1165 rc = -ENODEV;
1166 goto exit;
1169 INIT_HLIST_HEAD(&sdreq_list);
1171 tlvs_len = 0;
1173 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1174 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1175 nfc_sdp_genl_policy);
1177 if (rc != 0) {
1178 rc = -EINVAL;
1179 goto exit;
1182 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1183 continue;
1185 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1186 if (uri_len == 0)
1187 continue;
1189 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1190 if (uri == NULL || *uri == 0)
1191 continue;
1193 tid = local->sdreq_next_tid++;
1195 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1196 if (sdreq == NULL) {
1197 rc = -ENOMEM;
1198 goto exit;
1201 tlvs_len += sdreq->tlv_len;
1203 hlist_add_head(&sdreq->node, &sdreq_list);
1206 if (hlist_empty(&sdreq_list)) {
1207 rc = -EINVAL;
1208 goto exit;
1211 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1212 exit:
1213 device_unlock(&dev->dev);
1215 nfc_put_device(dev);
1217 return rc;
1220 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1222 struct nfc_dev *dev;
1223 int rc;
1224 u32 idx;
1225 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1227 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1228 return -EINVAL;
1230 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1232 dev = nfc_get_device(idx);
1233 if (!dev)
1234 return -ENODEV;
1236 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1237 sizeof(firmware_name));
1239 rc = nfc_fw_download(dev, firmware_name);
1241 nfc_put_device(dev);
1242 return rc;
1245 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1246 u32 result)
1248 struct sk_buff *msg;
1249 void *hdr;
1251 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1252 if (!msg)
1253 return -ENOMEM;
1255 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1256 NFC_CMD_FW_DOWNLOAD);
1257 if (!hdr)
1258 goto free_msg;
1260 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1261 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1262 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1263 goto nla_put_failure;
1265 genlmsg_end(msg, hdr);
1267 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1269 return 0;
1271 nla_put_failure:
1272 genlmsg_cancel(msg, hdr);
1273 free_msg:
1274 nlmsg_free(msg);
1275 return -EMSGSIZE;
1278 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1280 struct nfc_dev *dev;
1281 int rc;
1282 u32 idx, se_idx;
1284 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1285 !info->attrs[NFC_ATTR_SE_INDEX])
1286 return -EINVAL;
1288 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1289 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1291 dev = nfc_get_device(idx);
1292 if (!dev)
1293 return -ENODEV;
1295 rc = nfc_enable_se(dev, se_idx);
1297 nfc_put_device(dev);
1298 return rc;
1301 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1303 struct nfc_dev *dev;
1304 int rc;
1305 u32 idx, se_idx;
1307 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1308 !info->attrs[NFC_ATTR_SE_INDEX])
1309 return -EINVAL;
1311 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1312 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1314 dev = nfc_get_device(idx);
1315 if (!dev)
1316 return -ENODEV;
1318 rc = nfc_disable_se(dev, se_idx);
1320 nfc_put_device(dev);
1321 return rc;
1324 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1325 u32 portid, u32 seq,
1326 struct netlink_callback *cb,
1327 int flags)
1329 void *hdr;
1330 struct nfc_se *se, *n;
1332 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1333 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1334 NFC_CMD_GET_SE);
1335 if (!hdr)
1336 goto nla_put_failure;
1338 if (cb)
1339 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1341 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1342 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1343 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1344 goto nla_put_failure;
1346 genlmsg_end(msg, hdr);
1349 return 0;
1351 nla_put_failure:
1352 genlmsg_cancel(msg, hdr);
1353 return -EMSGSIZE;
1356 static int nfc_genl_dump_ses(struct sk_buff *skb,
1357 struct netlink_callback *cb)
1359 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1360 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1361 bool first_call = false;
1363 if (!iter) {
1364 first_call = true;
1365 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1366 if (!iter)
1367 return -ENOMEM;
1368 cb->args[0] = (long) iter;
1371 mutex_lock(&nfc_devlist_mutex);
1373 cb->seq = nfc_devlist_generation;
1375 if (first_call) {
1376 nfc_device_iter_init(iter);
1377 dev = nfc_device_iter_next(iter);
1380 while (dev) {
1381 int rc;
1383 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1384 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1385 if (rc < 0)
1386 break;
1388 dev = nfc_device_iter_next(iter);
1391 mutex_unlock(&nfc_devlist_mutex);
1393 cb->args[1] = (long) dev;
1395 return skb->len;
1398 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1400 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1402 nfc_device_iter_exit(iter);
1403 kfree(iter);
1405 return 0;
1408 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1409 u8 *apdu, size_t apdu_length,
1410 se_io_cb_t cb, void *cb_context)
1412 struct nfc_se *se;
1413 int rc;
1415 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1417 device_lock(&dev->dev);
1419 if (!device_is_registered(&dev->dev)) {
1420 rc = -ENODEV;
1421 goto error;
1424 if (!dev->dev_up) {
1425 rc = -ENODEV;
1426 goto error;
1429 if (!dev->ops->se_io) {
1430 rc = -EOPNOTSUPP;
1431 goto error;
1434 se = nfc_find_se(dev, se_idx);
1435 if (!se) {
1436 rc = -EINVAL;
1437 goto error;
1440 if (se->state != NFC_SE_ENABLED) {
1441 rc = -ENODEV;
1442 goto error;
1445 rc = dev->ops->se_io(dev, se_idx, apdu,
1446 apdu_length, cb, cb_context);
1448 error:
1449 device_unlock(&dev->dev);
1450 return rc;
1453 struct se_io_ctx {
1454 u32 dev_idx;
1455 u32 se_idx;
1458 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1460 struct se_io_ctx *ctx = context;
1461 struct sk_buff *msg;
1462 void *hdr;
1464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1465 if (!msg) {
1466 kfree(ctx);
1467 return;
1470 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1471 NFC_CMD_SE_IO);
1472 if (!hdr)
1473 goto free_msg;
1475 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1476 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1477 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1478 goto nla_put_failure;
1480 genlmsg_end(msg, hdr);
1482 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1484 kfree(ctx);
1486 return;
1488 nla_put_failure:
1489 genlmsg_cancel(msg, hdr);
1490 free_msg:
1491 nlmsg_free(msg);
1492 kfree(ctx);
1494 return;
1497 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1499 struct nfc_dev *dev;
1500 struct se_io_ctx *ctx;
1501 u32 dev_idx, se_idx;
1502 u8 *apdu;
1503 size_t apdu_len;
1505 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1506 !info->attrs[NFC_ATTR_SE_INDEX] ||
1507 !info->attrs[NFC_ATTR_SE_APDU])
1508 return -EINVAL;
1510 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1511 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1513 dev = nfc_get_device(dev_idx);
1514 if (!dev)
1515 return -ENODEV;
1517 if (!dev->ops || !dev->ops->se_io)
1518 return -ENOTSUPP;
1520 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1521 if (apdu_len == 0)
1522 return -EINVAL;
1524 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1525 if (!apdu)
1526 return -EINVAL;
1528 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1529 if (!ctx)
1530 return -ENOMEM;
1532 ctx->dev_idx = dev_idx;
1533 ctx->se_idx = se_idx;
1535 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1538 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1539 struct genl_info *info)
1541 struct nfc_dev *dev;
1542 struct nfc_vendor_cmd *cmd;
1543 u32 dev_idx, vid, subcmd;
1544 u8 *data;
1545 size_t data_len;
1546 int i, err;
1548 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1549 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1550 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1551 return -EINVAL;
1553 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1554 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1555 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1557 dev = nfc_get_device(dev_idx);
1558 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1559 return -ENODEV;
1561 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1562 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1563 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1564 if (data_len == 0)
1565 return -EINVAL;
1566 } else {
1567 data = NULL;
1568 data_len = 0;
1571 for (i = 0; i < dev->n_vendor_cmds; i++) {
1572 cmd = &dev->vendor_cmds[i];
1574 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1575 continue;
1577 dev->cur_cmd_info = info;
1578 err = cmd->doit(dev, data, data_len);
1579 dev->cur_cmd_info = NULL;
1580 return err;
1583 return -EOPNOTSUPP;
1586 /* message building helper */
1587 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1588 int flags, u8 cmd)
1590 /* since there is no private header just add the generic one */
1591 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1594 static struct sk_buff *
1595 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1596 u32 portid, u32 seq,
1597 enum nfc_attrs attr,
1598 u32 oui, u32 subcmd, gfp_t gfp)
1600 struct sk_buff *skb;
1601 void *hdr;
1603 skb = nlmsg_new(approxlen + 100, gfp);
1604 if (!skb)
1605 return NULL;
1607 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1608 if (!hdr) {
1609 kfree_skb(skb);
1610 return NULL;
1613 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1614 goto nla_put_failure;
1615 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1616 goto nla_put_failure;
1617 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1618 goto nla_put_failure;
1620 ((void **)skb->cb)[0] = dev;
1621 ((void **)skb->cb)[1] = hdr;
1623 return skb;
1625 nla_put_failure:
1626 kfree_skb(skb);
1627 return NULL;
1630 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1631 enum nfc_attrs attr,
1632 u32 oui, u32 subcmd,
1633 int approxlen)
1635 if (WARN_ON(!dev->cur_cmd_info))
1636 return NULL;
1638 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1639 dev->cur_cmd_info->snd_portid,
1640 dev->cur_cmd_info->snd_seq, attr,
1641 oui, subcmd, GFP_KERNEL);
1643 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1645 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1647 struct nfc_dev *dev = ((void **)skb->cb)[0];
1648 void *hdr = ((void **)skb->cb)[1];
1650 /* clear CB data for netlink core to own from now on */
1651 memset(skb->cb, 0, sizeof(skb->cb));
1653 if (WARN_ON(!dev->cur_cmd_info)) {
1654 kfree_skb(skb);
1655 return -EINVAL;
1658 genlmsg_end(skb, hdr);
1659 return genlmsg_reply(skb, dev->cur_cmd_info);
1661 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1663 static const struct genl_ops nfc_genl_ops[] = {
1665 .cmd = NFC_CMD_GET_DEVICE,
1666 .doit = nfc_genl_get_device,
1667 .dumpit = nfc_genl_dump_devices,
1668 .done = nfc_genl_dump_devices_done,
1669 .policy = nfc_genl_policy,
1672 .cmd = NFC_CMD_DEV_UP,
1673 .doit = nfc_genl_dev_up,
1674 .policy = nfc_genl_policy,
1677 .cmd = NFC_CMD_DEV_DOWN,
1678 .doit = nfc_genl_dev_down,
1679 .policy = nfc_genl_policy,
1682 .cmd = NFC_CMD_START_POLL,
1683 .doit = nfc_genl_start_poll,
1684 .policy = nfc_genl_policy,
1687 .cmd = NFC_CMD_STOP_POLL,
1688 .doit = nfc_genl_stop_poll,
1689 .policy = nfc_genl_policy,
1692 .cmd = NFC_CMD_DEP_LINK_UP,
1693 .doit = nfc_genl_dep_link_up,
1694 .policy = nfc_genl_policy,
1697 .cmd = NFC_CMD_DEP_LINK_DOWN,
1698 .doit = nfc_genl_dep_link_down,
1699 .policy = nfc_genl_policy,
1702 .cmd = NFC_CMD_GET_TARGET,
1703 .dumpit = nfc_genl_dump_targets,
1704 .done = nfc_genl_dump_targets_done,
1705 .policy = nfc_genl_policy,
1708 .cmd = NFC_CMD_LLC_GET_PARAMS,
1709 .doit = nfc_genl_llc_get_params,
1710 .policy = nfc_genl_policy,
1713 .cmd = NFC_CMD_LLC_SET_PARAMS,
1714 .doit = nfc_genl_llc_set_params,
1715 .policy = nfc_genl_policy,
1718 .cmd = NFC_CMD_LLC_SDREQ,
1719 .doit = nfc_genl_llc_sdreq,
1720 .policy = nfc_genl_policy,
1723 .cmd = NFC_CMD_FW_DOWNLOAD,
1724 .doit = nfc_genl_fw_download,
1725 .policy = nfc_genl_policy,
1728 .cmd = NFC_CMD_ENABLE_SE,
1729 .doit = nfc_genl_enable_se,
1730 .policy = nfc_genl_policy,
1733 .cmd = NFC_CMD_DISABLE_SE,
1734 .doit = nfc_genl_disable_se,
1735 .policy = nfc_genl_policy,
1738 .cmd = NFC_CMD_GET_SE,
1739 .dumpit = nfc_genl_dump_ses,
1740 .done = nfc_genl_dump_ses_done,
1741 .policy = nfc_genl_policy,
1744 .cmd = NFC_CMD_SE_IO,
1745 .doit = nfc_genl_se_io,
1746 .policy = nfc_genl_policy,
1749 .cmd = NFC_CMD_ACTIVATE_TARGET,
1750 .doit = nfc_genl_activate_target,
1751 .policy = nfc_genl_policy,
1754 .cmd = NFC_CMD_VENDOR,
1755 .doit = nfc_genl_vendor_cmd,
1756 .policy = nfc_genl_policy,
1761 struct urelease_work {
1762 struct work_struct w;
1763 u32 portid;
1766 static void nfc_urelease_event_work(struct work_struct *work)
1768 struct urelease_work *w = container_of(work, struct urelease_work, w);
1769 struct class_dev_iter iter;
1770 struct nfc_dev *dev;
1772 pr_debug("portid %d\n", w->portid);
1774 mutex_lock(&nfc_devlist_mutex);
1776 nfc_device_iter_init(&iter);
1777 dev = nfc_device_iter_next(&iter);
1779 while (dev) {
1780 mutex_lock(&dev->genl_data.genl_data_mutex);
1782 if (dev->genl_data.poll_req_portid == w->portid) {
1783 nfc_stop_poll(dev);
1784 dev->genl_data.poll_req_portid = 0;
1787 mutex_unlock(&dev->genl_data.genl_data_mutex);
1789 dev = nfc_device_iter_next(&iter);
1792 nfc_device_iter_exit(&iter);
1794 mutex_unlock(&nfc_devlist_mutex);
1796 kfree(w);
1799 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1800 unsigned long event, void *ptr)
1802 struct netlink_notify *n = ptr;
1803 struct urelease_work *w;
1805 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1806 goto out;
1808 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1810 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1811 if (w) {
1812 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1813 w->portid = n->portid;
1814 schedule_work((struct work_struct *) w);
1817 out:
1818 return NOTIFY_DONE;
1821 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1823 genl_data->poll_req_portid = 0;
1824 mutex_init(&genl_data->genl_data_mutex);
1827 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1829 mutex_destroy(&genl_data->genl_data_mutex);
1832 static struct notifier_block nl_notifier = {
1833 .notifier_call = nfc_genl_rcv_nl_event,
1837 * nfc_genl_init() - Initialize netlink interface
1839 * This initialization function registers the nfc netlink family.
1841 int __init nfc_genl_init(void)
1843 int rc;
1845 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1846 nfc_genl_ops,
1847 nfc_genl_mcgrps);
1848 if (rc)
1849 return rc;
1851 netlink_register_notifier(&nl_notifier);
1853 return 0;
1857 * nfc_genl_exit() - Deinitialize netlink interface
1859 * This exit function unregisters the nfc netlink family.
1861 void nfc_genl_exit(void)
1863 netlink_unregister_notifier(&nl_notifier);
1864 genl_unregister_family(&nfc_genl_family);