blob: f85f37ed19b23d0b9509e243d2c56b3070c6cbb6 [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 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010066};
67
68static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
69 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
70 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030071};
72
73static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010074 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030075{
76 void *hdr;
77
Eric W. Biederman15e47302012-09-07 20:12:54 +000078 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010079 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030080 if (!hdr)
81 return -EMSGSIZE;
82
83 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
84
David S. Miller1e6428d2012-03-29 23:23:57 -040085 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
86 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
87 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
88 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
89 goto nla_put_failure;
90 if (target->nfcid1_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
92 target->nfcid1))
93 goto nla_put_failure;
94 if (target->sensb_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
96 target->sensb_res))
97 goto nla_put_failure;
98 if (target->sensf_res_len > 0 &&
99 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
100 target->sensf_res))
101 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300102
Mark A. Greerf5f68722014-01-14 17:52:11 -0700103 if (target->is_iso15693) {
104 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
105 target->iso15693_dsfid) ||
106 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
107 sizeof(target->iso15693_uid), target->iso15693_uid))
108 goto nla_put_failure;
109 }
110
Johannes Berg053c0952015-01-16 22:09:00 +0100111 genlmsg_end(msg, hdr);
112 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300113
114nla_put_failure:
115 genlmsg_cancel(msg, hdr);
116 return -EMSGSIZE;
117}
118
119static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
120{
121 struct nfc_dev *dev;
122 int rc;
123 u32 idx;
124
125 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100126 nfc_genl_family.attrbuf,
127 nfc_genl_family.maxattr,
128 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300129 if (rc < 0)
130 return ERR_PTR(rc);
131
132 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
133 return ERR_PTR(-EINVAL);
134
135 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
136
137 dev = nfc_get_device(idx);
138 if (!dev)
139 return ERR_PTR(-ENODEV);
140
141 return dev;
142}
143
144static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100145 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300146{
147 int i = cb->args[0];
148 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
149 int rc;
150
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300151 if (!dev) {
152 dev = __get_device_from_cb(cb);
153 if (IS_ERR(dev))
154 return PTR_ERR(dev);
155
156 cb->args[1] = (long) dev;
157 }
158
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200159 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300160
161 cb->seq = dev->targets_generation;
162
163 while (i < dev->n_targets) {
164 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100165 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300166 if (rc < 0)
167 break;
168
169 i++;
170 }
171
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200172 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300173
174 cb->args[0] = i;
175
176 return skb->len;
177}
178
179static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
180{
181 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
182
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300183 if (dev)
184 nfc_put_device(dev);
185
186 return 0;
187}
188
189int nfc_genl_targets_found(struct nfc_dev *dev)
190{
191 struct sk_buff *msg;
192 void *hdr;
193
Eric W. Biederman15e47302012-09-07 20:12:54 +0000194 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300195
Thomas Graf58050fc2012-06-28 03:57:45 +0000196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300197 if (!msg)
198 return -ENOMEM;
199
200 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100201 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300202 if (!hdr)
203 goto free_msg;
204
David S. Miller1e6428d2012-03-29 23:23:57 -0400205 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
206 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300207
208 genlmsg_end(msg, hdr);
209
Johannes Berg2a94fe42013-11-19 15:19:39 +0100210 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300211
212nla_put_failure:
213 genlmsg_cancel(msg, hdr);
214free_msg:
215 nlmsg_free(msg);
216 return -EMSGSIZE;
217}
218
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200219int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
220{
221 struct sk_buff *msg;
222 void *hdr;
223
Thomas Graf58050fc2012-06-28 03:57:45 +0000224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200225 if (!msg)
226 return -ENOMEM;
227
228 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
229 NFC_EVENT_TARGET_LOST);
230 if (!hdr)
231 goto free_msg;
232
John W. Linville59ef43e2012-04-18 14:17:13 -0400233 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
234 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
235 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200236
237 genlmsg_end(msg, hdr);
238
Johannes Berg2a94fe42013-11-19 15:19:39 +0100239 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200240
241 return 0;
242
243nla_put_failure:
244 genlmsg_cancel(msg, hdr);
245free_msg:
246 nlmsg_free(msg);
247 return -EMSGSIZE;
248}
249
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200250int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
251{
252 struct sk_buff *msg;
253 void *hdr;
254
Thomas Graf58050fc2012-06-28 03:57:45 +0000255 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200256 if (!msg)
257 return -ENOMEM;
258
259 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
260 NFC_EVENT_TM_ACTIVATED);
261 if (!hdr)
262 goto free_msg;
263
264 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
265 goto nla_put_failure;
266 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
267 goto nla_put_failure;
268
269 genlmsg_end(msg, hdr);
270
Johannes Berg2a94fe42013-11-19 15:19:39 +0100271 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200272
273 return 0;
274
275nla_put_failure:
276 genlmsg_cancel(msg, hdr);
277free_msg:
278 nlmsg_free(msg);
279 return -EMSGSIZE;
280}
281
282int nfc_genl_tm_deactivated(struct nfc_dev *dev)
283{
284 struct sk_buff *msg;
285 void *hdr;
286
Thomas Graf58050fc2012-06-28 03:57:45 +0000287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200288 if (!msg)
289 return -ENOMEM;
290
291 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
292 NFC_EVENT_TM_DEACTIVATED);
293 if (!hdr)
294 goto free_msg;
295
296 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
297 goto nla_put_failure;
298
299 genlmsg_end(msg, hdr);
300
Johannes Berg2a94fe42013-11-19 15:19:39 +0100301 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200302
303 return 0;
304
305nla_put_failure:
306 genlmsg_cancel(msg, hdr);
307free_msg:
308 nlmsg_free(msg);
309 return -EMSGSIZE;
310}
311
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300312int nfc_genl_device_added(struct nfc_dev *dev)
313{
314 struct sk_buff *msg;
315 void *hdr;
316
Thomas Graf58050fc2012-06-28 03:57:45 +0000317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300318 if (!msg)
319 return -ENOMEM;
320
321 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100322 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300323 if (!hdr)
324 goto free_msg;
325
David S. Miller1e6428d2012-03-29 23:23:57 -0400326 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
327 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
328 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
329 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
330 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300331
332 genlmsg_end(msg, hdr);
333
Johannes Berg2a94fe42013-11-19 15:19:39 +0100334 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300335
336 return 0;
337
338nla_put_failure:
339 genlmsg_cancel(msg, hdr);
340free_msg:
341 nlmsg_free(msg);
342 return -EMSGSIZE;
343}
344
345int nfc_genl_device_removed(struct nfc_dev *dev)
346{
347 struct sk_buff *msg;
348 void *hdr;
349
Thomas Graf58050fc2012-06-28 03:57:45 +0000350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300351 if (!msg)
352 return -ENOMEM;
353
354 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100355 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300356 if (!hdr)
357 goto free_msg;
358
David S. Miller1e6428d2012-03-29 23:23:57 -0400359 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
360 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300361
362 genlmsg_end(msg, hdr);
363
Johannes Berg2a94fe42013-11-19 15:19:39 +0100364 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300365
366 return 0;
367
368nla_put_failure:
369 genlmsg_cancel(msg, hdr);
370free_msg:
371 nlmsg_free(msg);
372 return -EMSGSIZE;
373}
374
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100375int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
376{
377 struct sk_buff *msg;
378 struct nlattr *sdp_attr, *uri_attr;
379 struct nfc_llcp_sdp_tlv *sdres;
380 struct hlist_node *n;
381 void *hdr;
382 int rc = -EMSGSIZE;
383 int i;
384
385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
386 if (!msg)
387 return -ENOMEM;
388
389 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
390 NFC_EVENT_LLC_SDRES);
391 if (!hdr)
392 goto free_msg;
393
394 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
395 goto nla_put_failure;
396
397 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
398 if (sdp_attr == NULL) {
399 rc = -ENOMEM;
400 goto nla_put_failure;
401 }
402
403 i = 1;
404 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
405 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
406
407 uri_attr = nla_nest_start(msg, i++);
408 if (uri_attr == NULL) {
409 rc = -ENOMEM;
410 goto nla_put_failure;
411 }
412
413 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
414 goto nla_put_failure;
415
416 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
417 goto nla_put_failure;
418
419 nla_nest_end(msg, uri_attr);
420
421 hlist_del(&sdres->node);
422
423 nfc_llcp_free_sdp_tlv(sdres);
424 }
425
426 nla_nest_end(msg, sdp_attr);
427
428 genlmsg_end(msg, hdr);
429
Johannes Berg2a94fe42013-11-19 15:19:39 +0100430 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100431
432nla_put_failure:
433 genlmsg_cancel(msg, hdr);
434
435free_msg:
436 nlmsg_free(msg);
437
438 nfc_llcp_free_sdp_tlv_list(sdres_list);
439
440 return rc;
441}
442
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200443int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
444{
445 struct sk_buff *msg;
446 void *hdr;
447
448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
449 if (!msg)
450 return -ENOMEM;
451
452 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
453 NFC_EVENT_SE_ADDED);
454 if (!hdr)
455 goto free_msg;
456
457 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
458 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
459 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
460 goto nla_put_failure;
461
462 genlmsg_end(msg, hdr);
463
Johannes Berg2a94fe42013-11-19 15:19:39 +0100464 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200465
466 return 0;
467
468nla_put_failure:
469 genlmsg_cancel(msg, hdr);
470free_msg:
471 nlmsg_free(msg);
472 return -EMSGSIZE;
473}
474
475int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
476{
477 struct sk_buff *msg;
478 void *hdr;
479
480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
481 if (!msg)
482 return -ENOMEM;
483
484 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
485 NFC_EVENT_SE_REMOVED);
486 if (!hdr)
487 goto free_msg;
488
489 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
490 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
491 goto nla_put_failure;
492
493 genlmsg_end(msg, hdr);
494
Johannes Berg2a94fe42013-11-19 15:19:39 +0100495 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200496
497 return 0;
498
499nla_put_failure:
500 genlmsg_cancel(msg, hdr);
501free_msg:
502 nlmsg_free(msg);
503 return -EMSGSIZE;
504}
505
Christophe Ricard447b27c2015-02-01 22:26:16 +0100506int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
507 struct nfc_evt_transaction *evt_transaction)
508{
509 struct nfc_se *se;
510 struct sk_buff *msg;
511 void *hdr;
512
513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
514 if (!msg)
515 return -ENOMEM;
516
517 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
518 NFC_EVENT_SE_TRANSACTION);
519 if (!hdr)
520 goto free_msg;
521
522 se = nfc_find_se(dev, se_idx);
523 if (!se)
524 goto free_msg;
525
526 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
527 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
528 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
529 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
530 evt_transaction->aid) ||
531 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
532 evt_transaction->params))
533 goto nla_put_failure;
534
535 /* evt_transaction is no more used */
536 devm_kfree(&dev->dev, evt_transaction);
537
538 genlmsg_end(msg, hdr);
539
540 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
541
542 return 0;
543
544nla_put_failure:
545 genlmsg_cancel(msg, hdr);
546free_msg:
547 /* evt_transaction is no more used */
548 devm_kfree(&dev->dev, evt_transaction);
549 nlmsg_free(msg);
550 return -EMSGSIZE;
551}
552
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300553static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000554 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100555 struct netlink_callback *cb,
556 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300557{
558 void *hdr;
559
Eric W. Biederman15e47302012-09-07 20:12:54 +0000560 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100561 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300562 if (!hdr)
563 return -EMSGSIZE;
564
565 if (cb)
566 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
567
David S. Miller1e6428d2012-03-29 23:23:57 -0400568 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
569 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
570 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200571 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
572 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400573 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300574
Johannes Berg053c0952015-01-16 22:09:00 +0100575 genlmsg_end(msg, hdr);
576 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300577
578nla_put_failure:
579 genlmsg_cancel(msg, hdr);
580 return -EMSGSIZE;
581}
582
583static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100584 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300585{
586 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
587 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
588 bool first_call = false;
589
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300590 if (!iter) {
591 first_call = true;
592 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
593 if (!iter)
594 return -ENOMEM;
595 cb->args[0] = (long) iter;
596 }
597
598 mutex_lock(&nfc_devlist_mutex);
599
600 cb->seq = nfc_devlist_generation;
601
602 if (first_call) {
603 nfc_device_iter_init(iter);
604 dev = nfc_device_iter_next(iter);
605 }
606
607 while (dev) {
608 int rc;
609
Eric W. Biederman15e47302012-09-07 20:12:54 +0000610 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100611 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300612 if (rc < 0)
613 break;
614
615 dev = nfc_device_iter_next(iter);
616 }
617
618 mutex_unlock(&nfc_devlist_mutex);
619
620 cb->args[1] = (long) dev;
621
622 return skb->len;
623}
624
625static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
626{
627 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
628
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300629 nfc_device_iter_exit(iter);
630 kfree(iter);
631
632 return 0;
633}
634
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100635int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100636 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100637{
638 struct sk_buff *msg;
639 void *hdr;
640
641 pr_debug("DEP link is up\n");
642
Thomas Graf58050fc2012-06-28 03:57:45 +0000643 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100644 if (!msg)
645 return -ENOMEM;
646
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100647 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100648 if (!hdr)
649 goto free_msg;
650
David S. Miller1e6428d2012-03-29 23:23:57 -0400651 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
652 goto nla_put_failure;
653 if (rf_mode == NFC_RF_INITIATOR &&
654 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
655 goto nla_put_failure;
656 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
657 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
658 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100659
660 genlmsg_end(msg, hdr);
661
662 dev->dep_link_up = true;
663
Johannes Berg2a94fe42013-11-19 15:19:39 +0100664 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100665
666 return 0;
667
668nla_put_failure:
669 genlmsg_cancel(msg, hdr);
670free_msg:
671 nlmsg_free(msg);
672 return -EMSGSIZE;
673}
674
675int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
676{
677 struct sk_buff *msg;
678 void *hdr;
679
680 pr_debug("DEP link is down\n");
681
Thomas Graf58050fc2012-06-28 03:57:45 +0000682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100683 if (!msg)
684 return -ENOMEM;
685
686 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100687 NFC_CMD_DEP_LINK_DOWN);
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;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100693
694 genlmsg_end(msg, hdr);
695
Johannes Berg2a94fe42013-11-19 15:19:39 +0100696 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100697
698 return 0;
699
700nla_put_failure:
701 genlmsg_cancel(msg, hdr);
702free_msg:
703 nlmsg_free(msg);
704 return -EMSGSIZE;
705}
706
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300707static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
708{
709 struct sk_buff *msg;
710 struct nfc_dev *dev;
711 u32 idx;
712 int rc = -ENOBUFS;
713
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300714 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
715 return -EINVAL;
716
717 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
718
719 dev = nfc_get_device(idx);
720 if (!dev)
721 return -ENODEV;
722
Thomas Graf58050fc2012-06-28 03:57:45 +0000723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300724 if (!msg) {
725 rc = -ENOMEM;
726 goto out_putdev;
727 }
728
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100730 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300731 if (rc < 0)
732 goto out_free;
733
734 nfc_put_device(dev);
735
736 return genlmsg_reply(msg, info);
737
738out_free:
739 nlmsg_free(msg);
740out_putdev:
741 nfc_put_device(dev);
742 return rc;
743}
744
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300745static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
746{
747 struct nfc_dev *dev;
748 int rc;
749 u32 idx;
750
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
752 return -EINVAL;
753
754 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
755
756 dev = nfc_get_device(idx);
757 if (!dev)
758 return -ENODEV;
759
760 rc = nfc_dev_up(dev);
761
762 nfc_put_device(dev);
763 return rc;
764}
765
766static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
767{
768 struct nfc_dev *dev;
769 int rc;
770 u32 idx;
771
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300772 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
773 return -EINVAL;
774
775 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
776
777 dev = nfc_get_device(idx);
778 if (!dev)
779 return -ENODEV;
780
781 rc = nfc_dev_down(dev);
782
783 nfc_put_device(dev);
784 return rc;
785}
786
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300787static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
788{
789 struct nfc_dev *dev;
790 int rc;
791 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200792 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300793
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100794 pr_debug("Poll start\n");
795
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300796 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200797 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
798 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200799 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300800 return -EINVAL;
801
802 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200803
804 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
805 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200806
807 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
808 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200809 else if (info->attrs[NFC_ATTR_PROTOCOLS])
810 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300811
812 dev = nfc_get_device(idx);
813 if (!dev)
814 return -ENODEV;
815
816 mutex_lock(&dev->genl_data.genl_data_mutex);
817
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200818 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300819 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000820 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300821
822 mutex_unlock(&dev->genl_data.genl_data_mutex);
823
824 nfc_put_device(dev);
825 return rc;
826}
827
828static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
829{
830 struct nfc_dev *dev;
831 int rc;
832 u32 idx;
833
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300834 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
835 return -EINVAL;
836
837 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
838
839 dev = nfc_get_device(idx);
840 if (!dev)
841 return -ENODEV;
842
Samuel Ortiza831b912012-06-28 16:41:57 +0200843 device_lock(&dev->dev);
844
845 if (!dev->polling) {
846 device_unlock(&dev->dev);
847 return -EINVAL;
848 }
849
850 device_unlock(&dev->dev);
851
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300852 mutex_lock(&dev->genl_data.genl_data_mutex);
853
Eric W. Biederman15e47302012-09-07 20:12:54 +0000854 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300855 rc = -EBUSY;
856 goto out;
857 }
858
859 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000860 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300861
862out:
863 mutex_unlock(&dev->genl_data.genl_data_mutex);
864 nfc_put_device(dev);
865 return rc;
866}
867
Christophe Ricard3682f492014-12-02 21:27:50 +0100868static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
869{
870 struct nfc_dev *dev;
871 u32 device_idx, target_idx, protocol;
872 int rc;
873
874 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
875 return -EINVAL;
876
877 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
878
879 dev = nfc_get_device(device_idx);
880 if (!dev)
881 return -ENODEV;
882
883 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
884 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
885
886 nfc_deactivate_target(dev, target_idx);
887 rc = nfc_activate_target(dev, target_idx, protocol);
888
889 nfc_put_device(dev);
890 return 0;
891}
892
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100893static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
894{
895 struct nfc_dev *dev;
896 int rc, tgt_idx;
897 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100898 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100899
900 pr_debug("DEP link up\n");
901
902 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100903 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100904 return -EINVAL;
905
906 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
907 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
908 tgt_idx = NFC_TARGET_IDX_ANY;
909 else
910 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
911
912 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100913
914 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
915 return -EINVAL;
916
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100917 dev = nfc_get_device(idx);
918 if (!dev)
919 return -ENODEV;
920
Samuel Ortiz47807d32012-03-05 01:03:50 +0100921 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100922
923 nfc_put_device(dev);
924
925 return rc;
926}
927
928static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
929{
930 struct nfc_dev *dev;
931 int rc;
932 u32 idx;
933
934 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
935 return -EINVAL;
936
937 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
938
939 dev = nfc_get_device(idx);
940 if (!dev)
941 return -ENODEV;
942
943 rc = nfc_dep_link_down(dev);
944
945 nfc_put_device(dev);
946 return rc;
947}
948
Thierry Escande52feb442012-10-17 14:43:39 +0200949static int nfc_genl_send_params(struct sk_buff *msg,
950 struct nfc_llcp_local *local,
951 u32 portid, u32 seq)
952{
953 void *hdr;
954
955 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
956 NFC_CMD_LLC_GET_PARAMS);
957 if (!hdr)
958 return -EMSGSIZE;
959
960 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
961 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
962 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
963 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
964 goto nla_put_failure;
965
Johannes Berg053c0952015-01-16 22:09:00 +0100966 genlmsg_end(msg, hdr);
967 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +0200968
969nla_put_failure:
970
971 genlmsg_cancel(msg, hdr);
972 return -EMSGSIZE;
973}
974
975static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
976{
977 struct nfc_dev *dev;
978 struct nfc_llcp_local *local;
979 int rc = 0;
980 struct sk_buff *msg = NULL;
981 u32 idx;
982
983 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
984 return -EINVAL;
985
986 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
987
988 dev = nfc_get_device(idx);
989 if (!dev)
990 return -ENODEV;
991
992 device_lock(&dev->dev);
993
994 local = nfc_llcp_find_local(dev);
995 if (!local) {
996 rc = -ENODEV;
997 goto exit;
998 }
999
1000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1001 if (!msg) {
1002 rc = -ENOMEM;
1003 goto exit;
1004 }
1005
1006 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1007
1008exit:
1009 device_unlock(&dev->dev);
1010
1011 nfc_put_device(dev);
1012
1013 if (rc < 0) {
1014 if (msg)
1015 nlmsg_free(msg);
1016
1017 return rc;
1018 }
1019
1020 return genlmsg_reply(msg, info);
1021}
1022
1023static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1024{
1025 struct nfc_dev *dev;
1026 struct nfc_llcp_local *local;
1027 u8 rw = 0;
1028 u16 miux = 0;
1029 u32 idx;
1030 int rc = 0;
1031
1032 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1033 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1034 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1035 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1036 return -EINVAL;
1037
1038 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1039 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1040
1041 if (rw > LLCP_MAX_RW)
1042 return -EINVAL;
1043 }
1044
1045 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1046 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1047
1048 if (miux > LLCP_MAX_MIUX)
1049 return -EINVAL;
1050 }
1051
1052 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1053
1054 dev = nfc_get_device(idx);
1055 if (!dev)
1056 return -ENODEV;
1057
1058 device_lock(&dev->dev);
1059
1060 local = nfc_llcp_find_local(dev);
1061 if (!local) {
1062 nfc_put_device(dev);
1063 rc = -ENODEV;
1064 goto exit;
1065 }
1066
1067 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1068 if (dev->dep_link_up) {
1069 rc = -EINPROGRESS;
1070 goto exit;
1071 }
1072
1073 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1074 }
1075
1076 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1077 local->rw = rw;
1078
1079 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1080 local->miux = cpu_to_be16(miux);
1081
1082exit:
1083 device_unlock(&dev->dev);
1084
1085 nfc_put_device(dev);
1086
1087 return rc;
1088}
1089
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001090static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1091{
1092 struct nfc_dev *dev;
1093 struct nfc_llcp_local *local;
1094 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1095 u32 idx;
1096 u8 tid;
1097 char *uri;
1098 int rc = 0, rem;
1099 size_t uri_len, tlvs_len;
1100 struct hlist_head sdreq_list;
1101 struct nfc_llcp_sdp_tlv *sdreq;
1102
1103 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1104 !info->attrs[NFC_ATTR_LLC_SDP])
1105 return -EINVAL;
1106
1107 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1108
1109 dev = nfc_get_device(idx);
1110 if (!dev) {
1111 rc = -ENODEV;
1112 goto exit;
1113 }
1114
1115 device_lock(&dev->dev);
1116
1117 if (dev->dep_link_up == false) {
1118 rc = -ENOLINK;
1119 goto exit;
1120 }
1121
1122 local = nfc_llcp_find_local(dev);
1123 if (!local) {
1124 nfc_put_device(dev);
1125 rc = -ENODEV;
1126 goto exit;
1127 }
1128
1129 INIT_HLIST_HEAD(&sdreq_list);
1130
1131 tlvs_len = 0;
1132
1133 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1134 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1135 nfc_sdp_genl_policy);
1136
1137 if (rc != 0) {
1138 rc = -EINVAL;
1139 goto exit;
1140 }
1141
1142 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1143 continue;
1144
1145 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1146 if (uri_len == 0)
1147 continue;
1148
1149 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1150 if (uri == NULL || *uri == 0)
1151 continue;
1152
1153 tid = local->sdreq_next_tid++;
1154
1155 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1156 if (sdreq == NULL) {
1157 rc = -ENOMEM;
1158 goto exit;
1159 }
1160
1161 tlvs_len += sdreq->tlv_len;
1162
1163 hlist_add_head(&sdreq->node, &sdreq_list);
1164 }
1165
1166 if (hlist_empty(&sdreq_list)) {
1167 rc = -EINVAL;
1168 goto exit;
1169 }
1170
1171 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1172exit:
1173 device_unlock(&dev->dev);
1174
1175 nfc_put_device(dev);
1176
1177 return rc;
1178}
1179
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001180static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001181{
1182 struct nfc_dev *dev;
1183 int rc;
1184 u32 idx;
1185 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1186
1187 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1188 return -EINVAL;
1189
1190 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1191
1192 dev = nfc_get_device(idx);
1193 if (!dev)
1194 return -ENODEV;
1195
1196 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1197 sizeof(firmware_name));
1198
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001199 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001200
1201 nfc_put_device(dev);
1202 return rc;
1203}
1204
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001205int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1206 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001207{
1208 struct sk_buff *msg;
1209 void *hdr;
1210
1211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1212 if (!msg)
1213 return -ENOMEM;
1214
1215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001216 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001217 if (!hdr)
1218 goto free_msg;
1219
1220 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001221 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001222 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1223 goto nla_put_failure;
1224
1225 genlmsg_end(msg, hdr);
1226
Johannes Berg2a94fe42013-11-19 15:19:39 +01001227 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001228
1229 return 0;
1230
1231nla_put_failure:
1232 genlmsg_cancel(msg, hdr);
1233free_msg:
1234 nlmsg_free(msg);
1235 return -EMSGSIZE;
1236}
1237
Samuel Ortizbe085652013-05-10 17:07:32 +02001238static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1239{
1240 struct nfc_dev *dev;
1241 int rc;
1242 u32 idx, se_idx;
1243
1244 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1245 !info->attrs[NFC_ATTR_SE_INDEX])
1246 return -EINVAL;
1247
1248 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1249 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1250
1251 dev = nfc_get_device(idx);
1252 if (!dev)
1253 return -ENODEV;
1254
1255 rc = nfc_enable_se(dev, se_idx);
1256
1257 nfc_put_device(dev);
1258 return rc;
1259}
1260
1261static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1262{
1263 struct nfc_dev *dev;
1264 int rc;
1265 u32 idx, se_idx;
1266
1267 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1268 !info->attrs[NFC_ATTR_SE_INDEX])
1269 return -EINVAL;
1270
1271 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1272 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1273
1274 dev = nfc_get_device(idx);
1275 if (!dev)
1276 return -ENODEV;
1277
1278 rc = nfc_disable_se(dev, se_idx);
1279
1280 nfc_put_device(dev);
1281 return rc;
1282}
1283
Samuel Ortizac22ac42013-07-24 18:10:50 +02001284static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1285 u32 portid, u32 seq,
1286 struct netlink_callback *cb,
1287 int flags)
1288{
1289 void *hdr;
1290 struct nfc_se *se, *n;
1291
1292 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1293 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1294 NFC_CMD_GET_SE);
1295 if (!hdr)
1296 goto nla_put_failure;
1297
1298 if (cb)
1299 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1300
1301 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1302 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1303 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1304 goto nla_put_failure;
1305
Johannes Berg053c0952015-01-16 22:09:00 +01001306 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001307 }
1308
1309 return 0;
1310
1311nla_put_failure:
1312 genlmsg_cancel(msg, hdr);
1313 return -EMSGSIZE;
1314}
1315
1316static int nfc_genl_dump_ses(struct sk_buff *skb,
1317 struct netlink_callback *cb)
1318{
1319 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1320 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1321 bool first_call = false;
1322
1323 if (!iter) {
1324 first_call = true;
1325 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1326 if (!iter)
1327 return -ENOMEM;
1328 cb->args[0] = (long) iter;
1329 }
1330
1331 mutex_lock(&nfc_devlist_mutex);
1332
1333 cb->seq = nfc_devlist_generation;
1334
1335 if (first_call) {
1336 nfc_device_iter_init(iter);
1337 dev = nfc_device_iter_next(iter);
1338 }
1339
1340 while (dev) {
1341 int rc;
1342
1343 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1344 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1345 if (rc < 0)
1346 break;
1347
1348 dev = nfc_device_iter_next(iter);
1349 }
1350
1351 mutex_unlock(&nfc_devlist_mutex);
1352
1353 cb->args[1] = (long) dev;
1354
1355 return skb->len;
1356}
1357
1358static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1359{
1360 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1361
1362 nfc_device_iter_exit(iter);
1363 kfree(iter);
1364
1365 return 0;
1366}
1367
Christophe Ricardcd96db62014-12-02 21:27:51 +01001368static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1369 u8 *apdu, size_t apdu_length,
1370 se_io_cb_t cb, void *cb_context)
1371{
1372 struct nfc_se *se;
1373 int rc;
1374
1375 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1376
1377 device_lock(&dev->dev);
1378
1379 if (!device_is_registered(&dev->dev)) {
1380 rc = -ENODEV;
1381 goto error;
1382 }
1383
1384 if (!dev->dev_up) {
1385 rc = -ENODEV;
1386 goto error;
1387 }
1388
1389 if (!dev->ops->se_io) {
1390 rc = -EOPNOTSUPP;
1391 goto error;
1392 }
1393
1394 se = nfc_find_se(dev, se_idx);
1395 if (!se) {
1396 rc = -EINVAL;
1397 goto error;
1398 }
1399
1400 if (se->state != NFC_SE_ENABLED) {
1401 rc = -ENODEV;
1402 goto error;
1403 }
1404
1405 rc = dev->ops->se_io(dev, se_idx, apdu,
1406 apdu_length, cb, cb_context);
1407
1408error:
1409 device_unlock(&dev->dev);
1410 return rc;
1411}
1412
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001413struct se_io_ctx {
1414 u32 dev_idx;
1415 u32 se_idx;
1416};
1417
Samuel Ortizddc1a702013-10-07 14:18:44 +02001418static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001419{
1420 struct se_io_ctx *ctx = context;
1421 struct sk_buff *msg;
1422 void *hdr;
1423
1424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1425 if (!msg) {
1426 kfree(ctx);
1427 return;
1428 }
1429
1430 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1431 NFC_CMD_SE_IO);
1432 if (!hdr)
1433 goto free_msg;
1434
1435 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1436 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1437 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1438 goto nla_put_failure;
1439
1440 genlmsg_end(msg, hdr);
1441
Johannes Berg2a94fe42013-11-19 15:19:39 +01001442 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001443
1444 kfree(ctx);
1445
1446 return;
1447
1448nla_put_failure:
1449 genlmsg_cancel(msg, hdr);
1450free_msg:
1451 nlmsg_free(msg);
1452 kfree(ctx);
1453
1454 return;
1455}
1456
1457static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1458{
1459 struct nfc_dev *dev;
1460 struct se_io_ctx *ctx;
1461 u32 dev_idx, se_idx;
1462 u8 *apdu;
1463 size_t apdu_len;
1464
1465 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1466 !info->attrs[NFC_ATTR_SE_INDEX] ||
1467 !info->attrs[NFC_ATTR_SE_APDU])
1468 return -EINVAL;
1469
1470 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1471 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1472
1473 dev = nfc_get_device(dev_idx);
1474 if (!dev)
1475 return -ENODEV;
1476
1477 if (!dev->ops || !dev->ops->se_io)
1478 return -ENOTSUPP;
1479
1480 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1481 if (apdu_len == 0)
1482 return -EINVAL;
1483
1484 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1485 if (!apdu)
1486 return -EINVAL;
1487
1488 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1489 if (!ctx)
1490 return -ENOMEM;
1491
1492 ctx->dev_idx = dev_idx;
1493 ctx->se_idx = se_idx;
1494
Christophe Ricardcd96db62014-12-02 21:27:51 +01001495 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001496}
1497
Samuel Ortiz9e580952014-10-14 02:19:46 +02001498static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1499 struct genl_info *info)
1500{
1501 struct nfc_dev *dev;
1502 struct nfc_vendor_cmd *cmd;
1503 u32 dev_idx, vid, subcmd;
1504 u8 *data;
1505 size_t data_len;
1506 int i;
1507
1508 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1509 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1510 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1511 return -EINVAL;
1512
1513 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1514 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1515 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1516
1517 dev = nfc_get_device(dev_idx);
1518 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1519 return -ENODEV;
1520
1521 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1522 if (data) {
1523 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1524 if (data_len == 0)
1525 return -EINVAL;
1526 } else {
1527 data_len = 0;
1528 }
1529
1530 for (i = 0; i < dev->n_vendor_cmds; i++) {
1531 cmd = &dev->vendor_cmds[i];
1532
1533 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1534 continue;
1535
1536 return cmd->doit(dev, data, data_len);
1537 }
1538
1539 return -EOPNOTSUPP;
1540}
1541
Johannes Berg4534de82013-11-14 17:14:46 +01001542static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001543 {
1544 .cmd = NFC_CMD_GET_DEVICE,
1545 .doit = nfc_genl_get_device,
1546 .dumpit = nfc_genl_dump_devices,
1547 .done = nfc_genl_dump_devices_done,
1548 .policy = nfc_genl_policy,
1549 },
1550 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001551 .cmd = NFC_CMD_DEV_UP,
1552 .doit = nfc_genl_dev_up,
1553 .policy = nfc_genl_policy,
1554 },
1555 {
1556 .cmd = NFC_CMD_DEV_DOWN,
1557 .doit = nfc_genl_dev_down,
1558 .policy = nfc_genl_policy,
1559 },
1560 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001561 .cmd = NFC_CMD_START_POLL,
1562 .doit = nfc_genl_start_poll,
1563 .policy = nfc_genl_policy,
1564 },
1565 {
1566 .cmd = NFC_CMD_STOP_POLL,
1567 .doit = nfc_genl_stop_poll,
1568 .policy = nfc_genl_policy,
1569 },
1570 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001571 .cmd = NFC_CMD_DEP_LINK_UP,
1572 .doit = nfc_genl_dep_link_up,
1573 .policy = nfc_genl_policy,
1574 },
1575 {
1576 .cmd = NFC_CMD_DEP_LINK_DOWN,
1577 .doit = nfc_genl_dep_link_down,
1578 .policy = nfc_genl_policy,
1579 },
1580 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001581 .cmd = NFC_CMD_GET_TARGET,
1582 .dumpit = nfc_genl_dump_targets,
1583 .done = nfc_genl_dump_targets_done,
1584 .policy = nfc_genl_policy,
1585 },
Thierry Escande52feb442012-10-17 14:43:39 +02001586 {
1587 .cmd = NFC_CMD_LLC_GET_PARAMS,
1588 .doit = nfc_genl_llc_get_params,
1589 .policy = nfc_genl_policy,
1590 },
1591 {
1592 .cmd = NFC_CMD_LLC_SET_PARAMS,
1593 .doit = nfc_genl_llc_set_params,
1594 .policy = nfc_genl_policy,
1595 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001596 {
1597 .cmd = NFC_CMD_LLC_SDREQ,
1598 .doit = nfc_genl_llc_sdreq,
1599 .policy = nfc_genl_policy,
1600 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001601 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001602 .cmd = NFC_CMD_FW_DOWNLOAD,
1603 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001604 .policy = nfc_genl_policy,
1605 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001606 {
1607 .cmd = NFC_CMD_ENABLE_SE,
1608 .doit = nfc_genl_enable_se,
1609 .policy = nfc_genl_policy,
1610 },
1611 {
1612 .cmd = NFC_CMD_DISABLE_SE,
1613 .doit = nfc_genl_disable_se,
1614 .policy = nfc_genl_policy,
1615 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001616 {
1617 .cmd = NFC_CMD_GET_SE,
1618 .dumpit = nfc_genl_dump_ses,
1619 .done = nfc_genl_dump_ses_done,
1620 .policy = nfc_genl_policy,
1621 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001622 {
1623 .cmd = NFC_CMD_SE_IO,
1624 .doit = nfc_genl_se_io,
1625 .policy = nfc_genl_policy,
1626 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001627 {
1628 .cmd = NFC_CMD_ACTIVATE_TARGET,
1629 .doit = nfc_genl_activate_target,
1630 .policy = nfc_genl_policy,
1631 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001632 {
1633 .cmd = NFC_CMD_VENDOR,
1634 .doit = nfc_genl_vendor_cmd,
1635 .policy = nfc_genl_policy,
1636 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001637};
1638
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001639
1640struct urelease_work {
1641 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001642 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001643};
1644
1645static void nfc_urelease_event_work(struct work_struct *work)
1646{
1647 struct urelease_work *w = container_of(work, struct urelease_work, w);
1648 struct class_dev_iter iter;
1649 struct nfc_dev *dev;
1650
John W. Linvillec4876062012-09-28 11:11:16 -04001651 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001652
1653 mutex_lock(&nfc_devlist_mutex);
1654
1655 nfc_device_iter_init(&iter);
1656 dev = nfc_device_iter_next(&iter);
1657
1658 while (dev) {
1659 mutex_lock(&dev->genl_data.genl_data_mutex);
1660
John W. Linvillec4876062012-09-28 11:11:16 -04001661 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001662 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001663 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001664 }
1665
1666 mutex_unlock(&dev->genl_data.genl_data_mutex);
1667
1668 dev = nfc_device_iter_next(&iter);
1669 }
1670
1671 nfc_device_iter_exit(&iter);
1672
1673 mutex_unlock(&nfc_devlist_mutex);
1674
1675 kfree(w);
1676}
1677
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001678static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001679 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001680{
1681 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001682 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001683
1684 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1685 goto out;
1686
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001688
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001689 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1690 if (w) {
1691 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001692 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001693 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001694 }
1695
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001696out:
1697 return NOTIFY_DONE;
1698}
1699
1700void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1701{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001702 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001703 mutex_init(&genl_data->genl_data_mutex);
1704}
1705
1706void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1707{
1708 mutex_destroy(&genl_data->genl_data_mutex);
1709}
1710
1711static struct notifier_block nl_notifier = {
1712 .notifier_call = nfc_genl_rcv_nl_event,
1713};
1714
1715/**
1716 * nfc_genl_init() - Initialize netlink interface
1717 *
1718 * This initialization function registers the nfc netlink family.
1719 */
1720int __init nfc_genl_init(void)
1721{
1722 int rc;
1723
Johannes Berg2a94fe42013-11-19 15:19:39 +01001724 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1725 nfc_genl_ops,
1726 nfc_genl_mcgrps);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001727 if (rc)
1728 return rc;
1729
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001730 netlink_register_notifier(&nl_notifier);
1731
Johannes Berg2a94fe42013-11-19 15:19:39 +01001732 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001733}
1734
1735/**
1736 * nfc_genl_exit() - Deinitialize netlink interface
1737 *
1738 * This exit function unregisters the nfc netlink family.
1739 */
1740void nfc_genl_exit(void)
1741{
1742 netlink_unregister_notifier(&nl_notifier);
1743 genl_unregister_family(&nfc_genl_family);
1744}