blob: 3763036710aedfc38768793c44388b70375ae71b [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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030020 */
21
Samuel Ortiz52858b52011-12-14 16:43:05 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080023
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030024#include <net/genetlink.h>
25#include <linux/nfc.h>
26#include <linux/slab.h>
27
28#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020029#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020030
Johannes Berg2a94fe42013-11-19 15:19:39 +010031static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030033};
34
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020035static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030036 .id = GENL_ID_GENERATE,
37 .hdrsize = 0,
38 .name = NFC_GENL_NAME,
39 .version = NFC_GENL_VERSION,
40 .maxattr = NFC_ATTR_MAX,
41};
42
43static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
44 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
45 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
46 .len = NFC_DEVICE_NAME_MAXSIZE },
47 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010048 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010050 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020051 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010053 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
55 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010056 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020057 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020059 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010060};
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
Mark A. Greerf5f68722014-01-14 17:52:11 -070097 if (target->is_iso15693) {
98 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
99 target->iso15693_dsfid) ||
100 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
101 sizeof(target->iso15693_uid), target->iso15693_uid))
102 goto nla_put_failure;
103 }
104
Johannes Berg053c0952015-01-16 22:09:00 +0100105 genlmsg_end(msg, hdr);
106 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300107
108nla_put_failure:
109 genlmsg_cancel(msg, hdr);
110 return -EMSGSIZE;
111}
112
113static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
114{
115 struct nfc_dev *dev;
116 int rc;
117 u32 idx;
118
119 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100120 nfc_genl_family.attrbuf,
121 nfc_genl_family.maxattr,
122 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300123 if (rc < 0)
124 return ERR_PTR(rc);
125
126 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
127 return ERR_PTR(-EINVAL);
128
129 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
130
131 dev = nfc_get_device(idx);
132 if (!dev)
133 return ERR_PTR(-ENODEV);
134
135 return dev;
136}
137
138static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100139 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300140{
141 int i = cb->args[0];
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
143 int rc;
144
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145 if (!dev) {
146 dev = __get_device_from_cb(cb);
147 if (IS_ERR(dev))
148 return PTR_ERR(dev);
149
150 cb->args[1] = (long) dev;
151 }
152
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200153 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300154
155 cb->seq = dev->targets_generation;
156
157 while (i < dev->n_targets) {
158 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100159 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300160 if (rc < 0)
161 break;
162
163 i++;
164 }
165
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200166 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300167
168 cb->args[0] = i;
169
170 return skb->len;
171}
172
173static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
174{
175 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
176
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300177 if (dev)
178 nfc_put_device(dev);
179
180 return 0;
181}
182
183int nfc_genl_targets_found(struct nfc_dev *dev)
184{
185 struct sk_buff *msg;
186 void *hdr;
187
Eric W. Biederman15e47302012-09-07 20:12:54 +0000188 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300189
Thomas Graf58050fc2012-06-28 03:57:45 +0000190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300191 if (!msg)
192 return -ENOMEM;
193
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100195 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300196 if (!hdr)
197 goto free_msg;
198
David S. Miller1e6428d2012-03-29 23:23:57 -0400199 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
200 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300201
202 genlmsg_end(msg, hdr);
203
Johannes Berg2a94fe42013-11-19 15:19:39 +0100204 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300205
206nla_put_failure:
207 genlmsg_cancel(msg, hdr);
208free_msg:
209 nlmsg_free(msg);
210 return -EMSGSIZE;
211}
212
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200213int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
214{
215 struct sk_buff *msg;
216 void *hdr;
217
Thomas Graf58050fc2012-06-28 03:57:45 +0000218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200219 if (!msg)
220 return -ENOMEM;
221
222 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
223 NFC_EVENT_TARGET_LOST);
224 if (!hdr)
225 goto free_msg;
226
John W. Linville59ef43e2012-04-18 14:17:13 -0400227 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
228 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
229 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200230
231 genlmsg_end(msg, hdr);
232
Johannes Berg2a94fe42013-11-19 15:19:39 +0100233 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200234
235 return 0;
236
237nla_put_failure:
238 genlmsg_cancel(msg, hdr);
239free_msg:
240 nlmsg_free(msg);
241 return -EMSGSIZE;
242}
243
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200244int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
245{
246 struct sk_buff *msg;
247 void *hdr;
248
Thomas Graf58050fc2012-06-28 03:57:45 +0000249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200250 if (!msg)
251 return -ENOMEM;
252
253 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
254 NFC_EVENT_TM_ACTIVATED);
255 if (!hdr)
256 goto free_msg;
257
258 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
259 goto nla_put_failure;
260 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
261 goto nla_put_failure;
262
263 genlmsg_end(msg, hdr);
264
Johannes Berg2a94fe42013-11-19 15:19:39 +0100265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200266
267 return 0;
268
269nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271free_msg:
272 nlmsg_free(msg);
273 return -EMSGSIZE;
274}
275
276int nfc_genl_tm_deactivated(struct nfc_dev *dev)
277{
278 struct sk_buff *msg;
279 void *hdr;
280
Thomas Graf58050fc2012-06-28 03:57:45 +0000281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200282 if (!msg)
283 return -ENOMEM;
284
285 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
286 NFC_EVENT_TM_DEACTIVATED);
287 if (!hdr)
288 goto free_msg;
289
290 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
291 goto nla_put_failure;
292
293 genlmsg_end(msg, hdr);
294
Johannes Berg2a94fe42013-11-19 15:19:39 +0100295 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200296
297 return 0;
298
299nla_put_failure:
300 genlmsg_cancel(msg, hdr);
301free_msg:
302 nlmsg_free(msg);
303 return -EMSGSIZE;
304}
305
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300306int nfc_genl_device_added(struct nfc_dev *dev)
307{
308 struct sk_buff *msg;
309 void *hdr;
310
Thomas Graf58050fc2012-06-28 03:57:45 +0000311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300312 if (!msg)
313 return -ENOMEM;
314
315 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100316 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300317 if (!hdr)
318 goto free_msg;
319
David S. Miller1e6428d2012-03-29 23:23:57 -0400320 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
321 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
322 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
323 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
324 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300325
326 genlmsg_end(msg, hdr);
327
Johannes Berg2a94fe42013-11-19 15:19:39 +0100328 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300329
330 return 0;
331
332nla_put_failure:
333 genlmsg_cancel(msg, hdr);
334free_msg:
335 nlmsg_free(msg);
336 return -EMSGSIZE;
337}
338
339int nfc_genl_device_removed(struct nfc_dev *dev)
340{
341 struct sk_buff *msg;
342 void *hdr;
343
Thomas Graf58050fc2012-06-28 03:57:45 +0000344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300345 if (!msg)
346 return -ENOMEM;
347
348 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100349 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300350 if (!hdr)
351 goto free_msg;
352
David S. Miller1e6428d2012-03-29 23:23:57 -0400353 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
354 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300355
356 genlmsg_end(msg, hdr);
357
Johannes Berg2a94fe42013-11-19 15:19:39 +0100358 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300359
360 return 0;
361
362nla_put_failure:
363 genlmsg_cancel(msg, hdr);
364free_msg:
365 nlmsg_free(msg);
366 return -EMSGSIZE;
367}
368
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100369int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
370{
371 struct sk_buff *msg;
372 struct nlattr *sdp_attr, *uri_attr;
373 struct nfc_llcp_sdp_tlv *sdres;
374 struct hlist_node *n;
375 void *hdr;
376 int rc = -EMSGSIZE;
377 int i;
378
379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
380 if (!msg)
381 return -ENOMEM;
382
383 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
384 NFC_EVENT_LLC_SDRES);
385 if (!hdr)
386 goto free_msg;
387
388 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
389 goto nla_put_failure;
390
391 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
392 if (sdp_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
395 }
396
397 i = 1;
398 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
399 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
400
401 uri_attr = nla_nest_start(msg, i++);
402 if (uri_attr == NULL) {
403 rc = -ENOMEM;
404 goto nla_put_failure;
405 }
406
407 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
408 goto nla_put_failure;
409
410 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
411 goto nla_put_failure;
412
413 nla_nest_end(msg, uri_attr);
414
415 hlist_del(&sdres->node);
416
417 nfc_llcp_free_sdp_tlv(sdres);
418 }
419
420 nla_nest_end(msg, sdp_attr);
421
422 genlmsg_end(msg, hdr);
423
Johannes Berg2a94fe42013-11-19 15:19:39 +0100424 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100425
426nla_put_failure:
427 genlmsg_cancel(msg, hdr);
428
429free_msg:
430 nlmsg_free(msg);
431
432 nfc_llcp_free_sdp_tlv_list(sdres_list);
433
434 return rc;
435}
436
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200437int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
438{
439 struct sk_buff *msg;
440 void *hdr;
441
442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443 if (!msg)
444 return -ENOMEM;
445
446 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
447 NFC_EVENT_SE_ADDED);
448 if (!hdr)
449 goto free_msg;
450
451 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
452 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
453 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
454 goto nla_put_failure;
455
456 genlmsg_end(msg, hdr);
457
Johannes Berg2a94fe42013-11-19 15:19:39 +0100458 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200459
460 return 0;
461
462nla_put_failure:
463 genlmsg_cancel(msg, hdr);
464free_msg:
465 nlmsg_free(msg);
466 return -EMSGSIZE;
467}
468
469int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
470{
471 struct sk_buff *msg;
472 void *hdr;
473
474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
475 if (!msg)
476 return -ENOMEM;
477
478 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
479 NFC_EVENT_SE_REMOVED);
480 if (!hdr)
481 goto free_msg;
482
483 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
484 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
485 goto nla_put_failure;
486
487 genlmsg_end(msg, hdr);
488
Johannes Berg2a94fe42013-11-19 15:19:39 +0100489 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200490
491 return 0;
492
493nla_put_failure:
494 genlmsg_cancel(msg, hdr);
495free_msg:
496 nlmsg_free(msg);
497 return -EMSGSIZE;
498}
499
Christophe Ricard447b27c2015-02-01 22:26:16 +0100500int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
501 struct nfc_evt_transaction *evt_transaction)
502{
503 struct nfc_se *se;
504 struct sk_buff *msg;
505 void *hdr;
506
507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
508 if (!msg)
509 return -ENOMEM;
510
511 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
512 NFC_EVENT_SE_TRANSACTION);
513 if (!hdr)
514 goto free_msg;
515
516 se = nfc_find_se(dev, se_idx);
517 if (!se)
518 goto free_msg;
519
520 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
521 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
522 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
523 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
524 evt_transaction->aid) ||
525 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
526 evt_transaction->params))
527 goto nla_put_failure;
528
529 /* evt_transaction is no more used */
530 devm_kfree(&dev->dev, evt_transaction);
531
532 genlmsg_end(msg, hdr);
533
534 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
535
536 return 0;
537
538nla_put_failure:
539 genlmsg_cancel(msg, hdr);
540free_msg:
541 /* evt_transaction is no more used */
542 devm_kfree(&dev->dev, evt_transaction);
543 nlmsg_free(msg);
544 return -EMSGSIZE;
545}
546
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300547static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000548 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100549 struct netlink_callback *cb,
550 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300551{
552 void *hdr;
553
Eric W. Biederman15e47302012-09-07 20:12:54 +0000554 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100555 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300556 if (!hdr)
557 return -EMSGSIZE;
558
559 if (cb)
560 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
561
David S. Miller1e6428d2012-03-29 23:23:57 -0400562 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
563 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
564 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200565 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
566 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400567 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300568
Johannes Berg053c0952015-01-16 22:09:00 +0100569 genlmsg_end(msg, hdr);
570 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300571
572nla_put_failure:
573 genlmsg_cancel(msg, hdr);
574 return -EMSGSIZE;
575}
576
577static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100578 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300579{
580 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
581 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
582 bool first_call = false;
583
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300584 if (!iter) {
585 first_call = true;
586 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
587 if (!iter)
588 return -ENOMEM;
589 cb->args[0] = (long) iter;
590 }
591
592 mutex_lock(&nfc_devlist_mutex);
593
594 cb->seq = nfc_devlist_generation;
595
596 if (first_call) {
597 nfc_device_iter_init(iter);
598 dev = nfc_device_iter_next(iter);
599 }
600
601 while (dev) {
602 int rc;
603
Eric W. Biederman15e47302012-09-07 20:12:54 +0000604 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100605 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606 if (rc < 0)
607 break;
608
609 dev = nfc_device_iter_next(iter);
610 }
611
612 mutex_unlock(&nfc_devlist_mutex);
613
614 cb->args[1] = (long) dev;
615
616 return skb->len;
617}
618
619static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
620{
621 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
622
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300623 nfc_device_iter_exit(iter);
624 kfree(iter);
625
626 return 0;
627}
628
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100629int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100630 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100631{
632 struct sk_buff *msg;
633 void *hdr;
634
635 pr_debug("DEP link is up\n");
636
Thomas Graf58050fc2012-06-28 03:57:45 +0000637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100638 if (!msg)
639 return -ENOMEM;
640
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100641 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100642 if (!hdr)
643 goto free_msg;
644
David S. Miller1e6428d2012-03-29 23:23:57 -0400645 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
646 goto nla_put_failure;
647 if (rf_mode == NFC_RF_INITIATOR &&
648 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
649 goto nla_put_failure;
650 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
651 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
652 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100653
654 genlmsg_end(msg, hdr);
655
656 dev->dep_link_up = true;
657
Johannes Berg2a94fe42013-11-19 15:19:39 +0100658 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100659
660 return 0;
661
662nla_put_failure:
663 genlmsg_cancel(msg, hdr);
664free_msg:
665 nlmsg_free(msg);
666 return -EMSGSIZE;
667}
668
669int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
670{
671 struct sk_buff *msg;
672 void *hdr;
673
674 pr_debug("DEP link is down\n");
675
Thomas Graf58050fc2012-06-28 03:57:45 +0000676 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100677 if (!msg)
678 return -ENOMEM;
679
680 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100681 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100682 if (!hdr)
683 goto free_msg;
684
David S. Miller1e6428d2012-03-29 23:23:57 -0400685 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
686 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100687
688 genlmsg_end(msg, hdr);
689
Johannes Berg2a94fe42013-11-19 15:19:39 +0100690 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100691
692 return 0;
693
694nla_put_failure:
695 genlmsg_cancel(msg, hdr);
696free_msg:
697 nlmsg_free(msg);
698 return -EMSGSIZE;
699}
700
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300701static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
702{
703 struct sk_buff *msg;
704 struct nfc_dev *dev;
705 u32 idx;
706 int rc = -ENOBUFS;
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
Thomas Graf58050fc2012-06-28 03:57:45 +0000717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300718 if (!msg) {
719 rc = -ENOMEM;
720 goto out_putdev;
721 }
722
Eric W. Biederman15e47302012-09-07 20:12:54 +0000723 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100724 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300725 if (rc < 0)
726 goto out_free;
727
728 nfc_put_device(dev);
729
730 return genlmsg_reply(msg, info);
731
732out_free:
733 nlmsg_free(msg);
734out_putdev:
735 nfc_put_device(dev);
736 return rc;
737}
738
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300739static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
740{
741 struct nfc_dev *dev;
742 int rc;
743 u32 idx;
744
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300745 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
746 return -EINVAL;
747
748 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
749
750 dev = nfc_get_device(idx);
751 if (!dev)
752 return -ENODEV;
753
754 rc = nfc_dev_up(dev);
755
756 nfc_put_device(dev);
757 return rc;
758}
759
760static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
761{
762 struct nfc_dev *dev;
763 int rc;
764 u32 idx;
765
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300766 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
767 return -EINVAL;
768
769 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
770
771 dev = nfc_get_device(idx);
772 if (!dev)
773 return -ENODEV;
774
775 rc = nfc_dev_down(dev);
776
777 nfc_put_device(dev);
778 return rc;
779}
780
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
782{
783 struct nfc_dev *dev;
784 int rc;
785 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200786 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300787
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100788 pr_debug("Poll start\n");
789
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300790 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200791 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
792 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200793 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794 return -EINVAL;
795
796 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200797
798 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
799 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200800
801 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
802 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200803 else if (info->attrs[NFC_ATTR_PROTOCOLS])
804 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300805
806 dev = nfc_get_device(idx);
807 if (!dev)
808 return -ENODEV;
809
810 mutex_lock(&dev->genl_data.genl_data_mutex);
811
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200812 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300813 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000814 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300815
816 mutex_unlock(&dev->genl_data.genl_data_mutex);
817
818 nfc_put_device(dev);
819 return rc;
820}
821
822static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
823{
824 struct nfc_dev *dev;
825 int rc;
826 u32 idx;
827
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300828 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
829 return -EINVAL;
830
831 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
832
833 dev = nfc_get_device(idx);
834 if (!dev)
835 return -ENODEV;
836
Samuel Ortiza831b912012-06-28 16:41:57 +0200837 device_lock(&dev->dev);
838
839 if (!dev->polling) {
840 device_unlock(&dev->dev);
841 return -EINVAL;
842 }
843
844 device_unlock(&dev->dev);
845
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300846 mutex_lock(&dev->genl_data.genl_data_mutex);
847
Eric W. Biederman15e47302012-09-07 20:12:54 +0000848 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300849 rc = -EBUSY;
850 goto out;
851 }
852
853 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000854 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300855
856out:
857 mutex_unlock(&dev->genl_data.genl_data_mutex);
858 nfc_put_device(dev);
859 return rc;
860}
861
Christophe Ricard3682f492014-12-02 21:27:50 +0100862static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
863{
864 struct nfc_dev *dev;
865 u32 device_idx, target_idx, protocol;
866 int rc;
867
868 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
869 return -EINVAL;
870
871 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
872
873 dev = nfc_get_device(device_idx);
874 if (!dev)
875 return -ENODEV;
876
877 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
878 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
879
880 nfc_deactivate_target(dev, target_idx);
881 rc = nfc_activate_target(dev, target_idx, protocol);
882
883 nfc_put_device(dev);
884 return 0;
885}
886
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100887static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
888{
889 struct nfc_dev *dev;
890 int rc, tgt_idx;
891 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100892 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100893
894 pr_debug("DEP link up\n");
895
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100897 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100898 return -EINVAL;
899
900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
901 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
902 tgt_idx = NFC_TARGET_IDX_ANY;
903 else
904 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
905
906 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100907
908 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
909 return -EINVAL;
910
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100911 dev = nfc_get_device(idx);
912 if (!dev)
913 return -ENODEV;
914
Samuel Ortiz47807d32012-03-05 01:03:50 +0100915 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100916
917 nfc_put_device(dev);
918
919 return rc;
920}
921
922static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
923{
924 struct nfc_dev *dev;
925 int rc;
926 u32 idx;
927
928 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
929 return -EINVAL;
930
931 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
932
933 dev = nfc_get_device(idx);
934 if (!dev)
935 return -ENODEV;
936
937 rc = nfc_dep_link_down(dev);
938
939 nfc_put_device(dev);
940 return rc;
941}
942
Thierry Escande52feb442012-10-17 14:43:39 +0200943static int nfc_genl_send_params(struct sk_buff *msg,
944 struct nfc_llcp_local *local,
945 u32 portid, u32 seq)
946{
947 void *hdr;
948
949 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
950 NFC_CMD_LLC_GET_PARAMS);
951 if (!hdr)
952 return -EMSGSIZE;
953
954 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
955 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
956 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
957 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
958 goto nla_put_failure;
959
Johannes Berg053c0952015-01-16 22:09:00 +0100960 genlmsg_end(msg, hdr);
961 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +0200962
963nla_put_failure:
964
965 genlmsg_cancel(msg, hdr);
966 return -EMSGSIZE;
967}
968
969static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
970{
971 struct nfc_dev *dev;
972 struct nfc_llcp_local *local;
973 int rc = 0;
974 struct sk_buff *msg = NULL;
975 u32 idx;
976
977 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
978 return -EINVAL;
979
980 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
981
982 dev = nfc_get_device(idx);
983 if (!dev)
984 return -ENODEV;
985
986 device_lock(&dev->dev);
987
988 local = nfc_llcp_find_local(dev);
989 if (!local) {
990 rc = -ENODEV;
991 goto exit;
992 }
993
994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
995 if (!msg) {
996 rc = -ENOMEM;
997 goto exit;
998 }
999
1000 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1001
1002exit:
1003 device_unlock(&dev->dev);
1004
1005 nfc_put_device(dev);
1006
1007 if (rc < 0) {
1008 if (msg)
1009 nlmsg_free(msg);
1010
1011 return rc;
1012 }
1013
1014 return genlmsg_reply(msg, info);
1015}
1016
1017static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1018{
1019 struct nfc_dev *dev;
1020 struct nfc_llcp_local *local;
1021 u8 rw = 0;
1022 u16 miux = 0;
1023 u32 idx;
1024 int rc = 0;
1025
1026 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1027 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1028 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1029 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1030 return -EINVAL;
1031
1032 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1033 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1034
1035 if (rw > LLCP_MAX_RW)
1036 return -EINVAL;
1037 }
1038
1039 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1040 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1041
1042 if (miux > LLCP_MAX_MIUX)
1043 return -EINVAL;
1044 }
1045
1046 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1047
1048 dev = nfc_get_device(idx);
1049 if (!dev)
1050 return -ENODEV;
1051
1052 device_lock(&dev->dev);
1053
1054 local = nfc_llcp_find_local(dev);
1055 if (!local) {
1056 nfc_put_device(dev);
1057 rc = -ENODEV;
1058 goto exit;
1059 }
1060
1061 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1062 if (dev->dep_link_up) {
1063 rc = -EINPROGRESS;
1064 goto exit;
1065 }
1066
1067 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1068 }
1069
1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1071 local->rw = rw;
1072
1073 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1074 local->miux = cpu_to_be16(miux);
1075
1076exit:
1077 device_unlock(&dev->dev);
1078
1079 nfc_put_device(dev);
1080
1081 return rc;
1082}
1083
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001084static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1085{
1086 struct nfc_dev *dev;
1087 struct nfc_llcp_local *local;
1088 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1089 u32 idx;
1090 u8 tid;
1091 char *uri;
1092 int rc = 0, rem;
1093 size_t uri_len, tlvs_len;
1094 struct hlist_head sdreq_list;
1095 struct nfc_llcp_sdp_tlv *sdreq;
1096
1097 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1098 !info->attrs[NFC_ATTR_LLC_SDP])
1099 return -EINVAL;
1100
1101 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1102
1103 dev = nfc_get_device(idx);
1104 if (!dev) {
1105 rc = -ENODEV;
1106 goto exit;
1107 }
1108
1109 device_lock(&dev->dev);
1110
1111 if (dev->dep_link_up == false) {
1112 rc = -ENOLINK;
1113 goto exit;
1114 }
1115
1116 local = nfc_llcp_find_local(dev);
1117 if (!local) {
1118 nfc_put_device(dev);
1119 rc = -ENODEV;
1120 goto exit;
1121 }
1122
1123 INIT_HLIST_HEAD(&sdreq_list);
1124
1125 tlvs_len = 0;
1126
1127 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1128 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1129 nfc_sdp_genl_policy);
1130
1131 if (rc != 0) {
1132 rc = -EINVAL;
1133 goto exit;
1134 }
1135
1136 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1137 continue;
1138
1139 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1140 if (uri_len == 0)
1141 continue;
1142
1143 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1144 if (uri == NULL || *uri == 0)
1145 continue;
1146
1147 tid = local->sdreq_next_tid++;
1148
1149 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1150 if (sdreq == NULL) {
1151 rc = -ENOMEM;
1152 goto exit;
1153 }
1154
1155 tlvs_len += sdreq->tlv_len;
1156
1157 hlist_add_head(&sdreq->node, &sdreq_list);
1158 }
1159
1160 if (hlist_empty(&sdreq_list)) {
1161 rc = -EINVAL;
1162 goto exit;
1163 }
1164
1165 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1166exit:
1167 device_unlock(&dev->dev);
1168
1169 nfc_put_device(dev);
1170
1171 return rc;
1172}
1173
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001174static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001175{
1176 struct nfc_dev *dev;
1177 int rc;
1178 u32 idx;
1179 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1180
1181 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1182 return -EINVAL;
1183
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1185
1186 dev = nfc_get_device(idx);
1187 if (!dev)
1188 return -ENODEV;
1189
1190 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1191 sizeof(firmware_name));
1192
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001193 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001194
1195 nfc_put_device(dev);
1196 return rc;
1197}
1198
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001199int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1200 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001201{
1202 struct sk_buff *msg;
1203 void *hdr;
1204
1205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1206 if (!msg)
1207 return -ENOMEM;
1208
1209 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001210 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001211 if (!hdr)
1212 goto free_msg;
1213
1214 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001215 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001216 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1217 goto nla_put_failure;
1218
1219 genlmsg_end(msg, hdr);
1220
Johannes Berg2a94fe42013-11-19 15:19:39 +01001221 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001222
1223 return 0;
1224
1225nla_put_failure:
1226 genlmsg_cancel(msg, hdr);
1227free_msg:
1228 nlmsg_free(msg);
1229 return -EMSGSIZE;
1230}
1231
Samuel Ortizbe085652013-05-10 17:07:32 +02001232static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1233{
1234 struct nfc_dev *dev;
1235 int rc;
1236 u32 idx, se_idx;
1237
1238 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1239 !info->attrs[NFC_ATTR_SE_INDEX])
1240 return -EINVAL;
1241
1242 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1243 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1244
1245 dev = nfc_get_device(idx);
1246 if (!dev)
1247 return -ENODEV;
1248
1249 rc = nfc_enable_se(dev, se_idx);
1250
1251 nfc_put_device(dev);
1252 return rc;
1253}
1254
1255static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1256{
1257 struct nfc_dev *dev;
1258 int rc;
1259 u32 idx, se_idx;
1260
1261 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1262 !info->attrs[NFC_ATTR_SE_INDEX])
1263 return -EINVAL;
1264
1265 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1266 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1267
1268 dev = nfc_get_device(idx);
1269 if (!dev)
1270 return -ENODEV;
1271
1272 rc = nfc_disable_se(dev, se_idx);
1273
1274 nfc_put_device(dev);
1275 return rc;
1276}
1277
Samuel Ortizac22ac42013-07-24 18:10:50 +02001278static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1279 u32 portid, u32 seq,
1280 struct netlink_callback *cb,
1281 int flags)
1282{
1283 void *hdr;
1284 struct nfc_se *se, *n;
1285
1286 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1287 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1288 NFC_CMD_GET_SE);
1289 if (!hdr)
1290 goto nla_put_failure;
1291
1292 if (cb)
1293 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1294
1295 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1296 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1297 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1298 goto nla_put_failure;
1299
Johannes Berg053c0952015-01-16 22:09:00 +01001300 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001301 }
1302
1303 return 0;
1304
1305nla_put_failure:
1306 genlmsg_cancel(msg, hdr);
1307 return -EMSGSIZE;
1308}
1309
1310static int nfc_genl_dump_ses(struct sk_buff *skb,
1311 struct netlink_callback *cb)
1312{
1313 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1314 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1315 bool first_call = false;
1316
1317 if (!iter) {
1318 first_call = true;
1319 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1320 if (!iter)
1321 return -ENOMEM;
1322 cb->args[0] = (long) iter;
1323 }
1324
1325 mutex_lock(&nfc_devlist_mutex);
1326
1327 cb->seq = nfc_devlist_generation;
1328
1329 if (first_call) {
1330 nfc_device_iter_init(iter);
1331 dev = nfc_device_iter_next(iter);
1332 }
1333
1334 while (dev) {
1335 int rc;
1336
1337 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1338 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1339 if (rc < 0)
1340 break;
1341
1342 dev = nfc_device_iter_next(iter);
1343 }
1344
1345 mutex_unlock(&nfc_devlist_mutex);
1346
1347 cb->args[1] = (long) dev;
1348
1349 return skb->len;
1350}
1351
1352static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1353{
1354 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1355
1356 nfc_device_iter_exit(iter);
1357 kfree(iter);
1358
1359 return 0;
1360}
1361
Christophe Ricardcd96db62014-12-02 21:27:51 +01001362static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1363 u8 *apdu, size_t apdu_length,
1364 se_io_cb_t cb, void *cb_context)
1365{
1366 struct nfc_se *se;
1367 int rc;
1368
1369 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1370
1371 device_lock(&dev->dev);
1372
1373 if (!device_is_registered(&dev->dev)) {
1374 rc = -ENODEV;
1375 goto error;
1376 }
1377
1378 if (!dev->dev_up) {
1379 rc = -ENODEV;
1380 goto error;
1381 }
1382
1383 if (!dev->ops->se_io) {
1384 rc = -EOPNOTSUPP;
1385 goto error;
1386 }
1387
1388 se = nfc_find_se(dev, se_idx);
1389 if (!se) {
1390 rc = -EINVAL;
1391 goto error;
1392 }
1393
1394 if (se->state != NFC_SE_ENABLED) {
1395 rc = -ENODEV;
1396 goto error;
1397 }
1398
1399 rc = dev->ops->se_io(dev, se_idx, apdu,
1400 apdu_length, cb, cb_context);
1401
1402error:
1403 device_unlock(&dev->dev);
1404 return rc;
1405}
1406
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001407struct se_io_ctx {
1408 u32 dev_idx;
1409 u32 se_idx;
1410};
1411
Samuel Ortizddc1a702013-10-07 14:18:44 +02001412static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001413{
1414 struct se_io_ctx *ctx = context;
1415 struct sk_buff *msg;
1416 void *hdr;
1417
1418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1419 if (!msg) {
1420 kfree(ctx);
1421 return;
1422 }
1423
1424 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1425 NFC_CMD_SE_IO);
1426 if (!hdr)
1427 goto free_msg;
1428
1429 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1430 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1431 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1432 goto nla_put_failure;
1433
1434 genlmsg_end(msg, hdr);
1435
Johannes Berg2a94fe42013-11-19 15:19:39 +01001436 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001437
1438 kfree(ctx);
1439
1440 return;
1441
1442nla_put_failure:
1443 genlmsg_cancel(msg, hdr);
1444free_msg:
1445 nlmsg_free(msg);
1446 kfree(ctx);
1447
1448 return;
1449}
1450
1451static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1452{
1453 struct nfc_dev *dev;
1454 struct se_io_ctx *ctx;
1455 u32 dev_idx, se_idx;
1456 u8 *apdu;
1457 size_t apdu_len;
1458
1459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1460 !info->attrs[NFC_ATTR_SE_INDEX] ||
1461 !info->attrs[NFC_ATTR_SE_APDU])
1462 return -EINVAL;
1463
1464 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1465 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1466
1467 dev = nfc_get_device(dev_idx);
1468 if (!dev)
1469 return -ENODEV;
1470
1471 if (!dev->ops || !dev->ops->se_io)
1472 return -ENOTSUPP;
1473
1474 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1475 if (apdu_len == 0)
1476 return -EINVAL;
1477
1478 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1479 if (!apdu)
1480 return -EINVAL;
1481
1482 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1483 if (!ctx)
1484 return -ENOMEM;
1485
1486 ctx->dev_idx = dev_idx;
1487 ctx->se_idx = se_idx;
1488
Christophe Ricardcd96db62014-12-02 21:27:51 +01001489 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001490}
1491
Johannes Berg4534de82013-11-14 17:14:46 +01001492static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001493 {
1494 .cmd = NFC_CMD_GET_DEVICE,
1495 .doit = nfc_genl_get_device,
1496 .dumpit = nfc_genl_dump_devices,
1497 .done = nfc_genl_dump_devices_done,
1498 .policy = nfc_genl_policy,
1499 },
1500 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001501 .cmd = NFC_CMD_DEV_UP,
1502 .doit = nfc_genl_dev_up,
1503 .policy = nfc_genl_policy,
1504 },
1505 {
1506 .cmd = NFC_CMD_DEV_DOWN,
1507 .doit = nfc_genl_dev_down,
1508 .policy = nfc_genl_policy,
1509 },
1510 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001511 .cmd = NFC_CMD_START_POLL,
1512 .doit = nfc_genl_start_poll,
1513 .policy = nfc_genl_policy,
1514 },
1515 {
1516 .cmd = NFC_CMD_STOP_POLL,
1517 .doit = nfc_genl_stop_poll,
1518 .policy = nfc_genl_policy,
1519 },
1520 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001521 .cmd = NFC_CMD_DEP_LINK_UP,
1522 .doit = nfc_genl_dep_link_up,
1523 .policy = nfc_genl_policy,
1524 },
1525 {
1526 .cmd = NFC_CMD_DEP_LINK_DOWN,
1527 .doit = nfc_genl_dep_link_down,
1528 .policy = nfc_genl_policy,
1529 },
1530 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001531 .cmd = NFC_CMD_GET_TARGET,
1532 .dumpit = nfc_genl_dump_targets,
1533 .done = nfc_genl_dump_targets_done,
1534 .policy = nfc_genl_policy,
1535 },
Thierry Escande52feb442012-10-17 14:43:39 +02001536 {
1537 .cmd = NFC_CMD_LLC_GET_PARAMS,
1538 .doit = nfc_genl_llc_get_params,
1539 .policy = nfc_genl_policy,
1540 },
1541 {
1542 .cmd = NFC_CMD_LLC_SET_PARAMS,
1543 .doit = nfc_genl_llc_set_params,
1544 .policy = nfc_genl_policy,
1545 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001546 {
1547 .cmd = NFC_CMD_LLC_SDREQ,
1548 .doit = nfc_genl_llc_sdreq,
1549 .policy = nfc_genl_policy,
1550 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001551 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001552 .cmd = NFC_CMD_FW_DOWNLOAD,
1553 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001554 .policy = nfc_genl_policy,
1555 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001556 {
1557 .cmd = NFC_CMD_ENABLE_SE,
1558 .doit = nfc_genl_enable_se,
1559 .policy = nfc_genl_policy,
1560 },
1561 {
1562 .cmd = NFC_CMD_DISABLE_SE,
1563 .doit = nfc_genl_disable_se,
1564 .policy = nfc_genl_policy,
1565 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001566 {
1567 .cmd = NFC_CMD_GET_SE,
1568 .dumpit = nfc_genl_dump_ses,
1569 .done = nfc_genl_dump_ses_done,
1570 .policy = nfc_genl_policy,
1571 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001572 {
1573 .cmd = NFC_CMD_SE_IO,
1574 .doit = nfc_genl_se_io,
1575 .policy = nfc_genl_policy,
1576 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001577 {
1578 .cmd = NFC_CMD_ACTIVATE_TARGET,
1579 .doit = nfc_genl_activate_target,
1580 .policy = nfc_genl_policy,
1581 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001582};
1583
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001584
1585struct urelease_work {
1586 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001587 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001588};
1589
1590static void nfc_urelease_event_work(struct work_struct *work)
1591{
1592 struct urelease_work *w = container_of(work, struct urelease_work, w);
1593 struct class_dev_iter iter;
1594 struct nfc_dev *dev;
1595
John W. Linvillec4876062012-09-28 11:11:16 -04001596 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001597
1598 mutex_lock(&nfc_devlist_mutex);
1599
1600 nfc_device_iter_init(&iter);
1601 dev = nfc_device_iter_next(&iter);
1602
1603 while (dev) {
1604 mutex_lock(&dev->genl_data.genl_data_mutex);
1605
John W. Linvillec4876062012-09-28 11:11:16 -04001606 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001607 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001608 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001609 }
1610
1611 mutex_unlock(&dev->genl_data.genl_data_mutex);
1612
1613 dev = nfc_device_iter_next(&iter);
1614 }
1615
1616 nfc_device_iter_exit(&iter);
1617
1618 mutex_unlock(&nfc_devlist_mutex);
1619
1620 kfree(w);
1621}
1622
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001623static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001624 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001625{
1626 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001627 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001628
1629 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1630 goto out;
1631
Eric W. Biederman15e47302012-09-07 20:12:54 +00001632 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001633
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001634 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1635 if (w) {
1636 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001637 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001638 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001639 }
1640
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001641out:
1642 return NOTIFY_DONE;
1643}
1644
1645void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1646{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001647 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001648 mutex_init(&genl_data->genl_data_mutex);
1649}
1650
1651void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1652{
1653 mutex_destroy(&genl_data->genl_data_mutex);
1654}
1655
1656static struct notifier_block nl_notifier = {
1657 .notifier_call = nfc_genl_rcv_nl_event,
1658};
1659
1660/**
1661 * nfc_genl_init() - Initialize netlink interface
1662 *
1663 * This initialization function registers the nfc netlink family.
1664 */
1665int __init nfc_genl_init(void)
1666{
1667 int rc;
1668
Johannes Berg2a94fe42013-11-19 15:19:39 +01001669 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1670 nfc_genl_ops,
1671 nfc_genl_mcgrps);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001672 if (rc)
1673 return rc;
1674
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001675 netlink_register_notifier(&nl_notifier);
1676
Johannes Berg2a94fe42013-11-19 15:19:39 +01001677 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001678}
1679
1680/**
1681 * nfc_genl_exit() - Deinitialize netlink interface
1682 *
1683 * This exit function unregisters the nfc netlink family.
1684 */
1685void nfc_genl_exit(void)
1686{
1687 netlink_unregister_notifier(&nl_notifier);
1688 genl_unregister_family(&nfc_genl_family);
1689}