blob: a3dee05cb64bbb47920f3f9099edf093460c1823 [file] [log] [blame]
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
Samuel Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080025
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030026#include <net/genetlink.h>
27#include <linux/nfc.h>
28#include <linux/slab.h>
29
30#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020031#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020032
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030033static struct genl_multicast_group nfc_genl_event_mcgrp = {
34 .name = NFC_GENL_MCAST_EVENT_NAME,
35};
36
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020037static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030038 .id = GENL_ID_GENERATE,
39 .hdrsize = 0,
40 .name = NFC_GENL_NAME,
41 .version = NFC_GENL_VERSION,
42 .maxattr = NFC_ATTR_MAX,
43};
44
45static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
48 .len = NFC_DEVICE_NAME_MAXSIZE },
49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010050 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010052 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020053 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010055 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010058 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020059 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
60 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020061 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010062};
63
64static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
65 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
66 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030067};
68
69static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010070 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030071{
72 void *hdr;
73
Eric W. Biederman15e47302012-09-07 20:12:54 +000074 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010075 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030076 if (!hdr)
77 return -EMSGSIZE;
78
79 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
80
David S. Miller1e6428d2012-03-29 23:23:57 -040081 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
82 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
83 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
84 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
85 goto nla_put_failure;
86 if (target->nfcid1_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
88 target->nfcid1))
89 goto nla_put_failure;
90 if (target->sensb_res_len > 0 &&
91 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
92 target->sensb_res))
93 goto nla_put_failure;
94 if (target->sensf_res_len > 0 &&
95 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
96 target->sensf_res))
97 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030098
99 return genlmsg_end(msg, hdr);
100
101nla_put_failure:
102 genlmsg_cancel(msg, hdr);
103 return -EMSGSIZE;
104}
105
106static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
107{
108 struct nfc_dev *dev;
109 int rc;
110 u32 idx;
111
112 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100113 nfc_genl_family.attrbuf,
114 nfc_genl_family.maxattr,
115 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300116 if (rc < 0)
117 return ERR_PTR(rc);
118
119 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
120 return ERR_PTR(-EINVAL);
121
122 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
123
124 dev = nfc_get_device(idx);
125 if (!dev)
126 return ERR_PTR(-ENODEV);
127
128 return dev;
129}
130
131static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100132 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300133{
134 int i = cb->args[0];
135 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
136 int rc;
137
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300138 if (!dev) {
139 dev = __get_device_from_cb(cb);
140 if (IS_ERR(dev))
141 return PTR_ERR(dev);
142
143 cb->args[1] = (long) dev;
144 }
145
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200146 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300147
148 cb->seq = dev->targets_generation;
149
150 while (i < dev->n_targets) {
151 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100152 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300153 if (rc < 0)
154 break;
155
156 i++;
157 }
158
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200159 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300160
161 cb->args[0] = i;
162
163 return skb->len;
164}
165
166static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
167{
168 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
169
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300170 if (dev)
171 nfc_put_device(dev);
172
173 return 0;
174}
175
176int nfc_genl_targets_found(struct nfc_dev *dev)
177{
178 struct sk_buff *msg;
179 void *hdr;
180
Eric W. Biederman15e47302012-09-07 20:12:54 +0000181 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300182
Thomas Graf58050fc2012-06-28 03:57:45 +0000183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300184 if (!msg)
185 return -ENOMEM;
186
187 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100188 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300189 if (!hdr)
190 goto free_msg;
191
David S. Miller1e6428d2012-03-29 23:23:57 -0400192 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
193 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300194
195 genlmsg_end(msg, hdr);
196
197 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
198
199nla_put_failure:
200 genlmsg_cancel(msg, hdr);
201free_msg:
202 nlmsg_free(msg);
203 return -EMSGSIZE;
204}
205
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200206int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
207{
208 struct sk_buff *msg;
209 void *hdr;
210
Thomas Graf58050fc2012-06-28 03:57:45 +0000211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200212 if (!msg)
213 return -ENOMEM;
214
215 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
216 NFC_EVENT_TARGET_LOST);
217 if (!hdr)
218 goto free_msg;
219
John W. Linville59ef43e2012-04-18 14:17:13 -0400220 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
221 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
222 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200223
224 genlmsg_end(msg, hdr);
225
226 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
227
228 return 0;
229
230nla_put_failure:
231 genlmsg_cancel(msg, hdr);
232free_msg:
233 nlmsg_free(msg);
234 return -EMSGSIZE;
235}
236
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200237int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
238{
239 struct sk_buff *msg;
240 void *hdr;
241
Thomas Graf58050fc2012-06-28 03:57:45 +0000242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200243 if (!msg)
244 return -ENOMEM;
245
246 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
247 NFC_EVENT_TM_ACTIVATED);
248 if (!hdr)
249 goto free_msg;
250
251 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
252 goto nla_put_failure;
253 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
254 goto nla_put_failure;
255
256 genlmsg_end(msg, hdr);
257
258 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
259
260 return 0;
261
262nla_put_failure:
263 genlmsg_cancel(msg, hdr);
264free_msg:
265 nlmsg_free(msg);
266 return -EMSGSIZE;
267}
268
269int nfc_genl_tm_deactivated(struct nfc_dev *dev)
270{
271 struct sk_buff *msg;
272 void *hdr;
273
Thomas Graf58050fc2012-06-28 03:57:45 +0000274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200275 if (!msg)
276 return -ENOMEM;
277
278 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
279 NFC_EVENT_TM_DEACTIVATED);
280 if (!hdr)
281 goto free_msg;
282
283 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
284 goto nla_put_failure;
285
286 genlmsg_end(msg, hdr);
287
288 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
289
290 return 0;
291
292nla_put_failure:
293 genlmsg_cancel(msg, hdr);
294free_msg:
295 nlmsg_free(msg);
296 return -EMSGSIZE;
297}
298
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300299int nfc_genl_device_added(struct nfc_dev *dev)
300{
301 struct sk_buff *msg;
302 void *hdr;
303
Thomas Graf58050fc2012-06-28 03:57:45 +0000304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300305 if (!msg)
306 return -ENOMEM;
307
308 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100309 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300310 if (!hdr)
311 goto free_msg;
312
David S. Miller1e6428d2012-03-29 23:23:57 -0400313 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
314 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
315 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
316 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
317 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300318
319 genlmsg_end(msg, hdr);
320
321 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
322
323 return 0;
324
325nla_put_failure:
326 genlmsg_cancel(msg, hdr);
327free_msg:
328 nlmsg_free(msg);
329 return -EMSGSIZE;
330}
331
332int nfc_genl_device_removed(struct nfc_dev *dev)
333{
334 struct sk_buff *msg;
335 void *hdr;
336
Thomas Graf58050fc2012-06-28 03:57:45 +0000337 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300338 if (!msg)
339 return -ENOMEM;
340
341 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100342 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300343 if (!hdr)
344 goto free_msg;
345
David S. Miller1e6428d2012-03-29 23:23:57 -0400346 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
347 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300348
349 genlmsg_end(msg, hdr);
350
351 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
352
353 return 0;
354
355nla_put_failure:
356 genlmsg_cancel(msg, hdr);
357free_msg:
358 nlmsg_free(msg);
359 return -EMSGSIZE;
360}
361
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100362int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
363{
364 struct sk_buff *msg;
365 struct nlattr *sdp_attr, *uri_attr;
366 struct nfc_llcp_sdp_tlv *sdres;
367 struct hlist_node *n;
368 void *hdr;
369 int rc = -EMSGSIZE;
370 int i;
371
372 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
373 if (!msg)
374 return -ENOMEM;
375
376 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
377 NFC_EVENT_LLC_SDRES);
378 if (!hdr)
379 goto free_msg;
380
381 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
382 goto nla_put_failure;
383
384 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
385 if (sdp_attr == NULL) {
386 rc = -ENOMEM;
387 goto nla_put_failure;
388 }
389
390 i = 1;
391 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
392 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
393
394 uri_attr = nla_nest_start(msg, i++);
395 if (uri_attr == NULL) {
396 rc = -ENOMEM;
397 goto nla_put_failure;
398 }
399
400 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
401 goto nla_put_failure;
402
403 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
404 goto nla_put_failure;
405
406 nla_nest_end(msg, uri_attr);
407
408 hlist_del(&sdres->node);
409
410 nfc_llcp_free_sdp_tlv(sdres);
411 }
412
413 nla_nest_end(msg, sdp_attr);
414
415 genlmsg_end(msg, hdr);
416
417 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
418
419nla_put_failure:
420 genlmsg_cancel(msg, hdr);
421
422free_msg:
423 nlmsg_free(msg);
424
425 nfc_llcp_free_sdp_tlv_list(sdres_list);
426
427 return rc;
428}
429
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200430int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
431{
432 struct sk_buff *msg;
433 void *hdr;
434
435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
436 if (!msg)
437 return -ENOMEM;
438
439 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
440 NFC_EVENT_SE_ADDED);
441 if (!hdr)
442 goto free_msg;
443
444 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
445 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
446 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
447 goto nla_put_failure;
448
449 genlmsg_end(msg, hdr);
450
451 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
452
453 return 0;
454
455nla_put_failure:
456 genlmsg_cancel(msg, hdr);
457free_msg:
458 nlmsg_free(msg);
459 return -EMSGSIZE;
460}
461
462int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
463{
464 struct sk_buff *msg;
465 void *hdr;
466
467 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
468 if (!msg)
469 return -ENOMEM;
470
471 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
472 NFC_EVENT_SE_REMOVED);
473 if (!hdr)
474 goto free_msg;
475
476 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
477 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
478 goto nla_put_failure;
479
480 genlmsg_end(msg, hdr);
481
482 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
483
484 return 0;
485
486nla_put_failure:
487 genlmsg_cancel(msg, hdr);
488free_msg:
489 nlmsg_free(msg);
490 return -EMSGSIZE;
491}
492
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300493static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000494 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100495 struct netlink_callback *cb,
496 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300497{
498 void *hdr;
499
Eric W. Biederman15e47302012-09-07 20:12:54 +0000500 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100501 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300502 if (!hdr)
503 return -EMSGSIZE;
504
505 if (cb)
506 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
507
David S. Miller1e6428d2012-03-29 23:23:57 -0400508 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
509 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
510 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200511 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
512 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400513 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300514
515 return genlmsg_end(msg, hdr);
516
517nla_put_failure:
518 genlmsg_cancel(msg, hdr);
519 return -EMSGSIZE;
520}
521
522static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100523 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300524{
525 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
526 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
527 bool first_call = false;
528
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300529 if (!iter) {
530 first_call = true;
531 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
532 if (!iter)
533 return -ENOMEM;
534 cb->args[0] = (long) iter;
535 }
536
537 mutex_lock(&nfc_devlist_mutex);
538
539 cb->seq = nfc_devlist_generation;
540
541 if (first_call) {
542 nfc_device_iter_init(iter);
543 dev = nfc_device_iter_next(iter);
544 }
545
546 while (dev) {
547 int rc;
548
Eric W. Biederman15e47302012-09-07 20:12:54 +0000549 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100550 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300551 if (rc < 0)
552 break;
553
554 dev = nfc_device_iter_next(iter);
555 }
556
557 mutex_unlock(&nfc_devlist_mutex);
558
559 cb->args[1] = (long) dev;
560
561 return skb->len;
562}
563
564static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
565{
566 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
567
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300568 nfc_device_iter_exit(iter);
569 kfree(iter);
570
571 return 0;
572}
573
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100574int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100575 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100576{
577 struct sk_buff *msg;
578 void *hdr;
579
580 pr_debug("DEP link is up\n");
581
Thomas Graf58050fc2012-06-28 03:57:45 +0000582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100583 if (!msg)
584 return -ENOMEM;
585
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100586 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100587 if (!hdr)
588 goto free_msg;
589
David S. Miller1e6428d2012-03-29 23:23:57 -0400590 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
591 goto nla_put_failure;
592 if (rf_mode == NFC_RF_INITIATOR &&
593 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
594 goto nla_put_failure;
595 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
596 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
597 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100598
599 genlmsg_end(msg, hdr);
600
601 dev->dep_link_up = true;
602
603 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
604
605 return 0;
606
607nla_put_failure:
608 genlmsg_cancel(msg, hdr);
609free_msg:
610 nlmsg_free(msg);
611 return -EMSGSIZE;
612}
613
614int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
615{
616 struct sk_buff *msg;
617 void *hdr;
618
619 pr_debug("DEP link is down\n");
620
Thomas Graf58050fc2012-06-28 03:57:45 +0000621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100622 if (!msg)
623 return -ENOMEM;
624
625 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100626 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100627 if (!hdr)
628 goto free_msg;
629
David S. Miller1e6428d2012-03-29 23:23:57 -0400630 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
631 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100632
633 genlmsg_end(msg, hdr);
634
635 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
636
637 return 0;
638
639nla_put_failure:
640 genlmsg_cancel(msg, hdr);
641free_msg:
642 nlmsg_free(msg);
643 return -EMSGSIZE;
644}
645
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300646static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
647{
648 struct sk_buff *msg;
649 struct nfc_dev *dev;
650 u32 idx;
651 int rc = -ENOBUFS;
652
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300653 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
654 return -EINVAL;
655
656 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
657
658 dev = nfc_get_device(idx);
659 if (!dev)
660 return -ENODEV;
661
Thomas Graf58050fc2012-06-28 03:57:45 +0000662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300663 if (!msg) {
664 rc = -ENOMEM;
665 goto out_putdev;
666 }
667
Eric W. Biederman15e47302012-09-07 20:12:54 +0000668 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100669 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300670 if (rc < 0)
671 goto out_free;
672
673 nfc_put_device(dev);
674
675 return genlmsg_reply(msg, info);
676
677out_free:
678 nlmsg_free(msg);
679out_putdev:
680 nfc_put_device(dev);
681 return rc;
682}
683
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300684static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
685{
686 struct nfc_dev *dev;
687 int rc;
688 u32 idx;
689
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300690 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
691 return -EINVAL;
692
693 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
694
695 dev = nfc_get_device(idx);
696 if (!dev)
697 return -ENODEV;
698
699 rc = nfc_dev_up(dev);
700
701 nfc_put_device(dev);
702 return rc;
703}
704
705static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
706{
707 struct nfc_dev *dev;
708 int rc;
709 u32 idx;
710
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300711 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
712 return -EINVAL;
713
714 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
715
716 dev = nfc_get_device(idx);
717 if (!dev)
718 return -ENODEV;
719
720 rc = nfc_dev_down(dev);
721
722 nfc_put_device(dev);
723 return rc;
724}
725
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300726static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
727{
728 struct nfc_dev *dev;
729 int rc;
730 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200731 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300732
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100733 pr_debug("Poll start\n");
734
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300735 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200736 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
737 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200738 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300739 return -EINVAL;
740
741 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200742
743 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
744 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200745
746 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
747 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200748 else if (info->attrs[NFC_ATTR_PROTOCOLS])
749 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300750
751 dev = nfc_get_device(idx);
752 if (!dev)
753 return -ENODEV;
754
755 mutex_lock(&dev->genl_data.genl_data_mutex);
756
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200757 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300758 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000759 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300760
761 mutex_unlock(&dev->genl_data.genl_data_mutex);
762
763 nfc_put_device(dev);
764 return rc;
765}
766
767static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
768{
769 struct nfc_dev *dev;
770 int rc;
771 u32 idx;
772
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300773 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
774 return -EINVAL;
775
776 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
777
778 dev = nfc_get_device(idx);
779 if (!dev)
780 return -ENODEV;
781
Samuel Ortiza831b912012-06-28 16:41:57 +0200782 device_lock(&dev->dev);
783
784 if (!dev->polling) {
785 device_unlock(&dev->dev);
786 return -EINVAL;
787 }
788
789 device_unlock(&dev->dev);
790
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300791 mutex_lock(&dev->genl_data.genl_data_mutex);
792
Eric W. Biederman15e47302012-09-07 20:12:54 +0000793 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794 rc = -EBUSY;
795 goto out;
796 }
797
798 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000799 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300800
801out:
802 mutex_unlock(&dev->genl_data.genl_data_mutex);
803 nfc_put_device(dev);
804 return rc;
805}
806
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100807static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
808{
809 struct nfc_dev *dev;
810 int rc, tgt_idx;
811 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100812 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100813
814 pr_debug("DEP link up\n");
815
816 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100817 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100818 return -EINVAL;
819
820 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
821 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
822 tgt_idx = NFC_TARGET_IDX_ANY;
823 else
824 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
825
826 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100827
828 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
829 return -EINVAL;
830
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100831 dev = nfc_get_device(idx);
832 if (!dev)
833 return -ENODEV;
834
Samuel Ortiz47807d32012-03-05 01:03:50 +0100835 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100836
837 nfc_put_device(dev);
838
839 return rc;
840}
841
842static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
843{
844 struct nfc_dev *dev;
845 int rc;
846 u32 idx;
847
848 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
849 return -EINVAL;
850
851 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
852
853 dev = nfc_get_device(idx);
854 if (!dev)
855 return -ENODEV;
856
857 rc = nfc_dep_link_down(dev);
858
859 nfc_put_device(dev);
860 return rc;
861}
862
Thierry Escande52feb442012-10-17 14:43:39 +0200863static int nfc_genl_send_params(struct sk_buff *msg,
864 struct nfc_llcp_local *local,
865 u32 portid, u32 seq)
866{
867 void *hdr;
868
869 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
870 NFC_CMD_LLC_GET_PARAMS);
871 if (!hdr)
872 return -EMSGSIZE;
873
874 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
875 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
876 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
877 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
878 goto nla_put_failure;
879
880 return genlmsg_end(msg, hdr);
881
882nla_put_failure:
883
884 genlmsg_cancel(msg, hdr);
885 return -EMSGSIZE;
886}
887
888static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
889{
890 struct nfc_dev *dev;
891 struct nfc_llcp_local *local;
892 int rc = 0;
893 struct sk_buff *msg = NULL;
894 u32 idx;
895
896 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
897 return -EINVAL;
898
899 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
900
901 dev = nfc_get_device(idx);
902 if (!dev)
903 return -ENODEV;
904
905 device_lock(&dev->dev);
906
907 local = nfc_llcp_find_local(dev);
908 if (!local) {
909 rc = -ENODEV;
910 goto exit;
911 }
912
913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
914 if (!msg) {
915 rc = -ENOMEM;
916 goto exit;
917 }
918
919 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
920
921exit:
922 device_unlock(&dev->dev);
923
924 nfc_put_device(dev);
925
926 if (rc < 0) {
927 if (msg)
928 nlmsg_free(msg);
929
930 return rc;
931 }
932
933 return genlmsg_reply(msg, info);
934}
935
936static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
937{
938 struct nfc_dev *dev;
939 struct nfc_llcp_local *local;
940 u8 rw = 0;
941 u16 miux = 0;
942 u32 idx;
943 int rc = 0;
944
945 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
946 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
947 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
948 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
949 return -EINVAL;
950
951 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
952 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
953
954 if (rw > LLCP_MAX_RW)
955 return -EINVAL;
956 }
957
958 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
959 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
960
961 if (miux > LLCP_MAX_MIUX)
962 return -EINVAL;
963 }
964
965 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
966
967 dev = nfc_get_device(idx);
968 if (!dev)
969 return -ENODEV;
970
971 device_lock(&dev->dev);
972
973 local = nfc_llcp_find_local(dev);
974 if (!local) {
975 nfc_put_device(dev);
976 rc = -ENODEV;
977 goto exit;
978 }
979
980 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
981 if (dev->dep_link_up) {
982 rc = -EINPROGRESS;
983 goto exit;
984 }
985
986 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
987 }
988
989 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
990 local->rw = rw;
991
992 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
993 local->miux = cpu_to_be16(miux);
994
995exit:
996 device_unlock(&dev->dev);
997
998 nfc_put_device(dev);
999
1000 return rc;
1001}
1002
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001003static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1004{
1005 struct nfc_dev *dev;
1006 struct nfc_llcp_local *local;
1007 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1008 u32 idx;
1009 u8 tid;
1010 char *uri;
1011 int rc = 0, rem;
1012 size_t uri_len, tlvs_len;
1013 struct hlist_head sdreq_list;
1014 struct nfc_llcp_sdp_tlv *sdreq;
1015
1016 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1017 !info->attrs[NFC_ATTR_LLC_SDP])
1018 return -EINVAL;
1019
1020 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1021
1022 dev = nfc_get_device(idx);
1023 if (!dev) {
1024 rc = -ENODEV;
1025 goto exit;
1026 }
1027
1028 device_lock(&dev->dev);
1029
1030 if (dev->dep_link_up == false) {
1031 rc = -ENOLINK;
1032 goto exit;
1033 }
1034
1035 local = nfc_llcp_find_local(dev);
1036 if (!local) {
1037 nfc_put_device(dev);
1038 rc = -ENODEV;
1039 goto exit;
1040 }
1041
1042 INIT_HLIST_HEAD(&sdreq_list);
1043
1044 tlvs_len = 0;
1045
1046 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1047 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1048 nfc_sdp_genl_policy);
1049
1050 if (rc != 0) {
1051 rc = -EINVAL;
1052 goto exit;
1053 }
1054
1055 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1056 continue;
1057
1058 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1059 if (uri_len == 0)
1060 continue;
1061
1062 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1063 if (uri == NULL || *uri == 0)
1064 continue;
1065
1066 tid = local->sdreq_next_tid++;
1067
1068 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1069 if (sdreq == NULL) {
1070 rc = -ENOMEM;
1071 goto exit;
1072 }
1073
1074 tlvs_len += sdreq->tlv_len;
1075
1076 hlist_add_head(&sdreq->node, &sdreq_list);
1077 }
1078
1079 if (hlist_empty(&sdreq_list)) {
1080 rc = -EINVAL;
1081 goto exit;
1082 }
1083
1084 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1085exit:
1086 device_unlock(&dev->dev);
1087
1088 nfc_put_device(dev);
1089
1090 return rc;
1091}
1092
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001093static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001094{
1095 struct nfc_dev *dev;
1096 int rc;
1097 u32 idx;
1098 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1099
1100 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1101 return -EINVAL;
1102
1103 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1104
1105 dev = nfc_get_device(idx);
1106 if (!dev)
1107 return -ENODEV;
1108
1109 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1110 sizeof(firmware_name));
1111
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001112 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001113
1114 nfc_put_device(dev);
1115 return rc;
1116}
1117
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001118int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1119 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001120{
1121 struct sk_buff *msg;
1122 void *hdr;
1123
1124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1125 if (!msg)
1126 return -ENOMEM;
1127
1128 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001129 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001130 if (!hdr)
1131 goto free_msg;
1132
1133 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001134 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001135 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1136 goto nla_put_failure;
1137
1138 genlmsg_end(msg, hdr);
1139
1140 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1141
1142 return 0;
1143
1144nla_put_failure:
1145 genlmsg_cancel(msg, hdr);
1146free_msg:
1147 nlmsg_free(msg);
1148 return -EMSGSIZE;
1149}
1150
Samuel Ortizbe085652013-05-10 17:07:32 +02001151static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1152{
1153 struct nfc_dev *dev;
1154 int rc;
1155 u32 idx, se_idx;
1156
1157 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1158 !info->attrs[NFC_ATTR_SE_INDEX])
1159 return -EINVAL;
1160
1161 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1162 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1163
1164 dev = nfc_get_device(idx);
1165 if (!dev)
1166 return -ENODEV;
1167
1168 rc = nfc_enable_se(dev, se_idx);
1169
1170 nfc_put_device(dev);
1171 return rc;
1172}
1173
1174static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1175{
1176 struct nfc_dev *dev;
1177 int rc;
1178 u32 idx, se_idx;
1179
1180 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1181 !info->attrs[NFC_ATTR_SE_INDEX])
1182 return -EINVAL;
1183
1184 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1185 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1186
1187 dev = nfc_get_device(idx);
1188 if (!dev)
1189 return -ENODEV;
1190
1191 rc = nfc_disable_se(dev, se_idx);
1192
1193 nfc_put_device(dev);
1194 return rc;
1195}
1196
Samuel Ortizac22ac42013-07-24 18:10:50 +02001197static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1198 u32 portid, u32 seq,
1199 struct netlink_callback *cb,
1200 int flags)
1201{
1202 void *hdr;
1203 struct nfc_se *se, *n;
1204
1205 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1206 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1207 NFC_CMD_GET_SE);
1208 if (!hdr)
1209 goto nla_put_failure;
1210
1211 if (cb)
1212 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1213
1214 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1215 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1216 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1217 goto nla_put_failure;
1218
1219 if (genlmsg_end(msg, hdr) < 0)
1220 goto nla_put_failure;
1221 }
1222
1223 return 0;
1224
1225nla_put_failure:
1226 genlmsg_cancel(msg, hdr);
1227 return -EMSGSIZE;
1228}
1229
1230static int nfc_genl_dump_ses(struct sk_buff *skb,
1231 struct netlink_callback *cb)
1232{
1233 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1234 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1235 bool first_call = false;
1236
1237 if (!iter) {
1238 first_call = true;
1239 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1240 if (!iter)
1241 return -ENOMEM;
1242 cb->args[0] = (long) iter;
1243 }
1244
1245 mutex_lock(&nfc_devlist_mutex);
1246
1247 cb->seq = nfc_devlist_generation;
1248
1249 if (first_call) {
1250 nfc_device_iter_init(iter);
1251 dev = nfc_device_iter_next(iter);
1252 }
1253
1254 while (dev) {
1255 int rc;
1256
1257 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1258 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1259 if (rc < 0)
1260 break;
1261
1262 dev = nfc_device_iter_next(iter);
1263 }
1264
1265 mutex_unlock(&nfc_devlist_mutex);
1266
1267 cb->args[1] = (long) dev;
1268
1269 return skb->len;
1270}
1271
1272static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1273{
1274 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1275
1276 nfc_device_iter_exit(iter);
1277 kfree(iter);
1278
1279 return 0;
1280}
1281
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001282struct se_io_ctx {
1283 u32 dev_idx;
1284 u32 se_idx;
1285};
1286
1287void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1288{
1289 struct se_io_ctx *ctx = context;
1290 struct sk_buff *msg;
1291 void *hdr;
1292
1293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1294 if (!msg) {
1295 kfree(ctx);
1296 return;
1297 }
1298
1299 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1300 NFC_CMD_SE_IO);
1301 if (!hdr)
1302 goto free_msg;
1303
1304 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1305 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1306 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1307 goto nla_put_failure;
1308
1309 genlmsg_end(msg, hdr);
1310
1311 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1312
1313 kfree(ctx);
1314
1315 return;
1316
1317nla_put_failure:
1318 genlmsg_cancel(msg, hdr);
1319free_msg:
1320 nlmsg_free(msg);
1321 kfree(ctx);
1322
1323 return;
1324}
1325
1326static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1327{
1328 struct nfc_dev *dev;
1329 struct se_io_ctx *ctx;
1330 u32 dev_idx, se_idx;
1331 u8 *apdu;
1332 size_t apdu_len;
1333
1334 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1335 !info->attrs[NFC_ATTR_SE_INDEX] ||
1336 !info->attrs[NFC_ATTR_SE_APDU])
1337 return -EINVAL;
1338
1339 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1340 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1341
1342 dev = nfc_get_device(dev_idx);
1343 if (!dev)
1344 return -ENODEV;
1345
1346 if (!dev->ops || !dev->ops->se_io)
1347 return -ENOTSUPP;
1348
1349 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1350 if (apdu_len == 0)
1351 return -EINVAL;
1352
1353 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1354 if (!apdu)
1355 return -EINVAL;
1356
1357 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1358 if (!ctx)
1359 return -ENOMEM;
1360
1361 ctx->dev_idx = dev_idx;
1362 ctx->se_idx = se_idx;
1363
1364 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1365}
1366
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001367static struct genl_ops nfc_genl_ops[] = {
1368 {
1369 .cmd = NFC_CMD_GET_DEVICE,
1370 .doit = nfc_genl_get_device,
1371 .dumpit = nfc_genl_dump_devices,
1372 .done = nfc_genl_dump_devices_done,
1373 .policy = nfc_genl_policy,
1374 },
1375 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001376 .cmd = NFC_CMD_DEV_UP,
1377 .doit = nfc_genl_dev_up,
1378 .policy = nfc_genl_policy,
1379 },
1380 {
1381 .cmd = NFC_CMD_DEV_DOWN,
1382 .doit = nfc_genl_dev_down,
1383 .policy = nfc_genl_policy,
1384 },
1385 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001386 .cmd = NFC_CMD_START_POLL,
1387 .doit = nfc_genl_start_poll,
1388 .policy = nfc_genl_policy,
1389 },
1390 {
1391 .cmd = NFC_CMD_STOP_POLL,
1392 .doit = nfc_genl_stop_poll,
1393 .policy = nfc_genl_policy,
1394 },
1395 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001396 .cmd = NFC_CMD_DEP_LINK_UP,
1397 .doit = nfc_genl_dep_link_up,
1398 .policy = nfc_genl_policy,
1399 },
1400 {
1401 .cmd = NFC_CMD_DEP_LINK_DOWN,
1402 .doit = nfc_genl_dep_link_down,
1403 .policy = nfc_genl_policy,
1404 },
1405 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001406 .cmd = NFC_CMD_GET_TARGET,
1407 .dumpit = nfc_genl_dump_targets,
1408 .done = nfc_genl_dump_targets_done,
1409 .policy = nfc_genl_policy,
1410 },
Thierry Escande52feb442012-10-17 14:43:39 +02001411 {
1412 .cmd = NFC_CMD_LLC_GET_PARAMS,
1413 .doit = nfc_genl_llc_get_params,
1414 .policy = nfc_genl_policy,
1415 },
1416 {
1417 .cmd = NFC_CMD_LLC_SET_PARAMS,
1418 .doit = nfc_genl_llc_set_params,
1419 .policy = nfc_genl_policy,
1420 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001421 {
1422 .cmd = NFC_CMD_LLC_SDREQ,
1423 .doit = nfc_genl_llc_sdreq,
1424 .policy = nfc_genl_policy,
1425 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001426 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001427 .cmd = NFC_CMD_FW_DOWNLOAD,
1428 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001429 .policy = nfc_genl_policy,
1430 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001431 {
1432 .cmd = NFC_CMD_ENABLE_SE,
1433 .doit = nfc_genl_enable_se,
1434 .policy = nfc_genl_policy,
1435 },
1436 {
1437 .cmd = NFC_CMD_DISABLE_SE,
1438 .doit = nfc_genl_disable_se,
1439 .policy = nfc_genl_policy,
1440 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001441 {
1442 .cmd = NFC_CMD_GET_SE,
1443 .dumpit = nfc_genl_dump_ses,
1444 .done = nfc_genl_dump_ses_done,
1445 .policy = nfc_genl_policy,
1446 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001447 {
1448 .cmd = NFC_CMD_SE_IO,
1449 .doit = nfc_genl_se_io,
1450 .policy = nfc_genl_policy,
1451 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001452};
1453
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001454
1455struct urelease_work {
1456 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001457 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001458};
1459
1460static void nfc_urelease_event_work(struct work_struct *work)
1461{
1462 struct urelease_work *w = container_of(work, struct urelease_work, w);
1463 struct class_dev_iter iter;
1464 struct nfc_dev *dev;
1465
John W. Linvillec4876062012-09-28 11:11:16 -04001466 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001467
1468 mutex_lock(&nfc_devlist_mutex);
1469
1470 nfc_device_iter_init(&iter);
1471 dev = nfc_device_iter_next(&iter);
1472
1473 while (dev) {
1474 mutex_lock(&dev->genl_data.genl_data_mutex);
1475
John W. Linvillec4876062012-09-28 11:11:16 -04001476 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001477 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001478 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001479 }
1480
1481 mutex_unlock(&dev->genl_data.genl_data_mutex);
1482
1483 dev = nfc_device_iter_next(&iter);
1484 }
1485
1486 nfc_device_iter_exit(&iter);
1487
1488 mutex_unlock(&nfc_devlist_mutex);
1489
1490 kfree(w);
1491}
1492
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001493static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001494 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001495{
1496 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001497 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001498
1499 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1500 goto out;
1501
Eric W. Biederman15e47302012-09-07 20:12:54 +00001502 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001503
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001504 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1505 if (w) {
1506 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001507 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001508 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001509 }
1510
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001511out:
1512 return NOTIFY_DONE;
1513}
1514
1515void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1516{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001517 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001518 mutex_init(&genl_data->genl_data_mutex);
1519}
1520
1521void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1522{
1523 mutex_destroy(&genl_data->genl_data_mutex);
1524}
1525
1526static struct notifier_block nl_notifier = {
1527 .notifier_call = nfc_genl_rcv_nl_event,
1528};
1529
1530/**
1531 * nfc_genl_init() - Initialize netlink interface
1532 *
1533 * This initialization function registers the nfc netlink family.
1534 */
1535int __init nfc_genl_init(void)
1536{
1537 int rc;
1538
1539 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001540 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001541 if (rc)
1542 return rc;
1543
1544 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1545
1546 netlink_register_notifier(&nl_notifier);
1547
1548 return rc;
1549}
1550
1551/**
1552 * nfc_genl_exit() - Deinitialize netlink interface
1553 *
1554 * This exit function unregisters the nfc netlink family.
1555 */
1556void nfc_genl_exit(void)
1557{
1558 netlink_unregister_notifier(&nl_notifier);
1559 genl_unregister_family(&nfc_genl_family);
1560}