ALSA: hda/realtek - Change front mic location for Lenovo M710q
[linux/fpc-iii.git] / net / nfc / netlink.c
blob9f2875efb4ac9693ce89c636ebf3ed087ce5f0f6
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 .len = U8_MAX - 4 },
66 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
69 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
70 struct netlink_callback *cb, int flags)
72 void *hdr;
74 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
75 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
76 if (!hdr)
77 return -EMSGSIZE;
79 genl_dump_check_consistent(cb, hdr);
81 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
82 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
83 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
84 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
85 goto nla_put_failure;
86 if (target->nfcid1_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
88 target->nfcid1))
89 goto nla_put_failure;
90 if (target->sensb_res_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
92 target->sensb_res))
93 goto nla_put_failure;
94 if (target->sensf_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
96 target->sensf_res))
97 goto nla_put_failure;
99 if (target->is_iso15693) {
100 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
101 target->iso15693_dsfid) ||
102 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
103 sizeof(target->iso15693_uid), target->iso15693_uid))
104 goto nla_put_failure;
107 genlmsg_end(msg, hdr);
108 return 0;
110 nla_put_failure:
111 genlmsg_cancel(msg, hdr);
112 return -EMSGSIZE;
115 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
117 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
118 struct nfc_dev *dev;
119 int rc;
120 u32 idx;
122 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
123 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
124 NULL);
125 if (rc < 0)
126 return ERR_PTR(rc);
128 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
129 return ERR_PTR(-EINVAL);
131 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
133 dev = nfc_get_device(idx);
134 if (!dev)
135 return ERR_PTR(-ENODEV);
137 return dev;
140 static int nfc_genl_dump_targets(struct sk_buff *skb,
141 struct netlink_callback *cb)
143 int i = cb->args[0];
144 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
145 int rc;
147 if (!dev) {
148 dev = __get_device_from_cb(cb);
149 if (IS_ERR(dev))
150 return PTR_ERR(dev);
152 cb->args[1] = (long) dev;
155 device_lock(&dev->dev);
157 cb->seq = dev->targets_generation;
159 while (i < dev->n_targets) {
160 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
161 NLM_F_MULTI);
162 if (rc < 0)
163 break;
165 i++;
168 device_unlock(&dev->dev);
170 cb->args[0] = i;
172 return skb->len;
175 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
177 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
179 if (dev)
180 nfc_put_device(dev);
182 return 0;
185 int nfc_genl_targets_found(struct nfc_dev *dev)
187 struct sk_buff *msg;
188 void *hdr;
190 dev->genl_data.poll_req_portid = 0;
192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
193 if (!msg)
194 return -ENOMEM;
196 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
197 NFC_EVENT_TARGETS_FOUND);
198 if (!hdr)
199 goto free_msg;
201 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
202 goto nla_put_failure;
204 genlmsg_end(msg, hdr);
206 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
208 nla_put_failure:
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 free_msg:
240 nlmsg_free(msg);
241 return -EMSGSIZE;
244 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
246 struct sk_buff *msg;
247 void *hdr;
249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
250 if (!msg)
251 return -ENOMEM;
253 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
254 NFC_EVENT_TM_ACTIVATED);
255 if (!hdr)
256 goto free_msg;
258 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
259 goto nla_put_failure;
260 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
261 goto nla_put_failure;
263 genlmsg_end(msg, hdr);
265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
267 return 0;
269 nla_put_failure:
270 free_msg:
271 nlmsg_free(msg);
272 return -EMSGSIZE;
275 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
277 struct sk_buff *msg;
278 void *hdr;
280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
281 if (!msg)
282 return -ENOMEM;
284 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
285 NFC_EVENT_TM_DEACTIVATED);
286 if (!hdr)
287 goto free_msg;
289 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
290 goto nla_put_failure;
292 genlmsg_end(msg, hdr);
294 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
296 return 0;
298 nla_put_failure:
299 free_msg:
300 nlmsg_free(msg);
301 return -EMSGSIZE;
304 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
306 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
307 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
308 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
309 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
310 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
311 return -1;
312 return 0;
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 (nfc_genl_setup_device_added(dev, msg))
330 goto nla_put_failure;
332 genlmsg_end(msg, hdr);
334 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
336 return 0;
338 nla_put_failure:
339 free_msg:
340 nlmsg_free(msg);
341 return -EMSGSIZE;
344 int nfc_genl_device_removed(struct nfc_dev *dev)
346 struct sk_buff *msg;
347 void *hdr;
349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
350 if (!msg)
351 return -ENOMEM;
353 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
354 NFC_EVENT_DEVICE_REMOVED);
355 if (!hdr)
356 goto free_msg;
358 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
359 goto nla_put_failure;
361 genlmsg_end(msg, hdr);
363 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
365 return 0;
367 nla_put_failure:
368 free_msg:
369 nlmsg_free(msg);
370 return -EMSGSIZE;
373 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
375 struct sk_buff *msg;
376 struct nlattr *sdp_attr, *uri_attr;
377 struct nfc_llcp_sdp_tlv *sdres;
378 struct hlist_node *n;
379 void *hdr;
380 int rc = -EMSGSIZE;
381 int i;
383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
384 if (!msg)
385 return -ENOMEM;
387 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
388 NFC_EVENT_LLC_SDRES);
389 if (!hdr)
390 goto free_msg;
392 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
393 goto nla_put_failure;
395 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
396 if (sdp_attr == NULL) {
397 rc = -ENOMEM;
398 goto nla_put_failure;
401 i = 1;
402 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
403 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
405 uri_attr = nla_nest_start(msg, i++);
406 if (uri_attr == NULL) {
407 rc = -ENOMEM;
408 goto nla_put_failure;
411 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
412 goto nla_put_failure;
414 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
415 goto nla_put_failure;
417 nla_nest_end(msg, uri_attr);
419 hlist_del(&sdres->node);
421 nfc_llcp_free_sdp_tlv(sdres);
424 nla_nest_end(msg, sdp_attr);
426 genlmsg_end(msg, hdr);
428 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
430 nla_put_failure:
431 free_msg:
432 nlmsg_free(msg);
434 nfc_llcp_free_sdp_tlv_list(sdres_list);
436 return rc;
439 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
441 struct sk_buff *msg;
442 void *hdr;
444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
445 if (!msg)
446 return -ENOMEM;
448 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
449 NFC_EVENT_SE_ADDED);
450 if (!hdr)
451 goto free_msg;
453 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
454 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
455 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
456 goto nla_put_failure;
458 genlmsg_end(msg, hdr);
460 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
462 return 0;
464 nla_put_failure:
465 free_msg:
466 nlmsg_free(msg);
467 return -EMSGSIZE;
470 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
472 struct sk_buff *msg;
473 void *hdr;
475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
476 if (!msg)
477 return -ENOMEM;
479 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
480 NFC_EVENT_SE_REMOVED);
481 if (!hdr)
482 goto free_msg;
484 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
485 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
486 goto nla_put_failure;
488 genlmsg_end(msg, hdr);
490 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
492 return 0;
494 nla_put_failure:
495 free_msg:
496 nlmsg_free(msg);
497 return -EMSGSIZE;
500 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
501 struct nfc_evt_transaction *evt_transaction)
503 struct nfc_se *se;
504 struct sk_buff *msg;
505 void *hdr;
507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
508 if (!msg)
509 return -ENOMEM;
511 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
512 NFC_EVENT_SE_TRANSACTION);
513 if (!hdr)
514 goto free_msg;
516 se = nfc_find_se(dev, se_idx);
517 if (!se)
518 goto free_msg;
520 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
521 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
522 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
523 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
524 evt_transaction->aid) ||
525 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
526 evt_transaction->params))
527 goto nla_put_failure;
529 /* evt_transaction is no more used */
530 devm_kfree(&dev->dev, evt_transaction);
532 genlmsg_end(msg, hdr);
534 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
536 return 0;
538 nla_put_failure:
539 free_msg:
540 /* evt_transaction is no more used */
541 devm_kfree(&dev->dev, evt_transaction);
542 nlmsg_free(msg);
543 return -EMSGSIZE;
546 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
548 struct nfc_se *se;
549 struct sk_buff *msg;
550 void *hdr;
552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
553 if (!msg)
554 return -ENOMEM;
556 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
557 NFC_EVENT_SE_CONNECTIVITY);
558 if (!hdr)
559 goto free_msg;
561 se = nfc_find_se(dev, se_idx);
562 if (!se)
563 goto free_msg;
565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
566 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
567 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
568 goto nla_put_failure;
570 genlmsg_end(msg, hdr);
572 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
574 return 0;
576 nla_put_failure:
577 free_msg:
578 nlmsg_free(msg);
579 return -EMSGSIZE;
582 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
583 u32 portid, u32 seq,
584 struct netlink_callback *cb,
585 int flags)
587 void *hdr;
589 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
590 NFC_CMD_GET_DEVICE);
591 if (!hdr)
592 return -EMSGSIZE;
594 if (cb)
595 genl_dump_check_consistent(cb, hdr);
597 if (nfc_genl_setup_device_added(dev, msg))
598 goto nla_put_failure;
600 genlmsg_end(msg, hdr);
601 return 0;
603 nla_put_failure:
604 genlmsg_cancel(msg, hdr);
605 return -EMSGSIZE;
608 static int nfc_genl_dump_devices(struct sk_buff *skb,
609 struct netlink_callback *cb)
611 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
612 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
613 bool first_call = false;
615 if (!iter) {
616 first_call = true;
617 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
618 if (!iter)
619 return -ENOMEM;
620 cb->args[0] = (long) iter;
623 mutex_lock(&nfc_devlist_mutex);
625 cb->seq = nfc_devlist_generation;
627 if (first_call) {
628 nfc_device_iter_init(iter);
629 dev = nfc_device_iter_next(iter);
632 while (dev) {
633 int rc;
635 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
636 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
637 if (rc < 0)
638 break;
640 dev = nfc_device_iter_next(iter);
643 mutex_unlock(&nfc_devlist_mutex);
645 cb->args[1] = (long) dev;
647 return skb->len;
650 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
652 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
654 nfc_device_iter_exit(iter);
655 kfree(iter);
657 return 0;
660 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
661 u8 comm_mode, u8 rf_mode)
663 struct sk_buff *msg;
664 void *hdr;
666 pr_debug("DEP link is up\n");
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
669 if (!msg)
670 return -ENOMEM;
672 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
673 if (!hdr)
674 goto free_msg;
676 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
677 goto nla_put_failure;
678 if (rf_mode == NFC_RF_INITIATOR &&
679 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
680 goto nla_put_failure;
681 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
682 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
683 goto nla_put_failure;
685 genlmsg_end(msg, hdr);
687 dev->dep_link_up = true;
689 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
691 return 0;
693 nla_put_failure:
694 free_msg:
695 nlmsg_free(msg);
696 return -EMSGSIZE;
699 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
701 struct sk_buff *msg;
702 void *hdr;
704 pr_debug("DEP link is down\n");
706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
707 if (!msg)
708 return -ENOMEM;
710 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
711 NFC_CMD_DEP_LINK_DOWN);
712 if (!hdr)
713 goto free_msg;
715 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
716 goto nla_put_failure;
718 genlmsg_end(msg, hdr);
720 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
722 return 0;
724 nla_put_failure:
725 free_msg:
726 nlmsg_free(msg);
727 return -EMSGSIZE;
730 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
732 struct sk_buff *msg;
733 struct nfc_dev *dev;
734 u32 idx;
735 int rc = -ENOBUFS;
737 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
738 return -EINVAL;
740 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
742 dev = nfc_get_device(idx);
743 if (!dev)
744 return -ENODEV;
746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
747 if (!msg) {
748 rc = -ENOMEM;
749 goto out_putdev;
752 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
753 NULL, 0);
754 if (rc < 0)
755 goto out_free;
757 nfc_put_device(dev);
759 return genlmsg_reply(msg, info);
761 out_free:
762 nlmsg_free(msg);
763 out_putdev:
764 nfc_put_device(dev);
765 return rc;
768 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
770 struct nfc_dev *dev;
771 int rc;
772 u32 idx;
774 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
775 return -EINVAL;
777 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
779 dev = nfc_get_device(idx);
780 if (!dev)
781 return -ENODEV;
783 rc = nfc_dev_up(dev);
785 nfc_put_device(dev);
786 return rc;
789 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
791 struct nfc_dev *dev;
792 int rc;
793 u32 idx;
795 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
796 return -EINVAL;
798 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
800 dev = nfc_get_device(idx);
801 if (!dev)
802 return -ENODEV;
804 rc = nfc_dev_down(dev);
806 nfc_put_device(dev);
807 return rc;
810 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
812 struct nfc_dev *dev;
813 int rc;
814 u32 idx;
815 u32 im_protocols = 0, tm_protocols = 0;
817 pr_debug("Poll start\n");
819 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
820 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
821 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
822 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
823 return -EINVAL;
825 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
827 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
828 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
830 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
831 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
832 else if (info->attrs[NFC_ATTR_PROTOCOLS])
833 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
835 dev = nfc_get_device(idx);
836 if (!dev)
837 return -ENODEV;
839 mutex_lock(&dev->genl_data.genl_data_mutex);
841 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
842 if (!rc)
843 dev->genl_data.poll_req_portid = info->snd_portid;
845 mutex_unlock(&dev->genl_data.genl_data_mutex);
847 nfc_put_device(dev);
848 return rc;
851 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
853 struct nfc_dev *dev;
854 int rc;
855 u32 idx;
857 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
858 return -EINVAL;
860 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
862 dev = nfc_get_device(idx);
863 if (!dev)
864 return -ENODEV;
866 device_lock(&dev->dev);
868 if (!dev->polling) {
869 device_unlock(&dev->dev);
870 return -EINVAL;
873 device_unlock(&dev->dev);
875 mutex_lock(&dev->genl_data.genl_data_mutex);
877 if (dev->genl_data.poll_req_portid != info->snd_portid) {
878 rc = -EBUSY;
879 goto out;
882 rc = nfc_stop_poll(dev);
883 dev->genl_data.poll_req_portid = 0;
885 out:
886 mutex_unlock(&dev->genl_data.genl_data_mutex);
887 nfc_put_device(dev);
888 return rc;
891 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
893 struct nfc_dev *dev;
894 u32 device_idx, target_idx, protocol;
895 int rc;
897 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
898 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
899 !info->attrs[NFC_ATTR_PROTOCOLS])
900 return -EINVAL;
902 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
904 dev = nfc_get_device(device_idx);
905 if (!dev)
906 return -ENODEV;
908 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
909 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
911 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
912 rc = nfc_activate_target(dev, target_idx, protocol);
914 nfc_put_device(dev);
915 return rc;
918 static int nfc_genl_deactivate_target(struct sk_buff *skb,
919 struct genl_info *info)
921 struct nfc_dev *dev;
922 u32 device_idx, target_idx;
923 int rc;
925 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
926 !info->attrs[NFC_ATTR_TARGET_INDEX])
927 return -EINVAL;
929 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
931 dev = nfc_get_device(device_idx);
932 if (!dev)
933 return -ENODEV;
935 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
937 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
939 nfc_put_device(dev);
940 return rc;
943 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
945 struct nfc_dev *dev;
946 int rc, tgt_idx;
947 u32 idx;
948 u8 comm;
950 pr_debug("DEP link up\n");
952 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
953 !info->attrs[NFC_ATTR_COMM_MODE])
954 return -EINVAL;
956 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
957 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
958 tgt_idx = NFC_TARGET_IDX_ANY;
959 else
960 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
962 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
964 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
965 return -EINVAL;
967 dev = nfc_get_device(idx);
968 if (!dev)
969 return -ENODEV;
971 rc = nfc_dep_link_up(dev, tgt_idx, comm);
973 nfc_put_device(dev);
975 return rc;
978 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
980 struct nfc_dev *dev;
981 int rc;
982 u32 idx;
984 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
985 return -EINVAL;
987 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
989 dev = nfc_get_device(idx);
990 if (!dev)
991 return -ENODEV;
993 rc = nfc_dep_link_down(dev);
995 nfc_put_device(dev);
996 return rc;
999 static int nfc_genl_send_params(struct sk_buff *msg,
1000 struct nfc_llcp_local *local,
1001 u32 portid, u32 seq)
1003 void *hdr;
1005 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
1006 NFC_CMD_LLC_GET_PARAMS);
1007 if (!hdr)
1008 return -EMSGSIZE;
1010 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1011 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1012 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1013 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1014 goto nla_put_failure;
1016 genlmsg_end(msg, hdr);
1017 return 0;
1019 nla_put_failure:
1020 genlmsg_cancel(msg, hdr);
1021 return -EMSGSIZE;
1024 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1026 struct nfc_dev *dev;
1027 struct nfc_llcp_local *local;
1028 int rc = 0;
1029 struct sk_buff *msg = NULL;
1030 u32 idx;
1032 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1033 return -EINVAL;
1035 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1037 dev = nfc_get_device(idx);
1038 if (!dev)
1039 return -ENODEV;
1041 device_lock(&dev->dev);
1043 local = nfc_llcp_find_local(dev);
1044 if (!local) {
1045 rc = -ENODEV;
1046 goto exit;
1049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1050 if (!msg) {
1051 rc = -ENOMEM;
1052 goto exit;
1055 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1057 exit:
1058 device_unlock(&dev->dev);
1060 nfc_put_device(dev);
1062 if (rc < 0) {
1063 if (msg)
1064 nlmsg_free(msg);
1066 return rc;
1069 return genlmsg_reply(msg, info);
1072 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1074 struct nfc_dev *dev;
1075 struct nfc_llcp_local *local;
1076 u8 rw = 0;
1077 u16 miux = 0;
1078 u32 idx;
1079 int rc = 0;
1081 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1082 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1083 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1084 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1085 return -EINVAL;
1087 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1088 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1090 if (rw > LLCP_MAX_RW)
1091 return -EINVAL;
1094 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1095 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1097 if (miux > LLCP_MAX_MIUX)
1098 return -EINVAL;
1101 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1103 dev = nfc_get_device(idx);
1104 if (!dev)
1105 return -ENODEV;
1107 device_lock(&dev->dev);
1109 local = nfc_llcp_find_local(dev);
1110 if (!local) {
1111 nfc_put_device(dev);
1112 rc = -ENODEV;
1113 goto exit;
1116 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1117 if (dev->dep_link_up) {
1118 rc = -EINPROGRESS;
1119 goto exit;
1122 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1125 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1126 local->rw = rw;
1128 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1129 local->miux = cpu_to_be16(miux);
1131 exit:
1132 device_unlock(&dev->dev);
1134 nfc_put_device(dev);
1136 return rc;
1139 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1141 struct nfc_dev *dev;
1142 struct nfc_llcp_local *local;
1143 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1144 u32 idx;
1145 u8 tid;
1146 char *uri;
1147 int rc = 0, rem;
1148 size_t uri_len, tlvs_len;
1149 struct hlist_head sdreq_list;
1150 struct nfc_llcp_sdp_tlv *sdreq;
1152 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1153 !info->attrs[NFC_ATTR_LLC_SDP])
1154 return -EINVAL;
1156 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1158 dev = nfc_get_device(idx);
1159 if (!dev)
1160 return -ENODEV;
1162 device_lock(&dev->dev);
1164 if (dev->dep_link_up == false) {
1165 rc = -ENOLINK;
1166 goto exit;
1169 local = nfc_llcp_find_local(dev);
1170 if (!local) {
1171 nfc_put_device(dev);
1172 rc = -ENODEV;
1173 goto exit;
1176 INIT_HLIST_HEAD(&sdreq_list);
1178 tlvs_len = 0;
1180 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1181 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1182 nfc_sdp_genl_policy, info->extack);
1184 if (rc != 0) {
1185 rc = -EINVAL;
1186 goto exit;
1189 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1190 continue;
1192 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1193 if (uri_len == 0)
1194 continue;
1196 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1197 if (uri == NULL || *uri == 0)
1198 continue;
1200 tid = local->sdreq_next_tid++;
1202 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1203 if (sdreq == NULL) {
1204 rc = -ENOMEM;
1205 goto exit;
1208 tlvs_len += sdreq->tlv_len;
1210 hlist_add_head(&sdreq->node, &sdreq_list);
1213 if (hlist_empty(&sdreq_list)) {
1214 rc = -EINVAL;
1215 goto exit;
1218 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1219 exit:
1220 device_unlock(&dev->dev);
1222 nfc_put_device(dev);
1224 return rc;
1227 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1229 struct nfc_dev *dev;
1230 int rc;
1231 u32 idx;
1232 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1234 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1235 return -EINVAL;
1237 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1239 dev = nfc_get_device(idx);
1240 if (!dev)
1241 return -ENODEV;
1243 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1244 sizeof(firmware_name));
1246 rc = nfc_fw_download(dev, firmware_name);
1248 nfc_put_device(dev);
1249 return rc;
1252 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1253 u32 result)
1255 struct sk_buff *msg;
1256 void *hdr;
1258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1259 if (!msg)
1260 return -ENOMEM;
1262 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1263 NFC_CMD_FW_DOWNLOAD);
1264 if (!hdr)
1265 goto free_msg;
1267 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1268 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1269 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1270 goto nla_put_failure;
1272 genlmsg_end(msg, hdr);
1274 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1276 return 0;
1278 nla_put_failure:
1279 free_msg:
1280 nlmsg_free(msg);
1281 return -EMSGSIZE;
1284 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1286 struct nfc_dev *dev;
1287 int rc;
1288 u32 idx, se_idx;
1290 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1291 !info->attrs[NFC_ATTR_SE_INDEX])
1292 return -EINVAL;
1294 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1295 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1297 dev = nfc_get_device(idx);
1298 if (!dev)
1299 return -ENODEV;
1301 rc = nfc_enable_se(dev, se_idx);
1303 nfc_put_device(dev);
1304 return rc;
1307 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1309 struct nfc_dev *dev;
1310 int rc;
1311 u32 idx, se_idx;
1313 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1314 !info->attrs[NFC_ATTR_SE_INDEX])
1315 return -EINVAL;
1317 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1318 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1320 dev = nfc_get_device(idx);
1321 if (!dev)
1322 return -ENODEV;
1324 rc = nfc_disable_se(dev, se_idx);
1326 nfc_put_device(dev);
1327 return rc;
1330 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1331 u32 portid, u32 seq,
1332 struct netlink_callback *cb,
1333 int flags)
1335 void *hdr;
1336 struct nfc_se *se, *n;
1338 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1339 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1340 NFC_CMD_GET_SE);
1341 if (!hdr)
1342 goto nla_put_failure;
1344 if (cb)
1345 genl_dump_check_consistent(cb, hdr);
1347 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1348 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1349 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1350 goto nla_put_failure;
1352 genlmsg_end(msg, hdr);
1355 return 0;
1357 nla_put_failure:
1358 genlmsg_cancel(msg, hdr);
1359 return -EMSGSIZE;
1362 static int nfc_genl_dump_ses(struct sk_buff *skb,
1363 struct netlink_callback *cb)
1365 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1366 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1367 bool first_call = false;
1369 if (!iter) {
1370 first_call = true;
1371 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1372 if (!iter)
1373 return -ENOMEM;
1374 cb->args[0] = (long) iter;
1377 mutex_lock(&nfc_devlist_mutex);
1379 cb->seq = nfc_devlist_generation;
1381 if (first_call) {
1382 nfc_device_iter_init(iter);
1383 dev = nfc_device_iter_next(iter);
1386 while (dev) {
1387 int rc;
1389 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1390 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1391 if (rc < 0)
1392 break;
1394 dev = nfc_device_iter_next(iter);
1397 mutex_unlock(&nfc_devlist_mutex);
1399 cb->args[1] = (long) dev;
1401 return skb->len;
1404 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1406 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1408 nfc_device_iter_exit(iter);
1409 kfree(iter);
1411 return 0;
1414 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1415 u8 *apdu, size_t apdu_length,
1416 se_io_cb_t cb, void *cb_context)
1418 struct nfc_se *se;
1419 int rc;
1421 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1423 device_lock(&dev->dev);
1425 if (!device_is_registered(&dev->dev)) {
1426 rc = -ENODEV;
1427 goto error;
1430 if (!dev->dev_up) {
1431 rc = -ENODEV;
1432 goto error;
1435 if (!dev->ops->se_io) {
1436 rc = -EOPNOTSUPP;
1437 goto error;
1440 se = nfc_find_se(dev, se_idx);
1441 if (!se) {
1442 rc = -EINVAL;
1443 goto error;
1446 if (se->state != NFC_SE_ENABLED) {
1447 rc = -ENODEV;
1448 goto error;
1451 rc = dev->ops->se_io(dev, se_idx, apdu,
1452 apdu_length, cb, cb_context);
1454 error:
1455 device_unlock(&dev->dev);
1456 return rc;
1459 struct se_io_ctx {
1460 u32 dev_idx;
1461 u32 se_idx;
1464 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1466 struct se_io_ctx *ctx = context;
1467 struct sk_buff *msg;
1468 void *hdr;
1470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1471 if (!msg) {
1472 kfree(ctx);
1473 return;
1476 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1477 NFC_CMD_SE_IO);
1478 if (!hdr)
1479 goto free_msg;
1481 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1482 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1483 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1484 goto nla_put_failure;
1486 genlmsg_end(msg, hdr);
1488 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1490 kfree(ctx);
1492 return;
1494 nla_put_failure:
1495 free_msg:
1496 nlmsg_free(msg);
1497 kfree(ctx);
1499 return;
1502 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1504 struct nfc_dev *dev;
1505 struct se_io_ctx *ctx;
1506 u32 dev_idx, se_idx;
1507 u8 *apdu;
1508 size_t apdu_len;
1510 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1511 !info->attrs[NFC_ATTR_SE_INDEX] ||
1512 !info->attrs[NFC_ATTR_SE_APDU])
1513 return -EINVAL;
1515 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1516 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1518 dev = nfc_get_device(dev_idx);
1519 if (!dev)
1520 return -ENODEV;
1522 if (!dev->ops || !dev->ops->se_io)
1523 return -ENOTSUPP;
1525 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1526 if (apdu_len == 0)
1527 return -EINVAL;
1529 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1530 if (!apdu)
1531 return -EINVAL;
1533 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1534 if (!ctx)
1535 return -ENOMEM;
1537 ctx->dev_idx = dev_idx;
1538 ctx->se_idx = se_idx;
1540 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1543 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1544 struct genl_info *info)
1546 struct nfc_dev *dev;
1547 struct nfc_vendor_cmd *cmd;
1548 u32 dev_idx, vid, subcmd;
1549 u8 *data;
1550 size_t data_len;
1551 int i, err;
1553 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1554 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1555 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1556 return -EINVAL;
1558 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1559 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1560 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1562 dev = nfc_get_device(dev_idx);
1563 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1564 return -ENODEV;
1566 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1567 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1568 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1569 if (data_len == 0)
1570 return -EINVAL;
1571 } else {
1572 data = NULL;
1573 data_len = 0;
1576 for (i = 0; i < dev->n_vendor_cmds; i++) {
1577 cmd = &dev->vendor_cmds[i];
1579 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1580 continue;
1582 dev->cur_cmd_info = info;
1583 err = cmd->doit(dev, data, data_len);
1584 dev->cur_cmd_info = NULL;
1585 return err;
1588 return -EOPNOTSUPP;
1591 /* message building helper */
1592 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1593 int flags, u8 cmd)
1595 /* since there is no private header just add the generic one */
1596 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1599 static struct sk_buff *
1600 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1601 u32 portid, u32 seq,
1602 enum nfc_attrs attr,
1603 u32 oui, u32 subcmd, gfp_t gfp)
1605 struct sk_buff *skb;
1606 void *hdr;
1608 skb = nlmsg_new(approxlen + 100, gfp);
1609 if (!skb)
1610 return NULL;
1612 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1613 if (!hdr) {
1614 kfree_skb(skb);
1615 return NULL;
1618 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1619 goto nla_put_failure;
1620 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1621 goto nla_put_failure;
1622 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1623 goto nla_put_failure;
1625 ((void **)skb->cb)[0] = dev;
1626 ((void **)skb->cb)[1] = hdr;
1628 return skb;
1630 nla_put_failure:
1631 kfree_skb(skb);
1632 return NULL;
1635 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1636 enum nfc_attrs attr,
1637 u32 oui, u32 subcmd,
1638 int approxlen)
1640 if (WARN_ON(!dev->cur_cmd_info))
1641 return NULL;
1643 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1644 dev->cur_cmd_info->snd_portid,
1645 dev->cur_cmd_info->snd_seq, attr,
1646 oui, subcmd, GFP_KERNEL);
1648 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1650 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1652 struct nfc_dev *dev = ((void **)skb->cb)[0];
1653 void *hdr = ((void **)skb->cb)[1];
1655 /* clear CB data for netlink core to own from now on */
1656 memset(skb->cb, 0, sizeof(skb->cb));
1658 if (WARN_ON(!dev->cur_cmd_info)) {
1659 kfree_skb(skb);
1660 return -EINVAL;
1663 genlmsg_end(skb, hdr);
1664 return genlmsg_reply(skb, dev->cur_cmd_info);
1666 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1668 static const struct genl_ops nfc_genl_ops[] = {
1670 .cmd = NFC_CMD_GET_DEVICE,
1671 .doit = nfc_genl_get_device,
1672 .dumpit = nfc_genl_dump_devices,
1673 .done = nfc_genl_dump_devices_done,
1674 .policy = nfc_genl_policy,
1677 .cmd = NFC_CMD_DEV_UP,
1678 .doit = nfc_genl_dev_up,
1679 .policy = nfc_genl_policy,
1682 .cmd = NFC_CMD_DEV_DOWN,
1683 .doit = nfc_genl_dev_down,
1684 .policy = nfc_genl_policy,
1687 .cmd = NFC_CMD_START_POLL,
1688 .doit = nfc_genl_start_poll,
1689 .policy = nfc_genl_policy,
1692 .cmd = NFC_CMD_STOP_POLL,
1693 .doit = nfc_genl_stop_poll,
1694 .policy = nfc_genl_policy,
1697 .cmd = NFC_CMD_DEP_LINK_UP,
1698 .doit = nfc_genl_dep_link_up,
1699 .policy = nfc_genl_policy,
1702 .cmd = NFC_CMD_DEP_LINK_DOWN,
1703 .doit = nfc_genl_dep_link_down,
1704 .policy = nfc_genl_policy,
1707 .cmd = NFC_CMD_GET_TARGET,
1708 .dumpit = nfc_genl_dump_targets,
1709 .done = nfc_genl_dump_targets_done,
1710 .policy = nfc_genl_policy,
1713 .cmd = NFC_CMD_LLC_GET_PARAMS,
1714 .doit = nfc_genl_llc_get_params,
1715 .policy = nfc_genl_policy,
1718 .cmd = NFC_CMD_LLC_SET_PARAMS,
1719 .doit = nfc_genl_llc_set_params,
1720 .policy = nfc_genl_policy,
1723 .cmd = NFC_CMD_LLC_SDREQ,
1724 .doit = nfc_genl_llc_sdreq,
1725 .policy = nfc_genl_policy,
1728 .cmd = NFC_CMD_FW_DOWNLOAD,
1729 .doit = nfc_genl_fw_download,
1730 .policy = nfc_genl_policy,
1733 .cmd = NFC_CMD_ENABLE_SE,
1734 .doit = nfc_genl_enable_se,
1735 .policy = nfc_genl_policy,
1738 .cmd = NFC_CMD_DISABLE_SE,
1739 .doit = nfc_genl_disable_se,
1740 .policy = nfc_genl_policy,
1743 .cmd = NFC_CMD_GET_SE,
1744 .dumpit = nfc_genl_dump_ses,
1745 .done = nfc_genl_dump_ses_done,
1746 .policy = nfc_genl_policy,
1749 .cmd = NFC_CMD_SE_IO,
1750 .doit = nfc_genl_se_io,
1751 .policy = nfc_genl_policy,
1754 .cmd = NFC_CMD_ACTIVATE_TARGET,
1755 .doit = nfc_genl_activate_target,
1756 .policy = nfc_genl_policy,
1759 .cmd = NFC_CMD_VENDOR,
1760 .doit = nfc_genl_vendor_cmd,
1761 .policy = nfc_genl_policy,
1764 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1765 .doit = nfc_genl_deactivate_target,
1766 .policy = nfc_genl_policy,
1770 static struct genl_family nfc_genl_family __ro_after_init = {
1771 .hdrsize = 0,
1772 .name = NFC_GENL_NAME,
1773 .version = NFC_GENL_VERSION,
1774 .maxattr = NFC_ATTR_MAX,
1775 .module = THIS_MODULE,
1776 .ops = nfc_genl_ops,
1777 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1778 .mcgrps = nfc_genl_mcgrps,
1779 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1783 struct urelease_work {
1784 struct work_struct w;
1785 u32 portid;
1788 static void nfc_urelease_event_work(struct work_struct *work)
1790 struct urelease_work *w = container_of(work, struct urelease_work, w);
1791 struct class_dev_iter iter;
1792 struct nfc_dev *dev;
1794 pr_debug("portid %d\n", w->portid);
1796 mutex_lock(&nfc_devlist_mutex);
1798 nfc_device_iter_init(&iter);
1799 dev = nfc_device_iter_next(&iter);
1801 while (dev) {
1802 mutex_lock(&dev->genl_data.genl_data_mutex);
1804 if (dev->genl_data.poll_req_portid == w->portid) {
1805 nfc_stop_poll(dev);
1806 dev->genl_data.poll_req_portid = 0;
1809 mutex_unlock(&dev->genl_data.genl_data_mutex);
1811 dev = nfc_device_iter_next(&iter);
1814 nfc_device_iter_exit(&iter);
1816 mutex_unlock(&nfc_devlist_mutex);
1818 kfree(w);
1821 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1822 unsigned long event, void *ptr)
1824 struct netlink_notify *n = ptr;
1825 struct urelease_work *w;
1827 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1828 goto out;
1830 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1832 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1833 if (w) {
1834 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1835 w->portid = n->portid;
1836 schedule_work((struct work_struct *) w);
1839 out:
1840 return NOTIFY_DONE;
1843 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1845 genl_data->poll_req_portid = 0;
1846 mutex_init(&genl_data->genl_data_mutex);
1849 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1851 mutex_destroy(&genl_data->genl_data_mutex);
1854 static struct notifier_block nl_notifier = {
1855 .notifier_call = nfc_genl_rcv_nl_event,
1859 * nfc_genl_init() - Initialize netlink interface
1861 * This initialization function registers the nfc netlink family.
1863 int __init nfc_genl_init(void)
1865 int rc;
1867 rc = genl_register_family(&nfc_genl_family);
1868 if (rc)
1869 return rc;
1871 netlink_register_notifier(&nl_notifier);
1873 return 0;
1877 * nfc_genl_exit() - Deinitialize netlink interface
1879 * This exit function unregisters the nfc netlink family.
1881 void nfc_genl_exit(void)
1883 netlink_unregister_notifier(&nl_notifier);
1884 genl_unregister_family(&nfc_genl_family);