x86/lib/clear_page_64.S: Convert to ALTERNATIVE_2 macro
[linux/fpc-iii.git] / net / nfc / netlink.c
blob14a2d11581da7ededf0eff4ca09293a277853b2d
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 * 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.
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.
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/>.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
24 #include <net/genetlink.h>
25 #include <linux/nfc.h>
26 #include <linux/slab.h>
28 #include "nfc.h"
29 #include "llcp.h"
31 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
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,
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 },
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 },
67 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
68 struct netlink_callback *cb, int flags)
70 void *hdr;
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;
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
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;
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;
105 genlmsg_end(msg, hdr);
106 return 0;
108 nla_put_failure:
109 genlmsg_cancel(msg, hdr);
110 return -EMSGSIZE;
113 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
115 struct nfc_dev *dev;
116 int rc;
117 u32 idx;
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);
126 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
127 return ERR_PTR(-EINVAL);
129 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
131 dev = nfc_get_device(idx);
132 if (!dev)
133 return ERR_PTR(-ENODEV);
135 return dev;
138 static int nfc_genl_dump_targets(struct sk_buff *skb,
139 struct netlink_callback *cb)
141 int i = cb->args[0];
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
143 int rc;
145 if (!dev) {
146 dev = __get_device_from_cb(cb);
147 if (IS_ERR(dev))
148 return PTR_ERR(dev);
150 cb->args[1] = (long) dev;
153 device_lock(&dev->dev);
155 cb->seq = dev->targets_generation;
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;
163 i++;
166 device_unlock(&dev->dev);
168 cb->args[0] = i;
170 return skb->len;
173 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
175 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
177 if (dev)
178 nfc_put_device(dev);
180 return 0;
183 int nfc_genl_targets_found(struct nfc_dev *dev)
185 struct sk_buff *msg;
186 void *hdr;
188 dev->genl_data.poll_req_portid = 0;
190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
191 if (!msg)
192 return -ENOMEM;
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
195 NFC_EVENT_TARGETS_FOUND);
196 if (!hdr)
197 goto free_msg;
199 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
200 goto nla_put_failure;
202 genlmsg_end(msg, hdr);
204 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
206 nla_put_failure:
207 genlmsg_cancel(msg, hdr);
208 free_msg:
209 nlmsg_free(msg);
210 return -EMSGSIZE;
213 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
215 struct sk_buff *msg;
216 void *hdr;
218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
219 if (!msg)
220 return -ENOMEM;
222 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
223 NFC_EVENT_TARGET_LOST);
224 if (!hdr)
225 goto free_msg;
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;
231 genlmsg_end(msg, hdr);
233 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
235 return 0;
237 nla_put_failure:
238 genlmsg_cancel(msg, hdr);
239 free_msg:
240 nlmsg_free(msg);
241 return -EMSGSIZE;
244 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
246 struct sk_buff *msg;
247 void *hdr;
249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
250 if (!msg)
251 return -ENOMEM;
253 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
254 NFC_EVENT_TM_ACTIVATED);
255 if (!hdr)
256 goto free_msg;
258 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
259 goto nla_put_failure;
260 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
261 goto nla_put_failure;
263 genlmsg_end(msg, hdr);
265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
267 return 0;
269 nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271 free_msg:
272 nlmsg_free(msg);
273 return -EMSGSIZE;
276 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
278 struct sk_buff *msg;
279 void *hdr;
281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
282 if (!msg)
283 return -ENOMEM;
285 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
286 NFC_EVENT_TM_DEACTIVATED);
287 if (!hdr)
288 goto free_msg;
290 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
291 goto nla_put_failure;
293 genlmsg_end(msg, hdr);
295 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
297 return 0;
299 nla_put_failure:
300 genlmsg_cancel(msg, hdr);
301 free_msg:
302 nlmsg_free(msg);
303 return -EMSGSIZE;
306 int nfc_genl_device_added(struct nfc_dev *dev)
308 struct sk_buff *msg;
309 void *hdr;
311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
312 if (!msg)
313 return -ENOMEM;
315 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
316 NFC_EVENT_DEVICE_ADDED);
317 if (!hdr)
318 goto free_msg;
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;
326 genlmsg_end(msg, hdr);
328 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
330 return 0;
332 nla_put_failure:
333 genlmsg_cancel(msg, hdr);
334 free_msg:
335 nlmsg_free(msg);
336 return -EMSGSIZE;
339 int nfc_genl_device_removed(struct nfc_dev *dev)
341 struct sk_buff *msg;
342 void *hdr;
344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
345 if (!msg)
346 return -ENOMEM;
348 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
349 NFC_EVENT_DEVICE_REMOVED);
350 if (!hdr)
351 goto free_msg;
353 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
354 goto nla_put_failure;
356 genlmsg_end(msg, hdr);
358 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
360 return 0;
362 nla_put_failure:
363 genlmsg_cancel(msg, hdr);
364 free_msg:
365 nlmsg_free(msg);
366 return -EMSGSIZE;
369 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
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;
379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
380 if (!msg)
381 return -ENOMEM;
383 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
384 NFC_EVENT_LLC_SDRES);
385 if (!hdr)
386 goto free_msg;
388 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
389 goto nla_put_failure;
391 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
392 if (sdp_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
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);
401 uri_attr = nla_nest_start(msg, i++);
402 if (uri_attr == NULL) {
403 rc = -ENOMEM;
404 goto nla_put_failure;
407 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
408 goto nla_put_failure;
410 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
411 goto nla_put_failure;
413 nla_nest_end(msg, uri_attr);
415 hlist_del(&sdres->node);
417 nfc_llcp_free_sdp_tlv(sdres);
420 nla_nest_end(msg, sdp_attr);
422 genlmsg_end(msg, hdr);
424 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
426 nla_put_failure:
427 genlmsg_cancel(msg, hdr);
429 free_msg:
430 nlmsg_free(msg);
432 nfc_llcp_free_sdp_tlv_list(sdres_list);
434 return rc;
437 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
439 struct sk_buff *msg;
440 void *hdr;
442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443 if (!msg)
444 return -ENOMEM;
446 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
447 NFC_EVENT_SE_ADDED);
448 if (!hdr)
449 goto free_msg;
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;
456 genlmsg_end(msg, hdr);
458 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
460 return 0;
462 nla_put_failure:
463 genlmsg_cancel(msg, hdr);
464 free_msg:
465 nlmsg_free(msg);
466 return -EMSGSIZE;
469 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
471 struct sk_buff *msg;
472 void *hdr;
474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
475 if (!msg)
476 return -ENOMEM;
478 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
479 NFC_EVENT_SE_REMOVED);
480 if (!hdr)
481 goto free_msg;
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;
487 genlmsg_end(msg, hdr);
489 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
491 return 0;
493 nla_put_failure:
494 genlmsg_cancel(msg, hdr);
495 free_msg:
496 nlmsg_free(msg);
497 return -EMSGSIZE;
500 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
501 struct nfc_evt_transaction *evt_transaction)
503 struct nfc_se *se;
504 struct sk_buff *msg;
505 void *hdr;
507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
508 if (!msg)
509 return -ENOMEM;
511 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
512 NFC_EVENT_SE_TRANSACTION);
513 if (!hdr)
514 goto free_msg;
516 se = nfc_find_se(dev, se_idx);
517 if (!se)
518 goto free_msg;
520 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
521 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
522 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
523 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
524 evt_transaction->aid) ||
525 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
526 evt_transaction->params))
527 goto nla_put_failure;
529 /* evt_transaction is no more used */
530 devm_kfree(&dev->dev, evt_transaction);
532 genlmsg_end(msg, hdr);
534 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
536 return 0;
538 nla_put_failure:
539 genlmsg_cancel(msg, hdr);
540 free_msg:
541 /* evt_transaction is no more used */
542 devm_kfree(&dev->dev, evt_transaction);
543 nlmsg_free(msg);
544 return -EMSGSIZE;
547 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
548 u32 portid, u32 seq,
549 struct netlink_callback *cb,
550 int flags)
552 void *hdr;
554 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
555 NFC_CMD_GET_DEVICE);
556 if (!hdr)
557 return -EMSGSIZE;
559 if (cb)
560 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
562 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
563 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
564 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
565 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
566 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
567 goto nla_put_failure;
569 genlmsg_end(msg, hdr);
570 return 0;
572 nla_put_failure:
573 genlmsg_cancel(msg, hdr);
574 return -EMSGSIZE;
577 static int nfc_genl_dump_devices(struct sk_buff *skb,
578 struct netlink_callback *cb)
580 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
581 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
582 bool first_call = false;
584 if (!iter) {
585 first_call = true;
586 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
587 if (!iter)
588 return -ENOMEM;
589 cb->args[0] = (long) iter;
592 mutex_lock(&nfc_devlist_mutex);
594 cb->seq = nfc_devlist_generation;
596 if (first_call) {
597 nfc_device_iter_init(iter);
598 dev = nfc_device_iter_next(iter);
601 while (dev) {
602 int rc;
604 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
605 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
606 if (rc < 0)
607 break;
609 dev = nfc_device_iter_next(iter);
612 mutex_unlock(&nfc_devlist_mutex);
614 cb->args[1] = (long) dev;
616 return skb->len;
619 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
621 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
623 nfc_device_iter_exit(iter);
624 kfree(iter);
626 return 0;
629 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
630 u8 comm_mode, u8 rf_mode)
632 struct sk_buff *msg;
633 void *hdr;
635 pr_debug("DEP link is up\n");
637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
638 if (!msg)
639 return -ENOMEM;
641 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
642 if (!hdr)
643 goto free_msg;
645 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
646 goto nla_put_failure;
647 if (rf_mode == NFC_RF_INITIATOR &&
648 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
649 goto nla_put_failure;
650 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
651 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
652 goto nla_put_failure;
654 genlmsg_end(msg, hdr);
656 dev->dep_link_up = true;
658 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
660 return 0;
662 nla_put_failure:
663 genlmsg_cancel(msg, hdr);
664 free_msg:
665 nlmsg_free(msg);
666 return -EMSGSIZE;
669 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
671 struct sk_buff *msg;
672 void *hdr;
674 pr_debug("DEP link is down\n");
676 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
677 if (!msg)
678 return -ENOMEM;
680 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
681 NFC_CMD_DEP_LINK_DOWN);
682 if (!hdr)
683 goto free_msg;
685 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
686 goto nla_put_failure;
688 genlmsg_end(msg, hdr);
690 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
692 return 0;
694 nla_put_failure:
695 genlmsg_cancel(msg, hdr);
696 free_msg:
697 nlmsg_free(msg);
698 return -EMSGSIZE;
701 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
703 struct sk_buff *msg;
704 struct nfc_dev *dev;
705 u32 idx;
706 int rc = -ENOBUFS;
708 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
709 return -EINVAL;
711 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713 dev = nfc_get_device(idx);
714 if (!dev)
715 return -ENODEV;
717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
718 if (!msg) {
719 rc = -ENOMEM;
720 goto out_putdev;
723 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
724 NULL, 0);
725 if (rc < 0)
726 goto out_free;
728 nfc_put_device(dev);
730 return genlmsg_reply(msg, info);
732 out_free:
733 nlmsg_free(msg);
734 out_putdev:
735 nfc_put_device(dev);
736 return rc;
739 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
741 struct nfc_dev *dev;
742 int rc;
743 u32 idx;
745 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
746 return -EINVAL;
748 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
750 dev = nfc_get_device(idx);
751 if (!dev)
752 return -ENODEV;
754 rc = nfc_dev_up(dev);
756 nfc_put_device(dev);
757 return rc;
760 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
762 struct nfc_dev *dev;
763 int rc;
764 u32 idx;
766 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
767 return -EINVAL;
769 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
771 dev = nfc_get_device(idx);
772 if (!dev)
773 return -ENODEV;
775 rc = nfc_dev_down(dev);
777 nfc_put_device(dev);
778 return rc;
781 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
783 struct nfc_dev *dev;
784 int rc;
785 u32 idx;
786 u32 im_protocols = 0, tm_protocols = 0;
788 pr_debug("Poll start\n");
790 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
791 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
792 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
793 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
794 return -EINVAL;
796 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
798 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
799 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
801 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
802 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
803 else if (info->attrs[NFC_ATTR_PROTOCOLS])
804 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
806 dev = nfc_get_device(idx);
807 if (!dev)
808 return -ENODEV;
810 mutex_lock(&dev->genl_data.genl_data_mutex);
812 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
813 if (!rc)
814 dev->genl_data.poll_req_portid = info->snd_portid;
816 mutex_unlock(&dev->genl_data.genl_data_mutex);
818 nfc_put_device(dev);
819 return rc;
822 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
824 struct nfc_dev *dev;
825 int rc;
826 u32 idx;
828 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
829 return -EINVAL;
831 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
833 dev = nfc_get_device(idx);
834 if (!dev)
835 return -ENODEV;
837 device_lock(&dev->dev);
839 if (!dev->polling) {
840 device_unlock(&dev->dev);
841 return -EINVAL;
844 device_unlock(&dev->dev);
846 mutex_lock(&dev->genl_data.genl_data_mutex);
848 if (dev->genl_data.poll_req_portid != info->snd_portid) {
849 rc = -EBUSY;
850 goto out;
853 rc = nfc_stop_poll(dev);
854 dev->genl_data.poll_req_portid = 0;
856 out:
857 mutex_unlock(&dev->genl_data.genl_data_mutex);
858 nfc_put_device(dev);
859 return rc;
862 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
864 struct nfc_dev *dev;
865 u32 device_idx, target_idx, protocol;
866 int rc;
868 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
869 return -EINVAL;
871 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
873 dev = nfc_get_device(device_idx);
874 if (!dev)
875 return -ENODEV;
877 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
878 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
880 nfc_deactivate_target(dev, target_idx);
881 rc = nfc_activate_target(dev, target_idx, protocol);
883 nfc_put_device(dev);
884 return 0;
887 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
889 struct nfc_dev *dev;
890 int rc, tgt_idx;
891 u32 idx;
892 u8 comm;
894 pr_debug("DEP link up\n");
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
897 !info->attrs[NFC_ATTR_COMM_MODE])
898 return -EINVAL;
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
902 tgt_idx = NFC_TARGET_IDX_ANY;
903 else
904 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
906 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
908 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
909 return -EINVAL;
911 dev = nfc_get_device(idx);
912 if (!dev)
913 return -ENODEV;
915 rc = nfc_dep_link_up(dev, tgt_idx, comm);
917 nfc_put_device(dev);
919 return rc;
922 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
924 struct nfc_dev *dev;
925 int rc;
926 u32 idx;
928 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
929 return -EINVAL;
931 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
933 dev = nfc_get_device(idx);
934 if (!dev)
935 return -ENODEV;
937 rc = nfc_dep_link_down(dev);
939 nfc_put_device(dev);
940 return rc;
943 static int nfc_genl_send_params(struct sk_buff *msg,
944 struct nfc_llcp_local *local,
945 u32 portid, u32 seq)
947 void *hdr;
949 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
950 NFC_CMD_LLC_GET_PARAMS);
951 if (!hdr)
952 return -EMSGSIZE;
954 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
955 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
956 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
957 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
958 goto nla_put_failure;
960 genlmsg_end(msg, hdr);
961 return 0;
963 nla_put_failure:
965 genlmsg_cancel(msg, hdr);
966 return -EMSGSIZE;
969 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
971 struct nfc_dev *dev;
972 struct nfc_llcp_local *local;
973 int rc = 0;
974 struct sk_buff *msg = NULL;
975 u32 idx;
977 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
978 return -EINVAL;
980 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
982 dev = nfc_get_device(idx);
983 if (!dev)
984 return -ENODEV;
986 device_lock(&dev->dev);
988 local = nfc_llcp_find_local(dev);
989 if (!local) {
990 rc = -ENODEV;
991 goto exit;
994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
995 if (!msg) {
996 rc = -ENOMEM;
997 goto exit;
1000 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1002 exit:
1003 device_unlock(&dev->dev);
1005 nfc_put_device(dev);
1007 if (rc < 0) {
1008 if (msg)
1009 nlmsg_free(msg);
1011 return rc;
1014 return genlmsg_reply(msg, info);
1017 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1019 struct nfc_dev *dev;
1020 struct nfc_llcp_local *local;
1021 u8 rw = 0;
1022 u16 miux = 0;
1023 u32 idx;
1024 int rc = 0;
1026 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1027 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1028 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1029 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1030 return -EINVAL;
1032 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1033 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1035 if (rw > LLCP_MAX_RW)
1036 return -EINVAL;
1039 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1040 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1042 if (miux > LLCP_MAX_MIUX)
1043 return -EINVAL;
1046 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1048 dev = nfc_get_device(idx);
1049 if (!dev)
1050 return -ENODEV;
1052 device_lock(&dev->dev);
1054 local = nfc_llcp_find_local(dev);
1055 if (!local) {
1056 nfc_put_device(dev);
1057 rc = -ENODEV;
1058 goto exit;
1061 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1062 if (dev->dep_link_up) {
1063 rc = -EINPROGRESS;
1064 goto exit;
1067 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1071 local->rw = rw;
1073 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1074 local->miux = cpu_to_be16(miux);
1076 exit:
1077 device_unlock(&dev->dev);
1079 nfc_put_device(dev);
1081 return rc;
1084 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1086 struct nfc_dev *dev;
1087 struct nfc_llcp_local *local;
1088 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1089 u32 idx;
1090 u8 tid;
1091 char *uri;
1092 int rc = 0, rem;
1093 size_t uri_len, tlvs_len;
1094 struct hlist_head sdreq_list;
1095 struct nfc_llcp_sdp_tlv *sdreq;
1097 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1098 !info->attrs[NFC_ATTR_LLC_SDP])
1099 return -EINVAL;
1101 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1103 dev = nfc_get_device(idx);
1104 if (!dev) {
1105 rc = -ENODEV;
1106 goto exit;
1109 device_lock(&dev->dev);
1111 if (dev->dep_link_up == false) {
1112 rc = -ENOLINK;
1113 goto exit;
1116 local = nfc_llcp_find_local(dev);
1117 if (!local) {
1118 nfc_put_device(dev);
1119 rc = -ENODEV;
1120 goto exit;
1123 INIT_HLIST_HEAD(&sdreq_list);
1125 tlvs_len = 0;
1127 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1128 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1129 nfc_sdp_genl_policy);
1131 if (rc != 0) {
1132 rc = -EINVAL;
1133 goto exit;
1136 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1137 continue;
1139 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1140 if (uri_len == 0)
1141 continue;
1143 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1144 if (uri == NULL || *uri == 0)
1145 continue;
1147 tid = local->sdreq_next_tid++;
1149 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1150 if (sdreq == NULL) {
1151 rc = -ENOMEM;
1152 goto exit;
1155 tlvs_len += sdreq->tlv_len;
1157 hlist_add_head(&sdreq->node, &sdreq_list);
1160 if (hlist_empty(&sdreq_list)) {
1161 rc = -EINVAL;
1162 goto exit;
1165 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1166 exit:
1167 device_unlock(&dev->dev);
1169 nfc_put_device(dev);
1171 return rc;
1174 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1176 struct nfc_dev *dev;
1177 int rc;
1178 u32 idx;
1179 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1181 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1182 return -EINVAL;
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1186 dev = nfc_get_device(idx);
1187 if (!dev)
1188 return -ENODEV;
1190 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1191 sizeof(firmware_name));
1193 rc = nfc_fw_download(dev, firmware_name);
1195 nfc_put_device(dev);
1196 return rc;
1199 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1200 u32 result)
1202 struct sk_buff *msg;
1203 void *hdr;
1205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1206 if (!msg)
1207 return -ENOMEM;
1209 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1210 NFC_CMD_FW_DOWNLOAD);
1211 if (!hdr)
1212 goto free_msg;
1214 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1215 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1216 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1217 goto nla_put_failure;
1219 genlmsg_end(msg, hdr);
1221 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1223 return 0;
1225 nla_put_failure:
1226 genlmsg_cancel(msg, hdr);
1227 free_msg:
1228 nlmsg_free(msg);
1229 return -EMSGSIZE;
1232 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1234 struct nfc_dev *dev;
1235 int rc;
1236 u32 idx, se_idx;
1238 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1239 !info->attrs[NFC_ATTR_SE_INDEX])
1240 return -EINVAL;
1242 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1243 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1245 dev = nfc_get_device(idx);
1246 if (!dev)
1247 return -ENODEV;
1249 rc = nfc_enable_se(dev, se_idx);
1251 nfc_put_device(dev);
1252 return rc;
1255 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1257 struct nfc_dev *dev;
1258 int rc;
1259 u32 idx, se_idx;
1261 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1262 !info->attrs[NFC_ATTR_SE_INDEX])
1263 return -EINVAL;
1265 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1266 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1268 dev = nfc_get_device(idx);
1269 if (!dev)
1270 return -ENODEV;
1272 rc = nfc_disable_se(dev, se_idx);
1274 nfc_put_device(dev);
1275 return rc;
1278 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1279 u32 portid, u32 seq,
1280 struct netlink_callback *cb,
1281 int flags)
1283 void *hdr;
1284 struct nfc_se *se, *n;
1286 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1287 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1288 NFC_CMD_GET_SE);
1289 if (!hdr)
1290 goto nla_put_failure;
1292 if (cb)
1293 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1295 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1296 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1297 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1298 goto nla_put_failure;
1300 genlmsg_end(msg, hdr);
1303 return 0;
1305 nla_put_failure:
1306 genlmsg_cancel(msg, hdr);
1307 return -EMSGSIZE;
1310 static int nfc_genl_dump_ses(struct sk_buff *skb,
1311 struct netlink_callback *cb)
1313 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1314 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1315 bool first_call = false;
1317 if (!iter) {
1318 first_call = true;
1319 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1320 if (!iter)
1321 return -ENOMEM;
1322 cb->args[0] = (long) iter;
1325 mutex_lock(&nfc_devlist_mutex);
1327 cb->seq = nfc_devlist_generation;
1329 if (first_call) {
1330 nfc_device_iter_init(iter);
1331 dev = nfc_device_iter_next(iter);
1334 while (dev) {
1335 int rc;
1337 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1338 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1339 if (rc < 0)
1340 break;
1342 dev = nfc_device_iter_next(iter);
1345 mutex_unlock(&nfc_devlist_mutex);
1347 cb->args[1] = (long) dev;
1349 return skb->len;
1352 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1354 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1356 nfc_device_iter_exit(iter);
1357 kfree(iter);
1359 return 0;
1362 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1363 u8 *apdu, size_t apdu_length,
1364 se_io_cb_t cb, void *cb_context)
1366 struct nfc_se *se;
1367 int rc;
1369 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1371 device_lock(&dev->dev);
1373 if (!device_is_registered(&dev->dev)) {
1374 rc = -ENODEV;
1375 goto error;
1378 if (!dev->dev_up) {
1379 rc = -ENODEV;
1380 goto error;
1383 if (!dev->ops->se_io) {
1384 rc = -EOPNOTSUPP;
1385 goto error;
1388 se = nfc_find_se(dev, se_idx);
1389 if (!se) {
1390 rc = -EINVAL;
1391 goto error;
1394 if (se->state != NFC_SE_ENABLED) {
1395 rc = -ENODEV;
1396 goto error;
1399 rc = dev->ops->se_io(dev, se_idx, apdu,
1400 apdu_length, cb, cb_context);
1402 error:
1403 device_unlock(&dev->dev);
1404 return rc;
1407 struct se_io_ctx {
1408 u32 dev_idx;
1409 u32 se_idx;
1412 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1414 struct se_io_ctx *ctx = context;
1415 struct sk_buff *msg;
1416 void *hdr;
1418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1419 if (!msg) {
1420 kfree(ctx);
1421 return;
1424 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1425 NFC_CMD_SE_IO);
1426 if (!hdr)
1427 goto free_msg;
1429 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1430 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1431 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1432 goto nla_put_failure;
1434 genlmsg_end(msg, hdr);
1436 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1438 kfree(ctx);
1440 return;
1442 nla_put_failure:
1443 genlmsg_cancel(msg, hdr);
1444 free_msg:
1445 nlmsg_free(msg);
1446 kfree(ctx);
1448 return;
1451 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1453 struct nfc_dev *dev;
1454 struct se_io_ctx *ctx;
1455 u32 dev_idx, se_idx;
1456 u8 *apdu;
1457 size_t apdu_len;
1459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1460 !info->attrs[NFC_ATTR_SE_INDEX] ||
1461 !info->attrs[NFC_ATTR_SE_APDU])
1462 return -EINVAL;
1464 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1465 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1467 dev = nfc_get_device(dev_idx);
1468 if (!dev)
1469 return -ENODEV;
1471 if (!dev->ops || !dev->ops->se_io)
1472 return -ENOTSUPP;
1474 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1475 if (apdu_len == 0)
1476 return -EINVAL;
1478 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1479 if (!apdu)
1480 return -EINVAL;
1482 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1483 if (!ctx)
1484 return -ENOMEM;
1486 ctx->dev_idx = dev_idx;
1487 ctx->se_idx = se_idx;
1489 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1492 static const struct genl_ops nfc_genl_ops[] = {
1494 .cmd = NFC_CMD_GET_DEVICE,
1495 .doit = nfc_genl_get_device,
1496 .dumpit = nfc_genl_dump_devices,
1497 .done = nfc_genl_dump_devices_done,
1498 .policy = nfc_genl_policy,
1501 .cmd = NFC_CMD_DEV_UP,
1502 .doit = nfc_genl_dev_up,
1503 .policy = nfc_genl_policy,
1506 .cmd = NFC_CMD_DEV_DOWN,
1507 .doit = nfc_genl_dev_down,
1508 .policy = nfc_genl_policy,
1511 .cmd = NFC_CMD_START_POLL,
1512 .doit = nfc_genl_start_poll,
1513 .policy = nfc_genl_policy,
1516 .cmd = NFC_CMD_STOP_POLL,
1517 .doit = nfc_genl_stop_poll,
1518 .policy = nfc_genl_policy,
1521 .cmd = NFC_CMD_DEP_LINK_UP,
1522 .doit = nfc_genl_dep_link_up,
1523 .policy = nfc_genl_policy,
1526 .cmd = NFC_CMD_DEP_LINK_DOWN,
1527 .doit = nfc_genl_dep_link_down,
1528 .policy = nfc_genl_policy,
1531 .cmd = NFC_CMD_GET_TARGET,
1532 .dumpit = nfc_genl_dump_targets,
1533 .done = nfc_genl_dump_targets_done,
1534 .policy = nfc_genl_policy,
1537 .cmd = NFC_CMD_LLC_GET_PARAMS,
1538 .doit = nfc_genl_llc_get_params,
1539 .policy = nfc_genl_policy,
1542 .cmd = NFC_CMD_LLC_SET_PARAMS,
1543 .doit = nfc_genl_llc_set_params,
1544 .policy = nfc_genl_policy,
1547 .cmd = NFC_CMD_LLC_SDREQ,
1548 .doit = nfc_genl_llc_sdreq,
1549 .policy = nfc_genl_policy,
1552 .cmd = NFC_CMD_FW_DOWNLOAD,
1553 .doit = nfc_genl_fw_download,
1554 .policy = nfc_genl_policy,
1557 .cmd = NFC_CMD_ENABLE_SE,
1558 .doit = nfc_genl_enable_se,
1559 .policy = nfc_genl_policy,
1562 .cmd = NFC_CMD_DISABLE_SE,
1563 .doit = nfc_genl_disable_se,
1564 .policy = nfc_genl_policy,
1567 .cmd = NFC_CMD_GET_SE,
1568 .dumpit = nfc_genl_dump_ses,
1569 .done = nfc_genl_dump_ses_done,
1570 .policy = nfc_genl_policy,
1573 .cmd = NFC_CMD_SE_IO,
1574 .doit = nfc_genl_se_io,
1575 .policy = nfc_genl_policy,
1578 .cmd = NFC_CMD_ACTIVATE_TARGET,
1579 .doit = nfc_genl_activate_target,
1580 .policy = nfc_genl_policy,
1585 struct urelease_work {
1586 struct work_struct w;
1587 int portid;
1590 static void nfc_urelease_event_work(struct work_struct *work)
1592 struct urelease_work *w = container_of(work, struct urelease_work, w);
1593 struct class_dev_iter iter;
1594 struct nfc_dev *dev;
1596 pr_debug("portid %d\n", w->portid);
1598 mutex_lock(&nfc_devlist_mutex);
1600 nfc_device_iter_init(&iter);
1601 dev = nfc_device_iter_next(&iter);
1603 while (dev) {
1604 mutex_lock(&dev->genl_data.genl_data_mutex);
1606 if (dev->genl_data.poll_req_portid == w->portid) {
1607 nfc_stop_poll(dev);
1608 dev->genl_data.poll_req_portid = 0;
1611 mutex_unlock(&dev->genl_data.genl_data_mutex);
1613 dev = nfc_device_iter_next(&iter);
1616 nfc_device_iter_exit(&iter);
1618 mutex_unlock(&nfc_devlist_mutex);
1620 kfree(w);
1623 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1624 unsigned long event, void *ptr)
1626 struct netlink_notify *n = ptr;
1627 struct urelease_work *w;
1629 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1630 goto out;
1632 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1634 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1635 if (w) {
1636 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1637 w->portid = n->portid;
1638 schedule_work((struct work_struct *) w);
1641 out:
1642 return NOTIFY_DONE;
1645 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1647 genl_data->poll_req_portid = 0;
1648 mutex_init(&genl_data->genl_data_mutex);
1651 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1653 mutex_destroy(&genl_data->genl_data_mutex);
1656 static struct notifier_block nl_notifier = {
1657 .notifier_call = nfc_genl_rcv_nl_event,
1661 * nfc_genl_init() - Initialize netlink interface
1663 * This initialization function registers the nfc netlink family.
1665 int __init nfc_genl_init(void)
1667 int rc;
1669 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1670 nfc_genl_ops,
1671 nfc_genl_mcgrps);
1672 if (rc)
1673 return rc;
1675 netlink_register_notifier(&nl_notifier);
1677 return 0;
1681 * nfc_genl_exit() - Deinitialize netlink interface
1683 * This exit function unregisters the nfc netlink family.
1685 void nfc_genl_exit(void)
1687 netlink_unregister_notifier(&nl_notifier);
1688 genl_unregister_family(&nfc_genl_family);