blob: f0c4d61f37c0f623df3eb40a1222a87f9c875f69 [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"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020031#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020032
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030033static struct genl_multicast_group nfc_genl_event_mcgrp = {
34 .name = NFC_GENL_MCAST_EVENT_NAME,
35};
36
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020037static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030038 .id = GENL_ID_GENERATE,
39 .hdrsize = 0,
40 .name = NFC_GENL_NAME,
41 .version = NFC_GENL_VERSION,
42 .maxattr = NFC_ATTR_MAX,
43};
44
45static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
48 .len = NFC_DEVICE_NAME_MAXSIZE },
49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010050 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010052 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020053 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010055 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010058 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59};
60
61static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
62 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
63 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030064};
65
66static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010067 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030068{
69 void *hdr;
70
Eric W. Biederman15e47302012-09-07 20:12:54 +000071 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010072 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030073 if (!hdr)
74 return -EMSGSIZE;
75
76 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
77
David S. Miller1e6428d2012-03-29 23:23:57 -040078 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
79 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
80 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
81 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
82 goto nla_put_failure;
83 if (target->nfcid1_len > 0 &&
84 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
85 target->nfcid1))
86 goto nla_put_failure;
87 if (target->sensb_res_len > 0 &&
88 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
89 target->sensb_res))
90 goto nla_put_failure;
91 if (target->sensf_res_len > 0 &&
92 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
93 target->sensf_res))
94 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030095
96 return genlmsg_end(msg, hdr);
97
98nla_put_failure:
99 genlmsg_cancel(msg, hdr);
100 return -EMSGSIZE;
101}
102
103static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
104{
105 struct nfc_dev *dev;
106 int rc;
107 u32 idx;
108
109 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100110 nfc_genl_family.attrbuf,
111 nfc_genl_family.maxattr,
112 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300113 if (rc < 0)
114 return ERR_PTR(rc);
115
116 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
117 return ERR_PTR(-EINVAL);
118
119 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
120
121 dev = nfc_get_device(idx);
122 if (!dev)
123 return ERR_PTR(-ENODEV);
124
125 return dev;
126}
127
128static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100129 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300130{
131 int i = cb->args[0];
132 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
133 int rc;
134
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300135 if (!dev) {
136 dev = __get_device_from_cb(cb);
137 if (IS_ERR(dev))
138 return PTR_ERR(dev);
139
140 cb->args[1] = (long) dev;
141 }
142
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200143 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300144
145 cb->seq = dev->targets_generation;
146
147 while (i < dev->n_targets) {
148 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100149 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300150 if (rc < 0)
151 break;
152
153 i++;
154 }
155
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200156 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300157
158 cb->args[0] = i;
159
160 return skb->len;
161}
162
163static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
164{
165 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
166
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300167 if (dev)
168 nfc_put_device(dev);
169
170 return 0;
171}
172
173int nfc_genl_targets_found(struct nfc_dev *dev)
174{
175 struct sk_buff *msg;
176 void *hdr;
177
Eric W. Biederman15e47302012-09-07 20:12:54 +0000178 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300179
Thomas Graf58050fc2012-06-28 03:57:45 +0000180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300181 if (!msg)
182 return -ENOMEM;
183
184 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100185 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300186 if (!hdr)
187 goto free_msg;
188
David S. Miller1e6428d2012-03-29 23:23:57 -0400189 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
190 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300191
192 genlmsg_end(msg, hdr);
193
194 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
195
196nla_put_failure:
197 genlmsg_cancel(msg, hdr);
198free_msg:
199 nlmsg_free(msg);
200 return -EMSGSIZE;
201}
202
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200203int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
204{
205 struct sk_buff *msg;
206 void *hdr;
207
Thomas Graf58050fc2012-06-28 03:57:45 +0000208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200209 if (!msg)
210 return -ENOMEM;
211
212 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
213 NFC_EVENT_TARGET_LOST);
214 if (!hdr)
215 goto free_msg;
216
John W. Linville59ef43e2012-04-18 14:17:13 -0400217 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
218 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
219 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200220
221 genlmsg_end(msg, hdr);
222
223 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
224
225 return 0;
226
227nla_put_failure:
228 genlmsg_cancel(msg, hdr);
229free_msg:
230 nlmsg_free(msg);
231 return -EMSGSIZE;
232}
233
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200234int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
235{
236 struct sk_buff *msg;
237 void *hdr;
238
Thomas Graf58050fc2012-06-28 03:57:45 +0000239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200240 if (!msg)
241 return -ENOMEM;
242
243 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
244 NFC_EVENT_TM_ACTIVATED);
245 if (!hdr)
246 goto free_msg;
247
248 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
249 goto nla_put_failure;
250 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
251 goto nla_put_failure;
252
253 genlmsg_end(msg, hdr);
254
255 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
256
257 return 0;
258
259nla_put_failure:
260 genlmsg_cancel(msg, hdr);
261free_msg:
262 nlmsg_free(msg);
263 return -EMSGSIZE;
264}
265
266int nfc_genl_tm_deactivated(struct nfc_dev *dev)
267{
268 struct sk_buff *msg;
269 void *hdr;
270
Thomas Graf58050fc2012-06-28 03:57:45 +0000271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200272 if (!msg)
273 return -ENOMEM;
274
275 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
276 NFC_EVENT_TM_DEACTIVATED);
277 if (!hdr)
278 goto free_msg;
279
280 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
281 goto nla_put_failure;
282
283 genlmsg_end(msg, hdr);
284
285 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
286
287 return 0;
288
289nla_put_failure:
290 genlmsg_cancel(msg, hdr);
291free_msg:
292 nlmsg_free(msg);
293 return -EMSGSIZE;
294}
295
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300296int nfc_genl_device_added(struct nfc_dev *dev)
297{
298 struct sk_buff *msg;
299 void *hdr;
300
Thomas Graf58050fc2012-06-28 03:57:45 +0000301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300302 if (!msg)
303 return -ENOMEM;
304
305 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100306 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300307 if (!hdr)
308 goto free_msg;
309
David S. Miller1e6428d2012-03-29 23:23:57 -0400310 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
311 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
312 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
313 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
314 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300315
316 genlmsg_end(msg, hdr);
317
318 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
319
320 return 0;
321
322nla_put_failure:
323 genlmsg_cancel(msg, hdr);
324free_msg:
325 nlmsg_free(msg);
326 return -EMSGSIZE;
327}
328
329int nfc_genl_device_removed(struct nfc_dev *dev)
330{
331 struct sk_buff *msg;
332 void *hdr;
333
Thomas Graf58050fc2012-06-28 03:57:45 +0000334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300335 if (!msg)
336 return -ENOMEM;
337
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100339 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300340 if (!hdr)
341 goto free_msg;
342
David S. Miller1e6428d2012-03-29 23:23:57 -0400343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
344 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300345
346 genlmsg_end(msg, hdr);
347
348 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
349
350 return 0;
351
352nla_put_failure:
353 genlmsg_cancel(msg, hdr);
354free_msg:
355 nlmsg_free(msg);
356 return -EMSGSIZE;
357}
358
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100359int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
360{
361 struct sk_buff *msg;
362 struct nlattr *sdp_attr, *uri_attr;
363 struct nfc_llcp_sdp_tlv *sdres;
364 struct hlist_node *n;
365 void *hdr;
366 int rc = -EMSGSIZE;
367 int i;
368
369 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
370 if (!msg)
371 return -ENOMEM;
372
373 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
374 NFC_EVENT_LLC_SDRES);
375 if (!hdr)
376 goto free_msg;
377
378 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
379 goto nla_put_failure;
380
381 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
382 if (sdp_attr == NULL) {
383 rc = -ENOMEM;
384 goto nla_put_failure;
385 }
386
387 i = 1;
388 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
389 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
390
391 uri_attr = nla_nest_start(msg, i++);
392 if (uri_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
395 }
396
397 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
398 goto nla_put_failure;
399
400 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
401 goto nla_put_failure;
402
403 nla_nest_end(msg, uri_attr);
404
405 hlist_del(&sdres->node);
406
407 nfc_llcp_free_sdp_tlv(sdres);
408 }
409
410 nla_nest_end(msg, sdp_attr);
411
412 genlmsg_end(msg, hdr);
413
414 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
415
416nla_put_failure:
417 genlmsg_cancel(msg, hdr);
418
419free_msg:
420 nlmsg_free(msg);
421
422 nfc_llcp_free_sdp_tlv_list(sdres_list);
423
424 return rc;
425}
426
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300427static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000428 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100429 struct netlink_callback *cb,
430 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300431{
432 void *hdr;
433
Eric W. Biederman15e47302012-09-07 20:12:54 +0000434 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100435 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300436 if (!hdr)
437 return -EMSGSIZE;
438
439 if (cb)
440 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
441
David S. Miller1e6428d2012-03-29 23:23:57 -0400442 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
443 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
444 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100445 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200446 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
447 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400448 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300449
450 return genlmsg_end(msg, hdr);
451
452nla_put_failure:
453 genlmsg_cancel(msg, hdr);
454 return -EMSGSIZE;
455}
456
457static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100458 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300459{
460 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
461 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
462 bool first_call = false;
463
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300464 if (!iter) {
465 first_call = true;
466 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
467 if (!iter)
468 return -ENOMEM;
469 cb->args[0] = (long) iter;
470 }
471
472 mutex_lock(&nfc_devlist_mutex);
473
474 cb->seq = nfc_devlist_generation;
475
476 if (first_call) {
477 nfc_device_iter_init(iter);
478 dev = nfc_device_iter_next(iter);
479 }
480
481 while (dev) {
482 int rc;
483
Eric W. Biederman15e47302012-09-07 20:12:54 +0000484 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100485 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300486 if (rc < 0)
487 break;
488
489 dev = nfc_device_iter_next(iter);
490 }
491
492 mutex_unlock(&nfc_devlist_mutex);
493
494 cb->args[1] = (long) dev;
495
496 return skb->len;
497}
498
499static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
500{
501 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
502
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300503 nfc_device_iter_exit(iter);
504 kfree(iter);
505
506 return 0;
507}
508
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100509int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100510 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100511{
512 struct sk_buff *msg;
513 void *hdr;
514
515 pr_debug("DEP link is up\n");
516
Thomas Graf58050fc2012-06-28 03:57:45 +0000517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100518 if (!msg)
519 return -ENOMEM;
520
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100521 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100522 if (!hdr)
523 goto free_msg;
524
David S. Miller1e6428d2012-03-29 23:23:57 -0400525 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
526 goto nla_put_failure;
527 if (rf_mode == NFC_RF_INITIATOR &&
528 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
529 goto nla_put_failure;
530 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
531 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
532 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100533
534 genlmsg_end(msg, hdr);
535
536 dev->dep_link_up = true;
537
538 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
539
540 return 0;
541
542nla_put_failure:
543 genlmsg_cancel(msg, hdr);
544free_msg:
545 nlmsg_free(msg);
546 return -EMSGSIZE;
547}
548
549int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
550{
551 struct sk_buff *msg;
552 void *hdr;
553
554 pr_debug("DEP link is down\n");
555
Thomas Graf58050fc2012-06-28 03:57:45 +0000556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100557 if (!msg)
558 return -ENOMEM;
559
560 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100561 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100562 if (!hdr)
563 goto free_msg;
564
David S. Miller1e6428d2012-03-29 23:23:57 -0400565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
566 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100567
568 genlmsg_end(msg, hdr);
569
570 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
571
572 return 0;
573
574nla_put_failure:
575 genlmsg_cancel(msg, hdr);
576free_msg:
577 nlmsg_free(msg);
578 return -EMSGSIZE;
579}
580
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300581static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
582{
583 struct sk_buff *msg;
584 struct nfc_dev *dev;
585 u32 idx;
586 int rc = -ENOBUFS;
587
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300588 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
589 return -EINVAL;
590
591 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
592
593 dev = nfc_get_device(idx);
594 if (!dev)
595 return -ENODEV;
596
Thomas Graf58050fc2012-06-28 03:57:45 +0000597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300598 if (!msg) {
599 rc = -ENOMEM;
600 goto out_putdev;
601 }
602
Eric W. Biederman15e47302012-09-07 20:12:54 +0000603 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100604 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300605 if (rc < 0)
606 goto out_free;
607
608 nfc_put_device(dev);
609
610 return genlmsg_reply(msg, info);
611
612out_free:
613 nlmsg_free(msg);
614out_putdev:
615 nfc_put_device(dev);
616 return rc;
617}
618
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300619static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
620{
621 struct nfc_dev *dev;
622 int rc;
623 u32 idx;
624
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300625 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
626 return -EINVAL;
627
628 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
629
630 dev = nfc_get_device(idx);
631 if (!dev)
632 return -ENODEV;
633
634 rc = nfc_dev_up(dev);
635
636 nfc_put_device(dev);
637 return rc;
638}
639
640static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
641{
642 struct nfc_dev *dev;
643 int rc;
644 u32 idx;
645
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300646 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
647 return -EINVAL;
648
649 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
650
651 dev = nfc_get_device(idx);
652 if (!dev)
653 return -ENODEV;
654
655 rc = nfc_dev_down(dev);
656
657 nfc_put_device(dev);
658 return rc;
659}
660
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300661static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
662{
663 struct nfc_dev *dev;
664 int rc;
665 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200666 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300667
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100668 pr_debug("Poll start\n");
669
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300670 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200671 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
672 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200673 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300674 return -EINVAL;
675
676 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200677
678 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
679 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200680
681 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
682 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200683 else if (info->attrs[NFC_ATTR_PROTOCOLS])
684 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300685
686 dev = nfc_get_device(idx);
687 if (!dev)
688 return -ENODEV;
689
690 mutex_lock(&dev->genl_data.genl_data_mutex);
691
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200692 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300693 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000694 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300695
696 mutex_unlock(&dev->genl_data.genl_data_mutex);
697
698 nfc_put_device(dev);
699 return rc;
700}
701
702static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
703{
704 struct nfc_dev *dev;
705 int rc;
706 u32 idx;
707
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300708 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
709 return -EINVAL;
710
711 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
712
713 dev = nfc_get_device(idx);
714 if (!dev)
715 return -ENODEV;
716
Samuel Ortiza831b912012-06-28 16:41:57 +0200717 device_lock(&dev->dev);
718
719 if (!dev->polling) {
720 device_unlock(&dev->dev);
721 return -EINVAL;
722 }
723
724 device_unlock(&dev->dev);
725
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300726 mutex_lock(&dev->genl_data.genl_data_mutex);
727
Eric W. Biederman15e47302012-09-07 20:12:54 +0000728 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300729 rc = -EBUSY;
730 goto out;
731 }
732
733 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000734 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300735
736out:
737 mutex_unlock(&dev->genl_data.genl_data_mutex);
738 nfc_put_device(dev);
739 return rc;
740}
741
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100742static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
743{
744 struct nfc_dev *dev;
745 int rc, tgt_idx;
746 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100747 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100748
749 pr_debug("DEP link up\n");
750
751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100752 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100753 return -EINVAL;
754
755 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
756 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
757 tgt_idx = NFC_TARGET_IDX_ANY;
758 else
759 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
760
761 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100762
763 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
764 return -EINVAL;
765
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100766 dev = nfc_get_device(idx);
767 if (!dev)
768 return -ENODEV;
769
Samuel Ortiz47807d32012-03-05 01:03:50 +0100770 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100771
772 nfc_put_device(dev);
773
774 return rc;
775}
776
777static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
778{
779 struct nfc_dev *dev;
780 int rc;
781 u32 idx;
782
783 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
784 return -EINVAL;
785
786 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
787
788 dev = nfc_get_device(idx);
789 if (!dev)
790 return -ENODEV;
791
792 rc = nfc_dep_link_down(dev);
793
794 nfc_put_device(dev);
795 return rc;
796}
797
Thierry Escande52feb442012-10-17 14:43:39 +0200798static int nfc_genl_send_params(struct sk_buff *msg,
799 struct nfc_llcp_local *local,
800 u32 portid, u32 seq)
801{
802 void *hdr;
803
804 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
805 NFC_CMD_LLC_GET_PARAMS);
806 if (!hdr)
807 return -EMSGSIZE;
808
809 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
810 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
811 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
812 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
813 goto nla_put_failure;
814
815 return genlmsg_end(msg, hdr);
816
817nla_put_failure:
818
819 genlmsg_cancel(msg, hdr);
820 return -EMSGSIZE;
821}
822
823static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
824{
825 struct nfc_dev *dev;
826 struct nfc_llcp_local *local;
827 int rc = 0;
828 struct sk_buff *msg = NULL;
829 u32 idx;
830
831 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
832 return -EINVAL;
833
834 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
835
836 dev = nfc_get_device(idx);
837 if (!dev)
838 return -ENODEV;
839
840 device_lock(&dev->dev);
841
842 local = nfc_llcp_find_local(dev);
843 if (!local) {
844 rc = -ENODEV;
845 goto exit;
846 }
847
848 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
849 if (!msg) {
850 rc = -ENOMEM;
851 goto exit;
852 }
853
854 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
855
856exit:
857 device_unlock(&dev->dev);
858
859 nfc_put_device(dev);
860
861 if (rc < 0) {
862 if (msg)
863 nlmsg_free(msg);
864
865 return rc;
866 }
867
868 return genlmsg_reply(msg, info);
869}
870
871static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
872{
873 struct nfc_dev *dev;
874 struct nfc_llcp_local *local;
875 u8 rw = 0;
876 u16 miux = 0;
877 u32 idx;
878 int rc = 0;
879
880 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
881 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
882 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
883 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
884 return -EINVAL;
885
886 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
887 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
888
889 if (rw > LLCP_MAX_RW)
890 return -EINVAL;
891 }
892
893 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
894 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
895
896 if (miux > LLCP_MAX_MIUX)
897 return -EINVAL;
898 }
899
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901
902 dev = nfc_get_device(idx);
903 if (!dev)
904 return -ENODEV;
905
906 device_lock(&dev->dev);
907
908 local = nfc_llcp_find_local(dev);
909 if (!local) {
910 nfc_put_device(dev);
911 rc = -ENODEV;
912 goto exit;
913 }
914
915 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
916 if (dev->dep_link_up) {
917 rc = -EINPROGRESS;
918 goto exit;
919 }
920
921 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
922 }
923
924 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
925 local->rw = rw;
926
927 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
928 local->miux = cpu_to_be16(miux);
929
930exit:
931 device_unlock(&dev->dev);
932
933 nfc_put_device(dev);
934
935 return rc;
936}
937
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100938static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
939{
940 struct nfc_dev *dev;
941 struct nfc_llcp_local *local;
942 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
943 u32 idx;
944 u8 tid;
945 char *uri;
946 int rc = 0, rem;
947 size_t uri_len, tlvs_len;
948 struct hlist_head sdreq_list;
949 struct nfc_llcp_sdp_tlv *sdreq;
950
951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
952 !info->attrs[NFC_ATTR_LLC_SDP])
953 return -EINVAL;
954
955 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
956
957 dev = nfc_get_device(idx);
958 if (!dev) {
959 rc = -ENODEV;
960 goto exit;
961 }
962
963 device_lock(&dev->dev);
964
965 if (dev->dep_link_up == false) {
966 rc = -ENOLINK;
967 goto exit;
968 }
969
970 local = nfc_llcp_find_local(dev);
971 if (!local) {
972 nfc_put_device(dev);
973 rc = -ENODEV;
974 goto exit;
975 }
976
977 INIT_HLIST_HEAD(&sdreq_list);
978
979 tlvs_len = 0;
980
981 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
982 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
983 nfc_sdp_genl_policy);
984
985 if (rc != 0) {
986 rc = -EINVAL;
987 goto exit;
988 }
989
990 if (!sdp_attrs[NFC_SDP_ATTR_URI])
991 continue;
992
993 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
994 if (uri_len == 0)
995 continue;
996
997 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
998 if (uri == NULL || *uri == 0)
999 continue;
1000
1001 tid = local->sdreq_next_tid++;
1002
1003 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1004 if (sdreq == NULL) {
1005 rc = -ENOMEM;
1006 goto exit;
1007 }
1008
1009 tlvs_len += sdreq->tlv_len;
1010
1011 hlist_add_head(&sdreq->node, &sdreq_list);
1012 }
1013
1014 if (hlist_empty(&sdreq_list)) {
1015 rc = -EINVAL;
1016 goto exit;
1017 }
1018
1019 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1020exit:
1021 device_unlock(&dev->dev);
1022
1023 nfc_put_device(dev);
1024
1025 return rc;
1026}
1027
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001028static struct genl_ops nfc_genl_ops[] = {
1029 {
1030 .cmd = NFC_CMD_GET_DEVICE,
1031 .doit = nfc_genl_get_device,
1032 .dumpit = nfc_genl_dump_devices,
1033 .done = nfc_genl_dump_devices_done,
1034 .policy = nfc_genl_policy,
1035 },
1036 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001037 .cmd = NFC_CMD_DEV_UP,
1038 .doit = nfc_genl_dev_up,
1039 .policy = nfc_genl_policy,
1040 },
1041 {
1042 .cmd = NFC_CMD_DEV_DOWN,
1043 .doit = nfc_genl_dev_down,
1044 .policy = nfc_genl_policy,
1045 },
1046 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001047 .cmd = NFC_CMD_START_POLL,
1048 .doit = nfc_genl_start_poll,
1049 .policy = nfc_genl_policy,
1050 },
1051 {
1052 .cmd = NFC_CMD_STOP_POLL,
1053 .doit = nfc_genl_stop_poll,
1054 .policy = nfc_genl_policy,
1055 },
1056 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001057 .cmd = NFC_CMD_DEP_LINK_UP,
1058 .doit = nfc_genl_dep_link_up,
1059 .policy = nfc_genl_policy,
1060 },
1061 {
1062 .cmd = NFC_CMD_DEP_LINK_DOWN,
1063 .doit = nfc_genl_dep_link_down,
1064 .policy = nfc_genl_policy,
1065 },
1066 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001067 .cmd = NFC_CMD_GET_TARGET,
1068 .dumpit = nfc_genl_dump_targets,
1069 .done = nfc_genl_dump_targets_done,
1070 .policy = nfc_genl_policy,
1071 },
Thierry Escande52feb442012-10-17 14:43:39 +02001072 {
1073 .cmd = NFC_CMD_LLC_GET_PARAMS,
1074 .doit = nfc_genl_llc_get_params,
1075 .policy = nfc_genl_policy,
1076 },
1077 {
1078 .cmd = NFC_CMD_LLC_SET_PARAMS,
1079 .doit = nfc_genl_llc_set_params,
1080 .policy = nfc_genl_policy,
1081 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001082 {
1083 .cmd = NFC_CMD_LLC_SDREQ,
1084 .doit = nfc_genl_llc_sdreq,
1085 .policy = nfc_genl_policy,
1086 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001087};
1088
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001089
1090struct urelease_work {
1091 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001092 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001093};
1094
1095static void nfc_urelease_event_work(struct work_struct *work)
1096{
1097 struct urelease_work *w = container_of(work, struct urelease_work, w);
1098 struct class_dev_iter iter;
1099 struct nfc_dev *dev;
1100
John W. Linvillec4876062012-09-28 11:11:16 -04001101 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001102
1103 mutex_lock(&nfc_devlist_mutex);
1104
1105 nfc_device_iter_init(&iter);
1106 dev = nfc_device_iter_next(&iter);
1107
1108 while (dev) {
1109 mutex_lock(&dev->genl_data.genl_data_mutex);
1110
John W. Linvillec4876062012-09-28 11:11:16 -04001111 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001112 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001113 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001114 }
1115
1116 mutex_unlock(&dev->genl_data.genl_data_mutex);
1117
1118 dev = nfc_device_iter_next(&iter);
1119 }
1120
1121 nfc_device_iter_exit(&iter);
1122
1123 mutex_unlock(&nfc_devlist_mutex);
1124
1125 kfree(w);
1126}
1127
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001128static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001129 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001130{
1131 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001132 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001133
1134 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1135 goto out;
1136
Eric W. Biederman15e47302012-09-07 20:12:54 +00001137 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001138
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001139 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1140 if (w) {
1141 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001142 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001143 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001144 }
1145
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001146out:
1147 return NOTIFY_DONE;
1148}
1149
1150void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1151{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001152 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001153 mutex_init(&genl_data->genl_data_mutex);
1154}
1155
1156void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1157{
1158 mutex_destroy(&genl_data->genl_data_mutex);
1159}
1160
1161static struct notifier_block nl_notifier = {
1162 .notifier_call = nfc_genl_rcv_nl_event,
1163};
1164
1165/**
1166 * nfc_genl_init() - Initialize netlink interface
1167 *
1168 * This initialization function registers the nfc netlink family.
1169 */
1170int __init nfc_genl_init(void)
1171{
1172 int rc;
1173
1174 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001175 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001176 if (rc)
1177 return rc;
1178
1179 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1180
1181 netlink_register_notifier(&nl_notifier);
1182
1183 return rc;
1184}
1185
1186/**
1187 * nfc_genl_exit() - Deinitialize netlink interface
1188 *
1189 * This exit function unregisters the nfc netlink family.
1190 */
1191void nfc_genl_exit(void)
1192{
1193 netlink_unregister_notifier(&nl_notifier);
1194 genl_unregister_family(&nfc_genl_family);
1195}