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