blob: dbf74afe82fb6ea08564971b646df81a1730ccb1 [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
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020041static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030042 .id = GENL_ID_GENERATE,
43 .hdrsize = 0,
44 .name = NFC_GENL_NAME,
45 .version = NFC_GENL_VERSION,
46 .maxattr = NFC_ATTR_MAX,
47};
48
49static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
50 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
51 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
52 .len = NFC_DEVICE_NAME_MAXSIZE },
53 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010054 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
55 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010056 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020057 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
58 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010059 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
60 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
61 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010062 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020063 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
64 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020065 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Christophe Ricard29e76922015-08-19 21:26:43 +020066 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
67
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010068};
69
70static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
Kees Cook69b28c12018-02-14 15:45:07 -080071 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
72 .len = U8_MAX - 4 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010073 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030074};
75
76static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010077 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030078{
79 void *hdr;
80
Eric W. Biederman15e47302012-09-07 20:12:54 +000081 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010082 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030083 if (!hdr)
84 return -EMSGSIZE;
85
86 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
87
David S. Miller1e6428d2012-03-29 23:23:57 -040088 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
89 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
90 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
91 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
92 goto nla_put_failure;
93 if (target->nfcid1_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
95 target->nfcid1))
96 goto nla_put_failure;
97 if (target->sensb_res_len > 0 &&
98 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
99 target->sensb_res))
100 goto nla_put_failure;
101 if (target->sensf_res_len > 0 &&
102 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
103 target->sensf_res))
104 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300105
Mark A. Greerf5f68722014-01-14 17:52:11 -0700106 if (target->is_iso15693) {
107 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
108 target->iso15693_dsfid) ||
109 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
110 sizeof(target->iso15693_uid), target->iso15693_uid))
111 goto nla_put_failure;
112 }
113
Johannes Berg053c0952015-01-16 22:09:00 +0100114 genlmsg_end(msg, hdr);
115 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300116
117nla_put_failure:
118 genlmsg_cancel(msg, hdr);
119 return -EMSGSIZE;
120}
121
122static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
123{
124 struct nfc_dev *dev;
125 int rc;
126 u32 idx;
127
128 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100129 nfc_genl_family.attrbuf,
130 nfc_genl_family.maxattr,
131 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300132 if (rc < 0)
133 return ERR_PTR(rc);
134
135 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
136 return ERR_PTR(-EINVAL);
137
138 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
139
140 dev = nfc_get_device(idx);
141 if (!dev)
142 return ERR_PTR(-ENODEV);
143
144 return dev;
145}
146
147static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100148 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300149{
150 int i = cb->args[0];
151 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
152 int rc;
153
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300154 if (!dev) {
155 dev = __get_device_from_cb(cb);
156 if (IS_ERR(dev))
157 return PTR_ERR(dev);
158
159 cb->args[1] = (long) dev;
160 }
161
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200162 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300163
164 cb->seq = dev->targets_generation;
165
166 while (i < dev->n_targets) {
167 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100168 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300169 if (rc < 0)
170 break;
171
172 i++;
173 }
174
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200175 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300176
177 cb->args[0] = i;
178
179 return skb->len;
180}
181
182static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
183{
184 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
185
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300186 if (dev)
187 nfc_put_device(dev);
188
189 return 0;
190}
191
192int nfc_genl_targets_found(struct nfc_dev *dev)
193{
194 struct sk_buff *msg;
195 void *hdr;
196
Eric W. Biederman15e47302012-09-07 20:12:54 +0000197 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300198
Thomas Graf58050fc2012-06-28 03:57:45 +0000199 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300200 if (!msg)
201 return -ENOMEM;
202
203 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100204 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300205 if (!hdr)
206 goto free_msg;
207
David S. Miller1e6428d2012-03-29 23:23:57 -0400208 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
209 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300210
211 genlmsg_end(msg, hdr);
212
Johannes Berg2a94fe42013-11-19 15:19:39 +0100213 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300214
215nla_put_failure:
216 genlmsg_cancel(msg, hdr);
217free_msg:
218 nlmsg_free(msg);
219 return -EMSGSIZE;
220}
221
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200222int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
223{
224 struct sk_buff *msg;
225 void *hdr;
226
Thomas Graf58050fc2012-06-28 03:57:45 +0000227 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200228 if (!msg)
229 return -ENOMEM;
230
231 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
232 NFC_EVENT_TARGET_LOST);
233 if (!hdr)
234 goto free_msg;
235
John W. Linville59ef43e2012-04-18 14:17:13 -0400236 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
237 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
238 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200239
240 genlmsg_end(msg, hdr);
241
Johannes Berg2a94fe42013-11-19 15:19:39 +0100242 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200243
244 return 0;
245
246nla_put_failure:
247 genlmsg_cancel(msg, hdr);
248free_msg:
249 nlmsg_free(msg);
250 return -EMSGSIZE;
251}
252
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200253int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
254{
255 struct sk_buff *msg;
256 void *hdr;
257
Thomas Graf58050fc2012-06-28 03:57:45 +0000258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200259 if (!msg)
260 return -ENOMEM;
261
262 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
263 NFC_EVENT_TM_ACTIVATED);
264 if (!hdr)
265 goto free_msg;
266
267 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
268 goto nla_put_failure;
269 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
270 goto nla_put_failure;
271
272 genlmsg_end(msg, hdr);
273
Johannes Berg2a94fe42013-11-19 15:19:39 +0100274 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200275
276 return 0;
277
278nla_put_failure:
279 genlmsg_cancel(msg, hdr);
280free_msg:
281 nlmsg_free(msg);
282 return -EMSGSIZE;
283}
284
285int nfc_genl_tm_deactivated(struct nfc_dev *dev)
286{
287 struct sk_buff *msg;
288 void *hdr;
289
Thomas Graf58050fc2012-06-28 03:57:45 +0000290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200291 if (!msg)
292 return -ENOMEM;
293
294 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
295 NFC_EVENT_TM_DEACTIVATED);
296 if (!hdr)
297 goto free_msg;
298
299 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
300 goto nla_put_failure;
301
302 genlmsg_end(msg, hdr);
303
Johannes Berg2a94fe42013-11-19 15:19:39 +0100304 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200305
306 return 0;
307
308nla_put_failure:
309 genlmsg_cancel(msg, hdr);
310free_msg:
311 nlmsg_free(msg);
312 return -EMSGSIZE;
313}
314
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300315int nfc_genl_device_added(struct nfc_dev *dev)
316{
317 struct sk_buff *msg;
318 void *hdr;
319
Thomas Graf58050fc2012-06-28 03:57:45 +0000320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300321 if (!msg)
322 return -ENOMEM;
323
324 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100325 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300326 if (!hdr)
327 goto free_msg;
328
David S. Miller1e6428d2012-03-29 23:23:57 -0400329 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
330 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
331 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
332 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
333 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300334
335 genlmsg_end(msg, hdr);
336
Johannes Berg2a94fe42013-11-19 15:19:39 +0100337 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300338
339 return 0;
340
341nla_put_failure:
342 genlmsg_cancel(msg, hdr);
343free_msg:
344 nlmsg_free(msg);
345 return -EMSGSIZE;
346}
347
348int nfc_genl_device_removed(struct nfc_dev *dev)
349{
350 struct sk_buff *msg;
351 void *hdr;
352
Thomas Graf58050fc2012-06-28 03:57:45 +0000353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300354 if (!msg)
355 return -ENOMEM;
356
357 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100358 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300359 if (!hdr)
360 goto free_msg;
361
David S. Miller1e6428d2012-03-29 23:23:57 -0400362 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
363 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300364
365 genlmsg_end(msg, hdr);
366
Johannes Berg2a94fe42013-11-19 15:19:39 +0100367 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300368
369 return 0;
370
371nla_put_failure:
372 genlmsg_cancel(msg, hdr);
373free_msg:
374 nlmsg_free(msg);
375 return -EMSGSIZE;
376}
377
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100378int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
379{
380 struct sk_buff *msg;
381 struct nlattr *sdp_attr, *uri_attr;
382 struct nfc_llcp_sdp_tlv *sdres;
383 struct hlist_node *n;
384 void *hdr;
385 int rc = -EMSGSIZE;
386 int i;
387
388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
389 if (!msg)
390 return -ENOMEM;
391
392 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
393 NFC_EVENT_LLC_SDRES);
394 if (!hdr)
395 goto free_msg;
396
397 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
398 goto nla_put_failure;
399
400 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
401 if (sdp_attr == NULL) {
402 rc = -ENOMEM;
403 goto nla_put_failure;
404 }
405
406 i = 1;
407 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
408 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
409
410 uri_attr = nla_nest_start(msg, i++);
411 if (uri_attr == NULL) {
412 rc = -ENOMEM;
413 goto nla_put_failure;
414 }
415
416 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
417 goto nla_put_failure;
418
419 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
420 goto nla_put_failure;
421
422 nla_nest_end(msg, uri_attr);
423
424 hlist_del(&sdres->node);
425
426 nfc_llcp_free_sdp_tlv(sdres);
427 }
428
429 nla_nest_end(msg, sdp_attr);
430
431 genlmsg_end(msg, hdr);
432
Johannes Berg2a94fe42013-11-19 15:19:39 +0100433 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100434
435nla_put_failure:
436 genlmsg_cancel(msg, hdr);
437
438free_msg:
439 nlmsg_free(msg);
440
441 nfc_llcp_free_sdp_tlv_list(sdres_list);
442
443 return rc;
444}
445
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200446int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
447{
448 struct sk_buff *msg;
449 void *hdr;
450
451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
452 if (!msg)
453 return -ENOMEM;
454
455 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
456 NFC_EVENT_SE_ADDED);
457 if (!hdr)
458 goto free_msg;
459
460 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
461 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
462 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
463 goto nla_put_failure;
464
465 genlmsg_end(msg, hdr);
466
Johannes Berg2a94fe42013-11-19 15:19:39 +0100467 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200468
469 return 0;
470
471nla_put_failure:
472 genlmsg_cancel(msg, hdr);
473free_msg:
474 nlmsg_free(msg);
475 return -EMSGSIZE;
476}
477
478int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
479{
480 struct sk_buff *msg;
481 void *hdr;
482
483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
484 if (!msg)
485 return -ENOMEM;
486
487 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
488 NFC_EVENT_SE_REMOVED);
489 if (!hdr)
490 goto free_msg;
491
492 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
493 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
494 goto nla_put_failure;
495
496 genlmsg_end(msg, hdr);
497
Johannes Berg2a94fe42013-11-19 15:19:39 +0100498 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200499
500 return 0;
501
502nla_put_failure:
503 genlmsg_cancel(msg, hdr);
504free_msg:
505 nlmsg_free(msg);
506 return -EMSGSIZE;
507}
508
Christophe Ricard447b27c2015-02-01 22:26:16 +0100509int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
510 struct nfc_evt_transaction *evt_transaction)
511{
512 struct nfc_se *se;
513 struct sk_buff *msg;
514 void *hdr;
515
516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
517 if (!msg)
518 return -ENOMEM;
519
520 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
521 NFC_EVENT_SE_TRANSACTION);
522 if (!hdr)
523 goto free_msg;
524
525 se = nfc_find_se(dev, se_idx);
526 if (!se)
527 goto free_msg;
528
529 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
530 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
531 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
532 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
533 evt_transaction->aid) ||
534 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
535 evt_transaction->params))
536 goto nla_put_failure;
537
538 /* evt_transaction is no more used */
539 devm_kfree(&dev->dev, evt_transaction);
540
541 genlmsg_end(msg, hdr);
542
543 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
544
545 return 0;
546
547nla_put_failure:
548 genlmsg_cancel(msg, hdr);
549free_msg:
550 /* evt_transaction is no more used */
551 devm_kfree(&dev->dev, evt_transaction);
552 nlmsg_free(msg);
553 return -EMSGSIZE;
554}
555
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100556int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
557{
558 struct nfc_se *se;
559 struct sk_buff *msg;
560 void *hdr;
561
562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
563 if (!msg)
564 return -ENOMEM;
565
566 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
567 NFC_EVENT_SE_CONNECTIVITY);
568 if (!hdr)
569 goto free_msg;
570
571 se = nfc_find_se(dev, se_idx);
572 if (!se)
573 goto free_msg;
574
575 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
576 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
577 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
578 goto nla_put_failure;
579
580 genlmsg_end(msg, hdr);
581
582 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
583
584 return 0;
585
586nla_put_failure:
587 genlmsg_cancel(msg, hdr);
588free_msg:
589 nlmsg_free(msg);
590 return -EMSGSIZE;
591}
592
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300593static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000594 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100595 struct netlink_callback *cb,
596 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300597{
598 void *hdr;
599
Eric W. Biederman15e47302012-09-07 20:12:54 +0000600 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100601 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300602 if (!hdr)
603 return -EMSGSIZE;
604
605 if (cb)
606 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
607
David S. Miller1e6428d2012-03-29 23:23:57 -0400608 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
609 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
610 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200611 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
612 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400613 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300614
Johannes Berg053c0952015-01-16 22:09:00 +0100615 genlmsg_end(msg, hdr);
616 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300617
618nla_put_failure:
619 genlmsg_cancel(msg, hdr);
620 return -EMSGSIZE;
621}
622
623static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100624 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300625{
626 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
627 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
628 bool first_call = false;
629
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300630 if (!iter) {
631 first_call = true;
632 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
633 if (!iter)
634 return -ENOMEM;
635 cb->args[0] = (long) iter;
636 }
637
638 mutex_lock(&nfc_devlist_mutex);
639
640 cb->seq = nfc_devlist_generation;
641
642 if (first_call) {
643 nfc_device_iter_init(iter);
644 dev = nfc_device_iter_next(iter);
645 }
646
647 while (dev) {
648 int rc;
649
Eric W. Biederman15e47302012-09-07 20:12:54 +0000650 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100651 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300652 if (rc < 0)
653 break;
654
655 dev = nfc_device_iter_next(iter);
656 }
657
658 mutex_unlock(&nfc_devlist_mutex);
659
660 cb->args[1] = (long) dev;
661
662 return skb->len;
663}
664
665static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
666{
667 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
668
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300669 nfc_device_iter_exit(iter);
670 kfree(iter);
671
672 return 0;
673}
674
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100675int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100676 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100677{
678 struct sk_buff *msg;
679 void *hdr;
680
681 pr_debug("DEP link is up\n");
682
Thomas Graf58050fc2012-06-28 03:57:45 +0000683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100684 if (!msg)
685 return -ENOMEM;
686
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100687 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100688 if (!hdr)
689 goto free_msg;
690
David S. Miller1e6428d2012-03-29 23:23:57 -0400691 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
692 goto nla_put_failure;
693 if (rf_mode == NFC_RF_INITIATOR &&
694 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
695 goto nla_put_failure;
696 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
697 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
698 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100699
700 genlmsg_end(msg, hdr);
701
702 dev->dep_link_up = true;
703
Johannes Berg2a94fe42013-11-19 15:19:39 +0100704 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100705
706 return 0;
707
708nla_put_failure:
709 genlmsg_cancel(msg, hdr);
710free_msg:
711 nlmsg_free(msg);
712 return -EMSGSIZE;
713}
714
715int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
716{
717 struct sk_buff *msg;
718 void *hdr;
719
720 pr_debug("DEP link is down\n");
721
Thomas Graf58050fc2012-06-28 03:57:45 +0000722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100723 if (!msg)
724 return -ENOMEM;
725
726 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100727 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100728 if (!hdr)
729 goto free_msg;
730
David S. Miller1e6428d2012-03-29 23:23:57 -0400731 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
732 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100733
734 genlmsg_end(msg, hdr);
735
Johannes Berg2a94fe42013-11-19 15:19:39 +0100736 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100737
738 return 0;
739
740nla_put_failure:
741 genlmsg_cancel(msg, hdr);
742free_msg:
743 nlmsg_free(msg);
744 return -EMSGSIZE;
745}
746
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300747static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
748{
749 struct sk_buff *msg;
750 struct nfc_dev *dev;
751 u32 idx;
752 int rc = -ENOBUFS;
753
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300754 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
755 return -EINVAL;
756
757 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
758
759 dev = nfc_get_device(idx);
760 if (!dev)
761 return -ENODEV;
762
Thomas Graf58050fc2012-06-28 03:57:45 +0000763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300764 if (!msg) {
765 rc = -ENOMEM;
766 goto out_putdev;
767 }
768
Eric W. Biederman15e47302012-09-07 20:12:54 +0000769 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100770 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300771 if (rc < 0)
772 goto out_free;
773
774 nfc_put_device(dev);
775
776 return genlmsg_reply(msg, info);
777
778out_free:
779 nlmsg_free(msg);
780out_putdev:
781 nfc_put_device(dev);
782 return rc;
783}
784
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300785static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
786{
787 struct nfc_dev *dev;
788 int rc;
789 u32 idx;
790
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300791 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
792 return -EINVAL;
793
794 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
795
796 dev = nfc_get_device(idx);
797 if (!dev)
798 return -ENODEV;
799
800 rc = nfc_dev_up(dev);
801
802 nfc_put_device(dev);
803 return rc;
804}
805
806static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
807{
808 struct nfc_dev *dev;
809 int rc;
810 u32 idx;
811
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300812 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
813 return -EINVAL;
814
815 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
816
817 dev = nfc_get_device(idx);
818 if (!dev)
819 return -ENODEV;
820
821 rc = nfc_dev_down(dev);
822
823 nfc_put_device(dev);
824 return rc;
825}
826
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300827static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
828{
829 struct nfc_dev *dev;
830 int rc;
831 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200832 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300833
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100834 pr_debug("Poll start\n");
835
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300836 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200837 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
838 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200839 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300840 return -EINVAL;
841
842 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200843
844 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
845 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200846
847 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
848 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200849 else if (info->attrs[NFC_ATTR_PROTOCOLS])
850 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300851
852 dev = nfc_get_device(idx);
853 if (!dev)
854 return -ENODEV;
855
856 mutex_lock(&dev->genl_data.genl_data_mutex);
857
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200858 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300859 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000860 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300861
862 mutex_unlock(&dev->genl_data.genl_data_mutex);
863
864 nfc_put_device(dev);
865 return rc;
866}
867
868static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
869{
870 struct nfc_dev *dev;
871 int rc;
872 u32 idx;
873
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300874 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
875 return -EINVAL;
876
877 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
878
879 dev = nfc_get_device(idx);
880 if (!dev)
881 return -ENODEV;
882
Samuel Ortiza831b912012-06-28 16:41:57 +0200883 device_lock(&dev->dev);
884
885 if (!dev->polling) {
886 device_unlock(&dev->dev);
887 return -EINVAL;
888 }
889
890 device_unlock(&dev->dev);
891
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300892 mutex_lock(&dev->genl_data.genl_data_mutex);
893
Eric W. Biederman15e47302012-09-07 20:12:54 +0000894 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300895 rc = -EBUSY;
896 goto out;
897 }
898
899 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000900 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300901
902out:
903 mutex_unlock(&dev->genl_data.genl_data_mutex);
904 nfc_put_device(dev);
905 return rc;
906}
907
Christophe Ricard3682f492014-12-02 21:27:50 +0100908static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
909{
910 struct nfc_dev *dev;
911 u32 device_idx, target_idx, protocol;
912 int rc;
913
Mateusz Jurczykd1ac8a92017-05-24 12:42:26 +0200914 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
915 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
916 !info->attrs[NFC_ATTR_PROTOCOLS])
Christophe Ricard3682f492014-12-02 21:27:50 +0100917 return -EINVAL;
918
919 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
920
921 dev = nfc_get_device(device_idx);
922 if (!dev)
923 return -ENODEV;
924
925 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
926 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
927
Christophe Ricard96d45812015-10-25 22:54:43 +0100928 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
Christophe Ricard3682f492014-12-02 21:27:50 +0100929 rc = nfc_activate_target(dev, target_idx, protocol);
930
931 nfc_put_device(dev);
932 return 0;
933}
934
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100935static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
936{
937 struct nfc_dev *dev;
938 int rc, tgt_idx;
939 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100940 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100941
942 pr_debug("DEP link up\n");
943
944 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100945 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100946 return -EINVAL;
947
948 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
949 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
950 tgt_idx = NFC_TARGET_IDX_ANY;
951 else
952 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
953
954 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100955
956 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
957 return -EINVAL;
958
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100959 dev = nfc_get_device(idx);
960 if (!dev)
961 return -ENODEV;
962
Samuel Ortiz47807d32012-03-05 01:03:50 +0100963 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100964
965 nfc_put_device(dev);
966
967 return rc;
968}
969
970static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
971{
972 struct nfc_dev *dev;
973 int rc;
974 u32 idx;
975
976 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
977 return -EINVAL;
978
979 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
980
981 dev = nfc_get_device(idx);
982 if (!dev)
983 return -ENODEV;
984
985 rc = nfc_dep_link_down(dev);
986
987 nfc_put_device(dev);
988 return rc;
989}
990
Thierry Escande52feb442012-10-17 14:43:39 +0200991static int nfc_genl_send_params(struct sk_buff *msg,
992 struct nfc_llcp_local *local,
993 u32 portid, u32 seq)
994{
995 void *hdr;
996
997 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
998 NFC_CMD_LLC_GET_PARAMS);
999 if (!hdr)
1000 return -EMSGSIZE;
1001
1002 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1003 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1004 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1005 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1006 goto nla_put_failure;
1007
Johannes Berg053c0952015-01-16 22:09:00 +01001008 genlmsg_end(msg, hdr);
1009 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +02001010
1011nla_put_failure:
1012
1013 genlmsg_cancel(msg, hdr);
1014 return -EMSGSIZE;
1015}
1016
1017static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1018{
1019 struct nfc_dev *dev;
1020 struct nfc_llcp_local *local;
1021 int rc = 0;
1022 struct sk_buff *msg = NULL;
1023 u32 idx;
1024
1025 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1026 return -EINVAL;
1027
1028 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1029
1030 dev = nfc_get_device(idx);
1031 if (!dev)
1032 return -ENODEV;
1033
1034 device_lock(&dev->dev);
1035
1036 local = nfc_llcp_find_local(dev);
1037 if (!local) {
1038 rc = -ENODEV;
1039 goto exit;
1040 }
1041
1042 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1043 if (!msg) {
1044 rc = -ENOMEM;
1045 goto exit;
1046 }
1047
1048 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1049
1050exit:
1051 device_unlock(&dev->dev);
1052
1053 nfc_put_device(dev);
1054
1055 if (rc < 0) {
1056 if (msg)
1057 nlmsg_free(msg);
1058
1059 return rc;
1060 }
1061
1062 return genlmsg_reply(msg, info);
1063}
1064
1065static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1066{
1067 struct nfc_dev *dev;
1068 struct nfc_llcp_local *local;
1069 u8 rw = 0;
1070 u16 miux = 0;
1071 u32 idx;
1072 int rc = 0;
1073
1074 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1075 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1076 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1077 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1078 return -EINVAL;
1079
1080 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1081 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1082
1083 if (rw > LLCP_MAX_RW)
1084 return -EINVAL;
1085 }
1086
1087 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1088 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1089
1090 if (miux > LLCP_MAX_MIUX)
1091 return -EINVAL;
1092 }
1093
1094 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1095
1096 dev = nfc_get_device(idx);
1097 if (!dev)
1098 return -ENODEV;
1099
1100 device_lock(&dev->dev);
1101
1102 local = nfc_llcp_find_local(dev);
1103 if (!local) {
1104 nfc_put_device(dev);
1105 rc = -ENODEV;
1106 goto exit;
1107 }
1108
1109 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1110 if (dev->dep_link_up) {
1111 rc = -EINPROGRESS;
1112 goto exit;
1113 }
1114
1115 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1116 }
1117
1118 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1119 local->rw = rw;
1120
1121 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1122 local->miux = cpu_to_be16(miux);
1123
1124exit:
1125 device_unlock(&dev->dev);
1126
1127 nfc_put_device(dev);
1128
1129 return rc;
1130}
1131
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001132static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1133{
1134 struct nfc_dev *dev;
1135 struct nfc_llcp_local *local;
1136 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1137 u32 idx;
1138 u8 tid;
1139 char *uri;
1140 int rc = 0, rem;
1141 size_t uri_len, tlvs_len;
1142 struct hlist_head sdreq_list;
1143 struct nfc_llcp_sdp_tlv *sdreq;
1144
1145 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1146 !info->attrs[NFC_ATTR_LLC_SDP])
1147 return -EINVAL;
1148
1149 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1150
1151 dev = nfc_get_device(idx);
Julia Lawall9abebb82015-10-17 11:32:19 +02001152 if (!dev)
1153 return -ENODEV;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001154
1155 device_lock(&dev->dev);
1156
1157 if (dev->dep_link_up == false) {
1158 rc = -ENOLINK;
1159 goto exit;
1160 }
1161
1162 local = nfc_llcp_find_local(dev);
1163 if (!local) {
1164 nfc_put_device(dev);
1165 rc = -ENODEV;
1166 goto exit;
1167 }
1168
1169 INIT_HLIST_HEAD(&sdreq_list);
1170
1171 tlvs_len = 0;
1172
1173 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1174 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1175 nfc_sdp_genl_policy);
1176
1177 if (rc != 0) {
1178 rc = -EINVAL;
1179 goto exit;
1180 }
1181
1182 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1183 continue;
1184
1185 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1186 if (uri_len == 0)
1187 continue;
1188
1189 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1190 if (uri == NULL || *uri == 0)
1191 continue;
1192
1193 tid = local->sdreq_next_tid++;
1194
1195 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1196 if (sdreq == NULL) {
1197 rc = -ENOMEM;
1198 goto exit;
1199 }
1200
1201 tlvs_len += sdreq->tlv_len;
1202
1203 hlist_add_head(&sdreq->node, &sdreq_list);
1204 }
1205
1206 if (hlist_empty(&sdreq_list)) {
1207 rc = -EINVAL;
1208 goto exit;
1209 }
1210
1211 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1212exit:
1213 device_unlock(&dev->dev);
1214
1215 nfc_put_device(dev);
1216
1217 return rc;
1218}
1219
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001220static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001221{
1222 struct nfc_dev *dev;
1223 int rc;
1224 u32 idx;
1225 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1226
1227 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1228 return -EINVAL;
1229
1230 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1231
1232 dev = nfc_get_device(idx);
1233 if (!dev)
1234 return -ENODEV;
1235
1236 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1237 sizeof(firmware_name));
1238
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001239 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001240
1241 nfc_put_device(dev);
1242 return rc;
1243}
1244
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001245int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1246 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001247{
1248 struct sk_buff *msg;
1249 void *hdr;
1250
1251 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1252 if (!msg)
1253 return -ENOMEM;
1254
1255 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001256 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001257 if (!hdr)
1258 goto free_msg;
1259
1260 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001261 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001262 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1263 goto nla_put_failure;
1264
1265 genlmsg_end(msg, hdr);
1266
Johannes Berg2a94fe42013-11-19 15:19:39 +01001267 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001268
1269 return 0;
1270
1271nla_put_failure:
1272 genlmsg_cancel(msg, hdr);
1273free_msg:
1274 nlmsg_free(msg);
1275 return -EMSGSIZE;
1276}
1277
Samuel Ortizbe085652013-05-10 17:07:32 +02001278static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1279{
1280 struct nfc_dev *dev;
1281 int rc;
1282 u32 idx, se_idx;
1283
1284 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1285 !info->attrs[NFC_ATTR_SE_INDEX])
1286 return -EINVAL;
1287
1288 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1289 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1290
1291 dev = nfc_get_device(idx);
1292 if (!dev)
1293 return -ENODEV;
1294
1295 rc = nfc_enable_se(dev, se_idx);
1296
1297 nfc_put_device(dev);
1298 return rc;
1299}
1300
1301static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1302{
1303 struct nfc_dev *dev;
1304 int rc;
1305 u32 idx, se_idx;
1306
1307 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1308 !info->attrs[NFC_ATTR_SE_INDEX])
1309 return -EINVAL;
1310
1311 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1312 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1313
1314 dev = nfc_get_device(idx);
1315 if (!dev)
1316 return -ENODEV;
1317
1318 rc = nfc_disable_se(dev, se_idx);
1319
1320 nfc_put_device(dev);
1321 return rc;
1322}
1323
Samuel Ortizac22ac42013-07-24 18:10:50 +02001324static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1325 u32 portid, u32 seq,
1326 struct netlink_callback *cb,
1327 int flags)
1328{
1329 void *hdr;
1330 struct nfc_se *se, *n;
1331
1332 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1333 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1334 NFC_CMD_GET_SE);
1335 if (!hdr)
1336 goto nla_put_failure;
1337
1338 if (cb)
1339 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1340
1341 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1342 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1343 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1344 goto nla_put_failure;
1345
Johannes Berg053c0952015-01-16 22:09:00 +01001346 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001347 }
1348
1349 return 0;
1350
1351nla_put_failure:
1352 genlmsg_cancel(msg, hdr);
1353 return -EMSGSIZE;
1354}
1355
1356static int nfc_genl_dump_ses(struct sk_buff *skb,
1357 struct netlink_callback *cb)
1358{
1359 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1360 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1361 bool first_call = false;
1362
1363 if (!iter) {
1364 first_call = true;
1365 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1366 if (!iter)
1367 return -ENOMEM;
1368 cb->args[0] = (long) iter;
1369 }
1370
1371 mutex_lock(&nfc_devlist_mutex);
1372
1373 cb->seq = nfc_devlist_generation;
1374
1375 if (first_call) {
1376 nfc_device_iter_init(iter);
1377 dev = nfc_device_iter_next(iter);
1378 }
1379
1380 while (dev) {
1381 int rc;
1382
1383 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1384 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1385 if (rc < 0)
1386 break;
1387
1388 dev = nfc_device_iter_next(iter);
1389 }
1390
1391 mutex_unlock(&nfc_devlist_mutex);
1392
1393 cb->args[1] = (long) dev;
1394
1395 return skb->len;
1396}
1397
1398static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1399{
1400 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1401
1402 nfc_device_iter_exit(iter);
1403 kfree(iter);
1404
1405 return 0;
1406}
1407
Christophe Ricardcd96db62014-12-02 21:27:51 +01001408static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1409 u8 *apdu, size_t apdu_length,
1410 se_io_cb_t cb, void *cb_context)
1411{
1412 struct nfc_se *se;
1413 int rc;
1414
1415 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1416
1417 device_lock(&dev->dev);
1418
1419 if (!device_is_registered(&dev->dev)) {
1420 rc = -ENODEV;
1421 goto error;
1422 }
1423
1424 if (!dev->dev_up) {
1425 rc = -ENODEV;
1426 goto error;
1427 }
1428
1429 if (!dev->ops->se_io) {
1430 rc = -EOPNOTSUPP;
1431 goto error;
1432 }
1433
1434 se = nfc_find_se(dev, se_idx);
1435 if (!se) {
1436 rc = -EINVAL;
1437 goto error;
1438 }
1439
1440 if (se->state != NFC_SE_ENABLED) {
1441 rc = -ENODEV;
1442 goto error;
1443 }
1444
1445 rc = dev->ops->se_io(dev, se_idx, apdu,
1446 apdu_length, cb, cb_context);
1447
1448error:
1449 device_unlock(&dev->dev);
1450 return rc;
1451}
1452
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001453struct se_io_ctx {
1454 u32 dev_idx;
1455 u32 se_idx;
1456};
1457
Samuel Ortizddc1a702013-10-07 14:18:44 +02001458static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001459{
1460 struct se_io_ctx *ctx = context;
1461 struct sk_buff *msg;
1462 void *hdr;
1463
1464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1465 if (!msg) {
1466 kfree(ctx);
1467 return;
1468 }
1469
1470 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1471 NFC_CMD_SE_IO);
1472 if (!hdr)
1473 goto free_msg;
1474
1475 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1476 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1477 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1478 goto nla_put_failure;
1479
1480 genlmsg_end(msg, hdr);
1481
Johannes Berg2a94fe42013-11-19 15:19:39 +01001482 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001483
1484 kfree(ctx);
1485
1486 return;
1487
1488nla_put_failure:
1489 genlmsg_cancel(msg, hdr);
1490free_msg:
1491 nlmsg_free(msg);
1492 kfree(ctx);
1493
1494 return;
1495}
1496
1497static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1498{
1499 struct nfc_dev *dev;
1500 struct se_io_ctx *ctx;
1501 u32 dev_idx, se_idx;
1502 u8 *apdu;
1503 size_t apdu_len;
1504
1505 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1506 !info->attrs[NFC_ATTR_SE_INDEX] ||
1507 !info->attrs[NFC_ATTR_SE_APDU])
1508 return -EINVAL;
1509
1510 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1511 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1512
1513 dev = nfc_get_device(dev_idx);
1514 if (!dev)
1515 return -ENODEV;
1516
1517 if (!dev->ops || !dev->ops->se_io)
1518 return -ENOTSUPP;
1519
1520 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1521 if (apdu_len == 0)
1522 return -EINVAL;
1523
1524 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1525 if (!apdu)
1526 return -EINVAL;
1527
1528 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1529 if (!ctx)
1530 return -ENOMEM;
1531
1532 ctx->dev_idx = dev_idx;
1533 ctx->se_idx = se_idx;
1534
Christophe Ricardcd96db62014-12-02 21:27:51 +01001535 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001536}
1537
Samuel Ortiz9e580952014-10-14 02:19:46 +02001538static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1539 struct genl_info *info)
1540{
1541 struct nfc_dev *dev;
1542 struct nfc_vendor_cmd *cmd;
1543 u32 dev_idx, vid, subcmd;
1544 u8 *data;
1545 size_t data_len;
Christophe Ricard29e76922015-08-19 21:26:43 +02001546 int i, err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001547
1548 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1549 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1550 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1551 return -EINVAL;
1552
1553 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1554 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1555 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1556
1557 dev = nfc_get_device(dev_idx);
1558 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1559 return -ENODEV;
1560
Christophe Ricardfe202fe2015-08-14 22:33:40 +02001561 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1562 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
Samuel Ortiz9e580952014-10-14 02:19:46 +02001563 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1564 if (data_len == 0)
1565 return -EINVAL;
1566 } else {
Christophe Ricardadca3c32015-08-17 08:33:43 +02001567 data = NULL;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001568 data_len = 0;
1569 }
1570
1571 for (i = 0; i < dev->n_vendor_cmds; i++) {
1572 cmd = &dev->vendor_cmds[i];
1573
1574 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1575 continue;
1576
Christophe Ricard29e76922015-08-19 21:26:43 +02001577 dev->cur_cmd_info = info;
1578 err = cmd->doit(dev, data, data_len);
1579 dev->cur_cmd_info = NULL;
1580 return err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001581 }
1582
1583 return -EOPNOTSUPP;
1584}
1585
Christophe Ricard29e76922015-08-19 21:26:43 +02001586/* message building helper */
1587static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1588 int flags, u8 cmd)
1589{
1590 /* since there is no private header just add the generic one */
1591 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1592}
1593
1594static struct sk_buff *
1595__nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1596 u32 portid, u32 seq,
1597 enum nfc_attrs attr,
1598 u32 oui, u32 subcmd, gfp_t gfp)
1599{
1600 struct sk_buff *skb;
1601 void *hdr;
1602
1603 skb = nlmsg_new(approxlen + 100, gfp);
1604 if (!skb)
1605 return NULL;
1606
1607 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1608 if (!hdr) {
1609 kfree_skb(skb);
1610 return NULL;
1611 }
1612
1613 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1614 goto nla_put_failure;
1615 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1616 goto nla_put_failure;
1617 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1618 goto nla_put_failure;
1619
1620 ((void **)skb->cb)[0] = dev;
1621 ((void **)skb->cb)[1] = hdr;
1622
1623 return skb;
1624
1625nla_put_failure:
1626 kfree_skb(skb);
1627 return NULL;
1628}
1629
1630struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1631 enum nfc_attrs attr,
1632 u32 oui, u32 subcmd,
1633 int approxlen)
1634{
1635 if (WARN_ON(!dev->cur_cmd_info))
1636 return NULL;
1637
1638 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1639 dev->cur_cmd_info->snd_portid,
1640 dev->cur_cmd_info->snd_seq, attr,
1641 oui, subcmd, GFP_KERNEL);
1642}
1643EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1644
1645int nfc_vendor_cmd_reply(struct sk_buff *skb)
1646{
1647 struct nfc_dev *dev = ((void **)skb->cb)[0];
1648 void *hdr = ((void **)skb->cb)[1];
1649
1650 /* clear CB data for netlink core to own from now on */
1651 memset(skb->cb, 0, sizeof(skb->cb));
1652
1653 if (WARN_ON(!dev->cur_cmd_info)) {
1654 kfree_skb(skb);
1655 return -EINVAL;
1656 }
1657
1658 genlmsg_end(skb, hdr);
1659 return genlmsg_reply(skb, dev->cur_cmd_info);
1660}
1661EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1662
Johannes Berg4534de82013-11-14 17:14:46 +01001663static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001664 {
1665 .cmd = NFC_CMD_GET_DEVICE,
1666 .doit = nfc_genl_get_device,
1667 .dumpit = nfc_genl_dump_devices,
1668 .done = nfc_genl_dump_devices_done,
1669 .policy = nfc_genl_policy,
1670 },
1671 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001672 .cmd = NFC_CMD_DEV_UP,
1673 .doit = nfc_genl_dev_up,
1674 .policy = nfc_genl_policy,
1675 },
1676 {
1677 .cmd = NFC_CMD_DEV_DOWN,
1678 .doit = nfc_genl_dev_down,
1679 .policy = nfc_genl_policy,
1680 },
1681 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001682 .cmd = NFC_CMD_START_POLL,
1683 .doit = nfc_genl_start_poll,
1684 .policy = nfc_genl_policy,
1685 },
1686 {
1687 .cmd = NFC_CMD_STOP_POLL,
1688 .doit = nfc_genl_stop_poll,
1689 .policy = nfc_genl_policy,
1690 },
1691 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001692 .cmd = NFC_CMD_DEP_LINK_UP,
1693 .doit = nfc_genl_dep_link_up,
1694 .policy = nfc_genl_policy,
1695 },
1696 {
1697 .cmd = NFC_CMD_DEP_LINK_DOWN,
1698 .doit = nfc_genl_dep_link_down,
1699 .policy = nfc_genl_policy,
1700 },
1701 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001702 .cmd = NFC_CMD_GET_TARGET,
1703 .dumpit = nfc_genl_dump_targets,
1704 .done = nfc_genl_dump_targets_done,
1705 .policy = nfc_genl_policy,
1706 },
Thierry Escande52feb442012-10-17 14:43:39 +02001707 {
1708 .cmd = NFC_CMD_LLC_GET_PARAMS,
1709 .doit = nfc_genl_llc_get_params,
1710 .policy = nfc_genl_policy,
1711 },
1712 {
1713 .cmd = NFC_CMD_LLC_SET_PARAMS,
1714 .doit = nfc_genl_llc_set_params,
1715 .policy = nfc_genl_policy,
1716 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001717 {
1718 .cmd = NFC_CMD_LLC_SDREQ,
1719 .doit = nfc_genl_llc_sdreq,
1720 .policy = nfc_genl_policy,
1721 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001722 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001723 .cmd = NFC_CMD_FW_DOWNLOAD,
1724 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001725 .policy = nfc_genl_policy,
1726 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001727 {
1728 .cmd = NFC_CMD_ENABLE_SE,
1729 .doit = nfc_genl_enable_se,
1730 .policy = nfc_genl_policy,
1731 },
1732 {
1733 .cmd = NFC_CMD_DISABLE_SE,
1734 .doit = nfc_genl_disable_se,
1735 .policy = nfc_genl_policy,
1736 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001737 {
1738 .cmd = NFC_CMD_GET_SE,
1739 .dumpit = nfc_genl_dump_ses,
1740 .done = nfc_genl_dump_ses_done,
1741 .policy = nfc_genl_policy,
1742 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001743 {
1744 .cmd = NFC_CMD_SE_IO,
1745 .doit = nfc_genl_se_io,
1746 .policy = nfc_genl_policy,
1747 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001748 {
1749 .cmd = NFC_CMD_ACTIVATE_TARGET,
1750 .doit = nfc_genl_activate_target,
1751 .policy = nfc_genl_policy,
1752 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001753 {
1754 .cmd = NFC_CMD_VENDOR,
1755 .doit = nfc_genl_vendor_cmd,
1756 .policy = nfc_genl_policy,
1757 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001758};
1759
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001760
1761struct urelease_work {
1762 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001763 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001764};
1765
1766static void nfc_urelease_event_work(struct work_struct *work)
1767{
1768 struct urelease_work *w = container_of(work, struct urelease_work, w);
1769 struct class_dev_iter iter;
1770 struct nfc_dev *dev;
1771
John W. Linvillec4876062012-09-28 11:11:16 -04001772 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001773
1774 mutex_lock(&nfc_devlist_mutex);
1775
1776 nfc_device_iter_init(&iter);
1777 dev = nfc_device_iter_next(&iter);
1778
1779 while (dev) {
1780 mutex_lock(&dev->genl_data.genl_data_mutex);
1781
John W. Linvillec4876062012-09-28 11:11:16 -04001782 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001783 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001784 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001785 }
1786
1787 mutex_unlock(&dev->genl_data.genl_data_mutex);
1788
1789 dev = nfc_device_iter_next(&iter);
1790 }
1791
1792 nfc_device_iter_exit(&iter);
1793
1794 mutex_unlock(&nfc_devlist_mutex);
1795
1796 kfree(w);
1797}
1798
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001799static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001800 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001801{
1802 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001803 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001804
1805 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1806 goto out;
1807
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001809
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001810 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1811 if (w) {
1812 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001813 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001814 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001815 }
1816
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001817out:
1818 return NOTIFY_DONE;
1819}
1820
1821void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1822{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001824 mutex_init(&genl_data->genl_data_mutex);
1825}
1826
1827void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1828{
1829 mutex_destroy(&genl_data->genl_data_mutex);
1830}
1831
1832static struct notifier_block nl_notifier = {
1833 .notifier_call = nfc_genl_rcv_nl_event,
1834};
1835
1836/**
1837 * nfc_genl_init() - Initialize netlink interface
1838 *
1839 * This initialization function registers the nfc netlink family.
1840 */
1841int __init nfc_genl_init(void)
1842{
1843 int rc;
1844
Johannes Berg2a94fe42013-11-19 15:19:39 +01001845 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1846 nfc_genl_ops,
1847 nfc_genl_mcgrps);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001848 if (rc)
1849 return rc;
1850
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001851 netlink_register_notifier(&nl_notifier);
1852
Johannes Berg2a94fe42013-11-19 15:19:39 +01001853 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001854}
1855
1856/**
1857 * nfc_genl_exit() - Deinitialize netlink interface
1858 *
1859 * This exit function unregisters the nfc netlink family.
1860 */
1861void nfc_genl_exit(void)
1862{
1863 netlink_unregister_notifier(&nl_notifier);
1864 genl_unregister_family(&nfc_genl_family);
1865}