linux/net/nfc/netlink.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
   3 *
   4 * Authors:
   5 *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
   6 *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the
  20 * Free Software Foundation, Inc.,
  21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22 */
  23
  24#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  25
  26#include <net/genetlink.h>
  27#include <linux/nfc.h>
  28#include <linux/slab.h>
  29
  30#include "nfc.h"
  31#include "llcp.h"
  32
  33static const struct genl_multicast_group nfc_genl_mcgrps[] = {
  34        { .name = NFC_GENL_MCAST_EVENT_NAME, },
  35};
  36
  37static struct genl_family nfc_genl_family = {
  38        .id = GENL_ID_GENERATE,
  39        .hdrsize = 0,
  40        .name = NFC_GENL_NAME,
  41        .version = NFC_GENL_VERSION,
  42        .maxattr = NFC_ATTR_MAX,
  43};
  44
  45static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  46        [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  47        [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  48                                .len = NFC_DEVICE_NAME_MAXSIZE },
  49        [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  50        [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  51        [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  52        [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  53        [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
  54        [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
  55        [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
  56        [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
  57        [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
  58        [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
  59        [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
  60                                     .len = NFC_FIRMWARE_NAME_MAXSIZE },
  61        [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
  62};
  63
  64static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
  65        [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
  66        [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
  67};
  68
  69static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  70                                struct netlink_callback *cb, int flags)
  71{
  72        void *hdr;
  73
  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;
  78
  79        genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  80
  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;
  98
  99        return genlmsg_end(msg, hdr);
 100
 101nla_put_failure:
 102        genlmsg_cancel(msg, hdr);
 103        return -EMSGSIZE;
 104}
 105
 106static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
 107{
 108        struct nfc_dev *dev;
 109        int rc;
 110        u32 idx;
 111
 112        rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
 113                         nfc_genl_family.attrbuf,
 114                         nfc_genl_family.maxattr,
 115                         nfc_genl_policy);
 116        if (rc < 0)
 117                return ERR_PTR(rc);
 118
 119        if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
 120                return ERR_PTR(-EINVAL);
 121
 122        idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
 123
 124        dev = nfc_get_device(idx);
 125        if (!dev)
 126                return ERR_PTR(-ENODEV);
 127
 128        return dev;
 129}
 130
 131static int nfc_genl_dump_targets(struct sk_buff *skb,
 132                                 struct netlink_callback *cb)
 133{
 134        int i = cb->args[0];
 135        struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
 136        int rc;
 137
 138        if (!dev) {
 139                dev = __get_device_from_cb(cb);
 140                if (IS_ERR(dev))
 141                        return PTR_ERR(dev);
 142
 143                cb->args[1] = (long) dev;
 144        }
 145
 146        device_lock(&dev->dev);
 147
 148        cb->seq = dev->targets_generation;
 149
 150        while (i < dev->n_targets) {
 151                rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
 152                                          NLM_F_MULTI);
 153                if (rc < 0)
 154                        break;
 155
 156                i++;
 157        }
 158
 159        device_unlock(&dev->dev);
 160
 161        cb->args[0] = i;
 162
 163        return skb->len;
 164}
 165
 166static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
 167{
 168        struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
 169
 170        if (dev)
 171                nfc_put_device(dev);
 172
 173        return 0;
 174}
 175
 176int nfc_genl_targets_found(struct nfc_dev *dev)
 177{
 178        struct sk_buff *msg;
 179        void *hdr;
 180
 181        dev->genl_data.poll_req_portid = 0;
 182
 183        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 184        if (!msg)
 185                return -ENOMEM;
 186
 187        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 188                          NFC_EVENT_TARGETS_FOUND);
 189        if (!hdr)
 190                goto free_msg;
 191
 192        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 193                goto nla_put_failure;
 194
 195        genlmsg_end(msg, hdr);
 196
 197        return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
 198
 199nla_put_failure:
 200        genlmsg_cancel(msg, hdr);
 201free_msg:
 202        nlmsg_free(msg);
 203        return -EMSGSIZE;
 204}
 205
 206int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
 207{
 208        struct sk_buff *msg;
 209        void *hdr;
 210
 211        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 212        if (!msg)
 213                return -ENOMEM;
 214
 215        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 216                          NFC_EVENT_TARGET_LOST);
 217        if (!hdr)
 218                goto free_msg;
 219
 220        if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
 221            nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
 222                goto nla_put_failure;
 223
 224        genlmsg_end(msg, hdr);
 225
 226        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 227
 228        return 0;
 229
 230nla_put_failure:
 231        genlmsg_cancel(msg, hdr);
 232free_msg:
 233        nlmsg_free(msg);
 234        return -EMSGSIZE;
 235}
 236
 237int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
 238{
 239        struct sk_buff *msg;
 240        void *hdr;
 241
 242        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 243        if (!msg)
 244                return -ENOMEM;
 245
 246        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 247                          NFC_EVENT_TM_ACTIVATED);
 248        if (!hdr)
 249                goto free_msg;
 250
 251        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 252                goto nla_put_failure;
 253        if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
 254                goto nla_put_failure;
 255
 256        genlmsg_end(msg, hdr);
 257
 258        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 259
 260        return 0;
 261
 262nla_put_failure:
 263        genlmsg_cancel(msg, hdr);
 264free_msg:
 265        nlmsg_free(msg);
 266        return -EMSGSIZE;
 267}
 268
 269int nfc_genl_tm_deactivated(struct nfc_dev *dev)
 270{
 271        struct sk_buff *msg;
 272        void *hdr;
 273
 274        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 275        if (!msg)
 276                return -ENOMEM;
 277
 278        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 279                          NFC_EVENT_TM_DEACTIVATED);
 280        if (!hdr)
 281                goto free_msg;
 282
 283        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 284                goto nla_put_failure;
 285
 286        genlmsg_end(msg, hdr);
 287
 288        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 289
 290        return 0;
 291
 292nla_put_failure:
 293        genlmsg_cancel(msg, hdr);
 294free_msg:
 295        nlmsg_free(msg);
 296        return -EMSGSIZE;
 297}
 298
 299int nfc_genl_device_added(struct nfc_dev *dev)
 300{
 301        struct sk_buff *msg;
 302        void *hdr;
 303
 304        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 305        if (!msg)
 306                return -ENOMEM;
 307
 308        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 309                          NFC_EVENT_DEVICE_ADDED);
 310        if (!hdr)
 311                goto free_msg;
 312
 313        if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
 314            nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
 315            nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
 316            nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
 317                goto nla_put_failure;
 318
 319        genlmsg_end(msg, hdr);
 320
 321        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 322
 323        return 0;
 324
 325nla_put_failure:
 326        genlmsg_cancel(msg, hdr);
 327free_msg:
 328        nlmsg_free(msg);
 329        return -EMSGSIZE;
 330}
 331
 332int nfc_genl_device_removed(struct nfc_dev *dev)
 333{
 334        struct sk_buff *msg;
 335        void *hdr;
 336
 337        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 338        if (!msg)
 339                return -ENOMEM;
 340
 341        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 342                          NFC_EVENT_DEVICE_REMOVED);
 343        if (!hdr)
 344                goto free_msg;
 345
 346        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 347                goto nla_put_failure;
 348
 349        genlmsg_end(msg, hdr);
 350
 351        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 352
 353        return 0;
 354
 355nla_put_failure:
 356        genlmsg_cancel(msg, hdr);
 357free_msg:
 358        nlmsg_free(msg);
 359        return -EMSGSIZE;
 360}
 361
 362int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
 363{
 364        struct sk_buff *msg;
 365        struct nlattr *sdp_attr, *uri_attr;
 366        struct nfc_llcp_sdp_tlv *sdres;
 367        struct hlist_node *n;
 368        void *hdr;
 369        int rc = -EMSGSIZE;
 370        int i;
 371
 372        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 373        if (!msg)
 374                return -ENOMEM;
 375
 376        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 377                          NFC_EVENT_LLC_SDRES);
 378        if (!hdr)
 379                goto free_msg;
 380
 381        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 382                goto nla_put_failure;
 383
 384        sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
 385        if (sdp_attr == NULL) {
 386                rc = -ENOMEM;
 387                goto nla_put_failure;
 388        }
 389
 390        i = 1;
 391        hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
 392                pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
 393
 394                uri_attr = nla_nest_start(msg, i++);
 395                if (uri_attr == NULL) {
 396                        rc = -ENOMEM;
 397                        goto nla_put_failure;
 398                }
 399
 400                if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
 401                        goto nla_put_failure;
 402
 403                if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
 404                        goto nla_put_failure;
 405
 406                nla_nest_end(msg, uri_attr);
 407
 408                hlist_del(&sdres->node);
 409
 410                nfc_llcp_free_sdp_tlv(sdres);
 411        }
 412
 413        nla_nest_end(msg, sdp_attr);
 414
 415        genlmsg_end(msg, hdr);
 416
 417        return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
 418
 419nla_put_failure:
 420        genlmsg_cancel(msg, hdr);
 421
 422free_msg:
 423        nlmsg_free(msg);
 424
 425        nfc_llcp_free_sdp_tlv_list(sdres_list);
 426
 427        return rc;
 428}
 429
 430int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
 431{
 432        struct sk_buff *msg;
 433        void *hdr;
 434
 435        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 436        if (!msg)
 437                return -ENOMEM;
 438
 439        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 440                          NFC_EVENT_SE_ADDED);
 441        if (!hdr)
 442                goto free_msg;
 443
 444        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
 445            nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
 446            nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
 447                goto nla_put_failure;
 448
 449        genlmsg_end(msg, hdr);
 450
 451        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 452
 453        return 0;
 454
 455nla_put_failure:
 456        genlmsg_cancel(msg, hdr);
 457free_msg:
 458        nlmsg_free(msg);
 459        return -EMSGSIZE;
 460}
 461
 462int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
 463{
 464        struct sk_buff *msg;
 465        void *hdr;
 466
 467        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 468        if (!msg)
 469                return -ENOMEM;
 470
 471        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 472                          NFC_EVENT_SE_REMOVED);
 473        if (!hdr)
 474                goto free_msg;
 475
 476        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
 477            nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
 478                goto nla_put_failure;
 479
 480        genlmsg_end(msg, hdr);
 481
 482        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
 483
 484        return 0;
 485
 486nla_put_failure:
 487        genlmsg_cancel(msg, hdr);
 488free_msg:
 489        nlmsg_free(msg);
 490        return -EMSGSIZE;
 491}
 492
 493static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
 494                                u32 portid, u32 seq,
 495                                struct netlink_callback *cb,
 496                                int flags)
 497{
 498        void *hdr;
 499
 500        hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
 501                          NFC_CMD_GET_DEVICE);
 502        if (!hdr)
 503                return -EMSGSIZE;
 504
 505        if (cb)
 506                genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
 507
 508        if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
 509            nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
 510            nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
 511            nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
 512            nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
 513                goto nla_put_failure;
 514
 515        return genlmsg_end(msg, hdr);
 516
 517nla_put_failure:
 518        genlmsg_cancel(msg, hdr);
 519        return -EMSGSIZE;
 520}
 521
 522static int nfc_genl_dump_devices(struct sk_buff *skb,
 523                                 struct netlink_callback *cb)
 524{
 525        struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
 526        struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
 527        bool first_call = false;
 528
 529        if (!iter) {
 530                first_call = true;
 531                iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
 532                if (!iter)
 533                        return -ENOMEM;
 534                cb->args[0] = (long) iter;
 535        }
 536
 537        mutex_lock(&nfc_devlist_mutex);
 538
 539        cb->seq = nfc_devlist_generation;
 540
 541        if (first_call) {
 542                nfc_device_iter_init(iter);
 543                dev = nfc_device_iter_next(iter);
 544        }
 545
 546        while (dev) {
 547                int rc;
 548
 549                rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
 550                                          cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
 551                if (rc < 0)
 552                        break;
 553
 554                dev = nfc_device_iter_next(iter);
 555        }
 556
 557        mutex_unlock(&nfc_devlist_mutex);
 558
 559        cb->args[1] = (long) dev;
 560
 561        return skb->len;
 562}
 563
 564static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
 565{
 566        struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
 567
 568        nfc_device_iter_exit(iter);
 569        kfree(iter);
 570
 571        return 0;
 572}
 573
 574int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
 575                               u8 comm_mode, u8 rf_mode)
 576{
 577        struct sk_buff *msg;
 578        void *hdr;
 579
 580        pr_debug("DEP link is up\n");
 581
 582        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 583        if (!msg)
 584                return -ENOMEM;
 585
 586        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
 587        if (!hdr)
 588                goto free_msg;
 589
 590        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 591                goto nla_put_failure;
 592        if (rf_mode == NFC_RF_INITIATOR &&
 593            nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
 594                goto nla_put_failure;
 595        if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
 596            nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
 597                goto nla_put_failure;
 598
 599        genlmsg_end(msg, hdr);
 600
 601        dev->dep_link_up = true;
 602
 603        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
 604
 605        return 0;
 606
 607nla_put_failure:
 608        genlmsg_cancel(msg, hdr);
 609free_msg:
 610        nlmsg_free(msg);
 611        return -EMSGSIZE;
 612}
 613
 614int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
 615{
 616        struct sk_buff *msg;
 617        void *hdr;
 618
 619        pr_debug("DEP link is down\n");
 620
 621        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 622        if (!msg)
 623                return -ENOMEM;
 624
 625        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
 626                          NFC_CMD_DEP_LINK_DOWN);
 627        if (!hdr)
 628                goto free_msg;
 629
 630        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
 631                goto nla_put_failure;
 632
 633        genlmsg_end(msg, hdr);
 634
 635        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
 636
 637        return 0;
 638
 639nla_put_failure:
 640        genlmsg_cancel(msg, hdr);
 641free_msg:
 642        nlmsg_free(msg);
 643        return -EMSGSIZE;
 644}
 645
 646static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
 647{
 648        struct sk_buff *msg;
 649        struct nfc_dev *dev;
 650        u32 idx;
 651        int rc = -ENOBUFS;
 652
 653        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 654                return -EINVAL;
 655
 656        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 657
 658        dev = nfc_get_device(idx);
 659        if (!dev)
 660                return -ENODEV;
 661
 662        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 663        if (!msg) {
 664                rc = -ENOMEM;
 665                goto out_putdev;
 666        }
 667
 668        rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
 669                                  NULL, 0);
 670        if (rc < 0)
 671                goto out_free;
 672
 673        nfc_put_device(dev);
 674
 675        return genlmsg_reply(msg, info);
 676
 677out_free:
 678        nlmsg_free(msg);
 679out_putdev:
 680        nfc_put_device(dev);
 681        return rc;
 682}
 683
 684static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
 685{
 686        struct nfc_dev *dev;
 687        int rc;
 688        u32 idx;
 689
 690        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 691                return -EINVAL;
 692
 693        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 694
 695        dev = nfc_get_device(idx);
 696        if (!dev)
 697                return -ENODEV;
 698
 699        rc = nfc_dev_up(dev);
 700
 701        nfc_put_device(dev);
 702        return rc;
 703}
 704
 705static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
 706{
 707        struct nfc_dev *dev;
 708        int rc;
 709        u32 idx;
 710
 711        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 712                return -EINVAL;
 713
 714        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 715
 716        dev = nfc_get_device(idx);
 717        if (!dev)
 718                return -ENODEV;
 719
 720        rc = nfc_dev_down(dev);
 721
 722        nfc_put_device(dev);
 723        return rc;
 724}
 725
 726static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
 727{
 728        struct nfc_dev *dev;
 729        int rc;
 730        u32 idx;
 731        u32 im_protocols = 0, tm_protocols = 0;
 732
 733        pr_debug("Poll start\n");
 734
 735        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
 736            ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
 737              !info->attrs[NFC_ATTR_PROTOCOLS]) &&
 738              !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
 739                return -EINVAL;
 740
 741        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 742
 743        if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
 744                tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
 745
 746        if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
 747                im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
 748        else if (info->attrs[NFC_ATTR_PROTOCOLS])
 749                im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
 750
 751        dev = nfc_get_device(idx);
 752        if (!dev)
 753                return -ENODEV;
 754
 755        mutex_lock(&dev->genl_data.genl_data_mutex);
 756
 757        rc = nfc_start_poll(dev, im_protocols, tm_protocols);
 758        if (!rc)
 759                dev->genl_data.poll_req_portid = info->snd_portid;
 760
 761        mutex_unlock(&dev->genl_data.genl_data_mutex);
 762
 763        nfc_put_device(dev);
 764        return rc;
 765}
 766
 767static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
 768{
 769        struct nfc_dev *dev;
 770        int rc;
 771        u32 idx;
 772
 773        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 774                return -EINVAL;
 775
 776        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 777
 778        dev = nfc_get_device(idx);
 779        if (!dev)
 780                return -ENODEV;
 781
 782        device_lock(&dev->dev);
 783
 784        if (!dev->polling) {
 785                device_unlock(&dev->dev);
 786                return -EINVAL;
 787        }
 788
 789        device_unlock(&dev->dev);
 790
 791        mutex_lock(&dev->genl_data.genl_data_mutex);
 792
 793        if (dev->genl_data.poll_req_portid != info->snd_portid) {
 794                rc = -EBUSY;
 795                goto out;
 796        }
 797
 798        rc = nfc_stop_poll(dev);
 799        dev->genl_data.poll_req_portid = 0;
 800
 801out:
 802        mutex_unlock(&dev->genl_data.genl_data_mutex);
 803        nfc_put_device(dev);
 804        return rc;
 805}
 806
 807static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
 808{
 809        struct nfc_dev *dev;
 810        int rc, tgt_idx;
 811        u32 idx;
 812        u8 comm;
 813
 814        pr_debug("DEP link up\n");
 815
 816        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
 817            !info->attrs[NFC_ATTR_COMM_MODE])
 818                return -EINVAL;
 819
 820        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 821        if (!info->attrs[NFC_ATTR_TARGET_INDEX])
 822                tgt_idx = NFC_TARGET_IDX_ANY;
 823        else
 824                tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
 825
 826        comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
 827
 828        if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
 829                return -EINVAL;
 830
 831        dev = nfc_get_device(idx);
 832        if (!dev)
 833                return -ENODEV;
 834
 835        rc = nfc_dep_link_up(dev, tgt_idx, comm);
 836
 837        nfc_put_device(dev);
 838
 839        return rc;
 840}
 841
 842static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
 843{
 844        struct nfc_dev *dev;
 845        int rc;
 846        u32 idx;
 847
 848        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 849                return -EINVAL;
 850
 851        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 852
 853        dev = nfc_get_device(idx);
 854        if (!dev)
 855                return -ENODEV;
 856
 857        rc = nfc_dep_link_down(dev);
 858
 859        nfc_put_device(dev);
 860        return rc;
 861}
 862
 863static int nfc_genl_send_params(struct sk_buff *msg,
 864                                struct nfc_llcp_local *local,
 865                                u32 portid, u32 seq)
 866{
 867        void *hdr;
 868
 869        hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
 870                          NFC_CMD_LLC_GET_PARAMS);
 871        if (!hdr)
 872                return -EMSGSIZE;
 873
 874        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
 875            nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
 876            nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
 877            nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
 878                goto nla_put_failure;
 879
 880        return genlmsg_end(msg, hdr);
 881
 882nla_put_failure:
 883
 884        genlmsg_cancel(msg, hdr);
 885        return -EMSGSIZE;
 886}
 887
 888static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
 889{
 890        struct nfc_dev *dev;
 891        struct nfc_llcp_local *local;
 892        int rc = 0;
 893        struct sk_buff *msg = NULL;
 894        u32 idx;
 895
 896        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
 897                return -EINVAL;
 898
 899        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 900
 901        dev = nfc_get_device(idx);
 902        if (!dev)
 903                return -ENODEV;
 904
 905        device_lock(&dev->dev);
 906
 907        local = nfc_llcp_find_local(dev);
 908        if (!local) {
 909                rc = -ENODEV;
 910                goto exit;
 911        }
 912
 913        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 914        if (!msg) {
 915                rc = -ENOMEM;
 916                goto exit;
 917        }
 918
 919        rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
 920
 921exit:
 922        device_unlock(&dev->dev);
 923
 924        nfc_put_device(dev);
 925
 926        if (rc < 0) {
 927                if (msg)
 928                        nlmsg_free(msg);
 929
 930                return rc;
 931        }
 932
 933        return genlmsg_reply(msg, info);
 934}
 935
 936static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
 937{
 938        struct nfc_dev *dev;
 939        struct nfc_llcp_local *local;
 940        u8 rw = 0;
 941        u16 miux = 0;
 942        u32 idx;
 943        int rc = 0;
 944
 945        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
 946            (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
 947             !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
 948             !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
 949                return -EINVAL;
 950
 951        if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
 952                rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
 953
 954                if (rw > LLCP_MAX_RW)
 955                        return -EINVAL;
 956        }
 957
 958        if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
 959                miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
 960
 961                if (miux > LLCP_MAX_MIUX)
 962                        return -EINVAL;
 963        }
 964
 965        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
 966
 967        dev = nfc_get_device(idx);
 968        if (!dev)
 969                return -ENODEV;
 970
 971        device_lock(&dev->dev);
 972
 973        local = nfc_llcp_find_local(dev);
 974        if (!local) {
 975                nfc_put_device(dev);
 976                rc = -ENODEV;
 977                goto exit;
 978        }
 979
 980        if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
 981                if (dev->dep_link_up) {
 982                        rc = -EINPROGRESS;
 983                        goto exit;
 984                }
 985
 986                local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
 987        }
 988
 989        if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
 990                local->rw = rw;
 991
 992        if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
 993                local->miux = cpu_to_be16(miux);
 994
 995exit:
 996        device_unlock(&dev->dev);
 997
 998        nfc_put_device(dev);
 999
1000        return rc;
1001}
1002
1003static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1004{
1005        struct nfc_dev *dev;
1006        struct nfc_llcp_local *local;
1007        struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1008        u32 idx;
1009        u8 tid;
1010        char *uri;
1011        int rc = 0, rem;
1012        size_t uri_len, tlvs_len;
1013        struct hlist_head sdreq_list;
1014        struct nfc_llcp_sdp_tlv *sdreq;
1015
1016        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1017            !info->attrs[NFC_ATTR_LLC_SDP])
1018                return -EINVAL;
1019
1020        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1021
1022        dev = nfc_get_device(idx);
1023        if (!dev) {
1024                rc = -ENODEV;
1025                goto exit;
1026        }
1027
1028        device_lock(&dev->dev);
1029
1030        if (dev->dep_link_up == false) {
1031                rc = -ENOLINK;
1032                goto exit;
1033        }
1034
1035        local = nfc_llcp_find_local(dev);
1036        if (!local) {
1037                nfc_put_device(dev);
1038                rc = -ENODEV;
1039                goto exit;
1040        }
1041
1042        INIT_HLIST_HEAD(&sdreq_list);
1043
1044        tlvs_len = 0;
1045
1046        nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1047                rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1048                                      nfc_sdp_genl_policy);
1049
1050                if (rc != 0) {
1051                        rc = -EINVAL;
1052                        goto exit;
1053                }
1054
1055                if (!sdp_attrs[NFC_SDP_ATTR_URI])
1056                        continue;
1057
1058                uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1059                if (uri_len == 0)
1060                        continue;
1061
1062                uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1063                if (uri == NULL || *uri == 0)
1064                        continue;
1065
1066                tid = local->sdreq_next_tid++;
1067
1068                sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1069                if (sdreq == NULL) {
1070                        rc = -ENOMEM;
1071                        goto exit;
1072                }
1073
1074                tlvs_len += sdreq->tlv_len;
1075
1076                hlist_add_head(&sdreq->node, &sdreq_list);
1077        }
1078
1079        if (hlist_empty(&sdreq_list)) {
1080                rc = -EINVAL;
1081                goto exit;
1082        }
1083
1084        rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1085exit:
1086        device_unlock(&dev->dev);
1087
1088        nfc_put_device(dev);
1089
1090        return rc;
1091}
1092
1093static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1094{
1095        struct nfc_dev *dev;
1096        int rc;
1097        u32 idx;
1098        char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1099
1100        if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1101                return -EINVAL;
1102
1103        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1104
1105        dev = nfc_get_device(idx);
1106        if (!dev)
1107                return -ENODEV;
1108
1109        nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1110                    sizeof(firmware_name));
1111
1112        rc = nfc_fw_download(dev, firmware_name);
1113
1114        nfc_put_device(dev);
1115        return rc;
1116}
1117
1118int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1119                              u32 result)
1120{
1121        struct sk_buff *msg;
1122        void *hdr;
1123
1124        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1125        if (!msg)
1126                return -ENOMEM;
1127
1128        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1129                          NFC_CMD_FW_DOWNLOAD);
1130        if (!hdr)
1131                goto free_msg;
1132
1133        if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1134            nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1135            nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1136                goto nla_put_failure;
1137
1138        genlmsg_end(msg, hdr);
1139
1140        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1141
1142        return 0;
1143
1144nla_put_failure:
1145        genlmsg_cancel(msg, hdr);
1146free_msg:
1147        nlmsg_free(msg);
1148        return -EMSGSIZE;
1149}
1150
1151static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1152{
1153        struct nfc_dev *dev;
1154        int rc;
1155        u32 idx, se_idx;
1156
1157        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1158            !info->attrs[NFC_ATTR_SE_INDEX])
1159                return -EINVAL;
1160
1161        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1162        se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1163
1164        dev = nfc_get_device(idx);
1165        if (!dev)
1166                return -ENODEV;
1167
1168        rc = nfc_enable_se(dev, se_idx);
1169
1170        nfc_put_device(dev);
1171        return rc;
1172}
1173
1174static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1175{
1176        struct nfc_dev *dev;
1177        int rc;
1178        u32 idx, se_idx;
1179
1180        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1181            !info->attrs[NFC_ATTR_SE_INDEX])
1182                return -EINVAL;
1183
1184        idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1185        se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1186
1187        dev = nfc_get_device(idx);
1188        if (!dev)
1189                return -ENODEV;
1190
1191        rc = nfc_disable_se(dev, se_idx);
1192
1193        nfc_put_device(dev);
1194        return rc;
1195}
1196
1197static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1198                                u32 portid, u32 seq,
1199                                struct netlink_callback *cb,
1200                                int flags)
1201{
1202        void *hdr;
1203        struct nfc_se *se, *n;
1204
1205        list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1206                hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1207                                  NFC_CMD_GET_SE);
1208                if (!hdr)
1209                        goto nla_put_failure;
1210
1211                if (cb)
1212                        genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1213
1214                if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1215                    nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1216                    nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1217                        goto nla_put_failure;
1218
1219                if (genlmsg_end(msg, hdr) < 0)
1220                        goto nla_put_failure;
1221        }
1222
1223        return 0;
1224
1225nla_put_failure:
1226        genlmsg_cancel(msg, hdr);
1227        return -EMSGSIZE;
1228}
1229
1230static int nfc_genl_dump_ses(struct sk_buff *skb,
1231                                 struct netlink_callback *cb)
1232{
1233        struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1234        struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1235        bool first_call = false;
1236
1237        if (!iter) {
1238                first_call = true;
1239                iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1240                if (!iter)
1241                        return -ENOMEM;
1242                cb->args[0] = (long) iter;
1243        }
1244
1245        mutex_lock(&nfc_devlist_mutex);
1246
1247        cb->seq = nfc_devlist_generation;
1248
1249        if (first_call) {
1250                nfc_device_iter_init(iter);
1251                dev = nfc_device_iter_next(iter);
1252        }
1253
1254        while (dev) {
1255                int rc;
1256
1257                rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1258                                          cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1259                if (rc < 0)
1260                        break;
1261
1262                dev = nfc_device_iter_next(iter);
1263        }
1264
1265        mutex_unlock(&nfc_devlist_mutex);
1266
1267        cb->args[1] = (long) dev;
1268
1269        return skb->len;
1270}
1271
1272static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1273{
1274        struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1275
1276        nfc_device_iter_exit(iter);
1277        kfree(iter);
1278
1279        return 0;
1280}
1281
1282struct se_io_ctx {
1283        u32 dev_idx;
1284        u32 se_idx;
1285};
1286
1287static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1288{
1289        struct se_io_ctx *ctx = context;
1290        struct sk_buff *msg;
1291        void *hdr;
1292
1293        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1294        if (!msg) {
1295                kfree(ctx);
1296                return;
1297        }
1298
1299        hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1300                          NFC_CMD_SE_IO);
1301        if (!hdr)
1302                goto free_msg;
1303
1304        if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1305            nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1306            nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1307                goto nla_put_failure;
1308
1309        genlmsg_end(msg, hdr);
1310
1311        genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1312
1313        kfree(ctx);
1314
1315        return;
1316
1317nla_put_failure:
1318        genlmsg_cancel(msg, hdr);
1319free_msg:
1320        nlmsg_free(msg);
1321        kfree(ctx);
1322
1323        return;
1324}
1325
1326static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1327{
1328        struct nfc_dev *dev;
1329        struct se_io_ctx *ctx;
1330        u32 dev_idx, se_idx;
1331        u8 *apdu;
1332        size_t apdu_len;
1333
1334        if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1335            !info->attrs[NFC_ATTR_SE_INDEX] ||
1336            !info->attrs[NFC_ATTR_SE_APDU])
1337                return -EINVAL;
1338
1339        dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1340        se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1341
1342        dev = nfc_get_device(dev_idx);
1343        if (!dev)
1344                return -ENODEV;
1345
1346        if (!dev->ops || !dev->ops->se_io)
1347                return -ENOTSUPP;
1348
1349        apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1350        if (apdu_len == 0)
1351                return -EINVAL;
1352
1353        apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1354        if (!apdu)
1355                return -EINVAL;
1356
1357        ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1358        if (!ctx)
1359                return -ENOMEM;
1360
1361        ctx->dev_idx = dev_idx;
1362        ctx->se_idx = se_idx;
1363
1364        return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1365}
1366
1367static const struct genl_ops nfc_genl_ops[] = {
1368        {
1369                .cmd = NFC_CMD_GET_DEVICE,
1370                .doit = nfc_genl_get_device,
1371                .dumpit = nfc_genl_dump_devices,
1372                .done = nfc_genl_dump_devices_done,
1373                .policy = nfc_genl_policy,
1374        },
1375        {
1376                .cmd = NFC_CMD_DEV_UP,
1377                .doit = nfc_genl_dev_up,
1378                .policy = nfc_genl_policy,
1379        },
1380        {
1381                .cmd = NFC_CMD_DEV_DOWN,
1382                .doit = nfc_genl_dev_down,
1383                .policy = nfc_genl_policy,
1384        },
1385        {
1386                .cmd = NFC_CMD_START_POLL,
1387                .doit = nfc_genl_start_poll,
1388                .policy = nfc_genl_policy,
1389        },
1390        {
1391                .cmd = NFC_CMD_STOP_POLL,
1392                .doit = nfc_genl_stop_poll,
1393                .policy = nfc_genl_policy,
1394        },
1395        {
1396                .cmd = NFC_CMD_DEP_LINK_UP,
1397                .doit = nfc_genl_dep_link_up,
1398                .policy = nfc_genl_policy,
1399        },
1400        {
1401                .cmd = NFC_CMD_DEP_LINK_DOWN,
1402                .doit = nfc_genl_dep_link_down,
1403                .policy = nfc_genl_policy,
1404        },
1405        {
1406                .cmd = NFC_CMD_GET_TARGET,
1407                .dumpit = nfc_genl_dump_targets,
1408                .done = nfc_genl_dump_targets_done,
1409                .policy = nfc_genl_policy,
1410        },
1411        {
1412                .cmd = NFC_CMD_LLC_GET_PARAMS,
1413                .doit = nfc_genl_llc_get_params,
1414                .policy = nfc_genl_policy,
1415        },
1416        {
1417                .cmd = NFC_CMD_LLC_SET_PARAMS,
1418                .doit = nfc_genl_llc_set_params,
1419                .policy = nfc_genl_policy,
1420        },
1421        {
1422                .cmd = NFC_CMD_LLC_SDREQ,
1423                .doit = nfc_genl_llc_sdreq,
1424                .policy = nfc_genl_policy,
1425        },
1426        {
1427                .cmd = NFC_CMD_FW_DOWNLOAD,
1428                .doit = nfc_genl_fw_download,
1429                .policy = nfc_genl_policy,
1430        },
1431        {
1432                .cmd = NFC_CMD_ENABLE_SE,
1433                .doit = nfc_genl_enable_se,
1434                .policy = nfc_genl_policy,
1435        },
1436        {
1437                .cmd = NFC_CMD_DISABLE_SE,
1438                .doit = nfc_genl_disable_se,
1439                .policy = nfc_genl_policy,
1440        },
1441        {
1442                .cmd = NFC_CMD_GET_SE,
1443                .dumpit = nfc_genl_dump_ses,
1444                .done = nfc_genl_dump_ses_done,
1445                .policy = nfc_genl_policy,
1446        },
1447        {
1448                .cmd = NFC_CMD_SE_IO,
1449                .doit = nfc_genl_se_io,
1450                .policy = nfc_genl_policy,
1451        },
1452};
1453
1454
1455struct urelease_work {
1456        struct  work_struct w;
1457        int     portid;
1458};
1459
1460static void nfc_urelease_event_work(struct work_struct *work)
1461{
1462        struct urelease_work *w = container_of(work, struct urelease_work, w);
1463        struct class_dev_iter iter;
1464        struct nfc_dev *dev;
1465
1466        pr_debug("portid %d\n", w->portid);
1467
1468        mutex_lock(&nfc_devlist_mutex);
1469
1470        nfc_device_iter_init(&iter);
1471        dev = nfc_device_iter_next(&iter);
1472
1473        while (dev) {
1474                mutex_lock(&dev->genl_data.genl_data_mutex);
1475
1476                if (dev->genl_data.poll_req_portid == w->portid) {
1477                        nfc_stop_poll(dev);
1478                        dev->genl_data.poll_req_portid = 0;
1479                }
1480
1481                mutex_unlock(&dev->genl_data.genl_data_mutex);
1482
1483                dev = nfc_device_iter_next(&iter);
1484        }
1485
1486        nfc_device_iter_exit(&iter);
1487
1488        mutex_unlock(&nfc_devlist_mutex);
1489
1490        kfree(w);
1491}
1492
1493static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1494                                 unsigned long event, void *ptr)
1495{
1496        struct netlink_notify *n = ptr;
1497        struct urelease_work *w;
1498
1499        if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1500                goto out;
1501
1502        pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1503
1504        w = kmalloc(sizeof(*w), GFP_ATOMIC);
1505        if (w) {
1506                INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1507                w->portid = n->portid;
1508                schedule_work((struct work_struct *) w);
1509        }
1510
1511out:
1512        return NOTIFY_DONE;
1513}
1514
1515void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1516{
1517        genl_data->poll_req_portid = 0;
1518        mutex_init(&genl_data->genl_data_mutex);
1519}
1520
1521void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1522{
1523        mutex_destroy(&genl_data->genl_data_mutex);
1524}
1525
1526static struct notifier_block nl_notifier = {
1527        .notifier_call  = nfc_genl_rcv_nl_event,
1528};
1529
1530/**
1531 * nfc_genl_init() - Initialize netlink interface
1532 *
1533 * This initialization function registers the nfc netlink family.
1534 */
1535int __init nfc_genl_init(void)
1536{
1537        int rc;
1538
1539        rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1540                                                  nfc_genl_ops,
1541                                                  nfc_genl_mcgrps);
1542        if (rc)
1543                return rc;
1544
1545        netlink_register_notifier(&nl_notifier);
1546
1547        return 0;
1548}
1549
1550/**
1551 * nfc_genl_exit() - Deinitialize netlink interface
1552 *
1553 * This exit function unregisters the nfc netlink family.
1554 */
1555void nfc_genl_exit(void)
1556{
1557        netlink_unregister_notifier(&nl_notifier);
1558        genl_unregister_family(&nfc_genl_family);
1559}
1560