blob: 5aef1a5daf959d4e5f276a67ddc7ec719926f541 [file] [log] [blame]
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001/*
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
Joe Perches20c239c2011-11-29 11:37:33 -080024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030026#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 },
49};
50
51static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
52 struct netlink_callback *cb, int flags)
53{
54 void *hdr;
55
Joe Perches20c239c2011-11-29 11:37:33 -080056 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030057
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
73nla_put_failure:
74 genlmsg_cancel(msg, hdr);
75 return -EMSGSIZE;
76}
77
78static 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
103static 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
Joe Perches20c239c2011-11-29 11:37:33 -0800110 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300111
112 if (!dev) {
113 dev = __get_device_from_cb(cb);
114 if (IS_ERR(dev))
115 return PTR_ERR(dev);
116
117 cb->args[1] = (long) dev;
118 }
119
120 spin_lock_bh(&dev->targets_lock);
121
122 cb->seq = dev->targets_generation;
123
124 while (i < dev->n_targets) {
125 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
126 NLM_F_MULTI);
127 if (rc < 0)
128 break;
129
130 i++;
131 }
132
133 spin_unlock_bh(&dev->targets_lock);
134
135 cb->args[0] = i;
136
137 return skb->len;
138}
139
140static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
141{
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
143
Joe Perches20c239c2011-11-29 11:37:33 -0800144 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145
146 if (dev)
147 nfc_put_device(dev);
148
149 return 0;
150}
151
152int nfc_genl_targets_found(struct nfc_dev *dev)
153{
154 struct sk_buff *msg;
155 void *hdr;
156
Joe Perches20c239c2011-11-29 11:37:33 -0800157 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300158
159 dev->genl_data.poll_req_pid = 0;
160
161 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
162 if (!msg)
163 return -ENOMEM;
164
165 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
166 NFC_EVENT_TARGETS_FOUND);
167 if (!hdr)
168 goto free_msg;
169
170 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
171
172 genlmsg_end(msg, hdr);
173
174 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
175
176nla_put_failure:
177 genlmsg_cancel(msg, hdr);
178free_msg:
179 nlmsg_free(msg);
180 return -EMSGSIZE;
181}
182
183int nfc_genl_device_added(struct nfc_dev *dev)
184{
185 struct sk_buff *msg;
186 void *hdr;
187
Joe Perches20c239c2011-11-29 11:37:33 -0800188 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300189
190 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
191 if (!msg)
192 return -ENOMEM;
193
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
195 NFC_EVENT_DEVICE_ADDED);
196 if (!hdr)
197 goto free_msg;
198
199 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
200 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
201 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
202
203 genlmsg_end(msg, hdr);
204
205 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
206
207 return 0;
208
209nla_put_failure:
210 genlmsg_cancel(msg, hdr);
211free_msg:
212 nlmsg_free(msg);
213 return -EMSGSIZE;
214}
215
216int nfc_genl_device_removed(struct nfc_dev *dev)
217{
218 struct sk_buff *msg;
219 void *hdr;
220
Joe Perches20c239c2011-11-29 11:37:33 -0800221 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300222
223 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
224 if (!msg)
225 return -ENOMEM;
226
227 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
228 NFC_EVENT_DEVICE_REMOVED);
229 if (!hdr)
230 goto free_msg;
231
232 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
233
234 genlmsg_end(msg, hdr);
235
236 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
237
238 return 0;
239
240nla_put_failure:
241 genlmsg_cancel(msg, hdr);
242free_msg:
243 nlmsg_free(msg);
244 return -EMSGSIZE;
245}
246
247static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
248 u32 pid, u32 seq,
249 struct netlink_callback *cb,
250 int flags)
251{
252 void *hdr;
253
Joe Perches20c239c2011-11-29 11:37:33 -0800254 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300255
256 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
257 NFC_CMD_GET_DEVICE);
258 if (!hdr)
259 return -EMSGSIZE;
260
261 if (cb)
262 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
263
264 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
265 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
266 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
267
268 return genlmsg_end(msg, hdr);
269
270nla_put_failure:
271 genlmsg_cancel(msg, hdr);
272 return -EMSGSIZE;
273}
274
275static int nfc_genl_dump_devices(struct sk_buff *skb,
276 struct netlink_callback *cb)
277{
278 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
279 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
280 bool first_call = false;
281
Joe Perches20c239c2011-11-29 11:37:33 -0800282 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300283
284 if (!iter) {
285 first_call = true;
286 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
287 if (!iter)
288 return -ENOMEM;
289 cb->args[0] = (long) iter;
290 }
291
292 mutex_lock(&nfc_devlist_mutex);
293
294 cb->seq = nfc_devlist_generation;
295
296 if (first_call) {
297 nfc_device_iter_init(iter);
298 dev = nfc_device_iter_next(iter);
299 }
300
301 while (dev) {
302 int rc;
303
304 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
305 cb->nlh->nlmsg_seq,
306 cb, NLM_F_MULTI);
307 if (rc < 0)
308 break;
309
310 dev = nfc_device_iter_next(iter);
311 }
312
313 mutex_unlock(&nfc_devlist_mutex);
314
315 cb->args[1] = (long) dev;
316
317 return skb->len;
318}
319
320static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
321{
322 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
323
Joe Perches20c239c2011-11-29 11:37:33 -0800324 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300325
326 nfc_device_iter_exit(iter);
327 kfree(iter);
328
329 return 0;
330}
331
332static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
333{
334 struct sk_buff *msg;
335 struct nfc_dev *dev;
336 u32 idx;
337 int rc = -ENOBUFS;
338
Joe Perches20c239c2011-11-29 11:37:33 -0800339 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300340
341 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
342 return -EINVAL;
343
344 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
345
346 dev = nfc_get_device(idx);
347 if (!dev)
348 return -ENODEV;
349
350 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
351 if (!msg) {
352 rc = -ENOMEM;
353 goto out_putdev;
354 }
355
356 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
357 NULL, 0);
358 if (rc < 0)
359 goto out_free;
360
361 nfc_put_device(dev);
362
363 return genlmsg_reply(msg, info);
364
365out_free:
366 nlmsg_free(msg);
367out_putdev:
368 nfc_put_device(dev);
369 return rc;
370}
371
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300372static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
373{
374 struct nfc_dev *dev;
375 int rc;
376 u32 idx;
377
Joe Perches20c239c2011-11-29 11:37:33 -0800378 pr_debug("entry\n");
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300379
380 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
381 return -EINVAL;
382
383 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
384
385 dev = nfc_get_device(idx);
386 if (!dev)
387 return -ENODEV;
388
389 rc = nfc_dev_up(dev);
390
391 nfc_put_device(dev);
392 return rc;
393}
394
395static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
396{
397 struct nfc_dev *dev;
398 int rc;
399 u32 idx;
400
Joe Perches20c239c2011-11-29 11:37:33 -0800401 pr_debug("entry\n");
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300402
403 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
404 return -EINVAL;
405
406 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
407
408 dev = nfc_get_device(idx);
409 if (!dev)
410 return -ENODEV;
411
412 rc = nfc_dev_down(dev);
413
414 nfc_put_device(dev);
415 return rc;
416}
417
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300418static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
419{
420 struct nfc_dev *dev;
421 int rc;
422 u32 idx;
423 u32 protocols;
424
Joe Perches20c239c2011-11-29 11:37:33 -0800425 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300426
427 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
428 !info->attrs[NFC_ATTR_PROTOCOLS])
429 return -EINVAL;
430
431 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
432 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
433
434 dev = nfc_get_device(idx);
435 if (!dev)
436 return -ENODEV;
437
438 mutex_lock(&dev->genl_data.genl_data_mutex);
439
440 rc = nfc_start_poll(dev, protocols);
441 if (!rc)
442 dev->genl_data.poll_req_pid = info->snd_pid;
443
444 mutex_unlock(&dev->genl_data.genl_data_mutex);
445
446 nfc_put_device(dev);
447 return rc;
448}
449
450static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
451{
452 struct nfc_dev *dev;
453 int rc;
454 u32 idx;
455
Joe Perches20c239c2011-11-29 11:37:33 -0800456 pr_debug("entry\n");
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300457
458 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
459 return -EINVAL;
460
461 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
462
463 dev = nfc_get_device(idx);
464 if (!dev)
465 return -ENODEV;
466
467 mutex_lock(&dev->genl_data.genl_data_mutex);
468
469 if (dev->genl_data.poll_req_pid != info->snd_pid) {
470 rc = -EBUSY;
471 goto out;
472 }
473
474 rc = nfc_stop_poll(dev);
475 dev->genl_data.poll_req_pid = 0;
476
477out:
478 mutex_unlock(&dev->genl_data.genl_data_mutex);
479 nfc_put_device(dev);
480 return rc;
481}
482
483static struct genl_ops nfc_genl_ops[] = {
484 {
485 .cmd = NFC_CMD_GET_DEVICE,
486 .doit = nfc_genl_get_device,
487 .dumpit = nfc_genl_dump_devices,
488 .done = nfc_genl_dump_devices_done,
489 .policy = nfc_genl_policy,
490 },
491 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300492 .cmd = NFC_CMD_DEV_UP,
493 .doit = nfc_genl_dev_up,
494 .policy = nfc_genl_policy,
495 },
496 {
497 .cmd = NFC_CMD_DEV_DOWN,
498 .doit = nfc_genl_dev_down,
499 .policy = nfc_genl_policy,
500 },
501 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300502 .cmd = NFC_CMD_START_POLL,
503 .doit = nfc_genl_start_poll,
504 .policy = nfc_genl_policy,
505 },
506 {
507 .cmd = NFC_CMD_STOP_POLL,
508 .doit = nfc_genl_stop_poll,
509 .policy = nfc_genl_policy,
510 },
511 {
512 .cmd = NFC_CMD_GET_TARGET,
513 .dumpit = nfc_genl_dump_targets,
514 .done = nfc_genl_dump_targets_done,
515 .policy = nfc_genl_policy,
516 },
517};
518
519static int nfc_genl_rcv_nl_event(struct notifier_block *this,
520 unsigned long event, void *ptr)
521{
522 struct netlink_notify *n = ptr;
523 struct class_dev_iter iter;
524 struct nfc_dev *dev;
525
526 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
527 goto out;
528
Joe Perches20c239c2011-11-29 11:37:33 -0800529 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300530
531 nfc_device_iter_init(&iter);
532 dev = nfc_device_iter_next(&iter);
533
534 while (dev) {
535 mutex_lock(&dev->genl_data.genl_data_mutex);
536 if (dev->genl_data.poll_req_pid == n->pid) {
537 nfc_stop_poll(dev);
538 dev->genl_data.poll_req_pid = 0;
539 }
540 mutex_unlock(&dev->genl_data.genl_data_mutex);
541 dev = nfc_device_iter_next(&iter);
542 }
543
544 nfc_device_iter_exit(&iter);
545
546out:
547 return NOTIFY_DONE;
548}
549
550void nfc_genl_data_init(struct nfc_genl_data *genl_data)
551{
552 genl_data->poll_req_pid = 0;
553 mutex_init(&genl_data->genl_data_mutex);
554}
555
556void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
557{
558 mutex_destroy(&genl_data->genl_data_mutex);
559}
560
561static struct notifier_block nl_notifier = {
562 .notifier_call = nfc_genl_rcv_nl_event,
563};
564
565/**
566 * nfc_genl_init() - Initialize netlink interface
567 *
568 * This initialization function registers the nfc netlink family.
569 */
570int __init nfc_genl_init(void)
571{
572 int rc;
573
574 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
575 ARRAY_SIZE(nfc_genl_ops));
576 if (rc)
577 return rc;
578
579 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
580
581 netlink_register_notifier(&nl_notifier);
582
583 return rc;
584}
585
586/**
587 * nfc_genl_exit() - Deinitialize netlink interface
588 *
589 * This exit function unregisters the nfc netlink family.
590 */
591void nfc_genl_exit(void)
592{
593 netlink_unregister_notifier(&nl_notifier);
594 genl_unregister_family(&nfc_genl_family);
595}