blob: ebdb605f8dbd16f40e8028309340d46d18e75bee [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
Samuel Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080025
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 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010049 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010051 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030052};
53
54static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010055 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030056{
57 void *hdr;
58
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030059 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010060 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030061 if (!hdr)
62 return -EMSGSIZE;
63
64 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
65
66 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010067 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030068 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);
Ilan Elias288e0712011-12-22 11:51:54 +020070 if (target->nfcid1_len > 0)
71 NLA_PUT(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010072 target->nfcid1);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +020073 if (target->sensb_res_len > 0)
74 NLA_PUT(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010075 target->sensb_res);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +020076 if (target->sensf_res_len > 0)
77 NLA_PUT(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010078 target->sensf_res);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030079
80 return genlmsg_end(msg, hdr);
81
82nla_put_failure:
83 genlmsg_cancel(msg, hdr);
84 return -EMSGSIZE;
85}
86
87static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
88{
89 struct nfc_dev *dev;
90 int rc;
91 u32 idx;
92
93 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010094 nfc_genl_family.attrbuf,
95 nfc_genl_family.maxattr,
96 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030097 if (rc < 0)
98 return ERR_PTR(rc);
99
100 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
101 return ERR_PTR(-EINVAL);
102
103 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
104
105 dev = nfc_get_device(idx);
106 if (!dev)
107 return ERR_PTR(-ENODEV);
108
109 return dev;
110}
111
112static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100113 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300114{
115 int i = cb->args[0];
116 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
117 int rc;
118
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300119 if (!dev) {
120 dev = __get_device_from_cb(cb);
121 if (IS_ERR(dev))
122 return PTR_ERR(dev);
123
124 cb->args[1] = (long) dev;
125 }
126
127 spin_lock_bh(&dev->targets_lock);
128
129 cb->seq = dev->targets_generation;
130
131 while (i < dev->n_targets) {
132 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100133 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300134 if (rc < 0)
135 break;
136
137 i++;
138 }
139
140 spin_unlock_bh(&dev->targets_lock);
141
142 cb->args[0] = i;
143
144 return skb->len;
145}
146
147static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
148{
149 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
150
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300151 if (dev)
152 nfc_put_device(dev);
153
154 return 0;
155}
156
157int nfc_genl_targets_found(struct nfc_dev *dev)
158{
159 struct sk_buff *msg;
160 void *hdr;
161
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300162 dev->genl_data.poll_req_pid = 0;
163
164 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
165 if (!msg)
166 return -ENOMEM;
167
168 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100169 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300170 if (!hdr)
171 goto free_msg;
172
173 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
174
175 genlmsg_end(msg, hdr);
176
177 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
178
179nla_put_failure:
180 genlmsg_cancel(msg, hdr);
181free_msg:
182 nlmsg_free(msg);
183 return -EMSGSIZE;
184}
185
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200186int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
187{
188 struct sk_buff *msg;
189 void *hdr;
190
191 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
192 if (!msg)
193 return -ENOMEM;
194
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 NFC_EVENT_TARGET_LOST);
197 if (!hdr)
198 goto free_msg;
199
200 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
201 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
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
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300216int nfc_genl_device_added(struct nfc_dev *dev)
217{
218 struct sk_buff *msg;
219 void *hdr;
220
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300221 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
222 if (!msg)
223 return -ENOMEM;
224
225 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100226 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300227 if (!hdr)
228 goto free_msg;
229
230 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
231 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
232 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
Samuel Ortizc970a1a2012-03-05 01:03:34 +0100233 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300234
235 genlmsg_end(msg, hdr);
236
237 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
238
239 return 0;
240
241nla_put_failure:
242 genlmsg_cancel(msg, hdr);
243free_msg:
244 nlmsg_free(msg);
245 return -EMSGSIZE;
246}
247
248int nfc_genl_device_removed(struct nfc_dev *dev)
249{
250 struct sk_buff *msg;
251 void *hdr;
252
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300253 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
254 if (!msg)
255 return -ENOMEM;
256
257 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100258 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300259 if (!hdr)
260 goto free_msg;
261
262 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
263
264 genlmsg_end(msg, hdr);
265
266 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
267
268 return 0;
269
270nla_put_failure:
271 genlmsg_cancel(msg, hdr);
272free_msg:
273 nlmsg_free(msg);
274 return -EMSGSIZE;
275}
276
277static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100278 u32 pid, u32 seq,
279 struct netlink_callback *cb,
280 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300281{
282 void *hdr;
283
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300284 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100285 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300286 if (!hdr)
287 return -EMSGSIZE;
288
289 if (cb)
290 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
291
292 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
293 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
294 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
Samuel Ortizc970a1a2012-03-05 01:03:34 +0100295 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300296
297 return genlmsg_end(msg, hdr);
298
299nla_put_failure:
300 genlmsg_cancel(msg, hdr);
301 return -EMSGSIZE;
302}
303
304static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100305 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300306{
307 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
308 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
309 bool first_call = false;
310
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300311 if (!iter) {
312 first_call = true;
313 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
314 if (!iter)
315 return -ENOMEM;
316 cb->args[0] = (long) iter;
317 }
318
319 mutex_lock(&nfc_devlist_mutex);
320
321 cb->seq = nfc_devlist_generation;
322
323 if (first_call) {
324 nfc_device_iter_init(iter);
325 dev = nfc_device_iter_next(iter);
326 }
327
328 while (dev) {
329 int rc;
330
331 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100332 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300333 if (rc < 0)
334 break;
335
336 dev = nfc_device_iter_next(iter);
337 }
338
339 mutex_unlock(&nfc_devlist_mutex);
340
341 cb->args[1] = (long) dev;
342
343 return skb->len;
344}
345
346static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
347{
348 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
349
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300350 nfc_device_iter_exit(iter);
351 kfree(iter);
352
353 return 0;
354}
355
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100356int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100357 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100358{
359 struct sk_buff *msg;
360 void *hdr;
361
362 pr_debug("DEP link is up\n");
363
364 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
365 if (!msg)
366 return -ENOMEM;
367
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100368 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100369 if (!hdr)
370 goto free_msg;
371
372 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
373 if (rf_mode == NFC_RF_INITIATOR)
374 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
375 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
376 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
377
378 genlmsg_end(msg, hdr);
379
380 dev->dep_link_up = true;
381
382 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
383
384 return 0;
385
386nla_put_failure:
387 genlmsg_cancel(msg, hdr);
388free_msg:
389 nlmsg_free(msg);
390 return -EMSGSIZE;
391}
392
393int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
394{
395 struct sk_buff *msg;
396 void *hdr;
397
398 pr_debug("DEP link is down\n");
399
400 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
401 if (!msg)
402 return -ENOMEM;
403
404 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100405 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100406 if (!hdr)
407 goto free_msg;
408
409 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
410
411 genlmsg_end(msg, hdr);
412
413 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
414
415 return 0;
416
417nla_put_failure:
418 genlmsg_cancel(msg, hdr);
419free_msg:
420 nlmsg_free(msg);
421 return -EMSGSIZE;
422}
423
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300424static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
425{
426 struct sk_buff *msg;
427 struct nfc_dev *dev;
428 u32 idx;
429 int rc = -ENOBUFS;
430
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300431 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
432 return -EINVAL;
433
434 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
435
436 dev = nfc_get_device(idx);
437 if (!dev)
438 return -ENODEV;
439
440 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
441 if (!msg) {
442 rc = -ENOMEM;
443 goto out_putdev;
444 }
445
446 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100447 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300448 if (rc < 0)
449 goto out_free;
450
451 nfc_put_device(dev);
452
453 return genlmsg_reply(msg, info);
454
455out_free:
456 nlmsg_free(msg);
457out_putdev:
458 nfc_put_device(dev);
459 return rc;
460}
461
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300462static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
463{
464 struct nfc_dev *dev;
465 int rc;
466 u32 idx;
467
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300468 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
469 return -EINVAL;
470
471 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
472
473 dev = nfc_get_device(idx);
474 if (!dev)
475 return -ENODEV;
476
477 rc = nfc_dev_up(dev);
478
479 nfc_put_device(dev);
480 return rc;
481}
482
483static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
484{
485 struct nfc_dev *dev;
486 int rc;
487 u32 idx;
488
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300489 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
490 return -EINVAL;
491
492 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
493
494 dev = nfc_get_device(idx);
495 if (!dev)
496 return -ENODEV;
497
498 rc = nfc_dev_down(dev);
499
500 nfc_put_device(dev);
501 return rc;
502}
503
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300504static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
505{
506 struct nfc_dev *dev;
507 int rc;
508 u32 idx;
509 u32 protocols;
510
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100511 pr_debug("Poll start\n");
512
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300513 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100514 !info->attrs[NFC_ATTR_PROTOCOLS])
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300515 return -EINVAL;
516
517 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
518 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
519
520 dev = nfc_get_device(idx);
521 if (!dev)
522 return -ENODEV;
523
524 mutex_lock(&dev->genl_data.genl_data_mutex);
525
526 rc = nfc_start_poll(dev, protocols);
527 if (!rc)
528 dev->genl_data.poll_req_pid = info->snd_pid;
529
530 mutex_unlock(&dev->genl_data.genl_data_mutex);
531
532 nfc_put_device(dev);
533 return rc;
534}
535
536static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
537{
538 struct nfc_dev *dev;
539 int rc;
540 u32 idx;
541
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300542 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
543 return -EINVAL;
544
545 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
546
547 dev = nfc_get_device(idx);
548 if (!dev)
549 return -ENODEV;
550
551 mutex_lock(&dev->genl_data.genl_data_mutex);
552
553 if (dev->genl_data.poll_req_pid != info->snd_pid) {
554 rc = -EBUSY;
555 goto out;
556 }
557
558 rc = nfc_stop_poll(dev);
559 dev->genl_data.poll_req_pid = 0;
560
561out:
562 mutex_unlock(&dev->genl_data.genl_data_mutex);
563 nfc_put_device(dev);
564 return rc;
565}
566
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100567static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
568{
569 struct nfc_dev *dev;
570 int rc, tgt_idx;
571 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100572 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100573
574 pr_debug("DEP link up\n");
575
576 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100577 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100578 return -EINVAL;
579
580 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
581 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
582 tgt_idx = NFC_TARGET_IDX_ANY;
583 else
584 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
585
586 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100587
588 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
589 return -EINVAL;
590
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100591 dev = nfc_get_device(idx);
592 if (!dev)
593 return -ENODEV;
594
Samuel Ortiz47807d32012-03-05 01:03:50 +0100595 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100596
597 nfc_put_device(dev);
598
599 return rc;
600}
601
602static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
603{
604 struct nfc_dev *dev;
605 int rc;
606 u32 idx;
607
608 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
609 return -EINVAL;
610
611 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
612
613 dev = nfc_get_device(idx);
614 if (!dev)
615 return -ENODEV;
616
617 rc = nfc_dep_link_down(dev);
618
619 nfc_put_device(dev);
620 return rc;
621}
622
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300623static struct genl_ops nfc_genl_ops[] = {
624 {
625 .cmd = NFC_CMD_GET_DEVICE,
626 .doit = nfc_genl_get_device,
627 .dumpit = nfc_genl_dump_devices,
628 .done = nfc_genl_dump_devices_done,
629 .policy = nfc_genl_policy,
630 },
631 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300632 .cmd = NFC_CMD_DEV_UP,
633 .doit = nfc_genl_dev_up,
634 .policy = nfc_genl_policy,
635 },
636 {
637 .cmd = NFC_CMD_DEV_DOWN,
638 .doit = nfc_genl_dev_down,
639 .policy = nfc_genl_policy,
640 },
641 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300642 .cmd = NFC_CMD_START_POLL,
643 .doit = nfc_genl_start_poll,
644 .policy = nfc_genl_policy,
645 },
646 {
647 .cmd = NFC_CMD_STOP_POLL,
648 .doit = nfc_genl_stop_poll,
649 .policy = nfc_genl_policy,
650 },
651 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100652 .cmd = NFC_CMD_DEP_LINK_UP,
653 .doit = nfc_genl_dep_link_up,
654 .policy = nfc_genl_policy,
655 },
656 {
657 .cmd = NFC_CMD_DEP_LINK_DOWN,
658 .doit = nfc_genl_dep_link_down,
659 .policy = nfc_genl_policy,
660 },
661 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300662 .cmd = NFC_CMD_GET_TARGET,
663 .dumpit = nfc_genl_dump_targets,
664 .done = nfc_genl_dump_targets_done,
665 .policy = nfc_genl_policy,
666 },
667};
668
669static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100670 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300671{
672 struct netlink_notify *n = ptr;
673 struct class_dev_iter iter;
674 struct nfc_dev *dev;
675
676 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
677 goto out;
678
Joe Perches20c239c2011-11-29 11:37:33 -0800679 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300680
681 nfc_device_iter_init(&iter);
682 dev = nfc_device_iter_next(&iter);
683
684 while (dev) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300685 if (dev->genl_data.poll_req_pid == n->pid) {
686 nfc_stop_poll(dev);
687 dev->genl_data.poll_req_pid = 0;
688 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300689 dev = nfc_device_iter_next(&iter);
690 }
691
692 nfc_device_iter_exit(&iter);
693
694out:
695 return NOTIFY_DONE;
696}
697
698void nfc_genl_data_init(struct nfc_genl_data *genl_data)
699{
700 genl_data->poll_req_pid = 0;
701 mutex_init(&genl_data->genl_data_mutex);
702}
703
704void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
705{
706 mutex_destroy(&genl_data->genl_data_mutex);
707}
708
709static struct notifier_block nl_notifier = {
710 .notifier_call = nfc_genl_rcv_nl_event,
711};
712
713/**
714 * nfc_genl_init() - Initialize netlink interface
715 *
716 * This initialization function registers the nfc netlink family.
717 */
718int __init nfc_genl_init(void)
719{
720 int rc;
721
722 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100723 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300724 if (rc)
725 return rc;
726
727 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
728
729 netlink_register_notifier(&nl_notifier);
730
731 return rc;
732}
733
734/**
735 * nfc_genl_exit() - Deinitialize netlink interface
736 *
737 * This exit function unregisters the nfc netlink family.
738 */
739void nfc_genl_exit(void)
740{
741 netlink_unregister_notifier(&nl_notifier);
742 genl_unregister_family(&nfc_genl_family);
743}