blob: be387e6219a0f52d96e03192317ba88777ec6346 [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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030020 */
21
Samuel Ortiz52858b52011-12-14 16:43:05 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080023
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030024#include <net/genetlink.h>
25#include <linux/nfc.h>
26#include <linux/slab.h>
27
28#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020029#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020030
Johannes Berg2a94fe42013-11-19 15:19:39 +010031static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030033};
34
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020035static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030036 .id = GENL_ID_GENERATE,
37 .hdrsize = 0,
38 .name = NFC_GENL_NAME,
39 .version = NFC_GENL_VERSION,
40 .maxattr = NFC_ATTR_MAX,
41};
42
43static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
44 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
45 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
46 .len = NFC_DEVICE_NAME_MAXSIZE },
47 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010048 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010050 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020051 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010053 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
55 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010056 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020057 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020059 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010060};
61
62static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
63 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
64 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030065};
66
67static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010068 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030069{
70 void *hdr;
71
Eric W. Biederman15e47302012-09-07 20:12:54 +000072 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010073 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030074 if (!hdr)
75 return -EMSGSIZE;
76
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78
David S. Miller1e6428d2012-03-29 23:23:57 -040079 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
80 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
81 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
82 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
83 goto nla_put_failure;
84 if (target->nfcid1_len > 0 &&
85 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
86 target->nfcid1))
87 goto nla_put_failure;
88 if (target->sensb_res_len > 0 &&
89 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
90 target->sensb_res))
91 goto nla_put_failure;
92 if (target->sensf_res_len > 0 &&
93 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
94 target->sensf_res))
95 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030096
Mark A. Greerf5f68722014-01-14 17:52:11 -070097 if (target->is_iso15693) {
98 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
99 target->iso15693_dsfid) ||
100 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
101 sizeof(target->iso15693_uid), target->iso15693_uid))
102 goto nla_put_failure;
103 }
104
Johannes Berg053c0952015-01-16 22:09:00 +0100105 genlmsg_end(msg, hdr);
106 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300107
108nla_put_failure:
109 genlmsg_cancel(msg, hdr);
110 return -EMSGSIZE;
111}
112
113static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
114{
115 struct nfc_dev *dev;
116 int rc;
117 u32 idx;
118
119 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100120 nfc_genl_family.attrbuf,
121 nfc_genl_family.maxattr,
122 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300123 if (rc < 0)
124 return ERR_PTR(rc);
125
126 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
127 return ERR_PTR(-EINVAL);
128
129 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
130
131 dev = nfc_get_device(idx);
132 if (!dev)
133 return ERR_PTR(-ENODEV);
134
135 return dev;
136}
137
138static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100139 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300140{
141 int i = cb->args[0];
142 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
143 int rc;
144
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145 if (!dev) {
146 dev = __get_device_from_cb(cb);
147 if (IS_ERR(dev))
148 return PTR_ERR(dev);
149
150 cb->args[1] = (long) dev;
151 }
152
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200153 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300154
155 cb->seq = dev->targets_generation;
156
157 while (i < dev->n_targets) {
158 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100159 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300160 if (rc < 0)
161 break;
162
163 i++;
164 }
165
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200166 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300167
168 cb->args[0] = i;
169
170 return skb->len;
171}
172
173static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
174{
175 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
176
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300177 if (dev)
178 nfc_put_device(dev);
179
180 return 0;
181}
182
183int nfc_genl_targets_found(struct nfc_dev *dev)
184{
185 struct sk_buff *msg;
186 void *hdr;
187
Eric W. Biederman15e47302012-09-07 20:12:54 +0000188 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300189
Thomas Graf58050fc2012-06-28 03:57:45 +0000190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300191 if (!msg)
192 return -ENOMEM;
193
194 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100195 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300196 if (!hdr)
197 goto free_msg;
198
David S. Miller1e6428d2012-03-29 23:23:57 -0400199 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
200 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300201
202 genlmsg_end(msg, hdr);
203
Johannes Berg2a94fe42013-11-19 15:19:39 +0100204 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300205
206nla_put_failure:
207 genlmsg_cancel(msg, hdr);
208free_msg:
209 nlmsg_free(msg);
210 return -EMSGSIZE;
211}
212
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200213int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
214{
215 struct sk_buff *msg;
216 void *hdr;
217
Thomas Graf58050fc2012-06-28 03:57:45 +0000218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200219 if (!msg)
220 return -ENOMEM;
221
222 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
223 NFC_EVENT_TARGET_LOST);
224 if (!hdr)
225 goto free_msg;
226
John W. Linville59ef43e2012-04-18 14:17:13 -0400227 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
228 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
229 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200230
231 genlmsg_end(msg, hdr);
232
Johannes Berg2a94fe42013-11-19 15:19:39 +0100233 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200234
235 return 0;
236
237nla_put_failure:
238 genlmsg_cancel(msg, hdr);
239free_msg:
240 nlmsg_free(msg);
241 return -EMSGSIZE;
242}
243
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200244int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
245{
246 struct sk_buff *msg;
247 void *hdr;
248
Thomas Graf58050fc2012-06-28 03:57:45 +0000249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200250 if (!msg)
251 return -ENOMEM;
252
253 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
254 NFC_EVENT_TM_ACTIVATED);
255 if (!hdr)
256 goto free_msg;
257
258 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
259 goto nla_put_failure;
260 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
261 goto nla_put_failure;
262
263 genlmsg_end(msg, hdr);
264
Johannes Berg2a94fe42013-11-19 15:19:39 +0100265 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200266
267 return 0;
268
269nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271free_msg:
272 nlmsg_free(msg);
273 return -EMSGSIZE;
274}
275
276int nfc_genl_tm_deactivated(struct nfc_dev *dev)
277{
278 struct sk_buff *msg;
279 void *hdr;
280
Thomas Graf58050fc2012-06-28 03:57:45 +0000281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200282 if (!msg)
283 return -ENOMEM;
284
285 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
286 NFC_EVENT_TM_DEACTIVATED);
287 if (!hdr)
288 goto free_msg;
289
290 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
291 goto nla_put_failure;
292
293 genlmsg_end(msg, hdr);
294
Johannes Berg2a94fe42013-11-19 15:19:39 +0100295 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200296
297 return 0;
298
299nla_put_failure:
300 genlmsg_cancel(msg, hdr);
301free_msg:
302 nlmsg_free(msg);
303 return -EMSGSIZE;
304}
305
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300306int nfc_genl_device_added(struct nfc_dev *dev)
307{
308 struct sk_buff *msg;
309 void *hdr;
310
Thomas Graf58050fc2012-06-28 03:57:45 +0000311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300312 if (!msg)
313 return -ENOMEM;
314
315 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100316 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300317 if (!hdr)
318 goto free_msg;
319
David S. Miller1e6428d2012-03-29 23:23:57 -0400320 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
321 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
322 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
323 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
324 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300325
326 genlmsg_end(msg, hdr);
327
Johannes Berg2a94fe42013-11-19 15:19:39 +0100328 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300329
330 return 0;
331
332nla_put_failure:
333 genlmsg_cancel(msg, hdr);
334free_msg:
335 nlmsg_free(msg);
336 return -EMSGSIZE;
337}
338
339int nfc_genl_device_removed(struct nfc_dev *dev)
340{
341 struct sk_buff *msg;
342 void *hdr;
343
Thomas Graf58050fc2012-06-28 03:57:45 +0000344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300345 if (!msg)
346 return -ENOMEM;
347
348 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100349 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300350 if (!hdr)
351 goto free_msg;
352
David S. Miller1e6428d2012-03-29 23:23:57 -0400353 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
354 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300355
356 genlmsg_end(msg, hdr);
357
Johannes Berg2a94fe42013-11-19 15:19:39 +0100358 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300359
360 return 0;
361
362nla_put_failure:
363 genlmsg_cancel(msg, hdr);
364free_msg:
365 nlmsg_free(msg);
366 return -EMSGSIZE;
367}
368
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100369int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
370{
371 struct sk_buff *msg;
372 struct nlattr *sdp_attr, *uri_attr;
373 struct nfc_llcp_sdp_tlv *sdres;
374 struct hlist_node *n;
375 void *hdr;
376 int rc = -EMSGSIZE;
377 int i;
378
379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
380 if (!msg)
381 return -ENOMEM;
382
383 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
384 NFC_EVENT_LLC_SDRES);
385 if (!hdr)
386 goto free_msg;
387
388 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
389 goto nla_put_failure;
390
391 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
392 if (sdp_attr == NULL) {
393 rc = -ENOMEM;
394 goto nla_put_failure;
395 }
396
397 i = 1;
398 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
399 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
400
401 uri_attr = nla_nest_start(msg, i++);
402 if (uri_attr == NULL) {
403 rc = -ENOMEM;
404 goto nla_put_failure;
405 }
406
407 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
408 goto nla_put_failure;
409
410 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
411 goto nla_put_failure;
412
413 nla_nest_end(msg, uri_attr);
414
415 hlist_del(&sdres->node);
416
417 nfc_llcp_free_sdp_tlv(sdres);
418 }
419
420 nla_nest_end(msg, sdp_attr);
421
422 genlmsg_end(msg, hdr);
423
Johannes Berg2a94fe42013-11-19 15:19:39 +0100424 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100425
426nla_put_failure:
427 genlmsg_cancel(msg, hdr);
428
429free_msg:
430 nlmsg_free(msg);
431
432 nfc_llcp_free_sdp_tlv_list(sdres_list);
433
434 return rc;
435}
436
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200437int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
438{
439 struct sk_buff *msg;
440 void *hdr;
441
442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443 if (!msg)
444 return -ENOMEM;
445
446 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
447 NFC_EVENT_SE_ADDED);
448 if (!hdr)
449 goto free_msg;
450
451 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
452 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
453 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
454 goto nla_put_failure;
455
456 genlmsg_end(msg, hdr);
457
Johannes Berg2a94fe42013-11-19 15:19:39 +0100458 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200459
460 return 0;
461
462nla_put_failure:
463 genlmsg_cancel(msg, hdr);
464free_msg:
465 nlmsg_free(msg);
466 return -EMSGSIZE;
467}
468
469int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
470{
471 struct sk_buff *msg;
472 void *hdr;
473
474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
475 if (!msg)
476 return -ENOMEM;
477
478 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
479 NFC_EVENT_SE_REMOVED);
480 if (!hdr)
481 goto free_msg;
482
483 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
484 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
485 goto nla_put_failure;
486
487 genlmsg_end(msg, hdr);
488
Johannes Berg2a94fe42013-11-19 15:19:39 +0100489 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200490
491 return 0;
492
493nla_put_failure:
494 genlmsg_cancel(msg, hdr);
495free_msg:
496 nlmsg_free(msg);
497 return -EMSGSIZE;
498}
499
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300500static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000501 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100502 struct netlink_callback *cb,
503 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300504{
505 void *hdr;
506
Eric W. Biederman15e47302012-09-07 20:12:54 +0000507 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100508 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300509 if (!hdr)
510 return -EMSGSIZE;
511
512 if (cb)
513 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
514
David S. Miller1e6428d2012-03-29 23:23:57 -0400515 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
516 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
517 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200518 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
519 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400520 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300521
Johannes Berg053c0952015-01-16 22:09:00 +0100522 genlmsg_end(msg, hdr);
523 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300524
525nla_put_failure:
526 genlmsg_cancel(msg, hdr);
527 return -EMSGSIZE;
528}
529
530static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100531 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300532{
533 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
534 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
535 bool first_call = false;
536
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300537 if (!iter) {
538 first_call = true;
539 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
540 if (!iter)
541 return -ENOMEM;
542 cb->args[0] = (long) iter;
543 }
544
545 mutex_lock(&nfc_devlist_mutex);
546
547 cb->seq = nfc_devlist_generation;
548
549 if (first_call) {
550 nfc_device_iter_init(iter);
551 dev = nfc_device_iter_next(iter);
552 }
553
554 while (dev) {
555 int rc;
556
Eric W. Biederman15e47302012-09-07 20:12:54 +0000557 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100558 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300559 if (rc < 0)
560 break;
561
562 dev = nfc_device_iter_next(iter);
563 }
564
565 mutex_unlock(&nfc_devlist_mutex);
566
567 cb->args[1] = (long) dev;
568
569 return skb->len;
570}
571
572static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
573{
574 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
575
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300576 nfc_device_iter_exit(iter);
577 kfree(iter);
578
579 return 0;
580}
581
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100582int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100583 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100584{
585 struct sk_buff *msg;
586 void *hdr;
587
588 pr_debug("DEP link is up\n");
589
Thomas Graf58050fc2012-06-28 03:57:45 +0000590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100591 if (!msg)
592 return -ENOMEM;
593
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100594 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100595 if (!hdr)
596 goto free_msg;
597
David S. Miller1e6428d2012-03-29 23:23:57 -0400598 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
599 goto nla_put_failure;
600 if (rf_mode == NFC_RF_INITIATOR &&
601 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
602 goto nla_put_failure;
603 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
604 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
605 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100606
607 genlmsg_end(msg, hdr);
608
609 dev->dep_link_up = true;
610
Johannes Berg2a94fe42013-11-19 15:19:39 +0100611 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100612
613 return 0;
614
615nla_put_failure:
616 genlmsg_cancel(msg, hdr);
617free_msg:
618 nlmsg_free(msg);
619 return -EMSGSIZE;
620}
621
622int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
623{
624 struct sk_buff *msg;
625 void *hdr;
626
627 pr_debug("DEP link is down\n");
628
Thomas Graf58050fc2012-06-28 03:57:45 +0000629 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100630 if (!msg)
631 return -ENOMEM;
632
633 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100634 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100635 if (!hdr)
636 goto free_msg;
637
David S. Miller1e6428d2012-03-29 23:23:57 -0400638 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
639 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100640
641 genlmsg_end(msg, hdr);
642
Johannes Berg2a94fe42013-11-19 15:19:39 +0100643 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100644
645 return 0;
646
647nla_put_failure:
648 genlmsg_cancel(msg, hdr);
649free_msg:
650 nlmsg_free(msg);
651 return -EMSGSIZE;
652}
653
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300654static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
655{
656 struct sk_buff *msg;
657 struct nfc_dev *dev;
658 u32 idx;
659 int rc = -ENOBUFS;
660
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300661 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
662 return -EINVAL;
663
664 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
665
666 dev = nfc_get_device(idx);
667 if (!dev)
668 return -ENODEV;
669
Thomas Graf58050fc2012-06-28 03:57:45 +0000670 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300671 if (!msg) {
672 rc = -ENOMEM;
673 goto out_putdev;
674 }
675
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100677 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300678 if (rc < 0)
679 goto out_free;
680
681 nfc_put_device(dev);
682
683 return genlmsg_reply(msg, info);
684
685out_free:
686 nlmsg_free(msg);
687out_putdev:
688 nfc_put_device(dev);
689 return rc;
690}
691
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300692static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
693{
694 struct nfc_dev *dev;
695 int rc;
696 u32 idx;
697
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300698 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
699 return -EINVAL;
700
701 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
702
703 dev = nfc_get_device(idx);
704 if (!dev)
705 return -ENODEV;
706
707 rc = nfc_dev_up(dev);
708
709 nfc_put_device(dev);
710 return rc;
711}
712
713static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
714{
715 struct nfc_dev *dev;
716 int rc;
717 u32 idx;
718
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300719 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
720 return -EINVAL;
721
722 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
723
724 dev = nfc_get_device(idx);
725 if (!dev)
726 return -ENODEV;
727
728 rc = nfc_dev_down(dev);
729
730 nfc_put_device(dev);
731 return rc;
732}
733
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300734static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
735{
736 struct nfc_dev *dev;
737 int rc;
738 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200739 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300740
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100741 pr_debug("Poll start\n");
742
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300743 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200744 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
745 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200746 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300747 return -EINVAL;
748
749 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200750
751 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
752 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200753
754 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
755 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200756 else if (info->attrs[NFC_ATTR_PROTOCOLS])
757 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300758
759 dev = nfc_get_device(idx);
760 if (!dev)
761 return -ENODEV;
762
763 mutex_lock(&dev->genl_data.genl_data_mutex);
764
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200765 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300766 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000767 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300768
769 mutex_unlock(&dev->genl_data.genl_data_mutex);
770
771 nfc_put_device(dev);
772 return rc;
773}
774
775static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
776{
777 struct nfc_dev *dev;
778 int rc;
779 u32 idx;
780
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
782 return -EINVAL;
783
784 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
785
786 dev = nfc_get_device(idx);
787 if (!dev)
788 return -ENODEV;
789
Samuel Ortiza831b912012-06-28 16:41:57 +0200790 device_lock(&dev->dev);
791
792 if (!dev->polling) {
793 device_unlock(&dev->dev);
794 return -EINVAL;
795 }
796
797 device_unlock(&dev->dev);
798
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300799 mutex_lock(&dev->genl_data.genl_data_mutex);
800
Eric W. Biederman15e47302012-09-07 20:12:54 +0000801 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300802 rc = -EBUSY;
803 goto out;
804 }
805
806 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000807 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300808
809out:
810 mutex_unlock(&dev->genl_data.genl_data_mutex);
811 nfc_put_device(dev);
812 return rc;
813}
814
Christophe Ricard3682f492014-12-02 21:27:50 +0100815static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
816{
817 struct nfc_dev *dev;
818 u32 device_idx, target_idx, protocol;
819 int rc;
820
821 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
822 return -EINVAL;
823
824 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
825
826 dev = nfc_get_device(device_idx);
827 if (!dev)
828 return -ENODEV;
829
830 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
831 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
832
833 nfc_deactivate_target(dev, target_idx);
834 rc = nfc_activate_target(dev, target_idx, protocol);
835
836 nfc_put_device(dev);
837 return 0;
838}
839
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100840static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
841{
842 struct nfc_dev *dev;
843 int rc, tgt_idx;
844 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100845 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100846
847 pr_debug("DEP link up\n");
848
849 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100850 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100851 return -EINVAL;
852
853 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
854 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
855 tgt_idx = NFC_TARGET_IDX_ANY;
856 else
857 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
858
859 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100860
861 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
862 return -EINVAL;
863
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100864 dev = nfc_get_device(idx);
865 if (!dev)
866 return -ENODEV;
867
Samuel Ortiz47807d32012-03-05 01:03:50 +0100868 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100869
870 nfc_put_device(dev);
871
872 return rc;
873}
874
875static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
876{
877 struct nfc_dev *dev;
878 int rc;
879 u32 idx;
880
881 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
882 return -EINVAL;
883
884 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
885
886 dev = nfc_get_device(idx);
887 if (!dev)
888 return -ENODEV;
889
890 rc = nfc_dep_link_down(dev);
891
892 nfc_put_device(dev);
893 return rc;
894}
895
Thierry Escande52feb442012-10-17 14:43:39 +0200896static int nfc_genl_send_params(struct sk_buff *msg,
897 struct nfc_llcp_local *local,
898 u32 portid, u32 seq)
899{
900 void *hdr;
901
902 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
903 NFC_CMD_LLC_GET_PARAMS);
904 if (!hdr)
905 return -EMSGSIZE;
906
907 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
908 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
909 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
910 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
911 goto nla_put_failure;
912
Johannes Berg053c0952015-01-16 22:09:00 +0100913 genlmsg_end(msg, hdr);
914 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +0200915
916nla_put_failure:
917
918 genlmsg_cancel(msg, hdr);
919 return -EMSGSIZE;
920}
921
922static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
923{
924 struct nfc_dev *dev;
925 struct nfc_llcp_local *local;
926 int rc = 0;
927 struct sk_buff *msg = NULL;
928 u32 idx;
929
930 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
931 return -EINVAL;
932
933 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
934
935 dev = nfc_get_device(idx);
936 if (!dev)
937 return -ENODEV;
938
939 device_lock(&dev->dev);
940
941 local = nfc_llcp_find_local(dev);
942 if (!local) {
943 rc = -ENODEV;
944 goto exit;
945 }
946
947 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
948 if (!msg) {
949 rc = -ENOMEM;
950 goto exit;
951 }
952
953 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
954
955exit:
956 device_unlock(&dev->dev);
957
958 nfc_put_device(dev);
959
960 if (rc < 0) {
961 if (msg)
962 nlmsg_free(msg);
963
964 return rc;
965 }
966
967 return genlmsg_reply(msg, info);
968}
969
970static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
971{
972 struct nfc_dev *dev;
973 struct nfc_llcp_local *local;
974 u8 rw = 0;
975 u16 miux = 0;
976 u32 idx;
977 int rc = 0;
978
979 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
980 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
981 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
982 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
983 return -EINVAL;
984
985 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
986 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
987
988 if (rw > LLCP_MAX_RW)
989 return -EINVAL;
990 }
991
992 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
993 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
994
995 if (miux > LLCP_MAX_MIUX)
996 return -EINVAL;
997 }
998
999 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1000
1001 dev = nfc_get_device(idx);
1002 if (!dev)
1003 return -ENODEV;
1004
1005 device_lock(&dev->dev);
1006
1007 local = nfc_llcp_find_local(dev);
1008 if (!local) {
1009 nfc_put_device(dev);
1010 rc = -ENODEV;
1011 goto exit;
1012 }
1013
1014 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1015 if (dev->dep_link_up) {
1016 rc = -EINPROGRESS;
1017 goto exit;
1018 }
1019
1020 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1021 }
1022
1023 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1024 local->rw = rw;
1025
1026 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1027 local->miux = cpu_to_be16(miux);
1028
1029exit:
1030 device_unlock(&dev->dev);
1031
1032 nfc_put_device(dev);
1033
1034 return rc;
1035}
1036
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001037static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1038{
1039 struct nfc_dev *dev;
1040 struct nfc_llcp_local *local;
1041 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1042 u32 idx;
1043 u8 tid;
1044 char *uri;
1045 int rc = 0, rem;
1046 size_t uri_len, tlvs_len;
1047 struct hlist_head sdreq_list;
1048 struct nfc_llcp_sdp_tlv *sdreq;
1049
1050 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1051 !info->attrs[NFC_ATTR_LLC_SDP])
1052 return -EINVAL;
1053
1054 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1055
1056 dev = nfc_get_device(idx);
1057 if (!dev) {
1058 rc = -ENODEV;
1059 goto exit;
1060 }
1061
1062 device_lock(&dev->dev);
1063
1064 if (dev->dep_link_up == false) {
1065 rc = -ENOLINK;
1066 goto exit;
1067 }
1068
1069 local = nfc_llcp_find_local(dev);
1070 if (!local) {
1071 nfc_put_device(dev);
1072 rc = -ENODEV;
1073 goto exit;
1074 }
1075
1076 INIT_HLIST_HEAD(&sdreq_list);
1077
1078 tlvs_len = 0;
1079
1080 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1081 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1082 nfc_sdp_genl_policy);
1083
1084 if (rc != 0) {
1085 rc = -EINVAL;
1086 goto exit;
1087 }
1088
1089 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1090 continue;
1091
1092 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1093 if (uri_len == 0)
1094 continue;
1095
1096 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1097 if (uri == NULL || *uri == 0)
1098 continue;
1099
1100 tid = local->sdreq_next_tid++;
1101
1102 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1103 if (sdreq == NULL) {
1104 rc = -ENOMEM;
1105 goto exit;
1106 }
1107
1108 tlvs_len += sdreq->tlv_len;
1109
1110 hlist_add_head(&sdreq->node, &sdreq_list);
1111 }
1112
1113 if (hlist_empty(&sdreq_list)) {
1114 rc = -EINVAL;
1115 goto exit;
1116 }
1117
1118 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1119exit:
1120 device_unlock(&dev->dev);
1121
1122 nfc_put_device(dev);
1123
1124 return rc;
1125}
1126
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001127static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001128{
1129 struct nfc_dev *dev;
1130 int rc;
1131 u32 idx;
1132 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1133
1134 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1135 return -EINVAL;
1136
1137 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1138
1139 dev = nfc_get_device(idx);
1140 if (!dev)
1141 return -ENODEV;
1142
1143 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1144 sizeof(firmware_name));
1145
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001146 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001147
1148 nfc_put_device(dev);
1149 return rc;
1150}
1151
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001152int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1153 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001154{
1155 struct sk_buff *msg;
1156 void *hdr;
1157
1158 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1159 if (!msg)
1160 return -ENOMEM;
1161
1162 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001163 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001164 if (!hdr)
1165 goto free_msg;
1166
1167 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001168 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001169 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1170 goto nla_put_failure;
1171
1172 genlmsg_end(msg, hdr);
1173
Johannes Berg2a94fe42013-11-19 15:19:39 +01001174 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001175
1176 return 0;
1177
1178nla_put_failure:
1179 genlmsg_cancel(msg, hdr);
1180free_msg:
1181 nlmsg_free(msg);
1182 return -EMSGSIZE;
1183}
1184
Samuel Ortizbe085652013-05-10 17:07:32 +02001185static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1186{
1187 struct nfc_dev *dev;
1188 int rc;
1189 u32 idx, se_idx;
1190
1191 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1192 !info->attrs[NFC_ATTR_SE_INDEX])
1193 return -EINVAL;
1194
1195 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1196 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1197
1198 dev = nfc_get_device(idx);
1199 if (!dev)
1200 return -ENODEV;
1201
1202 rc = nfc_enable_se(dev, se_idx);
1203
1204 nfc_put_device(dev);
1205 return rc;
1206}
1207
1208static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1209{
1210 struct nfc_dev *dev;
1211 int rc;
1212 u32 idx, se_idx;
1213
1214 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1215 !info->attrs[NFC_ATTR_SE_INDEX])
1216 return -EINVAL;
1217
1218 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1219 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1220
1221 dev = nfc_get_device(idx);
1222 if (!dev)
1223 return -ENODEV;
1224
1225 rc = nfc_disable_se(dev, se_idx);
1226
1227 nfc_put_device(dev);
1228 return rc;
1229}
1230
Samuel Ortizac22ac42013-07-24 18:10:50 +02001231static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1232 u32 portid, u32 seq,
1233 struct netlink_callback *cb,
1234 int flags)
1235{
1236 void *hdr;
1237 struct nfc_se *se, *n;
1238
1239 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1240 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1241 NFC_CMD_GET_SE);
1242 if (!hdr)
1243 goto nla_put_failure;
1244
1245 if (cb)
1246 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1247
1248 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1249 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1250 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1251 goto nla_put_failure;
1252
Johannes Berg053c0952015-01-16 22:09:00 +01001253 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001254 }
1255
1256 return 0;
1257
1258nla_put_failure:
1259 genlmsg_cancel(msg, hdr);
1260 return -EMSGSIZE;
1261}
1262
1263static int nfc_genl_dump_ses(struct sk_buff *skb,
1264 struct netlink_callback *cb)
1265{
1266 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1267 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1268 bool first_call = false;
1269
1270 if (!iter) {
1271 first_call = true;
1272 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1273 if (!iter)
1274 return -ENOMEM;
1275 cb->args[0] = (long) iter;
1276 }
1277
1278 mutex_lock(&nfc_devlist_mutex);
1279
1280 cb->seq = nfc_devlist_generation;
1281
1282 if (first_call) {
1283 nfc_device_iter_init(iter);
1284 dev = nfc_device_iter_next(iter);
1285 }
1286
1287 while (dev) {
1288 int rc;
1289
1290 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1291 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1292 if (rc < 0)
1293 break;
1294
1295 dev = nfc_device_iter_next(iter);
1296 }
1297
1298 mutex_unlock(&nfc_devlist_mutex);
1299
1300 cb->args[1] = (long) dev;
1301
1302 return skb->len;
1303}
1304
1305static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1306{
1307 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1308
1309 nfc_device_iter_exit(iter);
1310 kfree(iter);
1311
1312 return 0;
1313}
1314
Christophe Ricardcd96db62014-12-02 21:27:51 +01001315static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1316 u8 *apdu, size_t apdu_length,
1317 se_io_cb_t cb, void *cb_context)
1318{
1319 struct nfc_se *se;
1320 int rc;
1321
1322 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1323
1324 device_lock(&dev->dev);
1325
1326 if (!device_is_registered(&dev->dev)) {
1327 rc = -ENODEV;
1328 goto error;
1329 }
1330
1331 if (!dev->dev_up) {
1332 rc = -ENODEV;
1333 goto error;
1334 }
1335
1336 if (!dev->ops->se_io) {
1337 rc = -EOPNOTSUPP;
1338 goto error;
1339 }
1340
1341 se = nfc_find_se(dev, se_idx);
1342 if (!se) {
1343 rc = -EINVAL;
1344 goto error;
1345 }
1346
1347 if (se->state != NFC_SE_ENABLED) {
1348 rc = -ENODEV;
1349 goto error;
1350 }
1351
1352 rc = dev->ops->se_io(dev, se_idx, apdu,
1353 apdu_length, cb, cb_context);
1354
1355error:
1356 device_unlock(&dev->dev);
1357 return rc;
1358}
1359
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001360struct se_io_ctx {
1361 u32 dev_idx;
1362 u32 se_idx;
1363};
1364
Samuel Ortizddc1a702013-10-07 14:18:44 +02001365static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001366{
1367 struct se_io_ctx *ctx = context;
1368 struct sk_buff *msg;
1369 void *hdr;
1370
1371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1372 if (!msg) {
1373 kfree(ctx);
1374 return;
1375 }
1376
1377 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1378 NFC_CMD_SE_IO);
1379 if (!hdr)
1380 goto free_msg;
1381
1382 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1383 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1384 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1385 goto nla_put_failure;
1386
1387 genlmsg_end(msg, hdr);
1388
Johannes Berg2a94fe42013-11-19 15:19:39 +01001389 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001390
1391 kfree(ctx);
1392
1393 return;
1394
1395nla_put_failure:
1396 genlmsg_cancel(msg, hdr);
1397free_msg:
1398 nlmsg_free(msg);
1399 kfree(ctx);
1400
1401 return;
1402}
1403
1404static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1405{
1406 struct nfc_dev *dev;
1407 struct se_io_ctx *ctx;
1408 u32 dev_idx, se_idx;
1409 u8 *apdu;
1410 size_t apdu_len;
1411
1412 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1413 !info->attrs[NFC_ATTR_SE_INDEX] ||
1414 !info->attrs[NFC_ATTR_SE_APDU])
1415 return -EINVAL;
1416
1417 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1418 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1419
1420 dev = nfc_get_device(dev_idx);
1421 if (!dev)
1422 return -ENODEV;
1423
1424 if (!dev->ops || !dev->ops->se_io)
1425 return -ENOTSUPP;
1426
1427 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1428 if (apdu_len == 0)
1429 return -EINVAL;
1430
1431 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1432 if (!apdu)
1433 return -EINVAL;
1434
1435 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1436 if (!ctx)
1437 return -ENOMEM;
1438
1439 ctx->dev_idx = dev_idx;
1440 ctx->se_idx = se_idx;
1441
Christophe Ricardcd96db62014-12-02 21:27:51 +01001442 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001443}
1444
Johannes Berg4534de82013-11-14 17:14:46 +01001445static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001446 {
1447 .cmd = NFC_CMD_GET_DEVICE,
1448 .doit = nfc_genl_get_device,
1449 .dumpit = nfc_genl_dump_devices,
1450 .done = nfc_genl_dump_devices_done,
1451 .policy = nfc_genl_policy,
1452 },
1453 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001454 .cmd = NFC_CMD_DEV_UP,
1455 .doit = nfc_genl_dev_up,
1456 .policy = nfc_genl_policy,
1457 },
1458 {
1459 .cmd = NFC_CMD_DEV_DOWN,
1460 .doit = nfc_genl_dev_down,
1461 .policy = nfc_genl_policy,
1462 },
1463 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001464 .cmd = NFC_CMD_START_POLL,
1465 .doit = nfc_genl_start_poll,
1466 .policy = nfc_genl_policy,
1467 },
1468 {
1469 .cmd = NFC_CMD_STOP_POLL,
1470 .doit = nfc_genl_stop_poll,
1471 .policy = nfc_genl_policy,
1472 },
1473 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001474 .cmd = NFC_CMD_DEP_LINK_UP,
1475 .doit = nfc_genl_dep_link_up,
1476 .policy = nfc_genl_policy,
1477 },
1478 {
1479 .cmd = NFC_CMD_DEP_LINK_DOWN,
1480 .doit = nfc_genl_dep_link_down,
1481 .policy = nfc_genl_policy,
1482 },
1483 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001484 .cmd = NFC_CMD_GET_TARGET,
1485 .dumpit = nfc_genl_dump_targets,
1486 .done = nfc_genl_dump_targets_done,
1487 .policy = nfc_genl_policy,
1488 },
Thierry Escande52feb442012-10-17 14:43:39 +02001489 {
1490 .cmd = NFC_CMD_LLC_GET_PARAMS,
1491 .doit = nfc_genl_llc_get_params,
1492 .policy = nfc_genl_policy,
1493 },
1494 {
1495 .cmd = NFC_CMD_LLC_SET_PARAMS,
1496 .doit = nfc_genl_llc_set_params,
1497 .policy = nfc_genl_policy,
1498 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001499 {
1500 .cmd = NFC_CMD_LLC_SDREQ,
1501 .doit = nfc_genl_llc_sdreq,
1502 .policy = nfc_genl_policy,
1503 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001504 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001505 .cmd = NFC_CMD_FW_DOWNLOAD,
1506 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001507 .policy = nfc_genl_policy,
1508 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001509 {
1510 .cmd = NFC_CMD_ENABLE_SE,
1511 .doit = nfc_genl_enable_se,
1512 .policy = nfc_genl_policy,
1513 },
1514 {
1515 .cmd = NFC_CMD_DISABLE_SE,
1516 .doit = nfc_genl_disable_se,
1517 .policy = nfc_genl_policy,
1518 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001519 {
1520 .cmd = NFC_CMD_GET_SE,
1521 .dumpit = nfc_genl_dump_ses,
1522 .done = nfc_genl_dump_ses_done,
1523 .policy = nfc_genl_policy,
1524 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001525 {
1526 .cmd = NFC_CMD_SE_IO,
1527 .doit = nfc_genl_se_io,
1528 .policy = nfc_genl_policy,
1529 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001530 {
1531 .cmd = NFC_CMD_ACTIVATE_TARGET,
1532 .doit = nfc_genl_activate_target,
1533 .policy = nfc_genl_policy,
1534 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001535};
1536
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001537
1538struct urelease_work {
1539 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001540 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001541};
1542
1543static void nfc_urelease_event_work(struct work_struct *work)
1544{
1545 struct urelease_work *w = container_of(work, struct urelease_work, w);
1546 struct class_dev_iter iter;
1547 struct nfc_dev *dev;
1548
John W. Linvillec4876062012-09-28 11:11:16 -04001549 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001550
1551 mutex_lock(&nfc_devlist_mutex);
1552
1553 nfc_device_iter_init(&iter);
1554 dev = nfc_device_iter_next(&iter);
1555
1556 while (dev) {
1557 mutex_lock(&dev->genl_data.genl_data_mutex);
1558
John W. Linvillec4876062012-09-28 11:11:16 -04001559 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001560 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001561 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001562 }
1563
1564 mutex_unlock(&dev->genl_data.genl_data_mutex);
1565
1566 dev = nfc_device_iter_next(&iter);
1567 }
1568
1569 nfc_device_iter_exit(&iter);
1570
1571 mutex_unlock(&nfc_devlist_mutex);
1572
1573 kfree(w);
1574}
1575
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001576static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001577 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001578{
1579 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001580 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001581
1582 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1583 goto out;
1584
Eric W. Biederman15e47302012-09-07 20:12:54 +00001585 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001586
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001587 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1588 if (w) {
1589 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001590 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001591 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001592 }
1593
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001594out:
1595 return NOTIFY_DONE;
1596}
1597
1598void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1599{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001600 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001601 mutex_init(&genl_data->genl_data_mutex);
1602}
1603
1604void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1605{
1606 mutex_destroy(&genl_data->genl_data_mutex);
1607}
1608
1609static struct notifier_block nl_notifier = {
1610 .notifier_call = nfc_genl_rcv_nl_event,
1611};
1612
1613/**
1614 * nfc_genl_init() - Initialize netlink interface
1615 *
1616 * This initialization function registers the nfc netlink family.
1617 */
1618int __init nfc_genl_init(void)
1619{
1620 int rc;
1621
Johannes Berg2a94fe42013-11-19 15:19:39 +01001622 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1623 nfc_genl_ops,
1624 nfc_genl_mcgrps);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001625 if (rc)
1626 return rc;
1627
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001628 netlink_register_notifier(&nl_notifier);
1629
Johannes Berg2a94fe42013-11-19 15:19:39 +01001630 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001631}
1632
1633/**
1634 * nfc_genl_exit() - Deinitialize netlink interface
1635 *
1636 * This exit function unregisters the nfc netlink family.
1637 */
1638void nfc_genl_exit(void)
1639{
1640 netlink_unregister_notifier(&nl_notifier);
1641 genl_unregister_family(&nfc_genl_family);
1642}