blob: 73fd51098f4dc1250ecc89fe160f56055b635987 [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
Thierry Escande52feb442012-10-17 14:43:39 +020032#include "llcp/llcp.h"
33
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030034static struct genl_multicast_group nfc_genl_event_mcgrp = {
35 .name = NFC_GENL_MCAST_EVENT_NAME,
36};
37
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020038static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030039 .id = GENL_ID_GENERATE,
40 .hdrsize = 0,
41 .name = NFC_GENL_NAME,
42 .version = NFC_GENL_VERSION,
43 .maxattr = NFC_ATTR_MAX,
44};
45
46static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
47 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
48 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
49 .len = NFC_DEVICE_NAME_MAXSIZE },
50 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010051 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
52 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010053 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020054 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
55 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010056 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
58 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010059 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
60};
61
62static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
63 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
64 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030065};
66
67static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010068 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030069{
70 void *hdr;
71
Eric W. Biederman15e47302012-09-07 20:12:54 +000072 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010073 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030074 if (!hdr)
75 return -EMSGSIZE;
76
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78
David S. Miller1e6428d2012-03-29 23:23:57 -040079 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
80 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
81 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
82 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
83 goto nla_put_failure;
84 if (target->nfcid1_len > 0 &&
85 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
86 target->nfcid1))
87 goto nla_put_failure;
88 if (target->sensb_res_len > 0 &&
89 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
90 target->sensb_res))
91 goto nla_put_failure;
92 if (target->sensf_res_len > 0 &&
93 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
94 target->sensf_res))
95 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030096
97 return genlmsg_end(msg, hdr);
98
99nla_put_failure:
100 genlmsg_cancel(msg, hdr);
101 return -EMSGSIZE;
102}
103
104static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
105{
106 struct nfc_dev *dev;
107 int rc;
108 u32 idx;
109
110 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100111 nfc_genl_family.attrbuf,
112 nfc_genl_family.maxattr,
113 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300114 if (rc < 0)
115 return ERR_PTR(rc);
116
117 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
118 return ERR_PTR(-EINVAL);
119
120 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
121
122 dev = nfc_get_device(idx);
123 if (!dev)
124 return ERR_PTR(-ENODEV);
125
126 return dev;
127}
128
129static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100130 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300131{
132 int i = cb->args[0];
133 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
134 int rc;
135
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300136 if (!dev) {
137 dev = __get_device_from_cb(cb);
138 if (IS_ERR(dev))
139 return PTR_ERR(dev);
140
141 cb->args[1] = (long) dev;
142 }
143
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200144 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145
146 cb->seq = dev->targets_generation;
147
148 while (i < dev->n_targets) {
149 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100150 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300151 if (rc < 0)
152 break;
153
154 i++;
155 }
156
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200157 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300158
159 cb->args[0] = i;
160
161 return skb->len;
162}
163
164static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
165{
166 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
167
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300168 if (dev)
169 nfc_put_device(dev);
170
171 return 0;
172}
173
174int nfc_genl_targets_found(struct nfc_dev *dev)
175{
176 struct sk_buff *msg;
177 void *hdr;
178
Eric W. Biederman15e47302012-09-07 20:12:54 +0000179 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300180
Thomas Graf58050fc2012-06-28 03:57:45 +0000181 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300182 if (!msg)
183 return -ENOMEM;
184
185 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100186 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300187 if (!hdr)
188 goto free_msg;
189
David S. Miller1e6428d2012-03-29 23:23:57 -0400190 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
191 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300192
193 genlmsg_end(msg, hdr);
194
195 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
196
197nla_put_failure:
198 genlmsg_cancel(msg, hdr);
199free_msg:
200 nlmsg_free(msg);
201 return -EMSGSIZE;
202}
203
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200204int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
205{
206 struct sk_buff *msg;
207 void *hdr;
208
Thomas Graf58050fc2012-06-28 03:57:45 +0000209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200210 if (!msg)
211 return -ENOMEM;
212
213 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
214 NFC_EVENT_TARGET_LOST);
215 if (!hdr)
216 goto free_msg;
217
John W. Linville59ef43e2012-04-18 14:17:13 -0400218 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
219 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
220 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200221
222 genlmsg_end(msg, hdr);
223
224 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
225
226 return 0;
227
228nla_put_failure:
229 genlmsg_cancel(msg, hdr);
230free_msg:
231 nlmsg_free(msg);
232 return -EMSGSIZE;
233}
234
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200235int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
236{
237 struct sk_buff *msg;
238 void *hdr;
239
Thomas Graf58050fc2012-06-28 03:57:45 +0000240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200241 if (!msg)
242 return -ENOMEM;
243
244 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
245 NFC_EVENT_TM_ACTIVATED);
246 if (!hdr)
247 goto free_msg;
248
249 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
250 goto nla_put_failure;
251 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
252 goto nla_put_failure;
253
254 genlmsg_end(msg, hdr);
255
256 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
257
258 return 0;
259
260nla_put_failure:
261 genlmsg_cancel(msg, hdr);
262free_msg:
263 nlmsg_free(msg);
264 return -EMSGSIZE;
265}
266
267int nfc_genl_tm_deactivated(struct nfc_dev *dev)
268{
269 struct sk_buff *msg;
270 void *hdr;
271
Thomas Graf58050fc2012-06-28 03:57:45 +0000272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200273 if (!msg)
274 return -ENOMEM;
275
276 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
277 NFC_EVENT_TM_DEACTIVATED);
278 if (!hdr)
279 goto free_msg;
280
281 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
282 goto nla_put_failure;
283
284 genlmsg_end(msg, hdr);
285
286 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
287
288 return 0;
289
290nla_put_failure:
291 genlmsg_cancel(msg, hdr);
292free_msg:
293 nlmsg_free(msg);
294 return -EMSGSIZE;
295}
296
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300297int nfc_genl_device_added(struct nfc_dev *dev)
298{
299 struct sk_buff *msg;
300 void *hdr;
301
Thomas Graf58050fc2012-06-28 03:57:45 +0000302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300303 if (!msg)
304 return -ENOMEM;
305
306 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100307 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300308 if (!hdr)
309 goto free_msg;
310
David S. Miller1e6428d2012-03-29 23:23:57 -0400311 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
312 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
313 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
314 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
315 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300316
317 genlmsg_end(msg, hdr);
318
319 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
320
321 return 0;
322
323nla_put_failure:
324 genlmsg_cancel(msg, hdr);
325free_msg:
326 nlmsg_free(msg);
327 return -EMSGSIZE;
328}
329
330int nfc_genl_device_removed(struct nfc_dev *dev)
331{
332 struct sk_buff *msg;
333 void *hdr;
334
Thomas Graf58050fc2012-06-28 03:57:45 +0000335 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300336 if (!msg)
337 return -ENOMEM;
338
339 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100340 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300341 if (!hdr)
342 goto free_msg;
343
David S. Miller1e6428d2012-03-29 23:23:57 -0400344 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
345 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300346
347 genlmsg_end(msg, hdr);
348
349 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
350
351 return 0;
352
353nla_put_failure:
354 genlmsg_cancel(msg, hdr);
355free_msg:
356 nlmsg_free(msg);
357 return -EMSGSIZE;
358}
359
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100360int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
361{
362 struct sk_buff *msg;
363 struct nlattr *sdp_attr, *uri_attr;
364 struct nfc_llcp_sdp_tlv *sdres;
365 struct hlist_node *n;
366 void *hdr;
367 int rc = -EMSGSIZE;
368 int i;
369
370 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
371 if (!msg)
372 return -ENOMEM;
373
374 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
375 NFC_EVENT_LLC_SDRES);
376 if (!hdr)
377 goto free_msg;
378
379 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
380 goto nla_put_failure;
381
382 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
383 if (sdp_attr == NULL) {
384 rc = -ENOMEM;
385 goto nla_put_failure;
386 }
387
388 i = 1;
389 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
390 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
391
392 uri_attr = nla_nest_start(msg, i++);
393 if (uri_attr == NULL) {
394 rc = -ENOMEM;
395 goto nla_put_failure;
396 }
397
398 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
399 goto nla_put_failure;
400
401 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
402 goto nla_put_failure;
403
404 nla_nest_end(msg, uri_attr);
405
406 hlist_del(&sdres->node);
407
408 nfc_llcp_free_sdp_tlv(sdres);
409 }
410
411 nla_nest_end(msg, sdp_attr);
412
413 genlmsg_end(msg, hdr);
414
415 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
416
417nla_put_failure:
418 genlmsg_cancel(msg, hdr);
419
420free_msg:
421 nlmsg_free(msg);
422
423 nfc_llcp_free_sdp_tlv_list(sdres_list);
424
425 return rc;
426}
427
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300428static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000429 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100430 struct netlink_callback *cb,
431 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300432{
433 void *hdr;
434
Eric W. Biederman15e47302012-09-07 20:12:54 +0000435 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100436 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300437 if (!hdr)
438 return -EMSGSIZE;
439
440 if (cb)
441 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
442
David S. Miller1e6428d2012-03-29 23:23:57 -0400443 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
444 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
445 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100446 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200447 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
448 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400449 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300450
451 return genlmsg_end(msg, hdr);
452
453nla_put_failure:
454 genlmsg_cancel(msg, hdr);
455 return -EMSGSIZE;
456}
457
458static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100459 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300460{
461 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
462 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
463 bool first_call = false;
464
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300465 if (!iter) {
466 first_call = true;
467 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
468 if (!iter)
469 return -ENOMEM;
470 cb->args[0] = (long) iter;
471 }
472
473 mutex_lock(&nfc_devlist_mutex);
474
475 cb->seq = nfc_devlist_generation;
476
477 if (first_call) {
478 nfc_device_iter_init(iter);
479 dev = nfc_device_iter_next(iter);
480 }
481
482 while (dev) {
483 int rc;
484
Eric W. Biederman15e47302012-09-07 20:12:54 +0000485 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100486 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300487 if (rc < 0)
488 break;
489
490 dev = nfc_device_iter_next(iter);
491 }
492
493 mutex_unlock(&nfc_devlist_mutex);
494
495 cb->args[1] = (long) dev;
496
497 return skb->len;
498}
499
500static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
501{
502 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
503
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300504 nfc_device_iter_exit(iter);
505 kfree(iter);
506
507 return 0;
508}
509
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100510int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100511 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100512{
513 struct sk_buff *msg;
514 void *hdr;
515
516 pr_debug("DEP link is up\n");
517
Thomas Graf58050fc2012-06-28 03:57:45 +0000518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100519 if (!msg)
520 return -ENOMEM;
521
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100522 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100523 if (!hdr)
524 goto free_msg;
525
David S. Miller1e6428d2012-03-29 23:23:57 -0400526 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
527 goto nla_put_failure;
528 if (rf_mode == NFC_RF_INITIATOR &&
529 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
530 goto nla_put_failure;
531 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
532 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
533 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100534
535 genlmsg_end(msg, hdr);
536
537 dev->dep_link_up = true;
538
539 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
540
541 return 0;
542
543nla_put_failure:
544 genlmsg_cancel(msg, hdr);
545free_msg:
546 nlmsg_free(msg);
547 return -EMSGSIZE;
548}
549
550int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
551{
552 struct sk_buff *msg;
553 void *hdr;
554
555 pr_debug("DEP link is down\n");
556
Thomas Graf58050fc2012-06-28 03:57:45 +0000557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100558 if (!msg)
559 return -ENOMEM;
560
561 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100562 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100563 if (!hdr)
564 goto free_msg;
565
David S. Miller1e6428d2012-03-29 23:23:57 -0400566 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
567 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100568
569 genlmsg_end(msg, hdr);
570
571 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
572
573 return 0;
574
575nla_put_failure:
576 genlmsg_cancel(msg, hdr);
577free_msg:
578 nlmsg_free(msg);
579 return -EMSGSIZE;
580}
581
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300582static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
583{
584 struct sk_buff *msg;
585 struct nfc_dev *dev;
586 u32 idx;
587 int rc = -ENOBUFS;
588
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300589 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
590 return -EINVAL;
591
592 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
593
594 dev = nfc_get_device(idx);
595 if (!dev)
596 return -ENODEV;
597
Thomas Graf58050fc2012-06-28 03:57:45 +0000598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300599 if (!msg) {
600 rc = -ENOMEM;
601 goto out_putdev;
602 }
603
Eric W. Biederman15e47302012-09-07 20:12:54 +0000604 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100605 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606 if (rc < 0)
607 goto out_free;
608
609 nfc_put_device(dev);
610
611 return genlmsg_reply(msg, info);
612
613out_free:
614 nlmsg_free(msg);
615out_putdev:
616 nfc_put_device(dev);
617 return rc;
618}
619
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300620static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
621{
622 struct nfc_dev *dev;
623 int rc;
624 u32 idx;
625
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300626 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
627 return -EINVAL;
628
629 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
630
631 dev = nfc_get_device(idx);
632 if (!dev)
633 return -ENODEV;
634
635 rc = nfc_dev_up(dev);
636
637 nfc_put_device(dev);
638 return rc;
639}
640
641static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
642{
643 struct nfc_dev *dev;
644 int rc;
645 u32 idx;
646
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300647 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
648 return -EINVAL;
649
650 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
651
652 dev = nfc_get_device(idx);
653 if (!dev)
654 return -ENODEV;
655
656 rc = nfc_dev_down(dev);
657
658 nfc_put_device(dev);
659 return rc;
660}
661
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300662static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
663{
664 struct nfc_dev *dev;
665 int rc;
666 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200667 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300668
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100669 pr_debug("Poll start\n");
670
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300671 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200672 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
673 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200674 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300675 return -EINVAL;
676
677 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200678
679 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
680 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200681
682 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
683 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200684 else if (info->attrs[NFC_ATTR_PROTOCOLS])
685 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300686
687 dev = nfc_get_device(idx);
688 if (!dev)
689 return -ENODEV;
690
691 mutex_lock(&dev->genl_data.genl_data_mutex);
692
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200693 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300694 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000695 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300696
697 mutex_unlock(&dev->genl_data.genl_data_mutex);
698
699 nfc_put_device(dev);
700 return rc;
701}
702
703static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
704{
705 struct nfc_dev *dev;
706 int rc;
707 u32 idx;
708
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300709 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
710 return -EINVAL;
711
712 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713
714 dev = nfc_get_device(idx);
715 if (!dev)
716 return -ENODEV;
717
Samuel Ortiza831b912012-06-28 16:41:57 +0200718 device_lock(&dev->dev);
719
720 if (!dev->polling) {
721 device_unlock(&dev->dev);
722 return -EINVAL;
723 }
724
725 device_unlock(&dev->dev);
726
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300727 mutex_lock(&dev->genl_data.genl_data_mutex);
728
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300730 rc = -EBUSY;
731 goto out;
732 }
733
734 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000735 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300736
737out:
738 mutex_unlock(&dev->genl_data.genl_data_mutex);
739 nfc_put_device(dev);
740 return rc;
741}
742
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100743static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
744{
745 struct nfc_dev *dev;
746 int rc, tgt_idx;
747 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100748 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100749
750 pr_debug("DEP link up\n");
751
752 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100753 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100754 return -EINVAL;
755
756 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
757 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
758 tgt_idx = NFC_TARGET_IDX_ANY;
759 else
760 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
761
762 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100763
764 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
765 return -EINVAL;
766
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100767 dev = nfc_get_device(idx);
768 if (!dev)
769 return -ENODEV;
770
Samuel Ortiz47807d32012-03-05 01:03:50 +0100771 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100772
773 nfc_put_device(dev);
774
775 return rc;
776}
777
778static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
779{
780 struct nfc_dev *dev;
781 int rc;
782 u32 idx;
783
784 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
785 return -EINVAL;
786
787 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
788
789 dev = nfc_get_device(idx);
790 if (!dev)
791 return -ENODEV;
792
793 rc = nfc_dep_link_down(dev);
794
795 nfc_put_device(dev);
796 return rc;
797}
798
Thierry Escande52feb442012-10-17 14:43:39 +0200799static int nfc_genl_send_params(struct sk_buff *msg,
800 struct nfc_llcp_local *local,
801 u32 portid, u32 seq)
802{
803 void *hdr;
804
805 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
806 NFC_CMD_LLC_GET_PARAMS);
807 if (!hdr)
808 return -EMSGSIZE;
809
810 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
811 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
812 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
813 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
814 goto nla_put_failure;
815
816 return genlmsg_end(msg, hdr);
817
818nla_put_failure:
819
820 genlmsg_cancel(msg, hdr);
821 return -EMSGSIZE;
822}
823
824static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
825{
826 struct nfc_dev *dev;
827 struct nfc_llcp_local *local;
828 int rc = 0;
829 struct sk_buff *msg = NULL;
830 u32 idx;
831
832 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
833 return -EINVAL;
834
835 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
836
837 dev = nfc_get_device(idx);
838 if (!dev)
839 return -ENODEV;
840
841 device_lock(&dev->dev);
842
843 local = nfc_llcp_find_local(dev);
844 if (!local) {
845 rc = -ENODEV;
846 goto exit;
847 }
848
849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
850 if (!msg) {
851 rc = -ENOMEM;
852 goto exit;
853 }
854
855 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
856
857exit:
858 device_unlock(&dev->dev);
859
860 nfc_put_device(dev);
861
862 if (rc < 0) {
863 if (msg)
864 nlmsg_free(msg);
865
866 return rc;
867 }
868
869 return genlmsg_reply(msg, info);
870}
871
872static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
873{
874 struct nfc_dev *dev;
875 struct nfc_llcp_local *local;
876 u8 rw = 0;
877 u16 miux = 0;
878 u32 idx;
879 int rc = 0;
880
881 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
882 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
883 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
884 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
885 return -EINVAL;
886
887 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
888 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
889
890 if (rw > LLCP_MAX_RW)
891 return -EINVAL;
892 }
893
894 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
895 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
896
897 if (miux > LLCP_MAX_MIUX)
898 return -EINVAL;
899 }
900
901 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
902
903 dev = nfc_get_device(idx);
904 if (!dev)
905 return -ENODEV;
906
907 device_lock(&dev->dev);
908
909 local = nfc_llcp_find_local(dev);
910 if (!local) {
911 nfc_put_device(dev);
912 rc = -ENODEV;
913 goto exit;
914 }
915
916 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
917 if (dev->dep_link_up) {
918 rc = -EINPROGRESS;
919 goto exit;
920 }
921
922 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
923 }
924
925 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
926 local->rw = rw;
927
928 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
929 local->miux = cpu_to_be16(miux);
930
931exit:
932 device_unlock(&dev->dev);
933
934 nfc_put_device(dev);
935
936 return rc;
937}
938
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100939static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
940{
941 struct nfc_dev *dev;
942 struct nfc_llcp_local *local;
943 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
944 u32 idx;
945 u8 tid;
946 char *uri;
947 int rc = 0, rem;
948 size_t uri_len, tlvs_len;
949 struct hlist_head sdreq_list;
950 struct nfc_llcp_sdp_tlv *sdreq;
951
952 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
953 !info->attrs[NFC_ATTR_LLC_SDP])
954 return -EINVAL;
955
956 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
957
958 dev = nfc_get_device(idx);
959 if (!dev) {
960 rc = -ENODEV;
961 goto exit;
962 }
963
964 device_lock(&dev->dev);
965
966 if (dev->dep_link_up == false) {
967 rc = -ENOLINK;
968 goto exit;
969 }
970
971 local = nfc_llcp_find_local(dev);
972 if (!local) {
973 nfc_put_device(dev);
974 rc = -ENODEV;
975 goto exit;
976 }
977
978 INIT_HLIST_HEAD(&sdreq_list);
979
980 tlvs_len = 0;
981
982 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
983 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
984 nfc_sdp_genl_policy);
985
986 if (rc != 0) {
987 rc = -EINVAL;
988 goto exit;
989 }
990
991 if (!sdp_attrs[NFC_SDP_ATTR_URI])
992 continue;
993
994 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
995 if (uri_len == 0)
996 continue;
997
998 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
999 if (uri == NULL || *uri == 0)
1000 continue;
1001
1002 tid = local->sdreq_next_tid++;
1003
1004 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1005 if (sdreq == NULL) {
1006 rc = -ENOMEM;
1007 goto exit;
1008 }
1009
1010 tlvs_len += sdreq->tlv_len;
1011
1012 hlist_add_head(&sdreq->node, &sdreq_list);
1013 }
1014
1015 if (hlist_empty(&sdreq_list)) {
1016 rc = -EINVAL;
1017 goto exit;
1018 }
1019
1020 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1021exit:
1022 device_unlock(&dev->dev);
1023
1024 nfc_put_device(dev);
1025
1026 return rc;
1027}
1028
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001029static struct genl_ops nfc_genl_ops[] = {
1030 {
1031 .cmd = NFC_CMD_GET_DEVICE,
1032 .doit = nfc_genl_get_device,
1033 .dumpit = nfc_genl_dump_devices,
1034 .done = nfc_genl_dump_devices_done,
1035 .policy = nfc_genl_policy,
1036 },
1037 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001038 .cmd = NFC_CMD_DEV_UP,
1039 .doit = nfc_genl_dev_up,
1040 .policy = nfc_genl_policy,
1041 },
1042 {
1043 .cmd = NFC_CMD_DEV_DOWN,
1044 .doit = nfc_genl_dev_down,
1045 .policy = nfc_genl_policy,
1046 },
1047 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001048 .cmd = NFC_CMD_START_POLL,
1049 .doit = nfc_genl_start_poll,
1050 .policy = nfc_genl_policy,
1051 },
1052 {
1053 .cmd = NFC_CMD_STOP_POLL,
1054 .doit = nfc_genl_stop_poll,
1055 .policy = nfc_genl_policy,
1056 },
1057 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001058 .cmd = NFC_CMD_DEP_LINK_UP,
1059 .doit = nfc_genl_dep_link_up,
1060 .policy = nfc_genl_policy,
1061 },
1062 {
1063 .cmd = NFC_CMD_DEP_LINK_DOWN,
1064 .doit = nfc_genl_dep_link_down,
1065 .policy = nfc_genl_policy,
1066 },
1067 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001068 .cmd = NFC_CMD_GET_TARGET,
1069 .dumpit = nfc_genl_dump_targets,
1070 .done = nfc_genl_dump_targets_done,
1071 .policy = nfc_genl_policy,
1072 },
Thierry Escande52feb442012-10-17 14:43:39 +02001073 {
1074 .cmd = NFC_CMD_LLC_GET_PARAMS,
1075 .doit = nfc_genl_llc_get_params,
1076 .policy = nfc_genl_policy,
1077 },
1078 {
1079 .cmd = NFC_CMD_LLC_SET_PARAMS,
1080 .doit = nfc_genl_llc_set_params,
1081 .policy = nfc_genl_policy,
1082 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001083 {
1084 .cmd = NFC_CMD_LLC_SDREQ,
1085 .doit = nfc_genl_llc_sdreq,
1086 .policy = nfc_genl_policy,
1087 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001088};
1089
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001090
1091struct urelease_work {
1092 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001093 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001094};
1095
1096static void nfc_urelease_event_work(struct work_struct *work)
1097{
1098 struct urelease_work *w = container_of(work, struct urelease_work, w);
1099 struct class_dev_iter iter;
1100 struct nfc_dev *dev;
1101
John W. Linvillec4876062012-09-28 11:11:16 -04001102 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001103
1104 mutex_lock(&nfc_devlist_mutex);
1105
1106 nfc_device_iter_init(&iter);
1107 dev = nfc_device_iter_next(&iter);
1108
1109 while (dev) {
1110 mutex_lock(&dev->genl_data.genl_data_mutex);
1111
John W. Linvillec4876062012-09-28 11:11:16 -04001112 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001113 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001114 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001115 }
1116
1117 mutex_unlock(&dev->genl_data.genl_data_mutex);
1118
1119 dev = nfc_device_iter_next(&iter);
1120 }
1121
1122 nfc_device_iter_exit(&iter);
1123
1124 mutex_unlock(&nfc_devlist_mutex);
1125
1126 kfree(w);
1127}
1128
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001129static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001130 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001131{
1132 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001133 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001134
1135 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1136 goto out;
1137
Eric W. Biederman15e47302012-09-07 20:12:54 +00001138 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001139
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001140 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1141 if (w) {
1142 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001143 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001144 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001145 }
1146
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001147out:
1148 return NOTIFY_DONE;
1149}
1150
1151void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1152{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001153 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001154 mutex_init(&genl_data->genl_data_mutex);
1155}
1156
1157void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1158{
1159 mutex_destroy(&genl_data->genl_data_mutex);
1160}
1161
1162static struct notifier_block nl_notifier = {
1163 .notifier_call = nfc_genl_rcv_nl_event,
1164};
1165
1166/**
1167 * nfc_genl_init() - Initialize netlink interface
1168 *
1169 * This initialization function registers the nfc netlink family.
1170 */
1171int __init nfc_genl_init(void)
1172{
1173 int rc;
1174
1175 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001176 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001177 if (rc)
1178 return rc;
1179
1180 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1181
1182 netlink_register_notifier(&nl_notifier);
1183
1184 return rc;
1185}
1186
1187/**
1188 * nfc_genl_exit() - Deinitialize netlink interface
1189 *
1190 * This exit function unregisters the nfc netlink family.
1191 */
1192void nfc_genl_exit(void)
1193{
1194 netlink_unregister_notifier(&nl_notifier);
1195 genl_unregister_family(&nfc_genl_family);
1196}