blob: 6404052d6c070ccf8f54c4827408d5f9291d6b67 [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
186int nfc_genl_device_added(struct nfc_dev *dev)
187{
188 struct sk_buff *msg;
189 void *hdr;
190
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300191 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,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100196 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300197 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_DEVICE_INDEX, dev->idx);
202 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
Samuel Ortizc970a1a2012-03-05 01:03:34 +0100203 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300204
205 genlmsg_end(msg, hdr);
206
207 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
208
209 return 0;
210
211nla_put_failure:
212 genlmsg_cancel(msg, hdr);
213free_msg:
214 nlmsg_free(msg);
215 return -EMSGSIZE;
216}
217
218int nfc_genl_device_removed(struct nfc_dev *dev)
219{
220 struct sk_buff *msg;
221 void *hdr;
222
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300223 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,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100228 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300229 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,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100248 u32 pid, u32 seq,
249 struct netlink_callback *cb,
250 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300251{
252 void *hdr;
253
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300254 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100255 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300256 if (!hdr)
257 return -EMSGSIZE;
258
259 if (cb)
260 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
261
262 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
263 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
264 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
Samuel Ortizc970a1a2012-03-05 01:03:34 +0100265 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300266
267 return genlmsg_end(msg, hdr);
268
269nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271 return -EMSGSIZE;
272}
273
274static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100275 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300276{
277 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
278 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
279 bool first_call = false;
280
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300281 if (!iter) {
282 first_call = true;
283 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
284 if (!iter)
285 return -ENOMEM;
286 cb->args[0] = (long) iter;
287 }
288
289 mutex_lock(&nfc_devlist_mutex);
290
291 cb->seq = nfc_devlist_generation;
292
293 if (first_call) {
294 nfc_device_iter_init(iter);
295 dev = nfc_device_iter_next(iter);
296 }
297
298 while (dev) {
299 int rc;
300
301 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100302 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300303 if (rc < 0)
304 break;
305
306 dev = nfc_device_iter_next(iter);
307 }
308
309 mutex_unlock(&nfc_devlist_mutex);
310
311 cb->args[1] = (long) dev;
312
313 return skb->len;
314}
315
316static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
317{
318 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
319
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300320 nfc_device_iter_exit(iter);
321 kfree(iter);
322
323 return 0;
324}
325
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100326int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100327 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100328{
329 struct sk_buff *msg;
330 void *hdr;
331
332 pr_debug("DEP link is up\n");
333
334 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
335 if (!msg)
336 return -ENOMEM;
337
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100339 if (!hdr)
340 goto free_msg;
341
342 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
343 if (rf_mode == NFC_RF_INITIATOR)
344 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
345 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
346 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
347
348 genlmsg_end(msg, hdr);
349
350 dev->dep_link_up = true;
351
352 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
353
354 return 0;
355
356nla_put_failure:
357 genlmsg_cancel(msg, hdr);
358free_msg:
359 nlmsg_free(msg);
360 return -EMSGSIZE;
361}
362
363int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
364{
365 struct sk_buff *msg;
366 void *hdr;
367
368 pr_debug("DEP link is down\n");
369
370 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
371 if (!msg)
372 return -ENOMEM;
373
374 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100375 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100376 if (!hdr)
377 goto free_msg;
378
379 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
380
381 genlmsg_end(msg, hdr);
382
383 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
384
385 return 0;
386
387nla_put_failure:
388 genlmsg_cancel(msg, hdr);
389free_msg:
390 nlmsg_free(msg);
391 return -EMSGSIZE;
392}
393
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300394static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
395{
396 struct sk_buff *msg;
397 struct nfc_dev *dev;
398 u32 idx;
399 int rc = -ENOBUFS;
400
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300401 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
402 return -EINVAL;
403
404 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
405
406 dev = nfc_get_device(idx);
407 if (!dev)
408 return -ENODEV;
409
410 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
411 if (!msg) {
412 rc = -ENOMEM;
413 goto out_putdev;
414 }
415
416 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100417 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300418 if (rc < 0)
419 goto out_free;
420
421 nfc_put_device(dev);
422
423 return genlmsg_reply(msg, info);
424
425out_free:
426 nlmsg_free(msg);
427out_putdev:
428 nfc_put_device(dev);
429 return rc;
430}
431
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300432static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
433{
434 struct nfc_dev *dev;
435 int rc;
436 u32 idx;
437
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300438 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
439 return -EINVAL;
440
441 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
442
443 dev = nfc_get_device(idx);
444 if (!dev)
445 return -ENODEV;
446
447 rc = nfc_dev_up(dev);
448
449 nfc_put_device(dev);
450 return rc;
451}
452
453static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
454{
455 struct nfc_dev *dev;
456 int rc;
457 u32 idx;
458
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
460 return -EINVAL;
461
462 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
463
464 dev = nfc_get_device(idx);
465 if (!dev)
466 return -ENODEV;
467
468 rc = nfc_dev_down(dev);
469
470 nfc_put_device(dev);
471 return rc;
472}
473
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300474static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
475{
476 struct nfc_dev *dev;
477 int rc;
478 u32 idx;
479 u32 protocols;
480
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100481 pr_debug("Poll start\n");
482
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300483 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100484 !info->attrs[NFC_ATTR_PROTOCOLS])
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300485 return -EINVAL;
486
487 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
488 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
489
490 dev = nfc_get_device(idx);
491 if (!dev)
492 return -ENODEV;
493
494 mutex_lock(&dev->genl_data.genl_data_mutex);
495
496 rc = nfc_start_poll(dev, protocols);
497 if (!rc)
498 dev->genl_data.poll_req_pid = info->snd_pid;
499
500 mutex_unlock(&dev->genl_data.genl_data_mutex);
501
502 nfc_put_device(dev);
503 return rc;
504}
505
506static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
507{
508 struct nfc_dev *dev;
509 int rc;
510 u32 idx;
511
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300512 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
513 return -EINVAL;
514
515 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
516
517 dev = nfc_get_device(idx);
518 if (!dev)
519 return -ENODEV;
520
521 mutex_lock(&dev->genl_data.genl_data_mutex);
522
523 if (dev->genl_data.poll_req_pid != info->snd_pid) {
524 rc = -EBUSY;
525 goto out;
526 }
527
528 rc = nfc_stop_poll(dev);
529 dev->genl_data.poll_req_pid = 0;
530
531out:
532 mutex_unlock(&dev->genl_data.genl_data_mutex);
533 nfc_put_device(dev);
534 return rc;
535}
536
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100537static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
538{
539 struct nfc_dev *dev;
540 int rc, tgt_idx;
541 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100542 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100543
544 pr_debug("DEP link up\n");
545
546 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100547 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100548 return -EINVAL;
549
550 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
551 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
552 tgt_idx = NFC_TARGET_IDX_ANY;
553 else
554 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
555
556 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100557
558 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
559 return -EINVAL;
560
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100561 dev = nfc_get_device(idx);
562 if (!dev)
563 return -ENODEV;
564
Samuel Ortiz47807d32012-03-05 01:03:50 +0100565 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100566
567 nfc_put_device(dev);
568
569 return rc;
570}
571
572static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
573{
574 struct nfc_dev *dev;
575 int rc;
576 u32 idx;
577
578 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
579 return -EINVAL;
580
581 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
582
583 dev = nfc_get_device(idx);
584 if (!dev)
585 return -ENODEV;
586
587 rc = nfc_dep_link_down(dev);
588
589 nfc_put_device(dev);
590 return rc;
591}
592
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300593static struct genl_ops nfc_genl_ops[] = {
594 {
595 .cmd = NFC_CMD_GET_DEVICE,
596 .doit = nfc_genl_get_device,
597 .dumpit = nfc_genl_dump_devices,
598 .done = nfc_genl_dump_devices_done,
599 .policy = nfc_genl_policy,
600 },
601 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300602 .cmd = NFC_CMD_DEV_UP,
603 .doit = nfc_genl_dev_up,
604 .policy = nfc_genl_policy,
605 },
606 {
607 .cmd = NFC_CMD_DEV_DOWN,
608 .doit = nfc_genl_dev_down,
609 .policy = nfc_genl_policy,
610 },
611 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300612 .cmd = NFC_CMD_START_POLL,
613 .doit = nfc_genl_start_poll,
614 .policy = nfc_genl_policy,
615 },
616 {
617 .cmd = NFC_CMD_STOP_POLL,
618 .doit = nfc_genl_stop_poll,
619 .policy = nfc_genl_policy,
620 },
621 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100622 .cmd = NFC_CMD_DEP_LINK_UP,
623 .doit = nfc_genl_dep_link_up,
624 .policy = nfc_genl_policy,
625 },
626 {
627 .cmd = NFC_CMD_DEP_LINK_DOWN,
628 .doit = nfc_genl_dep_link_down,
629 .policy = nfc_genl_policy,
630 },
631 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300632 .cmd = NFC_CMD_GET_TARGET,
633 .dumpit = nfc_genl_dump_targets,
634 .done = nfc_genl_dump_targets_done,
635 .policy = nfc_genl_policy,
636 },
637};
638
639static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100640 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300641{
642 struct netlink_notify *n = ptr;
643 struct class_dev_iter iter;
644 struct nfc_dev *dev;
645
646 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
647 goto out;
648
Joe Perches20c239c2011-11-29 11:37:33 -0800649 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300650
651 nfc_device_iter_init(&iter);
652 dev = nfc_device_iter_next(&iter);
653
654 while (dev) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300655 if (dev->genl_data.poll_req_pid == n->pid) {
656 nfc_stop_poll(dev);
657 dev->genl_data.poll_req_pid = 0;
658 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300659 dev = nfc_device_iter_next(&iter);
660 }
661
662 nfc_device_iter_exit(&iter);
663
664out:
665 return NOTIFY_DONE;
666}
667
668void nfc_genl_data_init(struct nfc_genl_data *genl_data)
669{
670 genl_data->poll_req_pid = 0;
671 mutex_init(&genl_data->genl_data_mutex);
672}
673
674void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
675{
676 mutex_destroy(&genl_data->genl_data_mutex);
677}
678
679static struct notifier_block nl_notifier = {
680 .notifier_call = nfc_genl_rcv_nl_event,
681};
682
683/**
684 * nfc_genl_init() - Initialize netlink interface
685 *
686 * This initialization function registers the nfc netlink family.
687 */
688int __init nfc_genl_init(void)
689{
690 int rc;
691
692 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100693 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300694 if (rc)
695 return rc;
696
697 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
698
699 netlink_register_notifier(&nl_notifier);
700
701 return rc;
702}
703
704/**
705 * nfc_genl_exit() - Deinitialize netlink interface
706 *
707 * This exit function unregisters the nfc netlink family.
708 */
709void nfc_genl_exit(void)
710{
711 netlink_unregister_notifier(&nl_notifier);
712 genl_unregister_family(&nfc_genl_family);
713}