blob: b05ad909778fe5978677f3f81a31360220f98eb7 [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 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010061};
62
63static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
64 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
65 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030066};
67
68static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010069 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030070{
71 void *hdr;
72
Eric W. Biederman15e47302012-09-07 20:12:54 +000073 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010074 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030075 if (!hdr)
76 return -EMSGSIZE;
77
78 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
79
David S. Miller1e6428d2012-03-29 23:23:57 -040080 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
81 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
82 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
83 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
84 goto nla_put_failure;
85 if (target->nfcid1_len > 0 &&
86 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
87 target->nfcid1))
88 goto nla_put_failure;
89 if (target->sensb_res_len > 0 &&
90 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
91 target->sensb_res))
92 goto nla_put_failure;
93 if (target->sensf_res_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
95 target->sensf_res))
96 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030097
98 return genlmsg_end(msg, hdr);
99
100nla_put_failure:
101 genlmsg_cancel(msg, hdr);
102 return -EMSGSIZE;
103}
104
105static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
106{
107 struct nfc_dev *dev;
108 int rc;
109 u32 idx;
110
111 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100112 nfc_genl_family.attrbuf,
113 nfc_genl_family.maxattr,
114 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300115 if (rc < 0)
116 return ERR_PTR(rc);
117
118 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
119 return ERR_PTR(-EINVAL);
120
121 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
122
123 dev = nfc_get_device(idx);
124 if (!dev)
125 return ERR_PTR(-ENODEV);
126
127 return dev;
128}
129
130static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100131 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300132{
133 int i = cb->args[0];
134 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
135 int rc;
136
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300137 if (!dev) {
138 dev = __get_device_from_cb(cb);
139 if (IS_ERR(dev))
140 return PTR_ERR(dev);
141
142 cb->args[1] = (long) dev;
143 }
144
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200145 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300146
147 cb->seq = dev->targets_generation;
148
149 while (i < dev->n_targets) {
150 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100151 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300152 if (rc < 0)
153 break;
154
155 i++;
156 }
157
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200158 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300159
160 cb->args[0] = i;
161
162 return skb->len;
163}
164
165static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
166{
167 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
168
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300169 if (dev)
170 nfc_put_device(dev);
171
172 return 0;
173}
174
175int nfc_genl_targets_found(struct nfc_dev *dev)
176{
177 struct sk_buff *msg;
178 void *hdr;
179
Eric W. Biederman15e47302012-09-07 20:12:54 +0000180 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300181
Thomas Graf58050fc2012-06-28 03:57:45 +0000182 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300183 if (!msg)
184 return -ENOMEM;
185
186 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100187 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300188 if (!hdr)
189 goto free_msg;
190
David S. Miller1e6428d2012-03-29 23:23:57 -0400191 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
192 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300193
194 genlmsg_end(msg, hdr);
195
196 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
197
198nla_put_failure:
199 genlmsg_cancel(msg, hdr);
200free_msg:
201 nlmsg_free(msg);
202 return -EMSGSIZE;
203}
204
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200205int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
206{
207 struct sk_buff *msg;
208 void *hdr;
209
Thomas Graf58050fc2012-06-28 03:57:45 +0000210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200211 if (!msg)
212 return -ENOMEM;
213
214 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
215 NFC_EVENT_TARGET_LOST);
216 if (!hdr)
217 goto free_msg;
218
John W. Linville59ef43e2012-04-18 14:17:13 -0400219 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
220 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
221 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200222
223 genlmsg_end(msg, hdr);
224
225 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
226
227 return 0;
228
229nla_put_failure:
230 genlmsg_cancel(msg, hdr);
231free_msg:
232 nlmsg_free(msg);
233 return -EMSGSIZE;
234}
235
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200236int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
237{
238 struct sk_buff *msg;
239 void *hdr;
240
Thomas Graf58050fc2012-06-28 03:57:45 +0000241 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200242 if (!msg)
243 return -ENOMEM;
244
245 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
246 NFC_EVENT_TM_ACTIVATED);
247 if (!hdr)
248 goto free_msg;
249
250 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
251 goto nla_put_failure;
252 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
253 goto nla_put_failure;
254
255 genlmsg_end(msg, hdr);
256
257 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
258
259 return 0;
260
261nla_put_failure:
262 genlmsg_cancel(msg, hdr);
263free_msg:
264 nlmsg_free(msg);
265 return -EMSGSIZE;
266}
267
268int nfc_genl_tm_deactivated(struct nfc_dev *dev)
269{
270 struct sk_buff *msg;
271 void *hdr;
272
Thomas Graf58050fc2012-06-28 03:57:45 +0000273 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200274 if (!msg)
275 return -ENOMEM;
276
277 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
278 NFC_EVENT_TM_DEACTIVATED);
279 if (!hdr)
280 goto free_msg;
281
282 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
283 goto nla_put_failure;
284
285 genlmsg_end(msg, hdr);
286
287 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
288
289 return 0;
290
291nla_put_failure:
292 genlmsg_cancel(msg, hdr);
293free_msg:
294 nlmsg_free(msg);
295 return -EMSGSIZE;
296}
297
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300298int nfc_genl_device_added(struct nfc_dev *dev)
299{
300 struct sk_buff *msg;
301 void *hdr;
302
Thomas Graf58050fc2012-06-28 03:57:45 +0000303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300304 if (!msg)
305 return -ENOMEM;
306
307 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100308 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300309 if (!hdr)
310 goto free_msg;
311
David S. Miller1e6428d2012-03-29 23:23:57 -0400312 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
313 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
314 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
315 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
316 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300317
318 genlmsg_end(msg, hdr);
319
320 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
321
322 return 0;
323
324nla_put_failure:
325 genlmsg_cancel(msg, hdr);
326free_msg:
327 nlmsg_free(msg);
328 return -EMSGSIZE;
329}
330
331int nfc_genl_device_removed(struct nfc_dev *dev)
332{
333 struct sk_buff *msg;
334 void *hdr;
335
Thomas Graf58050fc2012-06-28 03:57:45 +0000336 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300337 if (!msg)
338 return -ENOMEM;
339
340 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100341 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300342 if (!hdr)
343 goto free_msg;
344
David S. Miller1e6428d2012-03-29 23:23:57 -0400345 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
346 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300347
348 genlmsg_end(msg, hdr);
349
350 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
351
352 return 0;
353
354nla_put_failure:
355 genlmsg_cancel(msg, hdr);
356free_msg:
357 nlmsg_free(msg);
358 return -EMSGSIZE;
359}
360
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100361int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
362{
363 struct sk_buff *msg;
364 struct nlattr *sdp_attr, *uri_attr;
365 struct nfc_llcp_sdp_tlv *sdres;
366 struct hlist_node *n;
367 void *hdr;
368 int rc = -EMSGSIZE;
369 int i;
370
371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
372 if (!msg)
373 return -ENOMEM;
374
375 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
376 NFC_EVENT_LLC_SDRES);
377 if (!hdr)
378 goto free_msg;
379
380 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
381 goto nla_put_failure;
382
383 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
384 if (sdp_attr == NULL) {
385 rc = -ENOMEM;
386 goto nla_put_failure;
387 }
388
389 i = 1;
390 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
391 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
392
393 uri_attr = nla_nest_start(msg, i++);
394 if (uri_attr == NULL) {
395 rc = -ENOMEM;
396 goto nla_put_failure;
397 }
398
399 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
400 goto nla_put_failure;
401
402 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
403 goto nla_put_failure;
404
405 nla_nest_end(msg, uri_attr);
406
407 hlist_del(&sdres->node);
408
409 nfc_llcp_free_sdp_tlv(sdres);
410 }
411
412 nla_nest_end(msg, sdp_attr);
413
414 genlmsg_end(msg, hdr);
415
416 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
417
418nla_put_failure:
419 genlmsg_cancel(msg, hdr);
420
421free_msg:
422 nlmsg_free(msg);
423
424 nfc_llcp_free_sdp_tlv_list(sdres_list);
425
426 return rc;
427}
428
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200429int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
430{
431 struct sk_buff *msg;
432 void *hdr;
433
434 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
435 if (!msg)
436 return -ENOMEM;
437
438 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
439 NFC_EVENT_SE_ADDED);
440 if (!hdr)
441 goto free_msg;
442
443 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
444 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
445 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
446 goto nla_put_failure;
447
448 genlmsg_end(msg, hdr);
449
450 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
451
452 return 0;
453
454nla_put_failure:
455 genlmsg_cancel(msg, hdr);
456free_msg:
457 nlmsg_free(msg);
458 return -EMSGSIZE;
459}
460
461int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
462{
463 struct sk_buff *msg;
464 void *hdr;
465
466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
467 if (!msg)
468 return -ENOMEM;
469
470 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
471 NFC_EVENT_SE_REMOVED);
472 if (!hdr)
473 goto free_msg;
474
475 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
476 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
477 goto nla_put_failure;
478
479 genlmsg_end(msg, hdr);
480
481 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
482
483 return 0;
484
485nla_put_failure:
486 genlmsg_cancel(msg, hdr);
487free_msg:
488 nlmsg_free(msg);
489 return -EMSGSIZE;
490}
491
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300492static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000493 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100494 struct netlink_callback *cb,
495 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300496{
497 void *hdr;
498
Eric W. Biederman15e47302012-09-07 20:12:54 +0000499 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100500 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300501 if (!hdr)
502 return -EMSGSIZE;
503
504 if (cb)
505 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
506
David S. Miller1e6428d2012-03-29 23:23:57 -0400507 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
508 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
509 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200510 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
511 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400512 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300513
514 return genlmsg_end(msg, hdr);
515
516nla_put_failure:
517 genlmsg_cancel(msg, hdr);
518 return -EMSGSIZE;
519}
520
521static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100522 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300523{
524 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
525 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
526 bool first_call = false;
527
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300528 if (!iter) {
529 first_call = true;
530 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
531 if (!iter)
532 return -ENOMEM;
533 cb->args[0] = (long) iter;
534 }
535
536 mutex_lock(&nfc_devlist_mutex);
537
538 cb->seq = nfc_devlist_generation;
539
540 if (first_call) {
541 nfc_device_iter_init(iter);
542 dev = nfc_device_iter_next(iter);
543 }
544
545 while (dev) {
546 int rc;
547
Eric W. Biederman15e47302012-09-07 20:12:54 +0000548 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100549 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300550 if (rc < 0)
551 break;
552
553 dev = nfc_device_iter_next(iter);
554 }
555
556 mutex_unlock(&nfc_devlist_mutex);
557
558 cb->args[1] = (long) dev;
559
560 return skb->len;
561}
562
563static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
564{
565 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
566
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300567 nfc_device_iter_exit(iter);
568 kfree(iter);
569
570 return 0;
571}
572
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100573int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100574 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100575{
576 struct sk_buff *msg;
577 void *hdr;
578
579 pr_debug("DEP link is up\n");
580
Thomas Graf58050fc2012-06-28 03:57:45 +0000581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100582 if (!msg)
583 return -ENOMEM;
584
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100585 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100586 if (!hdr)
587 goto free_msg;
588
David S. Miller1e6428d2012-03-29 23:23:57 -0400589 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
590 goto nla_put_failure;
591 if (rf_mode == NFC_RF_INITIATOR &&
592 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
593 goto nla_put_failure;
594 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
595 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
596 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100597
598 genlmsg_end(msg, hdr);
599
600 dev->dep_link_up = true;
601
602 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
603
604 return 0;
605
606nla_put_failure:
607 genlmsg_cancel(msg, hdr);
608free_msg:
609 nlmsg_free(msg);
610 return -EMSGSIZE;
611}
612
613int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
614{
615 struct sk_buff *msg;
616 void *hdr;
617
618 pr_debug("DEP link is down\n");
619
Thomas Graf58050fc2012-06-28 03:57:45 +0000620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100621 if (!msg)
622 return -ENOMEM;
623
624 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100625 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100626 if (!hdr)
627 goto free_msg;
628
David S. Miller1e6428d2012-03-29 23:23:57 -0400629 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
630 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100631
632 genlmsg_end(msg, hdr);
633
634 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
635
636 return 0;
637
638nla_put_failure:
639 genlmsg_cancel(msg, hdr);
640free_msg:
641 nlmsg_free(msg);
642 return -EMSGSIZE;
643}
644
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300645static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
646{
647 struct sk_buff *msg;
648 struct nfc_dev *dev;
649 u32 idx;
650 int rc = -ENOBUFS;
651
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300652 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
653 return -EINVAL;
654
655 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
656
657 dev = nfc_get_device(idx);
658 if (!dev)
659 return -ENODEV;
660
Thomas Graf58050fc2012-06-28 03:57:45 +0000661 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300662 if (!msg) {
663 rc = -ENOMEM;
664 goto out_putdev;
665 }
666
Eric W. Biederman15e47302012-09-07 20:12:54 +0000667 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100668 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300669 if (rc < 0)
670 goto out_free;
671
672 nfc_put_device(dev);
673
674 return genlmsg_reply(msg, info);
675
676out_free:
677 nlmsg_free(msg);
678out_putdev:
679 nfc_put_device(dev);
680 return rc;
681}
682
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300683static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
684{
685 struct nfc_dev *dev;
686 int rc;
687 u32 idx;
688
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300689 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
690 return -EINVAL;
691
692 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
693
694 dev = nfc_get_device(idx);
695 if (!dev)
696 return -ENODEV;
697
698 rc = nfc_dev_up(dev);
699
700 nfc_put_device(dev);
701 return rc;
702}
703
704static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
705{
706 struct nfc_dev *dev;
707 int rc;
708 u32 idx;
709
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300710 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
711 return -EINVAL;
712
713 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
714
715 dev = nfc_get_device(idx);
716 if (!dev)
717 return -ENODEV;
718
719 rc = nfc_dev_down(dev);
720
721 nfc_put_device(dev);
722 return rc;
723}
724
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300725static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
726{
727 struct nfc_dev *dev;
728 int rc;
729 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200730 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300731
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100732 pr_debug("Poll start\n");
733
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300734 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200735 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
736 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200737 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300738 return -EINVAL;
739
740 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200741
742 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
743 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200744
745 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
746 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200747 else if (info->attrs[NFC_ATTR_PROTOCOLS])
748 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300749
750 dev = nfc_get_device(idx);
751 if (!dev)
752 return -ENODEV;
753
754 mutex_lock(&dev->genl_data.genl_data_mutex);
755
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200756 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300757 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000758 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300759
760 mutex_unlock(&dev->genl_data.genl_data_mutex);
761
762 nfc_put_device(dev);
763 return rc;
764}
765
766static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
767{
768 struct nfc_dev *dev;
769 int rc;
770 u32 idx;
771
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -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
Samuel Ortiza831b912012-06-28 16:41:57 +0200781 device_lock(&dev->dev);
782
783 if (!dev->polling) {
784 device_unlock(&dev->dev);
785 return -EINVAL;
786 }
787
788 device_unlock(&dev->dev);
789
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300790 mutex_lock(&dev->genl_data.genl_data_mutex);
791
Eric W. Biederman15e47302012-09-07 20:12:54 +0000792 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300793 rc = -EBUSY;
794 goto out;
795 }
796
797 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000798 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300799
800out:
801 mutex_unlock(&dev->genl_data.genl_data_mutex);
802 nfc_put_device(dev);
803 return rc;
804}
805
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100806static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
807{
808 struct nfc_dev *dev;
809 int rc, tgt_idx;
810 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100811 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100812
813 pr_debug("DEP link up\n");
814
815 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100816 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100817 return -EINVAL;
818
819 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
820 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
821 tgt_idx = NFC_TARGET_IDX_ANY;
822 else
823 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
824
825 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100826
827 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
828 return -EINVAL;
829
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100830 dev = nfc_get_device(idx);
831 if (!dev)
832 return -ENODEV;
833
Samuel Ortiz47807d32012-03-05 01:03:50 +0100834 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100835
836 nfc_put_device(dev);
837
838 return rc;
839}
840
841static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
842{
843 struct nfc_dev *dev;
844 int rc;
845 u32 idx;
846
847 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
848 return -EINVAL;
849
850 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
851
852 dev = nfc_get_device(idx);
853 if (!dev)
854 return -ENODEV;
855
856 rc = nfc_dep_link_down(dev);
857
858 nfc_put_device(dev);
859 return rc;
860}
861
Thierry Escande52feb442012-10-17 14:43:39 +0200862static int nfc_genl_send_params(struct sk_buff *msg,
863 struct nfc_llcp_local *local,
864 u32 portid, u32 seq)
865{
866 void *hdr;
867
868 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
869 NFC_CMD_LLC_GET_PARAMS);
870 if (!hdr)
871 return -EMSGSIZE;
872
873 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
874 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
875 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
876 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
877 goto nla_put_failure;
878
879 return genlmsg_end(msg, hdr);
880
881nla_put_failure:
882
883 genlmsg_cancel(msg, hdr);
884 return -EMSGSIZE;
885}
886
887static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
888{
889 struct nfc_dev *dev;
890 struct nfc_llcp_local *local;
891 int rc = 0;
892 struct sk_buff *msg = NULL;
893 u32 idx;
894
895 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
896 return -EINVAL;
897
898 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
899
900 dev = nfc_get_device(idx);
901 if (!dev)
902 return -ENODEV;
903
904 device_lock(&dev->dev);
905
906 local = nfc_llcp_find_local(dev);
907 if (!local) {
908 rc = -ENODEV;
909 goto exit;
910 }
911
912 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
913 if (!msg) {
914 rc = -ENOMEM;
915 goto exit;
916 }
917
918 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
919
920exit:
921 device_unlock(&dev->dev);
922
923 nfc_put_device(dev);
924
925 if (rc < 0) {
926 if (msg)
927 nlmsg_free(msg);
928
929 return rc;
930 }
931
932 return genlmsg_reply(msg, info);
933}
934
935static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
936{
937 struct nfc_dev *dev;
938 struct nfc_llcp_local *local;
939 u8 rw = 0;
940 u16 miux = 0;
941 u32 idx;
942 int rc = 0;
943
944 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
945 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
946 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
947 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
948 return -EINVAL;
949
950 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
951 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
952
953 if (rw > LLCP_MAX_RW)
954 return -EINVAL;
955 }
956
957 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
958 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
959
960 if (miux > LLCP_MAX_MIUX)
961 return -EINVAL;
962 }
963
964 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
965
966 dev = nfc_get_device(idx);
967 if (!dev)
968 return -ENODEV;
969
970 device_lock(&dev->dev);
971
972 local = nfc_llcp_find_local(dev);
973 if (!local) {
974 nfc_put_device(dev);
975 rc = -ENODEV;
976 goto exit;
977 }
978
979 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
980 if (dev->dep_link_up) {
981 rc = -EINPROGRESS;
982 goto exit;
983 }
984
985 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
986 }
987
988 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
989 local->rw = rw;
990
991 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
992 local->miux = cpu_to_be16(miux);
993
994exit:
995 device_unlock(&dev->dev);
996
997 nfc_put_device(dev);
998
999 return rc;
1000}
1001
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001002static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1003{
1004 struct nfc_dev *dev;
1005 struct nfc_llcp_local *local;
1006 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1007 u32 idx;
1008 u8 tid;
1009 char *uri;
1010 int rc = 0, rem;
1011 size_t uri_len, tlvs_len;
1012 struct hlist_head sdreq_list;
1013 struct nfc_llcp_sdp_tlv *sdreq;
1014
1015 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1016 !info->attrs[NFC_ATTR_LLC_SDP])
1017 return -EINVAL;
1018
1019 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1020
1021 dev = nfc_get_device(idx);
1022 if (!dev) {
1023 rc = -ENODEV;
1024 goto exit;
1025 }
1026
1027 device_lock(&dev->dev);
1028
1029 if (dev->dep_link_up == false) {
1030 rc = -ENOLINK;
1031 goto exit;
1032 }
1033
1034 local = nfc_llcp_find_local(dev);
1035 if (!local) {
1036 nfc_put_device(dev);
1037 rc = -ENODEV;
1038 goto exit;
1039 }
1040
1041 INIT_HLIST_HEAD(&sdreq_list);
1042
1043 tlvs_len = 0;
1044
1045 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1046 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1047 nfc_sdp_genl_policy);
1048
1049 if (rc != 0) {
1050 rc = -EINVAL;
1051 goto exit;
1052 }
1053
1054 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1055 continue;
1056
1057 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1058 if (uri_len == 0)
1059 continue;
1060
1061 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1062 if (uri == NULL || *uri == 0)
1063 continue;
1064
1065 tid = local->sdreq_next_tid++;
1066
1067 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1068 if (sdreq == NULL) {
1069 rc = -ENOMEM;
1070 goto exit;
1071 }
1072
1073 tlvs_len += sdreq->tlv_len;
1074
1075 hlist_add_head(&sdreq->node, &sdreq_list);
1076 }
1077
1078 if (hlist_empty(&sdreq_list)) {
1079 rc = -EINVAL;
1080 goto exit;
1081 }
1082
1083 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1084exit:
1085 device_unlock(&dev->dev);
1086
1087 nfc_put_device(dev);
1088
1089 return rc;
1090}
1091
Eric Lapuyade9674da82013-04-29 17:13:27 +02001092static int nfc_genl_fw_upload(struct sk_buff *skb, struct genl_info *info)
1093{
1094 struct nfc_dev *dev;
1095 int rc;
1096 u32 idx;
1097 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1098
1099 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1100 return -EINVAL;
1101
1102 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1103
1104 dev = nfc_get_device(idx);
1105 if (!dev)
1106 return -ENODEV;
1107
1108 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1109 sizeof(firmware_name));
1110
1111 rc = nfc_fw_upload(dev, firmware_name);
1112
1113 nfc_put_device(dev);
1114 return rc;
1115}
1116
1117int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
1118{
1119 struct sk_buff *msg;
1120 void *hdr;
1121
1122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1123 if (!msg)
1124 return -ENOMEM;
1125
1126 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1127 NFC_CMD_FW_UPLOAD);
1128 if (!hdr)
1129 goto free_msg;
1130
1131 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1132 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1133 goto nla_put_failure;
1134
1135 genlmsg_end(msg, hdr);
1136
1137 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1138
1139 return 0;
1140
1141nla_put_failure:
1142 genlmsg_cancel(msg, hdr);
1143free_msg:
1144 nlmsg_free(msg);
1145 return -EMSGSIZE;
1146}
1147
Samuel Ortizbe085652013-05-10 17:07:32 +02001148static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1149{
1150 struct nfc_dev *dev;
1151 int rc;
1152 u32 idx, se_idx;
1153
1154 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1155 !info->attrs[NFC_ATTR_SE_INDEX])
1156 return -EINVAL;
1157
1158 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1159 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1160
1161 dev = nfc_get_device(idx);
1162 if (!dev)
1163 return -ENODEV;
1164
1165 rc = nfc_enable_se(dev, se_idx);
1166
1167 nfc_put_device(dev);
1168 return rc;
1169}
1170
1171static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1172{
1173 struct nfc_dev *dev;
1174 int rc;
1175 u32 idx, se_idx;
1176
1177 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1178 !info->attrs[NFC_ATTR_SE_INDEX])
1179 return -EINVAL;
1180
1181 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1182 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1183
1184 dev = nfc_get_device(idx);
1185 if (!dev)
1186 return -ENODEV;
1187
1188 rc = nfc_disable_se(dev, se_idx);
1189
1190 nfc_put_device(dev);
1191 return rc;
1192}
1193
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001194static struct genl_ops nfc_genl_ops[] = {
1195 {
1196 .cmd = NFC_CMD_GET_DEVICE,
1197 .doit = nfc_genl_get_device,
1198 .dumpit = nfc_genl_dump_devices,
1199 .done = nfc_genl_dump_devices_done,
1200 .policy = nfc_genl_policy,
1201 },
1202 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001203 .cmd = NFC_CMD_DEV_UP,
1204 .doit = nfc_genl_dev_up,
1205 .policy = nfc_genl_policy,
1206 },
1207 {
1208 .cmd = NFC_CMD_DEV_DOWN,
1209 .doit = nfc_genl_dev_down,
1210 .policy = nfc_genl_policy,
1211 },
1212 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001213 .cmd = NFC_CMD_START_POLL,
1214 .doit = nfc_genl_start_poll,
1215 .policy = nfc_genl_policy,
1216 },
1217 {
1218 .cmd = NFC_CMD_STOP_POLL,
1219 .doit = nfc_genl_stop_poll,
1220 .policy = nfc_genl_policy,
1221 },
1222 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001223 .cmd = NFC_CMD_DEP_LINK_UP,
1224 .doit = nfc_genl_dep_link_up,
1225 .policy = nfc_genl_policy,
1226 },
1227 {
1228 .cmd = NFC_CMD_DEP_LINK_DOWN,
1229 .doit = nfc_genl_dep_link_down,
1230 .policy = nfc_genl_policy,
1231 },
1232 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001233 .cmd = NFC_CMD_GET_TARGET,
1234 .dumpit = nfc_genl_dump_targets,
1235 .done = nfc_genl_dump_targets_done,
1236 .policy = nfc_genl_policy,
1237 },
Thierry Escande52feb442012-10-17 14:43:39 +02001238 {
1239 .cmd = NFC_CMD_LLC_GET_PARAMS,
1240 .doit = nfc_genl_llc_get_params,
1241 .policy = nfc_genl_policy,
1242 },
1243 {
1244 .cmd = NFC_CMD_LLC_SET_PARAMS,
1245 .doit = nfc_genl_llc_set_params,
1246 .policy = nfc_genl_policy,
1247 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001248 {
1249 .cmd = NFC_CMD_LLC_SDREQ,
1250 .doit = nfc_genl_llc_sdreq,
1251 .policy = nfc_genl_policy,
1252 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001253 {
1254 .cmd = NFC_CMD_FW_UPLOAD,
1255 .doit = nfc_genl_fw_upload,
1256 .policy = nfc_genl_policy,
1257 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001258 {
1259 .cmd = NFC_CMD_ENABLE_SE,
1260 .doit = nfc_genl_enable_se,
1261 .policy = nfc_genl_policy,
1262 },
1263 {
1264 .cmd = NFC_CMD_DISABLE_SE,
1265 .doit = nfc_genl_disable_se,
1266 .policy = nfc_genl_policy,
1267 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001268};
1269
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001270
1271struct urelease_work {
1272 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001273 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001274};
1275
1276static void nfc_urelease_event_work(struct work_struct *work)
1277{
1278 struct urelease_work *w = container_of(work, struct urelease_work, w);
1279 struct class_dev_iter iter;
1280 struct nfc_dev *dev;
1281
John W. Linvillec4876062012-09-28 11:11:16 -04001282 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001283
1284 mutex_lock(&nfc_devlist_mutex);
1285
1286 nfc_device_iter_init(&iter);
1287 dev = nfc_device_iter_next(&iter);
1288
1289 while (dev) {
1290 mutex_lock(&dev->genl_data.genl_data_mutex);
1291
John W. Linvillec4876062012-09-28 11:11:16 -04001292 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001293 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001294 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001295 }
1296
1297 mutex_unlock(&dev->genl_data.genl_data_mutex);
1298
1299 dev = nfc_device_iter_next(&iter);
1300 }
1301
1302 nfc_device_iter_exit(&iter);
1303
1304 mutex_unlock(&nfc_devlist_mutex);
1305
1306 kfree(w);
1307}
1308
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001309static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001310 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001311{
1312 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001313 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001314
1315 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1316 goto out;
1317
Eric W. Biederman15e47302012-09-07 20:12:54 +00001318 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001319
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001320 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1321 if (w) {
1322 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001323 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001324 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001325 }
1326
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001327out:
1328 return NOTIFY_DONE;
1329}
1330
1331void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1332{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001333 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001334 mutex_init(&genl_data->genl_data_mutex);
1335}
1336
1337void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1338{
1339 mutex_destroy(&genl_data->genl_data_mutex);
1340}
1341
1342static struct notifier_block nl_notifier = {
1343 .notifier_call = nfc_genl_rcv_nl_event,
1344};
1345
1346/**
1347 * nfc_genl_init() - Initialize netlink interface
1348 *
1349 * This initialization function registers the nfc netlink family.
1350 */
1351int __init nfc_genl_init(void)
1352{
1353 int rc;
1354
1355 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001356 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001357 if (rc)
1358 return rc;
1359
1360 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1361
1362 netlink_register_notifier(&nl_notifier);
1363
1364 return rc;
1365}
1366
1367/**
1368 * nfc_genl_exit() - Deinitialize netlink interface
1369 *
1370 * This exit function unregisters the nfc netlink family.
1371 */
1372void nfc_genl_exit(void)
1373{
1374 netlink_unregister_notifier(&nl_notifier);
1375 genl_unregister_family(&nfc_genl_family);
1376}