Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[GitHub/mt8127/android_kernel_alcatel_ttab.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
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
52858b51 24#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
20c239c1 25
4d12b8b1
LRV
26#include <net/genetlink.h>
27#include <linux/nfc.h>
28#include <linux/slab.h>
29
30#include "nfc.h"
30cc4587 31#include "llcp.h"
52feb444 32
4d12b8b1
LRV
33static struct genl_multicast_group nfc_genl_event_mcgrp = {
34 .name = NFC_GENL_MCAST_EVENT_NAME,
35};
36
e5fe4cf8 37static struct genl_family nfc_genl_family = {
4d12b8b1
LRV
38 .id = GENL_ID_GENERATE,
39 .hdrsize = 0,
40 .name = NFC_GENL_NAME,
41 .version = NFC_GENL_VERSION,
42 .maxattr = NFC_ATTR_MAX,
43};
44
45static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
48 .len = NFC_DEVICE_NAME_MAXSIZE },
49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
1ed28f61
SO
50 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
c970a1ac 52 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
fe7c5800
SO
53 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
8af362d1
TE
55 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
d9b8d8e1
TE
58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59};
60
61static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
62 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
63 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
4d12b8b1
LRV
64};
65
66static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
0a40acb2 67 struct netlink_callback *cb, int flags)
4d12b8b1
LRV
68{
69 void *hdr;
70
15e47304 71 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
0a40acb2 72 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
4d12b8b1
LRV
73 if (!hdr)
74 return -EMSGSIZE;
75
76 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
77
1e6428d8
DM
78 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
79 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
80 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
81 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
82 goto nla_put_failure;
83 if (target->nfcid1_len > 0 &&
84 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
85 target->nfcid1))
86 goto nla_put_failure;
87 if (target->sensb_res_len > 0 &&
88 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
89 target->sensb_res))
90 goto nla_put_failure;
91 if (target->sensf_res_len > 0 &&
92 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
93 target->sensf_res))
94 goto nla_put_failure;
4d12b8b1
LRV
95
96 return genlmsg_end(msg, hdr);
97
98nla_put_failure:
99 genlmsg_cancel(msg, hdr);
100 return -EMSGSIZE;
101}
102
103static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
104{
105 struct nfc_dev *dev;
106 int rc;
107 u32 idx;
108
109 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
0a40acb2
SO
110 nfc_genl_family.attrbuf,
111 nfc_genl_family.maxattr,
112 nfc_genl_policy);
4d12b8b1
LRV
113 if (rc < 0)
114 return ERR_PTR(rc);
115
116 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
117 return ERR_PTR(-EINVAL);
118
119 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
120
121 dev = nfc_get_device(idx);
122 if (!dev)
123 return ERR_PTR(-ENODEV);
124
125 return dev;
126}
127
128static int nfc_genl_dump_targets(struct sk_buff *skb,
0a40acb2 129 struct netlink_callback *cb)
4d12b8b1
LRV
130{
131 int i = cb->args[0];
132 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
133 int rc;
134
4d12b8b1
LRV
135 if (!dev) {
136 dev = __get_device_from_cb(cb);
137 if (IS_ERR(dev))
138 return PTR_ERR(dev);
139
140 cb->args[1] = (long) dev;
141 }
142
d4ccb132 143 device_lock(&dev->dev);
4d12b8b1
LRV
144
145 cb->seq = dev->targets_generation;
146
147 while (i < dev->n_targets) {
148 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
0a40acb2 149 NLM_F_MULTI);
4d12b8b1
LRV
150 if (rc < 0)
151 break;
152
153 i++;
154 }
155
d4ccb132 156 device_unlock(&dev->dev);
4d12b8b1
LRV
157
158 cb->args[0] = i;
159
160 return skb->len;
161}
162
163static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
164{
165 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
166
4d12b8b1
LRV
167 if (dev)
168 nfc_put_device(dev);
169
170 return 0;
171}
172
173int nfc_genl_targets_found(struct nfc_dev *dev)
174{
175 struct sk_buff *msg;
176 void *hdr;
177
15e47304 178 dev->genl_data.poll_req_portid = 0;
4d12b8b1 179
58050fce 180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
4d12b8b1
LRV
181 if (!msg)
182 return -ENOMEM;
183
184 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 185 NFC_EVENT_TARGETS_FOUND);
4d12b8b1
LRV
186 if (!hdr)
187 goto free_msg;
188
1e6428d8
DM
189 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
190 goto nla_put_failure;
4d12b8b1
LRV
191
192 genlmsg_end(msg, hdr);
193
194 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
195
196nla_put_failure:
197 genlmsg_cancel(msg, hdr);
198free_msg:
199 nlmsg_free(msg);
200 return -EMSGSIZE;
201}
202
8112a5c9
SO
203int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
204{
205 struct sk_buff *msg;
206 void *hdr;
207
58050fce 208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8112a5c9
SO
209 if (!msg)
210 return -ENOMEM;
211
212 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
213 NFC_EVENT_TARGET_LOST);
214 if (!hdr)
215 goto free_msg;
216
59ef43e6
JL
217 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
218 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
219 goto nla_put_failure;
8112a5c9
SO
220
221 genlmsg_end(msg, hdr);
222
223 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
224
225 return 0;
226
fc40a8c1
SO
227nla_put_failure:
228 genlmsg_cancel(msg, hdr);
229free_msg:
230 nlmsg_free(msg);
231 return -EMSGSIZE;
232}
233
234int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
235{
236 struct sk_buff *msg;
237 void *hdr;
238
58050fce 239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fc40a8c1
SO
240 if (!msg)
241 return -ENOMEM;
242
243 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
244 NFC_EVENT_TM_ACTIVATED);
245 if (!hdr)
246 goto free_msg;
247
248 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
249 goto nla_put_failure;
250 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
251 goto nla_put_failure;
252
253 genlmsg_end(msg, hdr);
254
255 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
256
257 return 0;
258
259nla_put_failure:
260 genlmsg_cancel(msg, hdr);
261free_msg:
262 nlmsg_free(msg);
263 return -EMSGSIZE;
264}
265
266int nfc_genl_tm_deactivated(struct nfc_dev *dev)
267{
268 struct sk_buff *msg;
269 void *hdr;
270
58050fce 271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fc40a8c1
SO
272 if (!msg)
273 return -ENOMEM;
274
275 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
276 NFC_EVENT_TM_DEACTIVATED);
277 if (!hdr)
278 goto free_msg;
279
280 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
281 goto nla_put_failure;
282
283 genlmsg_end(msg, hdr);
284
285 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
286
287 return 0;
288
8112a5c9
SO
289nla_put_failure:
290 genlmsg_cancel(msg, hdr);
291free_msg:
292 nlmsg_free(msg);
293 return -EMSGSIZE;
294}
295
4d12b8b1
LRV
296int nfc_genl_device_added(struct nfc_dev *dev)
297{
298 struct sk_buff *msg;
299 void *hdr;
300
58050fce 301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
302 if (!msg)
303 return -ENOMEM;
304
305 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 306 NFC_EVENT_DEVICE_ADDED);
4d12b8b1
LRV
307 if (!hdr)
308 goto free_msg;
309
1e6428d8
DM
310 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
311 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
312 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
313 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
314 goto nla_put_failure;
4d12b8b1
LRV
315
316 genlmsg_end(msg, hdr);
317
318 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
319
320 return 0;
321
322nla_put_failure:
323 genlmsg_cancel(msg, hdr);
324free_msg:
325 nlmsg_free(msg);
326 return -EMSGSIZE;
327}
328
329int nfc_genl_device_removed(struct nfc_dev *dev)
330{
331 struct sk_buff *msg;
332 void *hdr;
333
58050fce 334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
335 if (!msg)
336 return -ENOMEM;
337
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 339 NFC_EVENT_DEVICE_REMOVED);
4d12b8b1
LRV
340 if (!hdr)
341 goto free_msg;
342
1e6428d8
DM
343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
344 goto nla_put_failure;
4d12b8b1
LRV
345
346 genlmsg_end(msg, hdr);
347
348 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
349
350 return 0;
351
352nla_put_failure:
353 genlmsg_cancel(msg, hdr);
354free_msg:
355 nlmsg_free(msg);
356 return -EMSGSIZE;
357}
358
d9b8d8e1
TE
359int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
360{
361 struct sk_buff *msg;
362 struct nlattr *sdp_attr, *uri_attr;
363 struct nfc_llcp_sdp_tlv *sdres;
364 struct hlist_node *n;
365 void *hdr;
366 int rc = -EMSGSIZE;
367 int i;
368
369 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
370 if (!msg)
371 return -ENOMEM;
372
373 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
374 NFC_EVENT_LLC_SDRES);
375 if (!hdr)
376 goto free_msg;
377
378 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
379 goto nla_put_failure;
380
381 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
382 if (sdp_attr == NULL) {
383 rc = -ENOMEM;
384 goto nla_put_failure;
385 }
386
387 i = 1;
388 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
389 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
390
391 uri_attr = nla_nest_start(msg, i++);
392 if (uri_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
395 }
396
397 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
398 goto nla_put_failure;
399
400 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
401 goto nla_put_failure;
402
403 nla_nest_end(msg, uri_attr);
404
405 hlist_del(&sdres->node);
406
407 nfc_llcp_free_sdp_tlv(sdres);
408 }
409
410 nla_nest_end(msg, sdp_attr);
411
412 genlmsg_end(msg, hdr);
413
414 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
415
416nla_put_failure:
417 genlmsg_cancel(msg, hdr);
418
419free_msg:
420 nlmsg_free(msg);
421
422 nfc_llcp_free_sdp_tlv_list(sdres_list);
423
424 return rc;
425}
426
4d12b8b1 427static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
15e47304 428 u32 portid, u32 seq,
0a40acb2
SO
429 struct netlink_callback *cb,
430 int flags)
4d12b8b1
LRV
431{
432 void *hdr;
433
15e47304 434 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
0a40acb2 435 NFC_CMD_GET_DEVICE);
4d12b8b1
LRV
436 if (!hdr)
437 return -EMSGSIZE;
438
439 if (cb)
440 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
441
1e6428d8
DM
442 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
443 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
444 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
390a1bd8 445 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
7ad39395
TE
446 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
447 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
1e6428d8 448 goto nla_put_failure;
4d12b8b1
LRV
449
450 return genlmsg_end(msg, hdr);
451
452nla_put_failure:
453 genlmsg_cancel(msg, hdr);
454 return -EMSGSIZE;
455}
456
457static int nfc_genl_dump_devices(struct sk_buff *skb,
0a40acb2 458 struct netlink_callback *cb)
4d12b8b1
LRV
459{
460 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
461 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
462 bool first_call = false;
463
4d12b8b1
LRV
464 if (!iter) {
465 first_call = true;
466 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
467 if (!iter)
468 return -ENOMEM;
469 cb->args[0] = (long) iter;
470 }
471
472 mutex_lock(&nfc_devlist_mutex);
473
474 cb->seq = nfc_devlist_generation;
475
476 if (first_call) {
477 nfc_device_iter_init(iter);
478 dev = nfc_device_iter_next(iter);
479 }
480
481 while (dev) {
482 int rc;
483
15e47304 484 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
0a40acb2 485 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
4d12b8b1
LRV
486 if (rc < 0)
487 break;
488
489 dev = nfc_device_iter_next(iter);
490 }
491
492 mutex_unlock(&nfc_devlist_mutex);
493
494 cb->args[1] = (long) dev;
495
496 return skb->len;
497}
498
499static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
500{
501 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
502
4d12b8b1
LRV
503 nfc_device_iter_exit(iter);
504 kfree(iter);
505
506 return 0;
507}
508
1ed28f61 509int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
0a40acb2 510 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
511{
512 struct sk_buff *msg;
513 void *hdr;
514
515 pr_debug("DEP link is up\n");
516
58050fce 517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1ed28f61
SO
518 if (!msg)
519 return -ENOMEM;
520
0a40acb2 521 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
1ed28f61
SO
522 if (!hdr)
523 goto free_msg;
524
1e6428d8
DM
525 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
526 goto nla_put_failure;
527 if (rf_mode == NFC_RF_INITIATOR &&
528 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
529 goto nla_put_failure;
530 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
531 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
532 goto nla_put_failure;
1ed28f61
SO
533
534 genlmsg_end(msg, hdr);
535
536 dev->dep_link_up = true;
537
538 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
539
540 return 0;
541
542nla_put_failure:
543 genlmsg_cancel(msg, hdr);
544free_msg:
545 nlmsg_free(msg);
546 return -EMSGSIZE;
547}
548
549int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
550{
551 struct sk_buff *msg;
552 void *hdr;
553
554 pr_debug("DEP link is down\n");
555
58050fce 556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1ed28f61
SO
557 if (!msg)
558 return -ENOMEM;
559
560 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 561 NFC_CMD_DEP_LINK_DOWN);
1ed28f61
SO
562 if (!hdr)
563 goto free_msg;
564
1e6428d8
DM
565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
566 goto nla_put_failure;
1ed28f61
SO
567
568 genlmsg_end(msg, hdr);
569
570 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
571
572 return 0;
573
574nla_put_failure:
575 genlmsg_cancel(msg, hdr);
576free_msg:
577 nlmsg_free(msg);
578 return -EMSGSIZE;
579}
580
4d12b8b1
LRV
581static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
582{
583 struct sk_buff *msg;
584 struct nfc_dev *dev;
585 u32 idx;
586 int rc = -ENOBUFS;
587
4d12b8b1
LRV
588 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
589 return -EINVAL;
590
591 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
592
593 dev = nfc_get_device(idx);
594 if (!dev)
595 return -ENODEV;
596
58050fce 597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4d12b8b1
LRV
598 if (!msg) {
599 rc = -ENOMEM;
600 goto out_putdev;
601 }
602
15e47304 603 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
0a40acb2 604 NULL, 0);
4d12b8b1
LRV
605 if (rc < 0)
606 goto out_free;
607
608 nfc_put_device(dev);
609
610 return genlmsg_reply(msg, info);
611
612out_free:
613 nlmsg_free(msg);
614out_putdev:
615 nfc_put_device(dev);
616 return rc;
617}
618
8b3fe7b5
IE
619static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
620{
621 struct nfc_dev *dev;
622 int rc;
623 u32 idx;
624
8b3fe7b5
IE
625 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
626 return -EINVAL;
627
628 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
629
630 dev = nfc_get_device(idx);
631 if (!dev)
632 return -ENODEV;
633
634 rc = nfc_dev_up(dev);
635
636 nfc_put_device(dev);
637 return rc;
638}
639
640static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
641{
642 struct nfc_dev *dev;
643 int rc;
644 u32 idx;
645
8b3fe7b5
IE
646 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
647 return -EINVAL;
648
649 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
650
651 dev = nfc_get_device(idx);
652 if (!dev)
653 return -ENODEV;
654
655 rc = nfc_dev_down(dev);
656
657 nfc_put_device(dev);
658 return rc;
659}
660
4d12b8b1
LRV
661static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
662{
663 struct nfc_dev *dev;
664 int rc;
665 u32 idx;
fe7c5800 666 u32 im_protocols = 0, tm_protocols = 0;
4d12b8b1 667
1ed28f61
SO
668 pr_debug("Poll start\n");
669
4d12b8b1 670 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
fe7c5800
SO
671 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
672 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
0f450772 673 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
4d12b8b1
LRV
674 return -EINVAL;
675
676 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
fe7c5800
SO
677
678 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
679 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
fe7c5800
SO
680
681 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
682 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
5e50ee3a
SO
683 else if (info->attrs[NFC_ATTR_PROTOCOLS])
684 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
4d12b8b1
LRV
685
686 dev = nfc_get_device(idx);
687 if (!dev)
688 return -ENODEV;
689
690 mutex_lock(&dev->genl_data.genl_data_mutex);
691
fe7c5800 692 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
4d12b8b1 693 if (!rc)
15e47304 694 dev->genl_data.poll_req_portid = info->snd_portid;
4d12b8b1
LRV
695
696 mutex_unlock(&dev->genl_data.genl_data_mutex);
697
698 nfc_put_device(dev);
699 return rc;
700}
701
702static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
703{
704 struct nfc_dev *dev;
705 int rc;
706 u32 idx;
707
4d12b8b1
LRV
708 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
709 return -EINVAL;
710
711 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
712
713 dev = nfc_get_device(idx);
714 if (!dev)
715 return -ENODEV;
716
a831b913
SO
717 device_lock(&dev->dev);
718
719 if (!dev->polling) {
720 device_unlock(&dev->dev);
721 return -EINVAL;
722 }
723
724 device_unlock(&dev->dev);
725
4d12b8b1
LRV
726 mutex_lock(&dev->genl_data.genl_data_mutex);
727
15e47304 728 if (dev->genl_data.poll_req_portid != info->snd_portid) {
4d12b8b1
LRV
729 rc = -EBUSY;
730 goto out;
731 }
732
733 rc = nfc_stop_poll(dev);
15e47304 734 dev->genl_data.poll_req_portid = 0;
4d12b8b1
LRV
735
736out:
737 mutex_unlock(&dev->genl_data.genl_data_mutex);
738 nfc_put_device(dev);
739 return rc;
740}
741
1ed28f61
SO
742static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
743{
744 struct nfc_dev *dev;
745 int rc, tgt_idx;
746 u32 idx;
47807d3d 747 u8 comm;
1ed28f61
SO
748
749 pr_debug("DEP link up\n");
750
751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
47807d3d 752 !info->attrs[NFC_ATTR_COMM_MODE])
1ed28f61
SO
753 return -EINVAL;
754
755 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
756 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
757 tgt_idx = NFC_TARGET_IDX_ANY;
758 else
759 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
760
761 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
1ed28f61
SO
762
763 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
764 return -EINVAL;
765
1ed28f61
SO
766 dev = nfc_get_device(idx);
767 if (!dev)
768 return -ENODEV;
769
47807d3d 770 rc = nfc_dep_link_up(dev, tgt_idx, comm);
1ed28f61
SO
771
772 nfc_put_device(dev);
773
774 return rc;
775}
776
777static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
778{
779 struct nfc_dev *dev;
780 int rc;
781 u32 idx;
782
783 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
784 return -EINVAL;
785
786 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
787
788 dev = nfc_get_device(idx);
789 if (!dev)
790 return -ENODEV;
791
792 rc = nfc_dep_link_down(dev);
793
794 nfc_put_device(dev);
795 return rc;
796}
797
52feb444
TE
798static int nfc_genl_send_params(struct sk_buff *msg,
799 struct nfc_llcp_local *local,
800 u32 portid, u32 seq)
801{
802 void *hdr;
803
804 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
805 NFC_CMD_LLC_GET_PARAMS);
806 if (!hdr)
807 return -EMSGSIZE;
808
809 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
810 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
811 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
812 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
813 goto nla_put_failure;
814
815 return genlmsg_end(msg, hdr);
816
817nla_put_failure:
818
819 genlmsg_cancel(msg, hdr);
820 return -EMSGSIZE;
821}
822
823static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
824{
825 struct nfc_dev *dev;
826 struct nfc_llcp_local *local;
827 int rc = 0;
828 struct sk_buff *msg = NULL;
829 u32 idx;
830
831 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
832 return -EINVAL;
833
834 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
835
836 dev = nfc_get_device(idx);
837 if (!dev)
838 return -ENODEV;
839
840 device_lock(&dev->dev);
841
842 local = nfc_llcp_find_local(dev);
843 if (!local) {
844 rc = -ENODEV;
845 goto exit;
846 }
847
848 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
849 if (!msg) {
850 rc = -ENOMEM;
851 goto exit;
852 }
853
854 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
855
856exit:
857 device_unlock(&dev->dev);
858
859 nfc_put_device(dev);
860
861 if (rc < 0) {
862 if (msg)
863 nlmsg_free(msg);
864
865 return rc;
866 }
867
868 return genlmsg_reply(msg, info);
869}
870
871static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
872{
873 struct nfc_dev *dev;
874 struct nfc_llcp_local *local;
875 u8 rw = 0;
876 u16 miux = 0;
877 u32 idx;
878 int rc = 0;
879
880 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
881 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
882 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
883 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
884 return -EINVAL;
885
886 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
887 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
888
889 if (rw > LLCP_MAX_RW)
890 return -EINVAL;
891 }
892
893 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
894 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
895
896 if (miux > LLCP_MAX_MIUX)
897 return -EINVAL;
898 }
899
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901
902 dev = nfc_get_device(idx);
903 if (!dev)
904 return -ENODEV;
905
906 device_lock(&dev->dev);
907
908 local = nfc_llcp_find_local(dev);
909 if (!local) {
910 nfc_put_device(dev);
911 rc = -ENODEV;
912 goto exit;
913 }
914
915 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
916 if (dev->dep_link_up) {
917 rc = -EINPROGRESS;
918 goto exit;
919 }
920
921 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
922 }
923
924 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
925 local->rw = rw;
926
927 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
928 local->miux = cpu_to_be16(miux);
929
930exit:
931 device_unlock(&dev->dev);
932
933 nfc_put_device(dev);
934
935 return rc;
936}
937
d9b8d8e1
TE
938static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
939{
940 struct nfc_dev *dev;
941 struct nfc_llcp_local *local;
942 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
943 u32 idx;
944 u8 tid;
945 char *uri;
946 int rc = 0, rem;
947 size_t uri_len, tlvs_len;
948 struct hlist_head sdreq_list;
949 struct nfc_llcp_sdp_tlv *sdreq;
950
951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
952 !info->attrs[NFC_ATTR_LLC_SDP])
953 return -EINVAL;
954
955 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
956
957 dev = nfc_get_device(idx);
958 if (!dev) {
959 rc = -ENODEV;
960 goto exit;
961 }
962
963 device_lock(&dev->dev);
964
965 if (dev->dep_link_up == false) {
966 rc = -ENOLINK;
967 goto exit;
968 }
969
970 local = nfc_llcp_find_local(dev);
971 if (!local) {
972 nfc_put_device(dev);
973 rc = -ENODEV;
974 goto exit;
975 }
976
977 INIT_HLIST_HEAD(&sdreq_list);
978
979 tlvs_len = 0;
980
981 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
982 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
983 nfc_sdp_genl_policy);
984
985 if (rc != 0) {
986 rc = -EINVAL;
987 goto exit;
988 }
989
990 if (!sdp_attrs[NFC_SDP_ATTR_URI])
991 continue;
992
993 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
994 if (uri_len == 0)
995 continue;
996
997 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
998 if (uri == NULL || *uri == 0)
999 continue;
1000
1001 tid = local->sdreq_next_tid++;
1002
1003 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1004 if (sdreq == NULL) {
1005 rc = -ENOMEM;
1006 goto exit;
1007 }
1008
1009 tlvs_len += sdreq->tlv_len;
1010
1011 hlist_add_head(&sdreq->node, &sdreq_list);
1012 }
1013
1014 if (hlist_empty(&sdreq_list)) {
1015 rc = -EINVAL;
1016 goto exit;
1017 }
1018
1019 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1020exit:
1021 device_unlock(&dev->dev);
1022
1023 nfc_put_device(dev);
1024
1025 return rc;
1026}
1027
4d12b8b1
LRV
1028static struct genl_ops nfc_genl_ops[] = {
1029 {
1030 .cmd = NFC_CMD_GET_DEVICE,
1031 .doit = nfc_genl_get_device,
1032 .dumpit = nfc_genl_dump_devices,
1033 .done = nfc_genl_dump_devices_done,
1034 .policy = nfc_genl_policy,
8b3fe7b5
IE
1035 },
1036 {
1037 .cmd = NFC_CMD_DEV_UP,
1038 .doit = nfc_genl_dev_up,
1039 .policy = nfc_genl_policy,
1040 },
1041 {
1042 .cmd = NFC_CMD_DEV_DOWN,
1043 .doit = nfc_genl_dev_down,
1044 .policy = nfc_genl_policy,
4d12b8b1
LRV
1045 },
1046 {
1047 .cmd = NFC_CMD_START_POLL,
1048 .doit = nfc_genl_start_poll,
1049 .policy = nfc_genl_policy,
1050 },
1051 {
1052 .cmd = NFC_CMD_STOP_POLL,
1053 .doit = nfc_genl_stop_poll,
1054 .policy = nfc_genl_policy,
1055 },
1ed28f61
SO
1056 {
1057 .cmd = NFC_CMD_DEP_LINK_UP,
1058 .doit = nfc_genl_dep_link_up,
1059 .policy = nfc_genl_policy,
1060 },
1061 {
1062 .cmd = NFC_CMD_DEP_LINK_DOWN,
1063 .doit = nfc_genl_dep_link_down,
1064 .policy = nfc_genl_policy,
1065 },
4d12b8b1
LRV
1066 {
1067 .cmd = NFC_CMD_GET_TARGET,
1068 .dumpit = nfc_genl_dump_targets,
1069 .done = nfc_genl_dump_targets_done,
1070 .policy = nfc_genl_policy,
1071 },
52feb444
TE
1072 {
1073 .cmd = NFC_CMD_LLC_GET_PARAMS,
1074 .doit = nfc_genl_llc_get_params,
1075 .policy = nfc_genl_policy,
1076 },
1077 {
1078 .cmd = NFC_CMD_LLC_SET_PARAMS,
1079 .doit = nfc_genl_llc_set_params,
1080 .policy = nfc_genl_policy,
1081 },
d9b8d8e1
TE
1082 {
1083 .cmd = NFC_CMD_LLC_SDREQ,
1084 .doit = nfc_genl_llc_sdreq,
1085 .policy = nfc_genl_policy,
1086 },
4d12b8b1
LRV
1087};
1088
3c0cc8aa
SJ
1089
1090struct urelease_work {
1091 struct work_struct w;
c487606f 1092 int portid;
3c0cc8aa
SJ
1093};
1094
1095static void nfc_urelease_event_work(struct work_struct *work)
4d12b8b1 1096{
3c0cc8aa 1097 struct urelease_work *w = container_of(work, struct urelease_work, w);
4d12b8b1
LRV
1098 struct class_dev_iter iter;
1099 struct nfc_dev *dev;
1100
c487606f 1101 pr_debug("portid %d\n", w->portid);
4d12b8b1 1102
3c0cc8aa 1103 mutex_lock(&nfc_devlist_mutex);
4d12b8b1
LRV
1104
1105 nfc_device_iter_init(&iter);
1106 dev = nfc_device_iter_next(&iter);
1107
1108 while (dev) {
3c0cc8aa
SJ
1109 mutex_lock(&dev->genl_data.genl_data_mutex);
1110
c487606f 1111 if (dev->genl_data.poll_req_portid == w->portid) {
4d12b8b1 1112 nfc_stop_poll(dev);
15e47304 1113 dev->genl_data.poll_req_portid = 0;
4d12b8b1 1114 }
3c0cc8aa
SJ
1115
1116 mutex_unlock(&dev->genl_data.genl_data_mutex);
1117
4d12b8b1
LRV
1118 dev = nfc_device_iter_next(&iter);
1119 }
1120
1121 nfc_device_iter_exit(&iter);
1122
3c0cc8aa
SJ
1123 mutex_unlock(&nfc_devlist_mutex);
1124
1125 kfree(w);
1126}
1127
1128static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1129 unsigned long event, void *ptr)
1130{
1131 struct netlink_notify *n = ptr;
1132 struct urelease_work *w;
1133
1134 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1135 goto out;
1136
c487606f 1137 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
3c0cc8aa
SJ
1138
1139 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1140 if (w) {
1141 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
c487606f 1142 w->portid = n->portid;
3c0cc8aa
SJ
1143 schedule_work((struct work_struct *) w);
1144 }
1145
4d12b8b1
LRV
1146out:
1147 return NOTIFY_DONE;
1148}
1149
1150void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1151{
15e47304 1152 genl_data->poll_req_portid = 0;
4d12b8b1
LRV
1153 mutex_init(&genl_data->genl_data_mutex);
1154}
1155
1156void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1157{
1158 mutex_destroy(&genl_data->genl_data_mutex);
1159}
1160
1161static struct notifier_block nl_notifier = {
1162 .notifier_call = nfc_genl_rcv_nl_event,
1163};
1164
1165/**
1166 * nfc_genl_init() - Initialize netlink interface
1167 *
1168 * This initialization function registers the nfc netlink family.
1169 */
1170int __init nfc_genl_init(void)
1171{
1172 int rc;
1173
1174 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
0a40acb2 1175 ARRAY_SIZE(nfc_genl_ops));
4d12b8b1
LRV
1176 if (rc)
1177 return rc;
1178
1179 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1180
1181 netlink_register_notifier(&nl_notifier);
1182
1183 return rc;
1184}
1185
1186/**
1187 * nfc_genl_exit() - Deinitialize netlink interface
1188 *
1189 * This exit function unregisters the nfc netlink family.
1190 */
1191void nfc_genl_exit(void)
1192{
1193 netlink_unregister_notifier(&nl_notifier);
1194 genl_unregister_family(&nfc_genl_family);
1195}