]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/nfc/netlink.c
NFC: Implement pn533 target mode polling loop
[mirror_ubuntu-zesty-kernel.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"
31
32static struct genl_multicast_group nfc_genl_event_mcgrp = {
33 .name = NFC_GENL_MCAST_EVENT_NAME,
34};
35
e5fe4cf8 36static struct genl_family nfc_genl_family = {
4d12b8b1
LRV
37 .id = GENL_ID_GENERATE,
38 .hdrsize = 0,
39 .name = NFC_GENL_NAME,
40 .version = NFC_GENL_VERSION,
41 .maxattr = NFC_ATTR_MAX,
42};
43
44static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
45 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
46 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
47 .len = NFC_DEVICE_NAME_MAXSIZE },
48 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
1ed28f61
SO
49 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
c970a1ac 51 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
fe7c5800
SO
52 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
53 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
4d12b8b1
LRV
54};
55
56static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
0a40acb2 57 struct netlink_callback *cb, int flags)
4d12b8b1
LRV
58{
59 void *hdr;
60
4d12b8b1 61 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
0a40acb2 62 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
4d12b8b1
LRV
63 if (!hdr)
64 return -EMSGSIZE;
65
66 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
67
1e6428d8
DM
68 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
69 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
70 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
71 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
72 goto nla_put_failure;
73 if (target->nfcid1_len > 0 &&
74 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
75 target->nfcid1))
76 goto nla_put_failure;
77 if (target->sensb_res_len > 0 &&
78 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
79 target->sensb_res))
80 goto nla_put_failure;
81 if (target->sensf_res_len > 0 &&
82 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
83 target->sensf_res))
84 goto nla_put_failure;
4d12b8b1
LRV
85
86 return genlmsg_end(msg, hdr);
87
88nla_put_failure:
89 genlmsg_cancel(msg, hdr);
90 return -EMSGSIZE;
91}
92
93static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
94{
95 struct nfc_dev *dev;
96 int rc;
97 u32 idx;
98
99 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
0a40acb2
SO
100 nfc_genl_family.attrbuf,
101 nfc_genl_family.maxattr,
102 nfc_genl_policy);
4d12b8b1
LRV
103 if (rc < 0)
104 return ERR_PTR(rc);
105
106 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
107 return ERR_PTR(-EINVAL);
108
109 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
110
111 dev = nfc_get_device(idx);
112 if (!dev)
113 return ERR_PTR(-ENODEV);
114
115 return dev;
116}
117
118static int nfc_genl_dump_targets(struct sk_buff *skb,
0a40acb2 119 struct netlink_callback *cb)
4d12b8b1
LRV
120{
121 int i = cb->args[0];
122 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
123 int rc;
124
4d12b8b1
LRV
125 if (!dev) {
126 dev = __get_device_from_cb(cb);
127 if (IS_ERR(dev))
128 return PTR_ERR(dev);
129
130 cb->args[1] = (long) dev;
131 }
132
d4ccb132 133 device_lock(&dev->dev);
4d12b8b1
LRV
134
135 cb->seq = dev->targets_generation;
136
137 while (i < dev->n_targets) {
138 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
0a40acb2 139 NLM_F_MULTI);
4d12b8b1
LRV
140 if (rc < 0)
141 break;
142
143 i++;
144 }
145
d4ccb132 146 device_unlock(&dev->dev);
4d12b8b1
LRV
147
148 cb->args[0] = i;
149
150 return skb->len;
151}
152
153static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
154{
155 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
156
4d12b8b1
LRV
157 if (dev)
158 nfc_put_device(dev);
159
160 return 0;
161}
162
163int nfc_genl_targets_found(struct nfc_dev *dev)
164{
165 struct sk_buff *msg;
166 void *hdr;
167
4d12b8b1
LRV
168 dev->genl_data.poll_req_pid = 0;
169
170 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
171 if (!msg)
172 return -ENOMEM;
173
174 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 175 NFC_EVENT_TARGETS_FOUND);
4d12b8b1
LRV
176 if (!hdr)
177 goto free_msg;
178
1e6428d8
DM
179 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
180 goto nla_put_failure;
4d12b8b1
LRV
181
182 genlmsg_end(msg, hdr);
183
184 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
185
186nla_put_failure:
187 genlmsg_cancel(msg, hdr);
188free_msg:
189 nlmsg_free(msg);
190 return -EMSGSIZE;
191}
192
8112a5c9
SO
193int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
194{
195 struct sk_buff *msg;
196 void *hdr;
197
198 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
199 if (!msg)
200 return -ENOMEM;
201
202 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
203 NFC_EVENT_TARGET_LOST);
204 if (!hdr)
205 goto free_msg;
206
59ef43e6
JL
207 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
208 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
209 goto nla_put_failure;
8112a5c9
SO
210
211 genlmsg_end(msg, hdr);
212
213 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
214
215 return 0;
216
217nla_put_failure:
218 genlmsg_cancel(msg, hdr);
219free_msg:
220 nlmsg_free(msg);
221 return -EMSGSIZE;
222}
223
4d12b8b1
LRV
224int nfc_genl_device_added(struct nfc_dev *dev)
225{
226 struct sk_buff *msg;
227 void *hdr;
228
4d12b8b1
LRV
229 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
230 if (!msg)
231 return -ENOMEM;
232
233 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 234 NFC_EVENT_DEVICE_ADDED);
4d12b8b1
LRV
235 if (!hdr)
236 goto free_msg;
237
1e6428d8
DM
238 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
239 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
240 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
241 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
242 goto nla_put_failure;
4d12b8b1
LRV
243
244 genlmsg_end(msg, hdr);
245
246 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
247
248 return 0;
249
250nla_put_failure:
251 genlmsg_cancel(msg, hdr);
252free_msg:
253 nlmsg_free(msg);
254 return -EMSGSIZE;
255}
256
257int nfc_genl_device_removed(struct nfc_dev *dev)
258{
259 struct sk_buff *msg;
260 void *hdr;
261
4d12b8b1
LRV
262 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
263 if (!msg)
264 return -ENOMEM;
265
266 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 267 NFC_EVENT_DEVICE_REMOVED);
4d12b8b1
LRV
268 if (!hdr)
269 goto free_msg;
270
1e6428d8
DM
271 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
272 goto nla_put_failure;
4d12b8b1
LRV
273
274 genlmsg_end(msg, hdr);
275
276 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
277
278 return 0;
279
280nla_put_failure:
281 genlmsg_cancel(msg, hdr);
282free_msg:
283 nlmsg_free(msg);
284 return -EMSGSIZE;
285}
286
287static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
0a40acb2
SO
288 u32 pid, u32 seq,
289 struct netlink_callback *cb,
290 int flags)
4d12b8b1
LRV
291{
292 void *hdr;
293
4d12b8b1 294 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
0a40acb2 295 NFC_CMD_GET_DEVICE);
4d12b8b1
LRV
296 if (!hdr)
297 return -EMSGSIZE;
298
299 if (cb)
300 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
301
1e6428d8
DM
302 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
303 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
304 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
305 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
306 goto nla_put_failure;
4d12b8b1
LRV
307
308 return genlmsg_end(msg, hdr);
309
310nla_put_failure:
311 genlmsg_cancel(msg, hdr);
312 return -EMSGSIZE;
313}
314
315static int nfc_genl_dump_devices(struct sk_buff *skb,
0a40acb2 316 struct netlink_callback *cb)
4d12b8b1
LRV
317{
318 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
319 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
320 bool first_call = false;
321
4d12b8b1
LRV
322 if (!iter) {
323 first_call = true;
324 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
325 if (!iter)
326 return -ENOMEM;
327 cb->args[0] = (long) iter;
328 }
329
330 mutex_lock(&nfc_devlist_mutex);
331
332 cb->seq = nfc_devlist_generation;
333
334 if (first_call) {
335 nfc_device_iter_init(iter);
336 dev = nfc_device_iter_next(iter);
337 }
338
339 while (dev) {
340 int rc;
341
342 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
0a40acb2 343 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
4d12b8b1
LRV
344 if (rc < 0)
345 break;
346
347 dev = nfc_device_iter_next(iter);
348 }
349
350 mutex_unlock(&nfc_devlist_mutex);
351
352 cb->args[1] = (long) dev;
353
354 return skb->len;
355}
356
357static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
358{
359 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
360
4d12b8b1
LRV
361 nfc_device_iter_exit(iter);
362 kfree(iter);
363
364 return 0;
365}
366
1ed28f61 367int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
0a40acb2 368 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
369{
370 struct sk_buff *msg;
371 void *hdr;
372
373 pr_debug("DEP link is up\n");
374
375 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
376 if (!msg)
377 return -ENOMEM;
378
0a40acb2 379 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
1ed28f61
SO
380 if (!hdr)
381 goto free_msg;
382
1e6428d8
DM
383 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
384 goto nla_put_failure;
385 if (rf_mode == NFC_RF_INITIATOR &&
386 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
387 goto nla_put_failure;
388 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
389 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
390 goto nla_put_failure;
1ed28f61
SO
391
392 genlmsg_end(msg, hdr);
393
394 dev->dep_link_up = true;
395
396 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
397
398 return 0;
399
400nla_put_failure:
401 genlmsg_cancel(msg, hdr);
402free_msg:
403 nlmsg_free(msg);
404 return -EMSGSIZE;
405}
406
407int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
408{
409 struct sk_buff *msg;
410 void *hdr;
411
412 pr_debug("DEP link is down\n");
413
414 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
415 if (!msg)
416 return -ENOMEM;
417
418 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0a40acb2 419 NFC_CMD_DEP_LINK_DOWN);
1ed28f61
SO
420 if (!hdr)
421 goto free_msg;
422
1e6428d8
DM
423 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
424 goto nla_put_failure;
1ed28f61
SO
425
426 genlmsg_end(msg, hdr);
427
428 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
429
430 return 0;
431
432nla_put_failure:
433 genlmsg_cancel(msg, hdr);
434free_msg:
435 nlmsg_free(msg);
436 return -EMSGSIZE;
437}
438
4d12b8b1
LRV
439static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
440{
441 struct sk_buff *msg;
442 struct nfc_dev *dev;
443 u32 idx;
444 int rc = -ENOBUFS;
445
4d12b8b1
LRV
446 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
447 return -EINVAL;
448
449 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
450
451 dev = nfc_get_device(idx);
452 if (!dev)
453 return -ENODEV;
454
455 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
456 if (!msg) {
457 rc = -ENOMEM;
458 goto out_putdev;
459 }
460
461 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
0a40acb2 462 NULL, 0);
4d12b8b1
LRV
463 if (rc < 0)
464 goto out_free;
465
466 nfc_put_device(dev);
467
468 return genlmsg_reply(msg, info);
469
470out_free:
471 nlmsg_free(msg);
472out_putdev:
473 nfc_put_device(dev);
474 return rc;
475}
476
8b3fe7b5
IE
477static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
478{
479 struct nfc_dev *dev;
480 int rc;
481 u32 idx;
482
8b3fe7b5
IE
483 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
484 return -EINVAL;
485
486 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
487
488 dev = nfc_get_device(idx);
489 if (!dev)
490 return -ENODEV;
491
492 rc = nfc_dev_up(dev);
493
494 nfc_put_device(dev);
495 return rc;
496}
497
498static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
499{
500 struct nfc_dev *dev;
501 int rc;
502 u32 idx;
503
8b3fe7b5
IE
504 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
505 return -EINVAL;
506
507 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
508
509 dev = nfc_get_device(idx);
510 if (!dev)
511 return -ENODEV;
512
513 rc = nfc_dev_down(dev);
514
515 nfc_put_device(dev);
516 return rc;
517}
518
4d12b8b1
LRV
519static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
520{
521 struct nfc_dev *dev;
522 int rc;
523 u32 idx;
fe7c5800 524 u32 im_protocols = 0, tm_protocols = 0;
4d12b8b1 525
1ed28f61
SO
526 pr_debug("Poll start\n");
527
4d12b8b1 528 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
fe7c5800
SO
529 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
530 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
531 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
4d12b8b1
LRV
532 return -EINVAL;
533
534 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
fe7c5800
SO
535
536 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
537 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
538 else if (info->attrs[NFC_ATTR_PROTOCOLS])
539 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
540
541 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
542 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
4d12b8b1
LRV
543
544 dev = nfc_get_device(idx);
545 if (!dev)
546 return -ENODEV;
547
548 mutex_lock(&dev->genl_data.genl_data_mutex);
549
fe7c5800 550 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
4d12b8b1
LRV
551 if (!rc)
552 dev->genl_data.poll_req_pid = info->snd_pid;
553
554 mutex_unlock(&dev->genl_data.genl_data_mutex);
555
556 nfc_put_device(dev);
557 return rc;
558}
559
560static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
561{
562 struct nfc_dev *dev;
563 int rc;
564 u32 idx;
565
4d12b8b1
LRV
566 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
567 return -EINVAL;
568
569 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
570
571 dev = nfc_get_device(idx);
572 if (!dev)
573 return -ENODEV;
574
575 mutex_lock(&dev->genl_data.genl_data_mutex);
576
577 if (dev->genl_data.poll_req_pid != info->snd_pid) {
578 rc = -EBUSY;
579 goto out;
580 }
581
582 rc = nfc_stop_poll(dev);
583 dev->genl_data.poll_req_pid = 0;
584
585out:
586 mutex_unlock(&dev->genl_data.genl_data_mutex);
587 nfc_put_device(dev);
588 return rc;
589}
590
1ed28f61
SO
591static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
592{
593 struct nfc_dev *dev;
594 int rc, tgt_idx;
595 u32 idx;
47807d3d 596 u8 comm;
1ed28f61
SO
597
598 pr_debug("DEP link up\n");
599
600 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
47807d3d 601 !info->attrs[NFC_ATTR_COMM_MODE])
1ed28f61
SO
602 return -EINVAL;
603
604 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
605 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
606 tgt_idx = NFC_TARGET_IDX_ANY;
607 else
608 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
609
610 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
1ed28f61
SO
611
612 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
613 return -EINVAL;
614
1ed28f61
SO
615 dev = nfc_get_device(idx);
616 if (!dev)
617 return -ENODEV;
618
47807d3d 619 rc = nfc_dep_link_up(dev, tgt_idx, comm);
1ed28f61
SO
620
621 nfc_put_device(dev);
622
623 return rc;
624}
625
626static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
627{
628 struct nfc_dev *dev;
629 int rc;
630 u32 idx;
631
632 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
633 return -EINVAL;
634
635 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
636
637 dev = nfc_get_device(idx);
638 if (!dev)
639 return -ENODEV;
640
641 rc = nfc_dep_link_down(dev);
642
643 nfc_put_device(dev);
644 return rc;
645}
646
4d12b8b1
LRV
647static struct genl_ops nfc_genl_ops[] = {
648 {
649 .cmd = NFC_CMD_GET_DEVICE,
650 .doit = nfc_genl_get_device,
651 .dumpit = nfc_genl_dump_devices,
652 .done = nfc_genl_dump_devices_done,
653 .policy = nfc_genl_policy,
8b3fe7b5
IE
654 },
655 {
656 .cmd = NFC_CMD_DEV_UP,
657 .doit = nfc_genl_dev_up,
658 .policy = nfc_genl_policy,
659 },
660 {
661 .cmd = NFC_CMD_DEV_DOWN,
662 .doit = nfc_genl_dev_down,
663 .policy = nfc_genl_policy,
4d12b8b1
LRV
664 },
665 {
666 .cmd = NFC_CMD_START_POLL,
667 .doit = nfc_genl_start_poll,
668 .policy = nfc_genl_policy,
669 },
670 {
671 .cmd = NFC_CMD_STOP_POLL,
672 .doit = nfc_genl_stop_poll,
673 .policy = nfc_genl_policy,
674 },
1ed28f61
SO
675 {
676 .cmd = NFC_CMD_DEP_LINK_UP,
677 .doit = nfc_genl_dep_link_up,
678 .policy = nfc_genl_policy,
679 },
680 {
681 .cmd = NFC_CMD_DEP_LINK_DOWN,
682 .doit = nfc_genl_dep_link_down,
683 .policy = nfc_genl_policy,
684 },
4d12b8b1
LRV
685 {
686 .cmd = NFC_CMD_GET_TARGET,
687 .dumpit = nfc_genl_dump_targets,
688 .done = nfc_genl_dump_targets_done,
689 .policy = nfc_genl_policy,
690 },
691};
692
693static int nfc_genl_rcv_nl_event(struct notifier_block *this,
0a40acb2 694 unsigned long event, void *ptr)
4d12b8b1
LRV
695{
696 struct netlink_notify *n = ptr;
697 struct class_dev_iter iter;
698 struct nfc_dev *dev;
699
700 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
701 goto out;
702
20c239c1 703 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
4d12b8b1
LRV
704
705 nfc_device_iter_init(&iter);
706 dev = nfc_device_iter_next(&iter);
707
708 while (dev) {
4d12b8b1
LRV
709 if (dev->genl_data.poll_req_pid == n->pid) {
710 nfc_stop_poll(dev);
711 dev->genl_data.poll_req_pid = 0;
712 }
4d12b8b1
LRV
713 dev = nfc_device_iter_next(&iter);
714 }
715
716 nfc_device_iter_exit(&iter);
717
718out:
719 return NOTIFY_DONE;
720}
721
722void nfc_genl_data_init(struct nfc_genl_data *genl_data)
723{
724 genl_data->poll_req_pid = 0;
725 mutex_init(&genl_data->genl_data_mutex);
726}
727
728void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
729{
730 mutex_destroy(&genl_data->genl_data_mutex);
731}
732
733static struct notifier_block nl_notifier = {
734 .notifier_call = nfc_genl_rcv_nl_event,
735};
736
737/**
738 * nfc_genl_init() - Initialize netlink interface
739 *
740 * This initialization function registers the nfc netlink family.
741 */
742int __init nfc_genl_init(void)
743{
744 int rc;
745
746 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
0a40acb2 747 ARRAY_SIZE(nfc_genl_ops));
4d12b8b1
LRV
748 if (rc)
749 return rc;
750
751 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
752
753 netlink_register_notifier(&nl_notifier);
754
755 return rc;
756}
757
758/**
759 * nfc_genl_exit() - Deinitialize netlink interface
760 *
761 * This exit function unregisters the nfc netlink family.
762 */
763void nfc_genl_exit(void)
764{
765 netlink_unregister_notifier(&nl_notifier);
766 genl_unregister_family(&nfc_genl_family);
767}