blob: 4c95319dc22b6fe4ec999da8248f028318cea628 [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 *
Samuel Ortiz9e580952014-10-14 02:19:46 +02008 * Vendor commands implementation based on net/wireless/nl80211.c
9 * which is:
10 *
11 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
12 * Copyright 2013-2014 Intel Mobile Communications GmbH
13 *
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030014 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080025 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030026 */
27
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080029
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030030#include <net/genetlink.h>
31#include <linux/nfc.h>
32#include <linux/slab.h>
33
34#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020035#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020036
Johannes Berg2a94fe42013-11-19 15:19:39 +010037static const struct genl_multicast_group nfc_genl_mcgrps[] = {
38 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030039};
40
Johannes Berg489111e2016-10-24 14:40:03 +020041static struct genl_family nfc_genl_family;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030042static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
43 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
44 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
45 .len = NFC_DEVICE_NAME_MAXSIZE },
46 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010047 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
48 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010049 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020050 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
51 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010052 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
53 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010055 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020056 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
57 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020058 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Christophe Ricard29e76922015-08-19 21:26:43 +020059 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
60
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010061};
62
63static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
64 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
65 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030066};
67
68static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010069 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030070{
71 void *hdr;
72
Eric W. Biederman15e47302012-09-07 20:12:54 +000073 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010074 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030075 if (!hdr)
76 return -EMSGSIZE;
77
78 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
79
David S. Miller1e6428d2012-03-29 23:23:57 -040080 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
81 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
82 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
83 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
84 goto nla_put_failure;
85 if (target->nfcid1_len > 0 &&
86 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
87 target->nfcid1))
88 goto nla_put_failure;
89 if (target->sensb_res_len > 0 &&
90 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
91 target->sensb_res))
92 goto nla_put_failure;
93 if (target->sensf_res_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
95 target->sensf_res))
96 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030097
Mark A. Greerf5f68722014-01-14 17:52:11 -070098 if (target->is_iso15693) {
99 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
100 target->iso15693_dsfid) ||
101 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
102 sizeof(target->iso15693_uid), target->iso15693_uid))
103 goto nla_put_failure;
104 }
105
Johannes Berg053c0952015-01-16 22:09:00 +0100106 genlmsg_end(msg, hdr);
107 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300108
109nla_put_failure:
110 genlmsg_cancel(msg, hdr);
111 return -EMSGSIZE;
112}
113
114static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
115{
Johannes Bergc90c39d2016-10-24 14:40:01 +0200116 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300117 struct nfc_dev *dev;
118 int rc;
119 u32 idx;
120
121 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Johannes Bergc90c39d2016-10-24 14:40:01 +0200122 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300123 if (rc < 0)
124 return ERR_PTR(rc);
125
Johannes Bergc90c39d2016-10-24 14:40:01 +0200126 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300127 return ERR_PTR(-EINVAL);
128
Johannes Bergc90c39d2016-10-24 14:40:01 +0200129 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300130
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
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900306static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
307{
308 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
309 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
310 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
311 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
312 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
313 return -1;
314 return 0;
315}
316
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300317int nfc_genl_device_added(struct nfc_dev *dev)
318{
319 struct sk_buff *msg;
320 void *hdr;
321
Thomas Graf58050fc2012-06-28 03:57:45 +0000322 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300323 if (!msg)
324 return -ENOMEM;
325
326 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100327 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300328 if (!hdr)
329 goto free_msg;
330
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900331 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400332 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300333
334 genlmsg_end(msg, hdr);
335
Johannes Berg2a94fe42013-11-19 15:19:39 +0100336 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300337
338 return 0;
339
340nla_put_failure:
341 genlmsg_cancel(msg, hdr);
342free_msg:
343 nlmsg_free(msg);
344 return -EMSGSIZE;
345}
346
347int nfc_genl_device_removed(struct nfc_dev *dev)
348{
349 struct sk_buff *msg;
350 void *hdr;
351
Thomas Graf58050fc2012-06-28 03:57:45 +0000352 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300353 if (!msg)
354 return -ENOMEM;
355
356 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100357 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300358 if (!hdr)
359 goto free_msg;
360
David S. Miller1e6428d2012-03-29 23:23:57 -0400361 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
362 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300363
364 genlmsg_end(msg, hdr);
365
Johannes Berg2a94fe42013-11-19 15:19:39 +0100366 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300367
368 return 0;
369
370nla_put_failure:
371 genlmsg_cancel(msg, hdr);
372free_msg:
373 nlmsg_free(msg);
374 return -EMSGSIZE;
375}
376
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100377int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
378{
379 struct sk_buff *msg;
380 struct nlattr *sdp_attr, *uri_attr;
381 struct nfc_llcp_sdp_tlv *sdres;
382 struct hlist_node *n;
383 void *hdr;
384 int rc = -EMSGSIZE;
385 int i;
386
387 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
388 if (!msg)
389 return -ENOMEM;
390
391 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
392 NFC_EVENT_LLC_SDRES);
393 if (!hdr)
394 goto free_msg;
395
396 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
397 goto nla_put_failure;
398
399 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
400 if (sdp_attr == NULL) {
401 rc = -ENOMEM;
402 goto nla_put_failure;
403 }
404
405 i = 1;
406 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
407 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
408
409 uri_attr = nla_nest_start(msg, i++);
410 if (uri_attr == NULL) {
411 rc = -ENOMEM;
412 goto nla_put_failure;
413 }
414
415 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
416 goto nla_put_failure;
417
418 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
419 goto nla_put_failure;
420
421 nla_nest_end(msg, uri_attr);
422
423 hlist_del(&sdres->node);
424
425 nfc_llcp_free_sdp_tlv(sdres);
426 }
427
428 nla_nest_end(msg, sdp_attr);
429
430 genlmsg_end(msg, hdr);
431
Johannes Berg2a94fe42013-11-19 15:19:39 +0100432 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100433
434nla_put_failure:
435 genlmsg_cancel(msg, hdr);
436
437free_msg:
438 nlmsg_free(msg);
439
440 nfc_llcp_free_sdp_tlv_list(sdres_list);
441
442 return rc;
443}
444
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200445int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
446{
447 struct sk_buff *msg;
448 void *hdr;
449
450 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
451 if (!msg)
452 return -ENOMEM;
453
454 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
455 NFC_EVENT_SE_ADDED);
456 if (!hdr)
457 goto free_msg;
458
459 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
460 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
461 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
462 goto nla_put_failure;
463
464 genlmsg_end(msg, hdr);
465
Johannes Berg2a94fe42013-11-19 15:19:39 +0100466 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200467
468 return 0;
469
470nla_put_failure:
471 genlmsg_cancel(msg, hdr);
472free_msg:
473 nlmsg_free(msg);
474 return -EMSGSIZE;
475}
476
477int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
478{
479 struct sk_buff *msg;
480 void *hdr;
481
482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
483 if (!msg)
484 return -ENOMEM;
485
486 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
487 NFC_EVENT_SE_REMOVED);
488 if (!hdr)
489 goto free_msg;
490
491 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
492 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
493 goto nla_put_failure;
494
495 genlmsg_end(msg, hdr);
496
Johannes Berg2a94fe42013-11-19 15:19:39 +0100497 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200498
499 return 0;
500
501nla_put_failure:
502 genlmsg_cancel(msg, hdr);
503free_msg:
504 nlmsg_free(msg);
505 return -EMSGSIZE;
506}
507
Christophe Ricard447b27c2015-02-01 22:26:16 +0100508int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
509 struct nfc_evt_transaction *evt_transaction)
510{
511 struct nfc_se *se;
512 struct sk_buff *msg;
513 void *hdr;
514
515 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
516 if (!msg)
517 return -ENOMEM;
518
519 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
520 NFC_EVENT_SE_TRANSACTION);
521 if (!hdr)
522 goto free_msg;
523
524 se = nfc_find_se(dev, se_idx);
525 if (!se)
526 goto free_msg;
527
528 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
529 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
530 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
531 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
532 evt_transaction->aid) ||
533 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
534 evt_transaction->params))
535 goto nla_put_failure;
536
537 /* evt_transaction is no more used */
538 devm_kfree(&dev->dev, evt_transaction);
539
540 genlmsg_end(msg, hdr);
541
542 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
543
544 return 0;
545
546nla_put_failure:
547 genlmsg_cancel(msg, hdr);
548free_msg:
549 /* evt_transaction is no more used */
550 devm_kfree(&dev->dev, evt_transaction);
551 nlmsg_free(msg);
552 return -EMSGSIZE;
553}
554
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100555int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
556{
557 struct nfc_se *se;
558 struct sk_buff *msg;
559 void *hdr;
560
561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
562 if (!msg)
563 return -ENOMEM;
564
565 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
566 NFC_EVENT_SE_CONNECTIVITY);
567 if (!hdr)
568 goto free_msg;
569
570 se = nfc_find_se(dev, se_idx);
571 if (!se)
572 goto free_msg;
573
574 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
575 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
576 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
577 goto nla_put_failure;
578
579 genlmsg_end(msg, hdr);
580
581 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
582
583 return 0;
584
585nla_put_failure:
586 genlmsg_cancel(msg, hdr);
587free_msg:
588 nlmsg_free(msg);
589 return -EMSGSIZE;
590}
591
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300592static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000593 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100594 struct netlink_callback *cb,
595 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300596{
597 void *hdr;
598
Eric W. Biederman15e47302012-09-07 20:12:54 +0000599 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100600 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300601 if (!hdr)
602 return -EMSGSIZE;
603
604 if (cb)
605 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
606
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900607 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400608 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300609
Johannes Berg053c0952015-01-16 22:09:00 +0100610 genlmsg_end(msg, hdr);
611 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300612
613nla_put_failure:
614 genlmsg_cancel(msg, hdr);
615 return -EMSGSIZE;
616}
617
618static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100619 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300620{
621 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
622 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
623 bool first_call = false;
624
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300625 if (!iter) {
626 first_call = true;
627 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
628 if (!iter)
629 return -ENOMEM;
630 cb->args[0] = (long) iter;
631 }
632
633 mutex_lock(&nfc_devlist_mutex);
634
635 cb->seq = nfc_devlist_generation;
636
637 if (first_call) {
638 nfc_device_iter_init(iter);
639 dev = nfc_device_iter_next(iter);
640 }
641
642 while (dev) {
643 int rc;
644
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100646 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300647 if (rc < 0)
648 break;
649
650 dev = nfc_device_iter_next(iter);
651 }
652
653 mutex_unlock(&nfc_devlist_mutex);
654
655 cb->args[1] = (long) dev;
656
657 return skb->len;
658}
659
660static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
661{
662 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
663
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300664 nfc_device_iter_exit(iter);
665 kfree(iter);
666
667 return 0;
668}
669
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100670int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100671 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100672{
673 struct sk_buff *msg;
674 void *hdr;
675
676 pr_debug("DEP link is up\n");
677
Thomas Graf58050fc2012-06-28 03:57:45 +0000678 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100679 if (!msg)
680 return -ENOMEM;
681
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100682 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100683 if (!hdr)
684 goto free_msg;
685
David S. Miller1e6428d2012-03-29 23:23:57 -0400686 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
687 goto nla_put_failure;
688 if (rf_mode == NFC_RF_INITIATOR &&
689 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
690 goto nla_put_failure;
691 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
692 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
693 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100694
695 genlmsg_end(msg, hdr);
696
697 dev->dep_link_up = true;
698
Johannes Berg2a94fe42013-11-19 15:19:39 +0100699 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100700
701 return 0;
702
703nla_put_failure:
704 genlmsg_cancel(msg, hdr);
705free_msg:
706 nlmsg_free(msg);
707 return -EMSGSIZE;
708}
709
710int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
711{
712 struct sk_buff *msg;
713 void *hdr;
714
715 pr_debug("DEP link is down\n");
716
Thomas Graf58050fc2012-06-28 03:57:45 +0000717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100718 if (!msg)
719 return -ENOMEM;
720
721 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100722 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100723 if (!hdr)
724 goto free_msg;
725
David S. Miller1e6428d2012-03-29 23:23:57 -0400726 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
727 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100728
729 genlmsg_end(msg, hdr);
730
Johannes Berg2a94fe42013-11-19 15:19:39 +0100731 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100732
733 return 0;
734
735nla_put_failure:
736 genlmsg_cancel(msg, hdr);
737free_msg:
738 nlmsg_free(msg);
739 return -EMSGSIZE;
740}
741
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300742static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
743{
744 struct sk_buff *msg;
745 struct nfc_dev *dev;
746 u32 idx;
747 int rc = -ENOBUFS;
748
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300749 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
750 return -EINVAL;
751
752 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
753
754 dev = nfc_get_device(idx);
755 if (!dev)
756 return -ENODEV;
757
Thomas Graf58050fc2012-06-28 03:57:45 +0000758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300759 if (!msg) {
760 rc = -ENOMEM;
761 goto out_putdev;
762 }
763
Eric W. Biederman15e47302012-09-07 20:12:54 +0000764 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100765 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300766 if (rc < 0)
767 goto out_free;
768
769 nfc_put_device(dev);
770
771 return genlmsg_reply(msg, info);
772
773out_free:
774 nlmsg_free(msg);
775out_putdev:
776 nfc_put_device(dev);
777 return rc;
778}
779
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300780static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
781{
782 struct nfc_dev *dev;
783 int rc;
784 u32 idx;
785
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300786 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
787 return -EINVAL;
788
789 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
790
791 dev = nfc_get_device(idx);
792 if (!dev)
793 return -ENODEV;
794
795 rc = nfc_dev_up(dev);
796
797 nfc_put_device(dev);
798 return rc;
799}
800
801static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
802{
803 struct nfc_dev *dev;
804 int rc;
805 u32 idx;
806
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300807 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
808 return -EINVAL;
809
810 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
811
812 dev = nfc_get_device(idx);
813 if (!dev)
814 return -ENODEV;
815
816 rc = nfc_dev_down(dev);
817
818 nfc_put_device(dev);
819 return rc;
820}
821
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300822static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
823{
824 struct nfc_dev *dev;
825 int rc;
826 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200827 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300828
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100829 pr_debug("Poll start\n");
830
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300831 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200832 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
833 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200834 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300835 return -EINVAL;
836
837 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200838
839 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
840 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200841
842 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
843 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200844 else if (info->attrs[NFC_ATTR_PROTOCOLS])
845 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300846
847 dev = nfc_get_device(idx);
848 if (!dev)
849 return -ENODEV;
850
851 mutex_lock(&dev->genl_data.genl_data_mutex);
852
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200853 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300854 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000855 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300856
857 mutex_unlock(&dev->genl_data.genl_data_mutex);
858
859 nfc_put_device(dev);
860 return rc;
861}
862
863static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
864{
865 struct nfc_dev *dev;
866 int rc;
867 u32 idx;
868
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300869 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
870 return -EINVAL;
871
872 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
873
874 dev = nfc_get_device(idx);
875 if (!dev)
876 return -ENODEV;
877
Samuel Ortiza831b912012-06-28 16:41:57 +0200878 device_lock(&dev->dev);
879
880 if (!dev->polling) {
881 device_unlock(&dev->dev);
882 return -EINVAL;
883 }
884
885 device_unlock(&dev->dev);
886
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300887 mutex_lock(&dev->genl_data.genl_data_mutex);
888
Eric W. Biederman15e47302012-09-07 20:12:54 +0000889 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300890 rc = -EBUSY;
891 goto out;
892 }
893
894 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300896
897out:
898 mutex_unlock(&dev->genl_data.genl_data_mutex);
899 nfc_put_device(dev);
900 return rc;
901}
902
Christophe Ricard3682f492014-12-02 21:27:50 +0100903static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
904{
905 struct nfc_dev *dev;
906 u32 device_idx, target_idx, protocol;
907 int rc;
908
909 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
910 return -EINVAL;
911
912 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
913
914 dev = nfc_get_device(device_idx);
915 if (!dev)
916 return -ENODEV;
917
918 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
919 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
920
Christophe Ricard96d45812015-10-25 22:54:43 +0100921 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
Christophe Ricard3682f492014-12-02 21:27:50 +0100922 rc = nfc_activate_target(dev, target_idx, protocol);
923
924 nfc_put_device(dev);
925 return 0;
926}
927
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100928static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
929{
930 struct nfc_dev *dev;
931 int rc, tgt_idx;
932 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100933 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100934
935 pr_debug("DEP link up\n");
936
937 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100938 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100939 return -EINVAL;
940
941 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
942 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
943 tgt_idx = NFC_TARGET_IDX_ANY;
944 else
945 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
946
947 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100948
949 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
950 return -EINVAL;
951
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100952 dev = nfc_get_device(idx);
953 if (!dev)
954 return -ENODEV;
955
Samuel Ortiz47807d32012-03-05 01:03:50 +0100956 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100957
958 nfc_put_device(dev);
959
960 return rc;
961}
962
963static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
964{
965 struct nfc_dev *dev;
966 int rc;
967 u32 idx;
968
969 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
970 return -EINVAL;
971
972 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
973
974 dev = nfc_get_device(idx);
975 if (!dev)
976 return -ENODEV;
977
978 rc = nfc_dep_link_down(dev);
979
980 nfc_put_device(dev);
981 return rc;
982}
983
Thierry Escande52feb442012-10-17 14:43:39 +0200984static int nfc_genl_send_params(struct sk_buff *msg,
985 struct nfc_llcp_local *local,
986 u32 portid, u32 seq)
987{
988 void *hdr;
989
990 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
991 NFC_CMD_LLC_GET_PARAMS);
992 if (!hdr)
993 return -EMSGSIZE;
994
995 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
996 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
997 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
998 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
999 goto nla_put_failure;
1000
Johannes Berg053c0952015-01-16 22:09:00 +01001001 genlmsg_end(msg, hdr);
1002 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +02001003
1004nla_put_failure:
1005
1006 genlmsg_cancel(msg, hdr);
1007 return -EMSGSIZE;
1008}
1009
1010static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1011{
1012 struct nfc_dev *dev;
1013 struct nfc_llcp_local *local;
1014 int rc = 0;
1015 struct sk_buff *msg = NULL;
1016 u32 idx;
1017
1018 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1019 return -EINVAL;
1020
1021 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1022
1023 dev = nfc_get_device(idx);
1024 if (!dev)
1025 return -ENODEV;
1026
1027 device_lock(&dev->dev);
1028
1029 local = nfc_llcp_find_local(dev);
1030 if (!local) {
1031 rc = -ENODEV;
1032 goto exit;
1033 }
1034
1035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1036 if (!msg) {
1037 rc = -ENOMEM;
1038 goto exit;
1039 }
1040
1041 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1042
1043exit:
1044 device_unlock(&dev->dev);
1045
1046 nfc_put_device(dev);
1047
1048 if (rc < 0) {
1049 if (msg)
1050 nlmsg_free(msg);
1051
1052 return rc;
1053 }
1054
1055 return genlmsg_reply(msg, info);
1056}
1057
1058static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1059{
1060 struct nfc_dev *dev;
1061 struct nfc_llcp_local *local;
1062 u8 rw = 0;
1063 u16 miux = 0;
1064 u32 idx;
1065 int rc = 0;
1066
1067 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1068 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1069 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1070 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1071 return -EINVAL;
1072
1073 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1074 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1075
1076 if (rw > LLCP_MAX_RW)
1077 return -EINVAL;
1078 }
1079
1080 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1081 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1082
1083 if (miux > LLCP_MAX_MIUX)
1084 return -EINVAL;
1085 }
1086
1087 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1088
1089 dev = nfc_get_device(idx);
1090 if (!dev)
1091 return -ENODEV;
1092
1093 device_lock(&dev->dev);
1094
1095 local = nfc_llcp_find_local(dev);
1096 if (!local) {
1097 nfc_put_device(dev);
1098 rc = -ENODEV;
1099 goto exit;
1100 }
1101
1102 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1103 if (dev->dep_link_up) {
1104 rc = -EINPROGRESS;
1105 goto exit;
1106 }
1107
1108 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1109 }
1110
1111 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1112 local->rw = rw;
1113
1114 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1115 local->miux = cpu_to_be16(miux);
1116
1117exit:
1118 device_unlock(&dev->dev);
1119
1120 nfc_put_device(dev);
1121
1122 return rc;
1123}
1124
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001125static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1126{
1127 struct nfc_dev *dev;
1128 struct nfc_llcp_local *local;
1129 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1130 u32 idx;
1131 u8 tid;
1132 char *uri;
1133 int rc = 0, rem;
1134 size_t uri_len, tlvs_len;
1135 struct hlist_head sdreq_list;
1136 struct nfc_llcp_sdp_tlv *sdreq;
1137
1138 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1139 !info->attrs[NFC_ATTR_LLC_SDP])
1140 return -EINVAL;
1141
1142 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1143
1144 dev = nfc_get_device(idx);
Julia Lawall9abebb82015-10-17 11:32:19 +02001145 if (!dev)
1146 return -ENODEV;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001147
1148 device_lock(&dev->dev);
1149
1150 if (dev->dep_link_up == false) {
1151 rc = -ENOLINK;
1152 goto exit;
1153 }
1154
1155 local = nfc_llcp_find_local(dev);
1156 if (!local) {
1157 nfc_put_device(dev);
1158 rc = -ENODEV;
1159 goto exit;
1160 }
1161
1162 INIT_HLIST_HEAD(&sdreq_list);
1163
1164 tlvs_len = 0;
1165
1166 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1167 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1168 nfc_sdp_genl_policy);
1169
1170 if (rc != 0) {
1171 rc = -EINVAL;
1172 goto exit;
1173 }
1174
1175 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1176 continue;
1177
1178 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1179 if (uri_len == 0)
1180 continue;
1181
1182 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1183 if (uri == NULL || *uri == 0)
1184 continue;
1185
1186 tid = local->sdreq_next_tid++;
1187
1188 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1189 if (sdreq == NULL) {
1190 rc = -ENOMEM;
1191 goto exit;
1192 }
1193
1194 tlvs_len += sdreq->tlv_len;
1195
1196 hlist_add_head(&sdreq->node, &sdreq_list);
1197 }
1198
1199 if (hlist_empty(&sdreq_list)) {
1200 rc = -EINVAL;
1201 goto exit;
1202 }
1203
1204 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1205exit:
1206 device_unlock(&dev->dev);
1207
1208 nfc_put_device(dev);
1209
1210 return rc;
1211}
1212
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001213static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001214{
1215 struct nfc_dev *dev;
1216 int rc;
1217 u32 idx;
1218 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1219
1220 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1221 return -EINVAL;
1222
1223 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1224
1225 dev = nfc_get_device(idx);
1226 if (!dev)
1227 return -ENODEV;
1228
1229 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1230 sizeof(firmware_name));
1231
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001232 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001233
1234 nfc_put_device(dev);
1235 return rc;
1236}
1237
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001238int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1239 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001240{
1241 struct sk_buff *msg;
1242 void *hdr;
1243
1244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1245 if (!msg)
1246 return -ENOMEM;
1247
1248 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001249 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001250 if (!hdr)
1251 goto free_msg;
1252
1253 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001254 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001255 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1256 goto nla_put_failure;
1257
1258 genlmsg_end(msg, hdr);
1259
Johannes Berg2a94fe42013-11-19 15:19:39 +01001260 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001261
1262 return 0;
1263
1264nla_put_failure:
1265 genlmsg_cancel(msg, hdr);
1266free_msg:
1267 nlmsg_free(msg);
1268 return -EMSGSIZE;
1269}
1270
Samuel Ortizbe085652013-05-10 17:07:32 +02001271static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1272{
1273 struct nfc_dev *dev;
1274 int rc;
1275 u32 idx, se_idx;
1276
1277 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1278 !info->attrs[NFC_ATTR_SE_INDEX])
1279 return -EINVAL;
1280
1281 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1282 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1283
1284 dev = nfc_get_device(idx);
1285 if (!dev)
1286 return -ENODEV;
1287
1288 rc = nfc_enable_se(dev, se_idx);
1289
1290 nfc_put_device(dev);
1291 return rc;
1292}
1293
1294static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1295{
1296 struct nfc_dev *dev;
1297 int rc;
1298 u32 idx, se_idx;
1299
1300 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1301 !info->attrs[NFC_ATTR_SE_INDEX])
1302 return -EINVAL;
1303
1304 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1305 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1306
1307 dev = nfc_get_device(idx);
1308 if (!dev)
1309 return -ENODEV;
1310
1311 rc = nfc_disable_se(dev, se_idx);
1312
1313 nfc_put_device(dev);
1314 return rc;
1315}
1316
Samuel Ortizac22ac42013-07-24 18:10:50 +02001317static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1318 u32 portid, u32 seq,
1319 struct netlink_callback *cb,
1320 int flags)
1321{
1322 void *hdr;
1323 struct nfc_se *se, *n;
1324
1325 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1326 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1327 NFC_CMD_GET_SE);
1328 if (!hdr)
1329 goto nla_put_failure;
1330
1331 if (cb)
1332 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1333
1334 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1335 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1336 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1337 goto nla_put_failure;
1338
Johannes Berg053c0952015-01-16 22:09:00 +01001339 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001340 }
1341
1342 return 0;
1343
1344nla_put_failure:
1345 genlmsg_cancel(msg, hdr);
1346 return -EMSGSIZE;
1347}
1348
1349static int nfc_genl_dump_ses(struct sk_buff *skb,
1350 struct netlink_callback *cb)
1351{
1352 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1353 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1354 bool first_call = false;
1355
1356 if (!iter) {
1357 first_call = true;
1358 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1359 if (!iter)
1360 return -ENOMEM;
1361 cb->args[0] = (long) iter;
1362 }
1363
1364 mutex_lock(&nfc_devlist_mutex);
1365
1366 cb->seq = nfc_devlist_generation;
1367
1368 if (first_call) {
1369 nfc_device_iter_init(iter);
1370 dev = nfc_device_iter_next(iter);
1371 }
1372
1373 while (dev) {
1374 int rc;
1375
1376 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1377 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1378 if (rc < 0)
1379 break;
1380
1381 dev = nfc_device_iter_next(iter);
1382 }
1383
1384 mutex_unlock(&nfc_devlist_mutex);
1385
1386 cb->args[1] = (long) dev;
1387
1388 return skb->len;
1389}
1390
1391static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1392{
1393 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1394
1395 nfc_device_iter_exit(iter);
1396 kfree(iter);
1397
1398 return 0;
1399}
1400
Christophe Ricardcd96db62014-12-02 21:27:51 +01001401static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1402 u8 *apdu, size_t apdu_length,
1403 se_io_cb_t cb, void *cb_context)
1404{
1405 struct nfc_se *se;
1406 int rc;
1407
1408 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1409
1410 device_lock(&dev->dev);
1411
1412 if (!device_is_registered(&dev->dev)) {
1413 rc = -ENODEV;
1414 goto error;
1415 }
1416
1417 if (!dev->dev_up) {
1418 rc = -ENODEV;
1419 goto error;
1420 }
1421
1422 if (!dev->ops->se_io) {
1423 rc = -EOPNOTSUPP;
1424 goto error;
1425 }
1426
1427 se = nfc_find_se(dev, se_idx);
1428 if (!se) {
1429 rc = -EINVAL;
1430 goto error;
1431 }
1432
1433 if (se->state != NFC_SE_ENABLED) {
1434 rc = -ENODEV;
1435 goto error;
1436 }
1437
1438 rc = dev->ops->se_io(dev, se_idx, apdu,
1439 apdu_length, cb, cb_context);
1440
1441error:
1442 device_unlock(&dev->dev);
1443 return rc;
1444}
1445
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001446struct se_io_ctx {
1447 u32 dev_idx;
1448 u32 se_idx;
1449};
1450
Samuel Ortizddc1a702013-10-07 14:18:44 +02001451static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001452{
1453 struct se_io_ctx *ctx = context;
1454 struct sk_buff *msg;
1455 void *hdr;
1456
1457 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1458 if (!msg) {
1459 kfree(ctx);
1460 return;
1461 }
1462
1463 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1464 NFC_CMD_SE_IO);
1465 if (!hdr)
1466 goto free_msg;
1467
1468 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1469 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1470 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1471 goto nla_put_failure;
1472
1473 genlmsg_end(msg, hdr);
1474
Johannes Berg2a94fe42013-11-19 15:19:39 +01001475 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001476
1477 kfree(ctx);
1478
1479 return;
1480
1481nla_put_failure:
1482 genlmsg_cancel(msg, hdr);
1483free_msg:
1484 nlmsg_free(msg);
1485 kfree(ctx);
1486
1487 return;
1488}
1489
1490static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1491{
1492 struct nfc_dev *dev;
1493 struct se_io_ctx *ctx;
1494 u32 dev_idx, se_idx;
1495 u8 *apdu;
1496 size_t apdu_len;
1497
1498 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1499 !info->attrs[NFC_ATTR_SE_INDEX] ||
1500 !info->attrs[NFC_ATTR_SE_APDU])
1501 return -EINVAL;
1502
1503 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1504 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1505
1506 dev = nfc_get_device(dev_idx);
1507 if (!dev)
1508 return -ENODEV;
1509
1510 if (!dev->ops || !dev->ops->se_io)
1511 return -ENOTSUPP;
1512
1513 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1514 if (apdu_len == 0)
1515 return -EINVAL;
1516
1517 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1518 if (!apdu)
1519 return -EINVAL;
1520
1521 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1522 if (!ctx)
1523 return -ENOMEM;
1524
1525 ctx->dev_idx = dev_idx;
1526 ctx->se_idx = se_idx;
1527
Christophe Ricardcd96db62014-12-02 21:27:51 +01001528 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001529}
1530
Samuel Ortiz9e580952014-10-14 02:19:46 +02001531static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1532 struct genl_info *info)
1533{
1534 struct nfc_dev *dev;
1535 struct nfc_vendor_cmd *cmd;
1536 u32 dev_idx, vid, subcmd;
1537 u8 *data;
1538 size_t data_len;
Christophe Ricard29e76922015-08-19 21:26:43 +02001539 int i, err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001540
1541 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1542 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1543 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1544 return -EINVAL;
1545
1546 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1547 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1548 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1549
1550 dev = nfc_get_device(dev_idx);
1551 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1552 return -ENODEV;
1553
Christophe Ricardfe202fe2015-08-14 22:33:40 +02001554 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1555 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
Samuel Ortiz9e580952014-10-14 02:19:46 +02001556 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1557 if (data_len == 0)
1558 return -EINVAL;
1559 } else {
Christophe Ricardadca3c32015-08-17 08:33:43 +02001560 data = NULL;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001561 data_len = 0;
1562 }
1563
1564 for (i = 0; i < dev->n_vendor_cmds; i++) {
1565 cmd = &dev->vendor_cmds[i];
1566
1567 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1568 continue;
1569
Christophe Ricard29e76922015-08-19 21:26:43 +02001570 dev->cur_cmd_info = info;
1571 err = cmd->doit(dev, data, data_len);
1572 dev->cur_cmd_info = NULL;
1573 return err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001574 }
1575
1576 return -EOPNOTSUPP;
1577}
1578
Christophe Ricard29e76922015-08-19 21:26:43 +02001579/* message building helper */
1580static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1581 int flags, u8 cmd)
1582{
1583 /* since there is no private header just add the generic one */
1584 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1585}
1586
1587static struct sk_buff *
1588__nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1589 u32 portid, u32 seq,
1590 enum nfc_attrs attr,
1591 u32 oui, u32 subcmd, gfp_t gfp)
1592{
1593 struct sk_buff *skb;
1594 void *hdr;
1595
1596 skb = nlmsg_new(approxlen + 100, gfp);
1597 if (!skb)
1598 return NULL;
1599
1600 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1601 if (!hdr) {
1602 kfree_skb(skb);
1603 return NULL;
1604 }
1605
1606 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1607 goto nla_put_failure;
1608 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1609 goto nla_put_failure;
1610 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1611 goto nla_put_failure;
1612
1613 ((void **)skb->cb)[0] = dev;
1614 ((void **)skb->cb)[1] = hdr;
1615
1616 return skb;
1617
1618nla_put_failure:
1619 kfree_skb(skb);
1620 return NULL;
1621}
1622
1623struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1624 enum nfc_attrs attr,
1625 u32 oui, u32 subcmd,
1626 int approxlen)
1627{
1628 if (WARN_ON(!dev->cur_cmd_info))
1629 return NULL;
1630
1631 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1632 dev->cur_cmd_info->snd_portid,
1633 dev->cur_cmd_info->snd_seq, attr,
1634 oui, subcmd, GFP_KERNEL);
1635}
1636EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1637
1638int nfc_vendor_cmd_reply(struct sk_buff *skb)
1639{
1640 struct nfc_dev *dev = ((void **)skb->cb)[0];
1641 void *hdr = ((void **)skb->cb)[1];
1642
1643 /* clear CB data for netlink core to own from now on */
1644 memset(skb->cb, 0, sizeof(skb->cb));
1645
1646 if (WARN_ON(!dev->cur_cmd_info)) {
1647 kfree_skb(skb);
1648 return -EINVAL;
1649 }
1650
1651 genlmsg_end(skb, hdr);
1652 return genlmsg_reply(skb, dev->cur_cmd_info);
1653}
1654EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1655
Johannes Berg4534de82013-11-14 17:14:46 +01001656static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001657 {
1658 .cmd = NFC_CMD_GET_DEVICE,
1659 .doit = nfc_genl_get_device,
1660 .dumpit = nfc_genl_dump_devices,
1661 .done = nfc_genl_dump_devices_done,
1662 .policy = nfc_genl_policy,
1663 },
1664 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001665 .cmd = NFC_CMD_DEV_UP,
1666 .doit = nfc_genl_dev_up,
1667 .policy = nfc_genl_policy,
1668 },
1669 {
1670 .cmd = NFC_CMD_DEV_DOWN,
1671 .doit = nfc_genl_dev_down,
1672 .policy = nfc_genl_policy,
1673 },
1674 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001675 .cmd = NFC_CMD_START_POLL,
1676 .doit = nfc_genl_start_poll,
1677 .policy = nfc_genl_policy,
1678 },
1679 {
1680 .cmd = NFC_CMD_STOP_POLL,
1681 .doit = nfc_genl_stop_poll,
1682 .policy = nfc_genl_policy,
1683 },
1684 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001685 .cmd = NFC_CMD_DEP_LINK_UP,
1686 .doit = nfc_genl_dep_link_up,
1687 .policy = nfc_genl_policy,
1688 },
1689 {
1690 .cmd = NFC_CMD_DEP_LINK_DOWN,
1691 .doit = nfc_genl_dep_link_down,
1692 .policy = nfc_genl_policy,
1693 },
1694 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001695 .cmd = NFC_CMD_GET_TARGET,
1696 .dumpit = nfc_genl_dump_targets,
1697 .done = nfc_genl_dump_targets_done,
1698 .policy = nfc_genl_policy,
1699 },
Thierry Escande52feb442012-10-17 14:43:39 +02001700 {
1701 .cmd = NFC_CMD_LLC_GET_PARAMS,
1702 .doit = nfc_genl_llc_get_params,
1703 .policy = nfc_genl_policy,
1704 },
1705 {
1706 .cmd = NFC_CMD_LLC_SET_PARAMS,
1707 .doit = nfc_genl_llc_set_params,
1708 .policy = nfc_genl_policy,
1709 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001710 {
1711 .cmd = NFC_CMD_LLC_SDREQ,
1712 .doit = nfc_genl_llc_sdreq,
1713 .policy = nfc_genl_policy,
1714 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001715 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001716 .cmd = NFC_CMD_FW_DOWNLOAD,
1717 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001718 .policy = nfc_genl_policy,
1719 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001720 {
1721 .cmd = NFC_CMD_ENABLE_SE,
1722 .doit = nfc_genl_enable_se,
1723 .policy = nfc_genl_policy,
1724 },
1725 {
1726 .cmd = NFC_CMD_DISABLE_SE,
1727 .doit = nfc_genl_disable_se,
1728 .policy = nfc_genl_policy,
1729 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001730 {
1731 .cmd = NFC_CMD_GET_SE,
1732 .dumpit = nfc_genl_dump_ses,
1733 .done = nfc_genl_dump_ses_done,
1734 .policy = nfc_genl_policy,
1735 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001736 {
1737 .cmd = NFC_CMD_SE_IO,
1738 .doit = nfc_genl_se_io,
1739 .policy = nfc_genl_policy,
1740 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001741 {
1742 .cmd = NFC_CMD_ACTIVATE_TARGET,
1743 .doit = nfc_genl_activate_target,
1744 .policy = nfc_genl_policy,
1745 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001746 {
1747 .cmd = NFC_CMD_VENDOR,
1748 .doit = nfc_genl_vendor_cmd,
1749 .policy = nfc_genl_policy,
1750 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001751};
1752
Johannes Berg56989f62016-10-24 14:40:05 +02001753static struct genl_family nfc_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001754 .hdrsize = 0,
1755 .name = NFC_GENL_NAME,
1756 .version = NFC_GENL_VERSION,
1757 .maxattr = NFC_ATTR_MAX,
1758 .module = THIS_MODULE,
1759 .ops = nfc_genl_ops,
1760 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1761 .mcgrps = nfc_genl_mcgrps,
1762 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1763};
1764
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001765
1766struct urelease_work {
1767 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001768 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001769};
1770
1771static void nfc_urelease_event_work(struct work_struct *work)
1772{
1773 struct urelease_work *w = container_of(work, struct urelease_work, w);
1774 struct class_dev_iter iter;
1775 struct nfc_dev *dev;
1776
John W. Linvillec4876062012-09-28 11:11:16 -04001777 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001778
1779 mutex_lock(&nfc_devlist_mutex);
1780
1781 nfc_device_iter_init(&iter);
1782 dev = nfc_device_iter_next(&iter);
1783
1784 while (dev) {
1785 mutex_lock(&dev->genl_data.genl_data_mutex);
1786
John W. Linvillec4876062012-09-28 11:11:16 -04001787 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001788 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001789 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001790 }
1791
1792 mutex_unlock(&dev->genl_data.genl_data_mutex);
1793
1794 dev = nfc_device_iter_next(&iter);
1795 }
1796
1797 nfc_device_iter_exit(&iter);
1798
1799 mutex_unlock(&nfc_devlist_mutex);
1800
1801 kfree(w);
1802}
1803
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001804static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001805 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001806{
1807 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001808 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001809
1810 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1811 goto out;
1812
Eric W. Biederman15e47302012-09-07 20:12:54 +00001813 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001814
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001815 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1816 if (w) {
1817 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001818 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001819 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001820 }
1821
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001822out:
1823 return NOTIFY_DONE;
1824}
1825
1826void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1827{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001828 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001829 mutex_init(&genl_data->genl_data_mutex);
1830}
1831
1832void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1833{
1834 mutex_destroy(&genl_data->genl_data_mutex);
1835}
1836
1837static struct notifier_block nl_notifier = {
1838 .notifier_call = nfc_genl_rcv_nl_event,
1839};
1840
1841/**
1842 * nfc_genl_init() - Initialize netlink interface
1843 *
1844 * This initialization function registers the nfc netlink family.
1845 */
1846int __init nfc_genl_init(void)
1847{
1848 int rc;
1849
Johannes Berg489111e2016-10-24 14:40:03 +02001850 rc = genl_register_family(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001851 if (rc)
1852 return rc;
1853
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001854 netlink_register_notifier(&nl_notifier);
1855
Johannes Berg2a94fe42013-11-19 15:19:39 +01001856 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001857}
1858
1859/**
1860 * nfc_genl_exit() - Deinitialize netlink interface
1861 *
1862 * This exit function unregisters the nfc netlink family.
1863 */
1864void nfc_genl_exit(void)
1865{
1866 netlink_unregister_notifier(&nl_notifier);
1867 genl_unregister_family(&nfc_genl_family);
1868}