NFC: st21nfcb: Add support for secure element
[deliverable/linux.git] / net / nfc / netlink.c
CommitLineData
4d12b8b1
LRV
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
98b32dec 19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4d12b8b1
LRV
20 */
21
52858b51 22#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
20c239c1 23
4d12b8b1
LRV
24#include <net/genetlink.h>
25#include <linux/nfc.h>
26#include <linux/slab.h>
27
28#include "nfc.h"
30cc4587 29#include "llcp.h"
52feb444 30
2a94fe48
JB
31static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
4d12b8b1
LRV
33};
34
e5fe4cf8 35static struct genl_family nfc_genl_family = {
4d12b8b1
LRV
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
43static 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 },
1ed28f61
SO
48 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
c970a1ac 50 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
fe7c5800
SO
51 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
8af362d1
TE
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 },
d9b8d8e1 56 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
9674da87
EL
57 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 .len = NFC_FIRMWARE_NAME_MAXSIZE },
5ce3f32b 59 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
d9b8d8e1
TE
60};
61
62static 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 },
4d12b8b1
LRV
65};
66
67static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
0a40acb2 68 struct netlink_callback *cb, int flags)
4d12b8b1
LRV
69{
70 void *hdr;
71
15e47304 72 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
0a40acb2 73 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
4d12b8b1
LRV
74 if (!hdr)
75 return -EMSGSIZE;
76
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78
1e6428d8
DM
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;
4d12b8b1 96
f5f6872e
MG
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
053c095a
JB
105 genlmsg_end(msg, hdr);
106 return 0;
4d12b8b1
LRV
107
108nla_put_failure:
109 genlmsg_cancel(msg, hdr);
110 return -EMSGSIZE;
111}
112
113static 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,
0a40acb2
SO
120 nfc_genl_family.attrbuf,
121 nfc_genl_family.maxattr,
122 nfc_genl_policy);
4d12b8b1
LRV
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
138static int nfc_genl_dump_targets(struct sk_buff *skb,
0a40acb2 139 struct netlink_callback *cb)
4d12b8b1
LRV
140{
141 int i = cb->args[0];
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
143 int rc;
144
4d12b8b1
LRV
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
d4ccb132 153 device_lock(&dev->dev);
4d12b8b1
LRV
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,
0a40acb2 159 NLM_F_MULTI);
4d12b8b1
LRV
160 if (rc < 0)
161 break;
162
163 i++;
164 }
165
d4ccb132 166 device_unlock(&dev->dev);
4d12b8b1
LRV
167
168 cb->args[0] = i;
169
170 return skb->len;
171}
172
173static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
174{
175 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
176
4d12b8b1
LRV
177 if (dev)
178 nfc_put_device(dev);
179
180 return 0;
181}
182
183int nfc_genl_targets_found(struct nfc_dev *dev)
184{
185 struct sk_buff *msg;
186 void *hdr;
187
15e47304 188 dev->genl_data.poll_req_portid = 0;
4d12b8b1 189
58050fce 190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
4d12b8b1
LRV
191 if (!msg)
192 return -ENOMEM;
193
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 195 NFC_EVENT_TARGETS_FOUND);
4d12b8b1
LRV
196 if (!hdr)
197 goto free_msg;
198
1e6428d8
DM
199 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
200 goto nla_put_failure;
4d12b8b1
LRV
201
202 genlmsg_end(msg, hdr);
203
2a94fe48 204 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
4d12b8b1
LRV
205
206nla_put_failure:
207 genlmsg_cancel(msg, hdr);
208free_msg:
209 nlmsg_free(msg);
210 return -EMSGSIZE;
211}
212
8112a5c9
SO
213int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
214{
215 struct sk_buff *msg;
216 void *hdr;
217
58050fce 218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8112a5c9
SO
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
59ef43e6
JL
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;
8112a5c9
SO
230
231 genlmsg_end(msg, hdr);
232
2a94fe48 233 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
8112a5c9
SO
234
235 return 0;
236
fc40a8c1
SO
237nla_put_failure:
238 genlmsg_cancel(msg, hdr);
239free_msg:
240 nlmsg_free(msg);
241 return -EMSGSIZE;
242}
243
244int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
245{
246 struct sk_buff *msg;
247 void *hdr;
248
58050fce 249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fc40a8c1
SO
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
2a94fe48 265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
fc40a8c1
SO
266
267 return 0;
268
269nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271free_msg:
272 nlmsg_free(msg);
273 return -EMSGSIZE;
274}
275
276int nfc_genl_tm_deactivated(struct nfc_dev *dev)
277{
278 struct sk_buff *msg;
279 void *hdr;
280
58050fce 281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fc40a8c1
SO
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
2a94fe48 295 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
fc40a8c1
SO
296
297 return 0;
298
8112a5c9
SO
299nla_put_failure:
300 genlmsg_cancel(msg, hdr);
301free_msg:
302 nlmsg_free(msg);
303 return -EMSGSIZE;
304}
305
4d12b8b1
LRV
306int nfc_genl_device_added(struct nfc_dev *dev)
307{
308 struct sk_buff *msg;
309 void *hdr;
310
58050fce 311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
312 if (!msg)
313 return -ENOMEM;
314
315 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 316 NFC_EVENT_DEVICE_ADDED);
4d12b8b1
LRV
317 if (!hdr)
318 goto free_msg;
319
1e6428d8
DM
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;
4d12b8b1
LRV
325
326 genlmsg_end(msg, hdr);
327
2a94fe48 328 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
4d12b8b1
LRV
329
330 return 0;
331
332nla_put_failure:
333 genlmsg_cancel(msg, hdr);
334free_msg:
335 nlmsg_free(msg);
336 return -EMSGSIZE;
337}
338
339int nfc_genl_device_removed(struct nfc_dev *dev)
340{
341 struct sk_buff *msg;
342 void *hdr;
343
58050fce 344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
345 if (!msg)
346 return -ENOMEM;
347
348 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 349 NFC_EVENT_DEVICE_REMOVED);
4d12b8b1
LRV
350 if (!hdr)
351 goto free_msg;
352
1e6428d8
DM
353 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
354 goto nla_put_failure;
4d12b8b1
LRV
355
356 genlmsg_end(msg, hdr);
357
2a94fe48 358 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
4d12b8b1
LRV
359
360 return 0;
361
362nla_put_failure:
363 genlmsg_cancel(msg, hdr);
364free_msg:
365 nlmsg_free(msg);
366 return -EMSGSIZE;
367}
368
d9b8d8e1
TE
369int 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
2a94fe48 424 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
d9b8d8e1
TE
425
426nla_put_failure:
427 genlmsg_cancel(msg, hdr);
428
429free_msg:
430 nlmsg_free(msg);
431
432 nfc_llcp_free_sdp_tlv_list(sdres_list);
433
434 return rc;
435}
436
2757c372
SO
437int 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
2a94fe48 458 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
2757c372
SO
459
460 return 0;
461
462nla_put_failure:
463 genlmsg_cancel(msg, hdr);
464free_msg:
465 nlmsg_free(msg);
466 return -EMSGSIZE;
467}
468
469int 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
2a94fe48 489 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
2757c372
SO
490
491 return 0;
492
493nla_put_failure:
494 genlmsg_cancel(msg, hdr);
495free_msg:
496 nlmsg_free(msg);
497 return -EMSGSIZE;
498}
499
4d12b8b1 500static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
15e47304 501 u32 portid, u32 seq,
0a40acb2
SO
502 struct netlink_callback *cb,
503 int flags)
4d12b8b1
LRV
504{
505 void *hdr;
506
15e47304 507 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
0a40acb2 508 NFC_CMD_GET_DEVICE);
4d12b8b1
LRV
509 if (!hdr)
510 return -EMSGSIZE;
511
512 if (cb)
513 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
514
1e6428d8
DM
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) ||
7ad39395
TE
518 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
519 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
1e6428d8 520 goto nla_put_failure;
4d12b8b1 521
053c095a
JB
522 genlmsg_end(msg, hdr);
523 return 0;
4d12b8b1
LRV
524
525nla_put_failure:
526 genlmsg_cancel(msg, hdr);
527 return -EMSGSIZE;
528}
529
530static int nfc_genl_dump_devices(struct sk_buff *skb,
0a40acb2 531 struct netlink_callback *cb)
4d12b8b1
LRV
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
4d12b8b1
LRV
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
15e47304 557 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
0a40acb2 558 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
4d12b8b1
LRV
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
572static 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
4d12b8b1
LRV
576 nfc_device_iter_exit(iter);
577 kfree(iter);
578
579 return 0;
580}
581
1ed28f61 582int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
0a40acb2 583 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
584{
585 struct sk_buff *msg;
586 void *hdr;
587
588 pr_debug("DEP link is up\n");
589
58050fce 590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1ed28f61
SO
591 if (!msg)
592 return -ENOMEM;
593
0a40acb2 594 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
1ed28f61
SO
595 if (!hdr)
596 goto free_msg;
597
1e6428d8
DM
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;
1ed28f61
SO
606
607 genlmsg_end(msg, hdr);
608
609 dev->dep_link_up = true;
610
2a94fe48 611 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
1ed28f61
SO
612
613 return 0;
614
615nla_put_failure:
616 genlmsg_cancel(msg, hdr);
617free_msg:
618 nlmsg_free(msg);
619 return -EMSGSIZE;
620}
621
622int 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
58050fce 629 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1ed28f61
SO
630 if (!msg)
631 return -ENOMEM;
632
633 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 634 NFC_CMD_DEP_LINK_DOWN);
1ed28f61
SO
635 if (!hdr)
636 goto free_msg;
637
1e6428d8
DM
638 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
639 goto nla_put_failure;
1ed28f61
SO
640
641 genlmsg_end(msg, hdr);
642
2a94fe48 643 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
1ed28f61
SO
644
645 return 0;
646
647nla_put_failure:
648 genlmsg_cancel(msg, hdr);
649free_msg:
650 nlmsg_free(msg);
651 return -EMSGSIZE;
652}
653
4d12b8b1
LRV
654static 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
4d12b8b1
LRV
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
58050fce 670 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
671 if (!msg) {
672 rc = -ENOMEM;
673 goto out_putdev;
674 }
675
15e47304 676 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
0a40acb2 677 NULL, 0);
4d12b8b1
LRV
678 if (rc < 0)
679 goto out_free;
680
681 nfc_put_device(dev);
682
683 return genlmsg_reply(msg, info);
684
685out_free:
686 nlmsg_free(msg);
687out_putdev:
688 nfc_put_device(dev);
689 return rc;
690}
691
8b3fe7b5
IE
692static 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
8b3fe7b5
IE
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
713static 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
8b3fe7b5
IE
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
4d12b8b1
LRV
734static 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;
fe7c5800 739 u32 im_protocols = 0, tm_protocols = 0;
4d12b8b1 740
1ed28f61
SO
741 pr_debug("Poll start\n");
742
4d12b8b1 743 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
fe7c5800
SO
744 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
745 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
0f450772 746 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
4d12b8b1
LRV
747 return -EINVAL;
748
749 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
fe7c5800
SO
750
751 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
752 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
fe7c5800
SO
753
754 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
755 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
5e50ee3a
SO
756 else if (info->attrs[NFC_ATTR_PROTOCOLS])
757 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
4d12b8b1
LRV
758
759 dev = nfc_get_device(idx);
760 if (!dev)
761 return -ENODEV;
762
763 mutex_lock(&dev->genl_data.genl_data_mutex);
764
fe7c5800 765 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
4d12b8b1 766 if (!rc)
15e47304 767 dev->genl_data.poll_req_portid = info->snd_portid;
4d12b8b1
LRV
768
769 mutex_unlock(&dev->genl_data.genl_data_mutex);
770
771 nfc_put_device(dev);
772 return rc;
773}
774
775static 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
4d12b8b1
LRV
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
a831b913
SO
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
4d12b8b1
LRV
799 mutex_lock(&dev->genl_data.genl_data_mutex);
800
15e47304 801 if (dev->genl_data.poll_req_portid != info->snd_portid) {
4d12b8b1
LRV
802 rc = -EBUSY;
803 goto out;
804 }
805
806 rc = nfc_stop_poll(dev);
15e47304 807 dev->genl_data.poll_req_portid = 0;
4d12b8b1
LRV
808
809out:
810 mutex_unlock(&dev->genl_data.genl_data_mutex);
811 nfc_put_device(dev);
812 return rc;
813}
814
3682f49f
CR
815static 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
1ed28f61
SO
840static 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;
47807d3d 845 u8 comm;
1ed28f61
SO
846
847 pr_debug("DEP link up\n");
848
849 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
47807d3d 850 !info->attrs[NFC_ATTR_COMM_MODE])
1ed28f61
SO
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]);
1ed28f61
SO
860
861 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
862 return -EINVAL;
863
1ed28f61
SO
864 dev = nfc_get_device(idx);
865 if (!dev)
866 return -ENODEV;
867
47807d3d 868 rc = nfc_dep_link_up(dev, tgt_idx, comm);
1ed28f61
SO
869
870 nfc_put_device(dev);
871
872 return rc;
873}
874
875static 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
52feb444
TE
896static 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
053c095a
JB
913 genlmsg_end(msg, hdr);
914 return 0;
52feb444
TE
915
916nla_put_failure:
917
918 genlmsg_cancel(msg, hdr);
919 return -EMSGSIZE;
920}
921
922static 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
955exit:
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
970static 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
1029exit:
1030 device_unlock(&dev->dev);
1031
1032 nfc_put_device(dev);
1033
1034 return rc;
1035}
1036
d9b8d8e1
TE
1037static 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);
1119exit:
1120 device_unlock(&dev->dev);
1121
1122 nfc_put_device(dev);
1123
1124 return rc;
1125}
1126
9ea7187c 1127static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
9674da87
EL
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
9ea7187c 1146 rc = nfc_fw_download(dev, firmware_name);
9674da87
EL
1147
1148 nfc_put_device(dev);
1149 return rc;
1150}
1151
352a5f5f
EL
1152int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1153 u32 result)
9674da87
EL
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,
9ea7187c 1163 NFC_CMD_FW_DOWNLOAD);
9674da87
EL
1164 if (!hdr)
1165 goto free_msg;
1166
1167 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
352a5f5f 1168 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
9674da87
EL
1169 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1170 goto nla_put_failure;
1171
1172 genlmsg_end(msg, hdr);
1173
2a94fe48 1174 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
9674da87
EL
1175
1176 return 0;
1177
1178nla_put_failure:
1179 genlmsg_cancel(msg, hdr);
1180free_msg:
1181 nlmsg_free(msg);
1182 return -EMSGSIZE;
1183}
1184
be085653
SO
1185static 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
1208static 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
ac22ac46
SO
1231static 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
053c095a 1253 genlmsg_end(msg, hdr);
ac22ac46
SO
1254 }
1255
1256 return 0;
1257
1258nla_put_failure:
1259 genlmsg_cancel(msg, hdr);
1260 return -EMSGSIZE;
1261}
1262
1263static 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
1305static 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
cd96db6f
CR
1315static 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
1355error:
1356 device_unlock(&dev->dev);
1357 return rc;
1358}
1359
5ce3f32b
SO
1360struct se_io_ctx {
1361 u32 dev_idx;
1362 u32 se_idx;
1363};
1364
ddc1a70b 1365static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
5ce3f32b
SO
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
2a94fe48 1389 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
5ce3f32b
SO
1390
1391 kfree(ctx);
1392
1393 return;
1394
1395nla_put_failure:
1396 genlmsg_cancel(msg, hdr);
1397free_msg:
1398 nlmsg_free(msg);
1399 kfree(ctx);
1400
1401 return;
1402}
1403
1404static 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
cd96db6f 1442 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
5ce3f32b
SO
1443}
1444
4534de83 1445static const struct genl_ops nfc_genl_ops[] = {
4d12b8b1
LRV
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,
8b3fe7b5
IE
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,
4d12b8b1
LRV
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 },
1ed28f61
SO
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 },
4d12b8b1
LRV
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 },
52feb444
TE
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 },
d9b8d8e1
TE
1499 {
1500 .cmd = NFC_CMD_LLC_SDREQ,
1501 .doit = nfc_genl_llc_sdreq,
1502 .policy = nfc_genl_policy,
1503 },
9674da87 1504 {
9ea7187c
SO
1505 .cmd = NFC_CMD_FW_DOWNLOAD,
1506 .doit = nfc_genl_fw_download,
9674da87
EL
1507 .policy = nfc_genl_policy,
1508 },
be085653
SO
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 },
ac22ac46
SO
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 },
5ce3f32b
SO
1525 {
1526 .cmd = NFC_CMD_SE_IO,
1527 .doit = nfc_genl_se_io,
1528 .policy = nfc_genl_policy,
1529 },
3682f49f
CR
1530 {
1531 .cmd = NFC_CMD_ACTIVATE_TARGET,
1532 .doit = nfc_genl_activate_target,
1533 .policy = nfc_genl_policy,
1534 },
4d12b8b1
LRV
1535};
1536
3c0cc8aa
SJ
1537
1538struct urelease_work {
1539 struct work_struct w;
c487606f 1540 int portid;
3c0cc8aa
SJ
1541};
1542
1543static void nfc_urelease_event_work(struct work_struct *work)
4d12b8b1 1544{
3c0cc8aa 1545 struct urelease_work *w = container_of(work, struct urelease_work, w);
4d12b8b1
LRV
1546 struct class_dev_iter iter;
1547 struct nfc_dev *dev;
1548
c487606f 1549 pr_debug("portid %d\n", w->portid);
4d12b8b1 1550
3c0cc8aa 1551 mutex_lock(&nfc_devlist_mutex);
4d12b8b1
LRV
1552
1553 nfc_device_iter_init(&iter);
1554 dev = nfc_device_iter_next(&iter);
1555
1556 while (dev) {
3c0cc8aa
SJ
1557 mutex_lock(&dev->genl_data.genl_data_mutex);
1558
c487606f 1559 if (dev->genl_data.poll_req_portid == w->portid) {
4d12b8b1 1560 nfc_stop_poll(dev);
15e47304 1561 dev->genl_data.poll_req_portid = 0;
4d12b8b1 1562 }
3c0cc8aa
SJ
1563
1564 mutex_unlock(&dev->genl_data.genl_data_mutex);
1565
4d12b8b1
LRV
1566 dev = nfc_device_iter_next(&iter);
1567 }
1568
1569 nfc_device_iter_exit(&iter);
1570
3c0cc8aa
SJ
1571 mutex_unlock(&nfc_devlist_mutex);
1572
1573 kfree(w);
1574}
1575
1576static 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
c487606f 1585 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
3c0cc8aa
SJ
1586
1587 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1588 if (w) {
1589 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
c487606f 1590 w->portid = n->portid;
3c0cc8aa
SJ
1591 schedule_work((struct work_struct *) w);
1592 }
1593
4d12b8b1
LRV
1594out:
1595 return NOTIFY_DONE;
1596}
1597
1598void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1599{
15e47304 1600 genl_data->poll_req_portid = 0;
4d12b8b1
LRV
1601 mutex_init(&genl_data->genl_data_mutex);
1602}
1603
1604void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1605{
1606 mutex_destroy(&genl_data->genl_data_mutex);
1607}
1608
1609static 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 */
1618int __init nfc_genl_init(void)
1619{
1620 int rc;
1621
2a94fe48
JB
1622 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1623 nfc_genl_ops,
1624 nfc_genl_mcgrps);
4d12b8b1
LRV
1625 if (rc)
1626 return rc;
1627
4d12b8b1
LRV
1628 netlink_register_notifier(&nl_notifier);
1629
2a94fe48 1630 return 0;
4d12b8b1
LRV
1631}
1632
1633/**
1634 * nfc_genl_exit() - Deinitialize netlink interface
1635 *
1636 * This exit function unregisters the nfc netlink family.
1637 */
1638void nfc_genl_exit(void)
1639{
1640 netlink_unregister_notifier(&nl_notifier);
1641 genl_unregister_family(&nfc_genl_family);
1642}
This page took 0.45948 seconds and 5 git commands to generate.