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