ARM: rockchip: fix broken build
[linux/fpc-iii.git] / net / nfc / netlink.c
blobf85f37ed19b23d0b9509e243d2c56b3070c6cbb6
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 },
68 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
69 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
70 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
73 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
74 struct netlink_callback *cb, int flags)
76 void *hdr;
78 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
79 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
80 if (!hdr)
81 return -EMSGSIZE;
83 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
85 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
86 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
87 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
88 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
89 goto nla_put_failure;
90 if (target->nfcid1_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
92 target->nfcid1))
93 goto nla_put_failure;
94 if (target->sensb_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
96 target->sensb_res))
97 goto nla_put_failure;
98 if (target->sensf_res_len > 0 &&
99 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
100 target->sensf_res))
101 goto nla_put_failure;
103 if (target->is_iso15693) {
104 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
105 target->iso15693_dsfid) ||
106 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
107 sizeof(target->iso15693_uid), target->iso15693_uid))
108 goto nla_put_failure;
111 genlmsg_end(msg, hdr);
112 return 0;
114 nla_put_failure:
115 genlmsg_cancel(msg, hdr);
116 return -EMSGSIZE;
119 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
121 struct nfc_dev *dev;
122 int rc;
123 u32 idx;
125 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
126 nfc_genl_family.attrbuf,
127 nfc_genl_family.maxattr,
128 nfc_genl_policy);
129 if (rc < 0)
130 return ERR_PTR(rc);
132 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
133 return ERR_PTR(-EINVAL);
135 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
137 dev = nfc_get_device(idx);
138 if (!dev)
139 return ERR_PTR(-ENODEV);
141 return dev;
144 static int nfc_genl_dump_targets(struct sk_buff *skb,
145 struct netlink_callback *cb)
147 int i = cb->args[0];
148 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
149 int rc;
151 if (!dev) {
152 dev = __get_device_from_cb(cb);
153 if (IS_ERR(dev))
154 return PTR_ERR(dev);
156 cb->args[1] = (long) dev;
159 device_lock(&dev->dev);
161 cb->seq = dev->targets_generation;
163 while (i < dev->n_targets) {
164 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
165 NLM_F_MULTI);
166 if (rc < 0)
167 break;
169 i++;
172 device_unlock(&dev->dev);
174 cb->args[0] = i;
176 return skb->len;
179 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
181 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
183 if (dev)
184 nfc_put_device(dev);
186 return 0;
189 int nfc_genl_targets_found(struct nfc_dev *dev)
191 struct sk_buff *msg;
192 void *hdr;
194 dev->genl_data.poll_req_portid = 0;
196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
197 if (!msg)
198 return -ENOMEM;
200 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
201 NFC_EVENT_TARGETS_FOUND);
202 if (!hdr)
203 goto free_msg;
205 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
206 goto nla_put_failure;
208 genlmsg_end(msg, hdr);
210 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
212 nla_put_failure:
213 genlmsg_cancel(msg, hdr);
214 free_msg:
215 nlmsg_free(msg);
216 return -EMSGSIZE;
219 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
221 struct sk_buff *msg;
222 void *hdr;
224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
225 if (!msg)
226 return -ENOMEM;
228 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
229 NFC_EVENT_TARGET_LOST);
230 if (!hdr)
231 goto free_msg;
233 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
234 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
235 goto nla_put_failure;
237 genlmsg_end(msg, hdr);
239 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
241 return 0;
243 nla_put_failure:
244 genlmsg_cancel(msg, hdr);
245 free_msg:
246 nlmsg_free(msg);
247 return -EMSGSIZE;
250 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
252 struct sk_buff *msg;
253 void *hdr;
255 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
256 if (!msg)
257 return -ENOMEM;
259 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
260 NFC_EVENT_TM_ACTIVATED);
261 if (!hdr)
262 goto free_msg;
264 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
265 goto nla_put_failure;
266 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
267 goto nla_put_failure;
269 genlmsg_end(msg, hdr);
271 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
273 return 0;
275 nla_put_failure:
276 genlmsg_cancel(msg, hdr);
277 free_msg:
278 nlmsg_free(msg);
279 return -EMSGSIZE;
282 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
284 struct sk_buff *msg;
285 void *hdr;
287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
288 if (!msg)
289 return -ENOMEM;
291 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
292 NFC_EVENT_TM_DEACTIVATED);
293 if (!hdr)
294 goto free_msg;
296 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
297 goto nla_put_failure;
299 genlmsg_end(msg, hdr);
301 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
303 return 0;
305 nla_put_failure:
306 genlmsg_cancel(msg, hdr);
307 free_msg:
308 nlmsg_free(msg);
309 return -EMSGSIZE;
312 int nfc_genl_device_added(struct nfc_dev *dev)
314 struct sk_buff *msg;
315 void *hdr;
317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
318 if (!msg)
319 return -ENOMEM;
321 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
322 NFC_EVENT_DEVICE_ADDED);
323 if (!hdr)
324 goto free_msg;
326 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
327 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
328 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
329 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
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 genlmsg_cancel(msg, hdr);
340 free_msg:
341 nlmsg_free(msg);
342 return -EMSGSIZE;
345 int nfc_genl_device_removed(struct nfc_dev *dev)
347 struct sk_buff *msg;
348 void *hdr;
350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
351 if (!msg)
352 return -ENOMEM;
354 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
355 NFC_EVENT_DEVICE_REMOVED);
356 if (!hdr)
357 goto free_msg;
359 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
360 goto nla_put_failure;
362 genlmsg_end(msg, hdr);
364 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
366 return 0;
368 nla_put_failure:
369 genlmsg_cancel(msg, hdr);
370 free_msg:
371 nlmsg_free(msg);
372 return -EMSGSIZE;
375 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
377 struct sk_buff *msg;
378 struct nlattr *sdp_attr, *uri_attr;
379 struct nfc_llcp_sdp_tlv *sdres;
380 struct hlist_node *n;
381 void *hdr;
382 int rc = -EMSGSIZE;
383 int i;
385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
386 if (!msg)
387 return -ENOMEM;
389 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
390 NFC_EVENT_LLC_SDRES);
391 if (!hdr)
392 goto free_msg;
394 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
395 goto nla_put_failure;
397 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
398 if (sdp_attr == NULL) {
399 rc = -ENOMEM;
400 goto nla_put_failure;
403 i = 1;
404 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
405 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
407 uri_attr = nla_nest_start(msg, i++);
408 if (uri_attr == NULL) {
409 rc = -ENOMEM;
410 goto nla_put_failure;
413 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
414 goto nla_put_failure;
416 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
417 goto nla_put_failure;
419 nla_nest_end(msg, uri_attr);
421 hlist_del(&sdres->node);
423 nfc_llcp_free_sdp_tlv(sdres);
426 nla_nest_end(msg, sdp_attr);
428 genlmsg_end(msg, hdr);
430 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
432 nla_put_failure:
433 genlmsg_cancel(msg, hdr);
435 free_msg:
436 nlmsg_free(msg);
438 nfc_llcp_free_sdp_tlv_list(sdres_list);
440 return rc;
443 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
445 struct sk_buff *msg;
446 void *hdr;
448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
449 if (!msg)
450 return -ENOMEM;
452 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
453 NFC_EVENT_SE_ADDED);
454 if (!hdr)
455 goto free_msg;
457 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
458 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
459 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
460 goto nla_put_failure;
462 genlmsg_end(msg, hdr);
464 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
466 return 0;
468 nla_put_failure:
469 genlmsg_cancel(msg, hdr);
470 free_msg:
471 nlmsg_free(msg);
472 return -EMSGSIZE;
475 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
477 struct sk_buff *msg;
478 void *hdr;
480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
481 if (!msg)
482 return -ENOMEM;
484 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
485 NFC_EVENT_SE_REMOVED);
486 if (!hdr)
487 goto free_msg;
489 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
490 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
491 goto nla_put_failure;
493 genlmsg_end(msg, hdr);
495 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
497 return 0;
499 nla_put_failure:
500 genlmsg_cancel(msg, hdr);
501 free_msg:
502 nlmsg_free(msg);
503 return -EMSGSIZE;
506 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
507 struct nfc_evt_transaction *evt_transaction)
509 struct nfc_se *se;
510 struct sk_buff *msg;
511 void *hdr;
513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
514 if (!msg)
515 return -ENOMEM;
517 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
518 NFC_EVENT_SE_TRANSACTION);
519 if (!hdr)
520 goto free_msg;
522 se = nfc_find_se(dev, se_idx);
523 if (!se)
524 goto free_msg;
526 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
527 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
528 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
529 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
530 evt_transaction->aid) ||
531 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
532 evt_transaction->params))
533 goto nla_put_failure;
535 /* evt_transaction is no more used */
536 devm_kfree(&dev->dev, evt_transaction);
538 genlmsg_end(msg, hdr);
540 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
542 return 0;
544 nla_put_failure:
545 genlmsg_cancel(msg, hdr);
546 free_msg:
547 /* evt_transaction is no more used */
548 devm_kfree(&dev->dev, evt_transaction);
549 nlmsg_free(msg);
550 return -EMSGSIZE;
553 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
554 u32 portid, u32 seq,
555 struct netlink_callback *cb,
556 int flags)
558 void *hdr;
560 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
561 NFC_CMD_GET_DEVICE);
562 if (!hdr)
563 return -EMSGSIZE;
565 if (cb)
566 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
568 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
569 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
570 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
571 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
572 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
573 goto nla_put_failure;
575 genlmsg_end(msg, hdr);
576 return 0;
578 nla_put_failure:
579 genlmsg_cancel(msg, hdr);
580 return -EMSGSIZE;
583 static int nfc_genl_dump_devices(struct sk_buff *skb,
584 struct netlink_callback *cb)
586 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
587 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
588 bool first_call = false;
590 if (!iter) {
591 first_call = true;
592 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
593 if (!iter)
594 return -ENOMEM;
595 cb->args[0] = (long) iter;
598 mutex_lock(&nfc_devlist_mutex);
600 cb->seq = nfc_devlist_generation;
602 if (first_call) {
603 nfc_device_iter_init(iter);
604 dev = nfc_device_iter_next(iter);
607 while (dev) {
608 int rc;
610 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
611 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
612 if (rc < 0)
613 break;
615 dev = nfc_device_iter_next(iter);
618 mutex_unlock(&nfc_devlist_mutex);
620 cb->args[1] = (long) dev;
622 return skb->len;
625 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
627 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
629 nfc_device_iter_exit(iter);
630 kfree(iter);
632 return 0;
635 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
636 u8 comm_mode, u8 rf_mode)
638 struct sk_buff *msg;
639 void *hdr;
641 pr_debug("DEP link is up\n");
643 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
644 if (!msg)
645 return -ENOMEM;
647 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
648 if (!hdr)
649 goto free_msg;
651 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
652 goto nla_put_failure;
653 if (rf_mode == NFC_RF_INITIATOR &&
654 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
655 goto nla_put_failure;
656 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
657 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
658 goto nla_put_failure;
660 genlmsg_end(msg, hdr);
662 dev->dep_link_up = true;
664 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
666 return 0;
668 nla_put_failure:
669 genlmsg_cancel(msg, hdr);
670 free_msg:
671 nlmsg_free(msg);
672 return -EMSGSIZE;
675 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
677 struct sk_buff *msg;
678 void *hdr;
680 pr_debug("DEP link is down\n");
682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
683 if (!msg)
684 return -ENOMEM;
686 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
687 NFC_CMD_DEP_LINK_DOWN);
688 if (!hdr)
689 goto free_msg;
691 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
692 goto nla_put_failure;
694 genlmsg_end(msg, hdr);
696 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
698 return 0;
700 nla_put_failure:
701 genlmsg_cancel(msg, hdr);
702 free_msg:
703 nlmsg_free(msg);
704 return -EMSGSIZE;
707 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
709 struct sk_buff *msg;
710 struct nfc_dev *dev;
711 u32 idx;
712 int rc = -ENOBUFS;
714 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
715 return -EINVAL;
717 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
719 dev = nfc_get_device(idx);
720 if (!dev)
721 return -ENODEV;
723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
724 if (!msg) {
725 rc = -ENOMEM;
726 goto out_putdev;
729 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
730 NULL, 0);
731 if (rc < 0)
732 goto out_free;
734 nfc_put_device(dev);
736 return genlmsg_reply(msg, info);
738 out_free:
739 nlmsg_free(msg);
740 out_putdev:
741 nfc_put_device(dev);
742 return rc;
745 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
747 struct nfc_dev *dev;
748 int rc;
749 u32 idx;
751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
752 return -EINVAL;
754 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
756 dev = nfc_get_device(idx);
757 if (!dev)
758 return -ENODEV;
760 rc = nfc_dev_up(dev);
762 nfc_put_device(dev);
763 return rc;
766 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
768 struct nfc_dev *dev;
769 int rc;
770 u32 idx;
772 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
773 return -EINVAL;
775 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
777 dev = nfc_get_device(idx);
778 if (!dev)
779 return -ENODEV;
781 rc = nfc_dev_down(dev);
783 nfc_put_device(dev);
784 return rc;
787 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
789 struct nfc_dev *dev;
790 int rc;
791 u32 idx;
792 u32 im_protocols = 0, tm_protocols = 0;
794 pr_debug("Poll start\n");
796 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
797 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
798 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
799 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
800 return -EINVAL;
802 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
804 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
805 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
807 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
808 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
809 else if (info->attrs[NFC_ATTR_PROTOCOLS])
810 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
812 dev = nfc_get_device(idx);
813 if (!dev)
814 return -ENODEV;
816 mutex_lock(&dev->genl_data.genl_data_mutex);
818 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
819 if (!rc)
820 dev->genl_data.poll_req_portid = info->snd_portid;
822 mutex_unlock(&dev->genl_data.genl_data_mutex);
824 nfc_put_device(dev);
825 return rc;
828 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
830 struct nfc_dev *dev;
831 int rc;
832 u32 idx;
834 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
835 return -EINVAL;
837 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
839 dev = nfc_get_device(idx);
840 if (!dev)
841 return -ENODEV;
843 device_lock(&dev->dev);
845 if (!dev->polling) {
846 device_unlock(&dev->dev);
847 return -EINVAL;
850 device_unlock(&dev->dev);
852 mutex_lock(&dev->genl_data.genl_data_mutex);
854 if (dev->genl_data.poll_req_portid != info->snd_portid) {
855 rc = -EBUSY;
856 goto out;
859 rc = nfc_stop_poll(dev);
860 dev->genl_data.poll_req_portid = 0;
862 out:
863 mutex_unlock(&dev->genl_data.genl_data_mutex);
864 nfc_put_device(dev);
865 return rc;
868 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
870 struct nfc_dev *dev;
871 u32 device_idx, target_idx, protocol;
872 int rc;
874 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
875 return -EINVAL;
877 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
879 dev = nfc_get_device(device_idx);
880 if (!dev)
881 return -ENODEV;
883 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
884 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
886 nfc_deactivate_target(dev, target_idx);
887 rc = nfc_activate_target(dev, target_idx, protocol);
889 nfc_put_device(dev);
890 return 0;
893 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
895 struct nfc_dev *dev;
896 int rc, tgt_idx;
897 u32 idx;
898 u8 comm;
900 pr_debug("DEP link up\n");
902 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
903 !info->attrs[NFC_ATTR_COMM_MODE])
904 return -EINVAL;
906 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
907 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
908 tgt_idx = NFC_TARGET_IDX_ANY;
909 else
910 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
912 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
914 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
915 return -EINVAL;
917 dev = nfc_get_device(idx);
918 if (!dev)
919 return -ENODEV;
921 rc = nfc_dep_link_up(dev, tgt_idx, comm);
923 nfc_put_device(dev);
925 return rc;
928 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
930 struct nfc_dev *dev;
931 int rc;
932 u32 idx;
934 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
935 return -EINVAL;
937 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
939 dev = nfc_get_device(idx);
940 if (!dev)
941 return -ENODEV;
943 rc = nfc_dep_link_down(dev);
945 nfc_put_device(dev);
946 return rc;
949 static int nfc_genl_send_params(struct sk_buff *msg,
950 struct nfc_llcp_local *local,
951 u32 portid, u32 seq)
953 void *hdr;
955 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
956 NFC_CMD_LLC_GET_PARAMS);
957 if (!hdr)
958 return -EMSGSIZE;
960 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
961 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
962 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
963 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
964 goto nla_put_failure;
966 genlmsg_end(msg, hdr);
967 return 0;
969 nla_put_failure:
971 genlmsg_cancel(msg, hdr);
972 return -EMSGSIZE;
975 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
977 struct nfc_dev *dev;
978 struct nfc_llcp_local *local;
979 int rc = 0;
980 struct sk_buff *msg = NULL;
981 u32 idx;
983 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
984 return -EINVAL;
986 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
988 dev = nfc_get_device(idx);
989 if (!dev)
990 return -ENODEV;
992 device_lock(&dev->dev);
994 local = nfc_llcp_find_local(dev);
995 if (!local) {
996 rc = -ENODEV;
997 goto exit;
1000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1001 if (!msg) {
1002 rc = -ENOMEM;
1003 goto exit;
1006 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1008 exit:
1009 device_unlock(&dev->dev);
1011 nfc_put_device(dev);
1013 if (rc < 0) {
1014 if (msg)
1015 nlmsg_free(msg);
1017 return rc;
1020 return genlmsg_reply(msg, info);
1023 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1025 struct nfc_dev *dev;
1026 struct nfc_llcp_local *local;
1027 u8 rw = 0;
1028 u16 miux = 0;
1029 u32 idx;
1030 int rc = 0;
1032 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1033 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1034 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1035 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1036 return -EINVAL;
1038 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1039 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1041 if (rw > LLCP_MAX_RW)
1042 return -EINVAL;
1045 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1046 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1048 if (miux > LLCP_MAX_MIUX)
1049 return -EINVAL;
1052 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1054 dev = nfc_get_device(idx);
1055 if (!dev)
1056 return -ENODEV;
1058 device_lock(&dev->dev);
1060 local = nfc_llcp_find_local(dev);
1061 if (!local) {
1062 nfc_put_device(dev);
1063 rc = -ENODEV;
1064 goto exit;
1067 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1068 if (dev->dep_link_up) {
1069 rc = -EINPROGRESS;
1070 goto exit;
1073 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1076 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1077 local->rw = rw;
1079 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1080 local->miux = cpu_to_be16(miux);
1082 exit:
1083 device_unlock(&dev->dev);
1085 nfc_put_device(dev);
1087 return rc;
1090 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1092 struct nfc_dev *dev;
1093 struct nfc_llcp_local *local;
1094 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1095 u32 idx;
1096 u8 tid;
1097 char *uri;
1098 int rc = 0, rem;
1099 size_t uri_len, tlvs_len;
1100 struct hlist_head sdreq_list;
1101 struct nfc_llcp_sdp_tlv *sdreq;
1103 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1104 !info->attrs[NFC_ATTR_LLC_SDP])
1105 return -EINVAL;
1107 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1109 dev = nfc_get_device(idx);
1110 if (!dev) {
1111 rc = -ENODEV;
1112 goto exit;
1115 device_lock(&dev->dev);
1117 if (dev->dep_link_up == false) {
1118 rc = -ENOLINK;
1119 goto exit;
1122 local = nfc_llcp_find_local(dev);
1123 if (!local) {
1124 nfc_put_device(dev);
1125 rc = -ENODEV;
1126 goto exit;
1129 INIT_HLIST_HEAD(&sdreq_list);
1131 tlvs_len = 0;
1133 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1134 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1135 nfc_sdp_genl_policy);
1137 if (rc != 0) {
1138 rc = -EINVAL;
1139 goto exit;
1142 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1143 continue;
1145 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1146 if (uri_len == 0)
1147 continue;
1149 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1150 if (uri == NULL || *uri == 0)
1151 continue;
1153 tid = local->sdreq_next_tid++;
1155 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1156 if (sdreq == NULL) {
1157 rc = -ENOMEM;
1158 goto exit;
1161 tlvs_len += sdreq->tlv_len;
1163 hlist_add_head(&sdreq->node, &sdreq_list);
1166 if (hlist_empty(&sdreq_list)) {
1167 rc = -EINVAL;
1168 goto exit;
1171 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1172 exit:
1173 device_unlock(&dev->dev);
1175 nfc_put_device(dev);
1177 return rc;
1180 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1182 struct nfc_dev *dev;
1183 int rc;
1184 u32 idx;
1185 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1187 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1188 return -EINVAL;
1190 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1192 dev = nfc_get_device(idx);
1193 if (!dev)
1194 return -ENODEV;
1196 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1197 sizeof(firmware_name));
1199 rc = nfc_fw_download(dev, firmware_name);
1201 nfc_put_device(dev);
1202 return rc;
1205 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1206 u32 result)
1208 struct sk_buff *msg;
1209 void *hdr;
1211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1212 if (!msg)
1213 return -ENOMEM;
1215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1216 NFC_CMD_FW_DOWNLOAD);
1217 if (!hdr)
1218 goto free_msg;
1220 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1221 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1222 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1223 goto nla_put_failure;
1225 genlmsg_end(msg, hdr);
1227 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1229 return 0;
1231 nla_put_failure:
1232 genlmsg_cancel(msg, hdr);
1233 free_msg:
1234 nlmsg_free(msg);
1235 return -EMSGSIZE;
1238 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1240 struct nfc_dev *dev;
1241 int rc;
1242 u32 idx, se_idx;
1244 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1245 !info->attrs[NFC_ATTR_SE_INDEX])
1246 return -EINVAL;
1248 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1249 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1251 dev = nfc_get_device(idx);
1252 if (!dev)
1253 return -ENODEV;
1255 rc = nfc_enable_se(dev, se_idx);
1257 nfc_put_device(dev);
1258 return rc;
1261 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1263 struct nfc_dev *dev;
1264 int rc;
1265 u32 idx, se_idx;
1267 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1268 !info->attrs[NFC_ATTR_SE_INDEX])
1269 return -EINVAL;
1271 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1272 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1274 dev = nfc_get_device(idx);
1275 if (!dev)
1276 return -ENODEV;
1278 rc = nfc_disable_se(dev, se_idx);
1280 nfc_put_device(dev);
1281 return rc;
1284 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1285 u32 portid, u32 seq,
1286 struct netlink_callback *cb,
1287 int flags)
1289 void *hdr;
1290 struct nfc_se *se, *n;
1292 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1293 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1294 NFC_CMD_GET_SE);
1295 if (!hdr)
1296 goto nla_put_failure;
1298 if (cb)
1299 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1301 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1302 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1303 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1304 goto nla_put_failure;
1306 genlmsg_end(msg, hdr);
1309 return 0;
1311 nla_put_failure:
1312 genlmsg_cancel(msg, hdr);
1313 return -EMSGSIZE;
1316 static int nfc_genl_dump_ses(struct sk_buff *skb,
1317 struct netlink_callback *cb)
1319 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1320 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1321 bool first_call = false;
1323 if (!iter) {
1324 first_call = true;
1325 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1326 if (!iter)
1327 return -ENOMEM;
1328 cb->args[0] = (long) iter;
1331 mutex_lock(&nfc_devlist_mutex);
1333 cb->seq = nfc_devlist_generation;
1335 if (first_call) {
1336 nfc_device_iter_init(iter);
1337 dev = nfc_device_iter_next(iter);
1340 while (dev) {
1341 int rc;
1343 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1344 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1345 if (rc < 0)
1346 break;
1348 dev = nfc_device_iter_next(iter);
1351 mutex_unlock(&nfc_devlist_mutex);
1353 cb->args[1] = (long) dev;
1355 return skb->len;
1358 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1360 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1362 nfc_device_iter_exit(iter);
1363 kfree(iter);
1365 return 0;
1368 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1369 u8 *apdu, size_t apdu_length,
1370 se_io_cb_t cb, void *cb_context)
1372 struct nfc_se *se;
1373 int rc;
1375 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1377 device_lock(&dev->dev);
1379 if (!device_is_registered(&dev->dev)) {
1380 rc = -ENODEV;
1381 goto error;
1384 if (!dev->dev_up) {
1385 rc = -ENODEV;
1386 goto error;
1389 if (!dev->ops->se_io) {
1390 rc = -EOPNOTSUPP;
1391 goto error;
1394 se = nfc_find_se(dev, se_idx);
1395 if (!se) {
1396 rc = -EINVAL;
1397 goto error;
1400 if (se->state != NFC_SE_ENABLED) {
1401 rc = -ENODEV;
1402 goto error;
1405 rc = dev->ops->se_io(dev, se_idx, apdu,
1406 apdu_length, cb, cb_context);
1408 error:
1409 device_unlock(&dev->dev);
1410 return rc;
1413 struct se_io_ctx {
1414 u32 dev_idx;
1415 u32 se_idx;
1418 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1420 struct se_io_ctx *ctx = context;
1421 struct sk_buff *msg;
1422 void *hdr;
1424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1425 if (!msg) {
1426 kfree(ctx);
1427 return;
1430 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1431 NFC_CMD_SE_IO);
1432 if (!hdr)
1433 goto free_msg;
1435 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1436 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1437 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1438 goto nla_put_failure;
1440 genlmsg_end(msg, hdr);
1442 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1444 kfree(ctx);
1446 return;
1448 nla_put_failure:
1449 genlmsg_cancel(msg, hdr);
1450 free_msg:
1451 nlmsg_free(msg);
1452 kfree(ctx);
1454 return;
1457 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1459 struct nfc_dev *dev;
1460 struct se_io_ctx *ctx;
1461 u32 dev_idx, se_idx;
1462 u8 *apdu;
1463 size_t apdu_len;
1465 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1466 !info->attrs[NFC_ATTR_SE_INDEX] ||
1467 !info->attrs[NFC_ATTR_SE_APDU])
1468 return -EINVAL;
1470 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1471 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1473 dev = nfc_get_device(dev_idx);
1474 if (!dev)
1475 return -ENODEV;
1477 if (!dev->ops || !dev->ops->se_io)
1478 return -ENOTSUPP;
1480 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1481 if (apdu_len == 0)
1482 return -EINVAL;
1484 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1485 if (!apdu)
1486 return -EINVAL;
1488 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1489 if (!ctx)
1490 return -ENOMEM;
1492 ctx->dev_idx = dev_idx;
1493 ctx->se_idx = se_idx;
1495 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1498 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1499 struct genl_info *info)
1501 struct nfc_dev *dev;
1502 struct nfc_vendor_cmd *cmd;
1503 u32 dev_idx, vid, subcmd;
1504 u8 *data;
1505 size_t data_len;
1506 int i;
1508 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1509 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1510 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1511 return -EINVAL;
1513 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1514 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1515 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1517 dev = nfc_get_device(dev_idx);
1518 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1519 return -ENODEV;
1521 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1522 if (data) {
1523 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1524 if (data_len == 0)
1525 return -EINVAL;
1526 } else {
1527 data_len = 0;
1530 for (i = 0; i < dev->n_vendor_cmds; i++) {
1531 cmd = &dev->vendor_cmds[i];
1533 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1534 continue;
1536 return cmd->doit(dev, data, data_len);
1539 return -EOPNOTSUPP;
1542 static const struct genl_ops nfc_genl_ops[] = {
1544 .cmd = NFC_CMD_GET_DEVICE,
1545 .doit = nfc_genl_get_device,
1546 .dumpit = nfc_genl_dump_devices,
1547 .done = nfc_genl_dump_devices_done,
1548 .policy = nfc_genl_policy,
1551 .cmd = NFC_CMD_DEV_UP,
1552 .doit = nfc_genl_dev_up,
1553 .policy = nfc_genl_policy,
1556 .cmd = NFC_CMD_DEV_DOWN,
1557 .doit = nfc_genl_dev_down,
1558 .policy = nfc_genl_policy,
1561 .cmd = NFC_CMD_START_POLL,
1562 .doit = nfc_genl_start_poll,
1563 .policy = nfc_genl_policy,
1566 .cmd = NFC_CMD_STOP_POLL,
1567 .doit = nfc_genl_stop_poll,
1568 .policy = nfc_genl_policy,
1571 .cmd = NFC_CMD_DEP_LINK_UP,
1572 .doit = nfc_genl_dep_link_up,
1573 .policy = nfc_genl_policy,
1576 .cmd = NFC_CMD_DEP_LINK_DOWN,
1577 .doit = nfc_genl_dep_link_down,
1578 .policy = nfc_genl_policy,
1581 .cmd = NFC_CMD_GET_TARGET,
1582 .dumpit = nfc_genl_dump_targets,
1583 .done = nfc_genl_dump_targets_done,
1584 .policy = nfc_genl_policy,
1587 .cmd = NFC_CMD_LLC_GET_PARAMS,
1588 .doit = nfc_genl_llc_get_params,
1589 .policy = nfc_genl_policy,
1592 .cmd = NFC_CMD_LLC_SET_PARAMS,
1593 .doit = nfc_genl_llc_set_params,
1594 .policy = nfc_genl_policy,
1597 .cmd = NFC_CMD_LLC_SDREQ,
1598 .doit = nfc_genl_llc_sdreq,
1599 .policy = nfc_genl_policy,
1602 .cmd = NFC_CMD_FW_DOWNLOAD,
1603 .doit = nfc_genl_fw_download,
1604 .policy = nfc_genl_policy,
1607 .cmd = NFC_CMD_ENABLE_SE,
1608 .doit = nfc_genl_enable_se,
1609 .policy = nfc_genl_policy,
1612 .cmd = NFC_CMD_DISABLE_SE,
1613 .doit = nfc_genl_disable_se,
1614 .policy = nfc_genl_policy,
1617 .cmd = NFC_CMD_GET_SE,
1618 .dumpit = nfc_genl_dump_ses,
1619 .done = nfc_genl_dump_ses_done,
1620 .policy = nfc_genl_policy,
1623 .cmd = NFC_CMD_SE_IO,
1624 .doit = nfc_genl_se_io,
1625 .policy = nfc_genl_policy,
1628 .cmd = NFC_CMD_ACTIVATE_TARGET,
1629 .doit = nfc_genl_activate_target,
1630 .policy = nfc_genl_policy,
1633 .cmd = NFC_CMD_VENDOR,
1634 .doit = nfc_genl_vendor_cmd,
1635 .policy = nfc_genl_policy,
1640 struct urelease_work {
1641 struct work_struct w;
1642 u32 portid;
1645 static void nfc_urelease_event_work(struct work_struct *work)
1647 struct urelease_work *w = container_of(work, struct urelease_work, w);
1648 struct class_dev_iter iter;
1649 struct nfc_dev *dev;
1651 pr_debug("portid %d\n", w->portid);
1653 mutex_lock(&nfc_devlist_mutex);
1655 nfc_device_iter_init(&iter);
1656 dev = nfc_device_iter_next(&iter);
1658 while (dev) {
1659 mutex_lock(&dev->genl_data.genl_data_mutex);
1661 if (dev->genl_data.poll_req_portid == w->portid) {
1662 nfc_stop_poll(dev);
1663 dev->genl_data.poll_req_portid = 0;
1666 mutex_unlock(&dev->genl_data.genl_data_mutex);
1668 dev = nfc_device_iter_next(&iter);
1671 nfc_device_iter_exit(&iter);
1673 mutex_unlock(&nfc_devlist_mutex);
1675 kfree(w);
1678 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1679 unsigned long event, void *ptr)
1681 struct netlink_notify *n = ptr;
1682 struct urelease_work *w;
1684 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1685 goto out;
1687 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1689 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1690 if (w) {
1691 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1692 w->portid = n->portid;
1693 schedule_work((struct work_struct *) w);
1696 out:
1697 return NOTIFY_DONE;
1700 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1702 genl_data->poll_req_portid = 0;
1703 mutex_init(&genl_data->genl_data_mutex);
1706 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1708 mutex_destroy(&genl_data->genl_data_mutex);
1711 static struct notifier_block nl_notifier = {
1712 .notifier_call = nfc_genl_rcv_nl_event,
1716 * nfc_genl_init() - Initialize netlink interface
1718 * This initialization function registers the nfc netlink family.
1720 int __init nfc_genl_init(void)
1722 int rc;
1724 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1725 nfc_genl_ops,
1726 nfc_genl_mcgrps);
1727 if (rc)
1728 return rc;
1730 netlink_register_notifier(&nl_notifier);
1732 return 0;
1736 * nfc_genl_exit() - Deinitialize netlink interface
1738 * This exit function unregisters the nfc netlink family.
1740 void nfc_genl_exit(void)
1742 netlink_unregister_notifier(&nl_notifier);
1743 genl_unregister_family(&nfc_genl_family);