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