blob: 63975c039b85dfa00a4402102d6f95fd5b2d6236 [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"
31
Thierry Escande52feb442012-10-17 14:43:39 +020032#include "llcp/llcp.h"
33
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030034static struct genl_multicast_group nfc_genl_event_mcgrp = {
35 .name = NFC_GENL_MCAST_EVENT_NAME,
36};
37
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020038static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030039 .id = GENL_ID_GENERATE,
40 .hdrsize = 0,
41 .name = NFC_GENL_NAME,
42 .version = NFC_GENL_VERSION,
43 .maxattr = NFC_ATTR_MAX,
44};
45
46static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
47 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
48 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
49 .len = NFC_DEVICE_NAME_MAXSIZE },
50 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010051 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
52 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010053 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020054 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
55 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010056 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
57 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
58 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030059};
60
61static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010062 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030063{
64 void *hdr;
65
Eric W. Biederman15e47302012-09-07 20:12:54 +000066 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010067 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030068 if (!hdr)
69 return -EMSGSIZE;
70
71 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
72
David S. Miller1e6428d2012-03-29 23:23:57 -040073 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
74 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
75 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
76 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
77 goto nla_put_failure;
78 if (target->nfcid1_len > 0 &&
79 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
80 target->nfcid1))
81 goto nla_put_failure;
82 if (target->sensb_res_len > 0 &&
83 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
84 target->sensb_res))
85 goto nla_put_failure;
86 if (target->sensf_res_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
88 target->sensf_res))
89 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030090
91 return genlmsg_end(msg, hdr);
92
93nla_put_failure:
94 genlmsg_cancel(msg, hdr);
95 return -EMSGSIZE;
96}
97
98static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
99{
100 struct nfc_dev *dev;
101 int rc;
102 u32 idx;
103
104 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100105 nfc_genl_family.attrbuf,
106 nfc_genl_family.maxattr,
107 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300108 if (rc < 0)
109 return ERR_PTR(rc);
110
111 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
112 return ERR_PTR(-EINVAL);
113
114 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
115
116 dev = nfc_get_device(idx);
117 if (!dev)
118 return ERR_PTR(-ENODEV);
119
120 return dev;
121}
122
123static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100124 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300125{
126 int i = cb->args[0];
127 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
128 int rc;
129
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300130 if (!dev) {
131 dev = __get_device_from_cb(cb);
132 if (IS_ERR(dev))
133 return PTR_ERR(dev);
134
135 cb->args[1] = (long) dev;
136 }
137
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200138 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300139
140 cb->seq = dev->targets_generation;
141
142 while (i < dev->n_targets) {
143 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100144 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145 if (rc < 0)
146 break;
147
148 i++;
149 }
150
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200151 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300152
153 cb->args[0] = i;
154
155 return skb->len;
156}
157
158static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
159{
160 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
161
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300162 if (dev)
163 nfc_put_device(dev);
164
165 return 0;
166}
167
168int nfc_genl_targets_found(struct nfc_dev *dev)
169{
170 struct sk_buff *msg;
171 void *hdr;
172
Eric W. Biederman15e47302012-09-07 20:12:54 +0000173 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300174
Thomas Graf58050fc2012-06-28 03:57:45 +0000175 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300176 if (!msg)
177 return -ENOMEM;
178
179 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100180 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300181 if (!hdr)
182 goto free_msg;
183
David S. Miller1e6428d2012-03-29 23:23:57 -0400184 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
185 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300186
187 genlmsg_end(msg, hdr);
188
189 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
190
191nla_put_failure:
192 genlmsg_cancel(msg, hdr);
193free_msg:
194 nlmsg_free(msg);
195 return -EMSGSIZE;
196}
197
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200198int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
199{
200 struct sk_buff *msg;
201 void *hdr;
202
Thomas Graf58050fc2012-06-28 03:57:45 +0000203 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200204 if (!msg)
205 return -ENOMEM;
206
207 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
208 NFC_EVENT_TARGET_LOST);
209 if (!hdr)
210 goto free_msg;
211
John W. Linville59ef43e2012-04-18 14:17:13 -0400212 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
213 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
214 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200215
216 genlmsg_end(msg, hdr);
217
218 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
219
220 return 0;
221
222nla_put_failure:
223 genlmsg_cancel(msg, hdr);
224free_msg:
225 nlmsg_free(msg);
226 return -EMSGSIZE;
227}
228
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200229int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
230{
231 struct sk_buff *msg;
232 void *hdr;
233
Thomas Graf58050fc2012-06-28 03:57:45 +0000234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200235 if (!msg)
236 return -ENOMEM;
237
238 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
239 NFC_EVENT_TM_ACTIVATED);
240 if (!hdr)
241 goto free_msg;
242
243 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
244 goto nla_put_failure;
245 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
246 goto nla_put_failure;
247
248 genlmsg_end(msg, hdr);
249
250 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
251
252 return 0;
253
254nla_put_failure:
255 genlmsg_cancel(msg, hdr);
256free_msg:
257 nlmsg_free(msg);
258 return -EMSGSIZE;
259}
260
261int nfc_genl_tm_deactivated(struct nfc_dev *dev)
262{
263 struct sk_buff *msg;
264 void *hdr;
265
Thomas Graf58050fc2012-06-28 03:57:45 +0000266 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200267 if (!msg)
268 return -ENOMEM;
269
270 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
271 NFC_EVENT_TM_DEACTIVATED);
272 if (!hdr)
273 goto free_msg;
274
275 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
276 goto nla_put_failure;
277
278 genlmsg_end(msg, hdr);
279
280 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
281
282 return 0;
283
284nla_put_failure:
285 genlmsg_cancel(msg, hdr);
286free_msg:
287 nlmsg_free(msg);
288 return -EMSGSIZE;
289}
290
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300291int nfc_genl_device_added(struct nfc_dev *dev)
292{
293 struct sk_buff *msg;
294 void *hdr;
295
Thomas Graf58050fc2012-06-28 03:57:45 +0000296 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300297 if (!msg)
298 return -ENOMEM;
299
300 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100301 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300302 if (!hdr)
303 goto free_msg;
304
David S. Miller1e6428d2012-03-29 23:23:57 -0400305 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
306 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
307 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
308 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
309 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300310
311 genlmsg_end(msg, hdr);
312
313 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
314
315 return 0;
316
317nla_put_failure:
318 genlmsg_cancel(msg, hdr);
319free_msg:
320 nlmsg_free(msg);
321 return -EMSGSIZE;
322}
323
324int nfc_genl_device_removed(struct nfc_dev *dev)
325{
326 struct sk_buff *msg;
327 void *hdr;
328
Thomas Graf58050fc2012-06-28 03:57:45 +0000329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300330 if (!msg)
331 return -ENOMEM;
332
333 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100334 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300335 if (!hdr)
336 goto free_msg;
337
David S. Miller1e6428d2012-03-29 23:23:57 -0400338 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
339 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300340
341 genlmsg_end(msg, hdr);
342
343 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
344
345 return 0;
346
347nla_put_failure:
348 genlmsg_cancel(msg, hdr);
349free_msg:
350 nlmsg_free(msg);
351 return -EMSGSIZE;
352}
353
354static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000355 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100356 struct netlink_callback *cb,
357 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300358{
359 void *hdr;
360
Eric W. Biederman15e47302012-09-07 20:12:54 +0000361 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100362 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300363 if (!hdr)
364 return -EMSGSIZE;
365
366 if (cb)
367 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
368
David S. Miller1e6428d2012-03-29 23:23:57 -0400369 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
370 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
371 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100372 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200373 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
374 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400375 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300376
377 return genlmsg_end(msg, hdr);
378
379nla_put_failure:
380 genlmsg_cancel(msg, hdr);
381 return -EMSGSIZE;
382}
383
384static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100385 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300386{
387 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
388 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
389 bool first_call = false;
390
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300391 if (!iter) {
392 first_call = true;
393 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
394 if (!iter)
395 return -ENOMEM;
396 cb->args[0] = (long) iter;
397 }
398
399 mutex_lock(&nfc_devlist_mutex);
400
401 cb->seq = nfc_devlist_generation;
402
403 if (first_call) {
404 nfc_device_iter_init(iter);
405 dev = nfc_device_iter_next(iter);
406 }
407
408 while (dev) {
409 int rc;
410
Eric W. Biederman15e47302012-09-07 20:12:54 +0000411 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100412 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300413 if (rc < 0)
414 break;
415
416 dev = nfc_device_iter_next(iter);
417 }
418
419 mutex_unlock(&nfc_devlist_mutex);
420
421 cb->args[1] = (long) dev;
422
423 return skb->len;
424}
425
426static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
427{
428 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
429
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300430 nfc_device_iter_exit(iter);
431 kfree(iter);
432
433 return 0;
434}
435
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100436int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100437 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100438{
439 struct sk_buff *msg;
440 void *hdr;
441
442 pr_debug("DEP link is up\n");
443
Thomas Graf58050fc2012-06-28 03:57:45 +0000444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100445 if (!msg)
446 return -ENOMEM;
447
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100448 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100449 if (!hdr)
450 goto free_msg;
451
David S. Miller1e6428d2012-03-29 23:23:57 -0400452 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
453 goto nla_put_failure;
454 if (rf_mode == NFC_RF_INITIATOR &&
455 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
456 goto nla_put_failure;
457 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
458 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
459 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100460
461 genlmsg_end(msg, hdr);
462
463 dev->dep_link_up = true;
464
465 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
466
467 return 0;
468
469nla_put_failure:
470 genlmsg_cancel(msg, hdr);
471free_msg:
472 nlmsg_free(msg);
473 return -EMSGSIZE;
474}
475
476int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
477{
478 struct sk_buff *msg;
479 void *hdr;
480
481 pr_debug("DEP link is down\n");
482
Thomas Graf58050fc2012-06-28 03:57:45 +0000483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100484 if (!msg)
485 return -ENOMEM;
486
487 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100488 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100489 if (!hdr)
490 goto free_msg;
491
David S. Miller1e6428d2012-03-29 23:23:57 -0400492 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
493 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100494
495 genlmsg_end(msg, hdr);
496
497 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
498
499 return 0;
500
501nla_put_failure:
502 genlmsg_cancel(msg, hdr);
503free_msg:
504 nlmsg_free(msg);
505 return -EMSGSIZE;
506}
507
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300508static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
509{
510 struct sk_buff *msg;
511 struct nfc_dev *dev;
512 u32 idx;
513 int rc = -ENOBUFS;
514
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300515 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
516 return -EINVAL;
517
518 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
519
520 dev = nfc_get_device(idx);
521 if (!dev)
522 return -ENODEV;
523
Thomas Graf58050fc2012-06-28 03:57:45 +0000524 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300525 if (!msg) {
526 rc = -ENOMEM;
527 goto out_putdev;
528 }
529
Eric W. Biederman15e47302012-09-07 20:12:54 +0000530 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100531 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300532 if (rc < 0)
533 goto out_free;
534
535 nfc_put_device(dev);
536
537 return genlmsg_reply(msg, info);
538
539out_free:
540 nlmsg_free(msg);
541out_putdev:
542 nfc_put_device(dev);
543 return rc;
544}
545
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300546static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
547{
548 struct nfc_dev *dev;
549 int rc;
550 u32 idx;
551
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300552 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
553 return -EINVAL;
554
555 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
556
557 dev = nfc_get_device(idx);
558 if (!dev)
559 return -ENODEV;
560
561 rc = nfc_dev_up(dev);
562
563 nfc_put_device(dev);
564 return rc;
565}
566
567static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
568{
569 struct nfc_dev *dev;
570 int rc;
571 u32 idx;
572
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300573 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
574 return -EINVAL;
575
576 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
577
578 dev = nfc_get_device(idx);
579 if (!dev)
580 return -ENODEV;
581
582 rc = nfc_dev_down(dev);
583
584 nfc_put_device(dev);
585 return rc;
586}
587
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300588static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
589{
590 struct nfc_dev *dev;
591 int rc;
592 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200593 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300594
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100595 pr_debug("Poll start\n");
596
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300597 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200598 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
599 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200600 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300601 return -EINVAL;
602
603 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200604
605 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
606 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200607
608 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
609 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200610 else if (info->attrs[NFC_ATTR_PROTOCOLS])
611 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300612
613 dev = nfc_get_device(idx);
614 if (!dev)
615 return -ENODEV;
616
617 mutex_lock(&dev->genl_data.genl_data_mutex);
618
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200619 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300620 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000621 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300622
623 mutex_unlock(&dev->genl_data.genl_data_mutex);
624
625 nfc_put_device(dev);
626 return rc;
627}
628
629static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
630{
631 struct nfc_dev *dev;
632 int rc;
633 u32 idx;
634
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300635 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
636 return -EINVAL;
637
638 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
639
640 dev = nfc_get_device(idx);
641 if (!dev)
642 return -ENODEV;
643
Samuel Ortiza831b912012-06-28 16:41:57 +0200644 device_lock(&dev->dev);
645
646 if (!dev->polling) {
647 device_unlock(&dev->dev);
648 return -EINVAL;
649 }
650
651 device_unlock(&dev->dev);
652
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300653 mutex_lock(&dev->genl_data.genl_data_mutex);
654
Eric W. Biederman15e47302012-09-07 20:12:54 +0000655 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300656 rc = -EBUSY;
657 goto out;
658 }
659
660 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000661 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300662
663out:
664 mutex_unlock(&dev->genl_data.genl_data_mutex);
665 nfc_put_device(dev);
666 return rc;
667}
668
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100669static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
670{
671 struct nfc_dev *dev;
672 int rc, tgt_idx;
673 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100674 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100675
676 pr_debug("DEP link up\n");
677
678 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100679 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100680 return -EINVAL;
681
682 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
683 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
684 tgt_idx = NFC_TARGET_IDX_ANY;
685 else
686 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
687
688 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100689
690 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
691 return -EINVAL;
692
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100693 dev = nfc_get_device(idx);
694 if (!dev)
695 return -ENODEV;
696
Samuel Ortiz47807d32012-03-05 01:03:50 +0100697 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100698
699 nfc_put_device(dev);
700
701 return rc;
702}
703
704static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
705{
706 struct nfc_dev *dev;
707 int rc;
708 u32 idx;
709
710 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_dep_link_down(dev);
720
721 nfc_put_device(dev);
722 return rc;
723}
724
Thierry Escande52feb442012-10-17 14:43:39 +0200725static int nfc_genl_send_params(struct sk_buff *msg,
726 struct nfc_llcp_local *local,
727 u32 portid, u32 seq)
728{
729 void *hdr;
730
731 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
732 NFC_CMD_LLC_GET_PARAMS);
733 if (!hdr)
734 return -EMSGSIZE;
735
736 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
737 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
738 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
739 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
740 goto nla_put_failure;
741
742 return genlmsg_end(msg, hdr);
743
744nla_put_failure:
745
746 genlmsg_cancel(msg, hdr);
747 return -EMSGSIZE;
748}
749
750static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
751{
752 struct nfc_dev *dev;
753 struct nfc_llcp_local *local;
754 int rc = 0;
755 struct sk_buff *msg = NULL;
756 u32 idx;
757
758 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
759 return -EINVAL;
760
761 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
762
763 dev = nfc_get_device(idx);
764 if (!dev)
765 return -ENODEV;
766
767 device_lock(&dev->dev);
768
769 local = nfc_llcp_find_local(dev);
770 if (!local) {
771 rc = -ENODEV;
772 goto exit;
773 }
774
775 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
776 if (!msg) {
777 rc = -ENOMEM;
778 goto exit;
779 }
780
781 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
782
783exit:
784 device_unlock(&dev->dev);
785
786 nfc_put_device(dev);
787
788 if (rc < 0) {
789 if (msg)
790 nlmsg_free(msg);
791
792 return rc;
793 }
794
795 return genlmsg_reply(msg, info);
796}
797
798static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
799{
800 struct nfc_dev *dev;
801 struct nfc_llcp_local *local;
802 u8 rw = 0;
803 u16 miux = 0;
804 u32 idx;
805 int rc = 0;
806
807 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
808 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
809 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
810 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
811 return -EINVAL;
812
813 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
814 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
815
816 if (rw > LLCP_MAX_RW)
817 return -EINVAL;
818 }
819
820 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
821 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
822
823 if (miux > LLCP_MAX_MIUX)
824 return -EINVAL;
825 }
826
827 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
828
829 dev = nfc_get_device(idx);
830 if (!dev)
831 return -ENODEV;
832
833 device_lock(&dev->dev);
834
835 local = nfc_llcp_find_local(dev);
836 if (!local) {
837 nfc_put_device(dev);
838 rc = -ENODEV;
839 goto exit;
840 }
841
842 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
843 if (dev->dep_link_up) {
844 rc = -EINPROGRESS;
845 goto exit;
846 }
847
848 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
849 }
850
851 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
852 local->rw = rw;
853
854 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
855 local->miux = cpu_to_be16(miux);
856
857exit:
858 device_unlock(&dev->dev);
859
860 nfc_put_device(dev);
861
862 return rc;
863}
864
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300865static struct genl_ops nfc_genl_ops[] = {
866 {
867 .cmd = NFC_CMD_GET_DEVICE,
868 .doit = nfc_genl_get_device,
869 .dumpit = nfc_genl_dump_devices,
870 .done = nfc_genl_dump_devices_done,
871 .policy = nfc_genl_policy,
872 },
873 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300874 .cmd = NFC_CMD_DEV_UP,
875 .doit = nfc_genl_dev_up,
876 .policy = nfc_genl_policy,
877 },
878 {
879 .cmd = NFC_CMD_DEV_DOWN,
880 .doit = nfc_genl_dev_down,
881 .policy = nfc_genl_policy,
882 },
883 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300884 .cmd = NFC_CMD_START_POLL,
885 .doit = nfc_genl_start_poll,
886 .policy = nfc_genl_policy,
887 },
888 {
889 .cmd = NFC_CMD_STOP_POLL,
890 .doit = nfc_genl_stop_poll,
891 .policy = nfc_genl_policy,
892 },
893 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100894 .cmd = NFC_CMD_DEP_LINK_UP,
895 .doit = nfc_genl_dep_link_up,
896 .policy = nfc_genl_policy,
897 },
898 {
899 .cmd = NFC_CMD_DEP_LINK_DOWN,
900 .doit = nfc_genl_dep_link_down,
901 .policy = nfc_genl_policy,
902 },
903 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300904 .cmd = NFC_CMD_GET_TARGET,
905 .dumpit = nfc_genl_dump_targets,
906 .done = nfc_genl_dump_targets_done,
907 .policy = nfc_genl_policy,
908 },
Thierry Escande52feb442012-10-17 14:43:39 +0200909 {
910 .cmd = NFC_CMD_LLC_GET_PARAMS,
911 .doit = nfc_genl_llc_get_params,
912 .policy = nfc_genl_policy,
913 },
914 {
915 .cmd = NFC_CMD_LLC_SET_PARAMS,
916 .doit = nfc_genl_llc_set_params,
917 .policy = nfc_genl_policy,
918 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300919};
920
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200921
922struct urelease_work {
923 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -0400924 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200925};
926
927static void nfc_urelease_event_work(struct work_struct *work)
928{
929 struct urelease_work *w = container_of(work, struct urelease_work, w);
930 struct class_dev_iter iter;
931 struct nfc_dev *dev;
932
John W. Linvillec4876062012-09-28 11:11:16 -0400933 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200934
935 mutex_lock(&nfc_devlist_mutex);
936
937 nfc_device_iter_init(&iter);
938 dev = nfc_device_iter_next(&iter);
939
940 while (dev) {
941 mutex_lock(&dev->genl_data.genl_data_mutex);
942
John W. Linvillec4876062012-09-28 11:11:16 -0400943 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200944 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -0400945 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200946 }
947
948 mutex_unlock(&dev->genl_data.genl_data_mutex);
949
950 dev = nfc_device_iter_next(&iter);
951 }
952
953 nfc_device_iter_exit(&iter);
954
955 mutex_unlock(&nfc_devlist_mutex);
956
957 kfree(w);
958}
959
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300960static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100961 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300962{
963 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200964 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300965
966 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
967 goto out;
968
Eric W. Biederman15e47302012-09-07 20:12:54 +0000969 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300970
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200971 w = kmalloc(sizeof(*w), GFP_ATOMIC);
972 if (w) {
973 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -0400974 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200975 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300976 }
977
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300978out:
979 return NOTIFY_DONE;
980}
981
982void nfc_genl_data_init(struct nfc_genl_data *genl_data)
983{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000984 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300985 mutex_init(&genl_data->genl_data_mutex);
986}
987
988void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
989{
990 mutex_destroy(&genl_data->genl_data_mutex);
991}
992
993static struct notifier_block nl_notifier = {
994 .notifier_call = nfc_genl_rcv_nl_event,
995};
996
997/**
998 * nfc_genl_init() - Initialize netlink interface
999 *
1000 * This initialization function registers the nfc netlink family.
1001 */
1002int __init nfc_genl_init(void)
1003{
1004 int rc;
1005
1006 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001007 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001008 if (rc)
1009 return rc;
1010
1011 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1012
1013 netlink_register_notifier(&nl_notifier);
1014
1015 return rc;
1016}
1017
1018/**
1019 * nfc_genl_exit() - Deinitialize netlink interface
1020 *
1021 * This exit function unregisters the nfc netlink family.
1022 */
1023void nfc_genl_exit(void)
1024{
1025 netlink_unregister_notifier(&nl_notifier);
1026 genl_unregister_family(&nfc_genl_family);
1027}