blob: ab9edfdd4b85cfffa33ec6e7103d9f679e2d6ea5 [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
32static struct genl_multicast_group nfc_genl_event_mcgrp = {
33 .name = NFC_GENL_MCAST_EVENT_NAME,
34};
35
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020036static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030037 .id = GENL_ID_GENERATE,
38 .hdrsize = 0,
39 .name = NFC_GENL_NAME,
40 .version = NFC_GENL_VERSION,
41 .maxattr = NFC_ATTR_MAX,
42};
43
44static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
45 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
46 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
47 .len = NFC_DEVICE_NAME_MAXSIZE },
48 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010049 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010051 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020052 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
53 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030054};
55
56static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010057 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030058{
59 void *hdr;
60
Eric W. Biederman15e47302012-09-07 20:12:54 +000061 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010062 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030063 if (!hdr)
64 return -EMSGSIZE;
65
66 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
67
David S. Miller1e6428d2012-03-29 23:23:57 -040068 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
69 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
70 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
71 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
72 goto nla_put_failure;
73 if (target->nfcid1_len > 0 &&
74 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
75 target->nfcid1))
76 goto nla_put_failure;
77 if (target->sensb_res_len > 0 &&
78 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
79 target->sensb_res))
80 goto nla_put_failure;
81 if (target->sensf_res_len > 0 &&
82 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
83 target->sensf_res))
84 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030085
86 return genlmsg_end(msg, hdr);
87
88nla_put_failure:
89 genlmsg_cancel(msg, hdr);
90 return -EMSGSIZE;
91}
92
93static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
94{
95 struct nfc_dev *dev;
96 int rc;
97 u32 idx;
98
99 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100100 nfc_genl_family.attrbuf,
101 nfc_genl_family.maxattr,
102 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300103 if (rc < 0)
104 return ERR_PTR(rc);
105
106 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
107 return ERR_PTR(-EINVAL);
108
109 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
110
111 dev = nfc_get_device(idx);
112 if (!dev)
113 return ERR_PTR(-ENODEV);
114
115 return dev;
116}
117
118static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100119 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300120{
121 int i = cb->args[0];
122 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
123 int rc;
124
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300125 if (!dev) {
126 dev = __get_device_from_cb(cb);
127 if (IS_ERR(dev))
128 return PTR_ERR(dev);
129
130 cb->args[1] = (long) dev;
131 }
132
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200133 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300134
135 cb->seq = dev->targets_generation;
136
137 while (i < dev->n_targets) {
138 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100139 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300140 if (rc < 0)
141 break;
142
143 i++;
144 }
145
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200146 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300147
148 cb->args[0] = i;
149
150 return skb->len;
151}
152
153static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
154{
155 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
156
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300157 if (dev)
158 nfc_put_device(dev);
159
160 return 0;
161}
162
163int nfc_genl_targets_found(struct nfc_dev *dev)
164{
165 struct sk_buff *msg;
166 void *hdr;
167
Eric W. Biederman15e47302012-09-07 20:12:54 +0000168 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300169
Thomas Graf58050fc2012-06-28 03:57:45 +0000170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300171 if (!msg)
172 return -ENOMEM;
173
174 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100175 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300176 if (!hdr)
177 goto free_msg;
178
David S. Miller1e6428d2012-03-29 23:23:57 -0400179 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
180 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300181
182 genlmsg_end(msg, hdr);
183
184 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
185
186nla_put_failure:
187 genlmsg_cancel(msg, hdr);
188free_msg:
189 nlmsg_free(msg);
190 return -EMSGSIZE;
191}
192
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200193int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
194{
195 struct sk_buff *msg;
196 void *hdr;
197
Thomas Graf58050fc2012-06-28 03:57:45 +0000198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200199 if (!msg)
200 return -ENOMEM;
201
202 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
203 NFC_EVENT_TARGET_LOST);
204 if (!hdr)
205 goto free_msg;
206
John W. Linville59ef43e2012-04-18 14:17:13 -0400207 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
208 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
209 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200210
211 genlmsg_end(msg, hdr);
212
213 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
214
215 return 0;
216
217nla_put_failure:
218 genlmsg_cancel(msg, hdr);
219free_msg:
220 nlmsg_free(msg);
221 return -EMSGSIZE;
222}
223
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200224int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
225{
226 struct sk_buff *msg;
227 void *hdr;
228
Thomas Graf58050fc2012-06-28 03:57:45 +0000229 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200230 if (!msg)
231 return -ENOMEM;
232
233 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
234 NFC_EVENT_TM_ACTIVATED);
235 if (!hdr)
236 goto free_msg;
237
238 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
239 goto nla_put_failure;
240 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
241 goto nla_put_failure;
242
243 genlmsg_end(msg, hdr);
244
245 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
246
247 return 0;
248
249nla_put_failure:
250 genlmsg_cancel(msg, hdr);
251free_msg:
252 nlmsg_free(msg);
253 return -EMSGSIZE;
254}
255
256int nfc_genl_tm_deactivated(struct nfc_dev *dev)
257{
258 struct sk_buff *msg;
259 void *hdr;
260
Thomas Graf58050fc2012-06-28 03:57:45 +0000261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200262 if (!msg)
263 return -ENOMEM;
264
265 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
266 NFC_EVENT_TM_DEACTIVATED);
267 if (!hdr)
268 goto free_msg;
269
270 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
271 goto nla_put_failure;
272
273 genlmsg_end(msg, hdr);
274
275 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
276
277 return 0;
278
279nla_put_failure:
280 genlmsg_cancel(msg, hdr);
281free_msg:
282 nlmsg_free(msg);
283 return -EMSGSIZE;
284}
285
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300286int nfc_genl_device_added(struct nfc_dev *dev)
287{
288 struct sk_buff *msg;
289 void *hdr;
290
Thomas Graf58050fc2012-06-28 03:57:45 +0000291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300292 if (!msg)
293 return -ENOMEM;
294
295 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100296 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300297 if (!hdr)
298 goto free_msg;
299
David S. Miller1e6428d2012-03-29 23:23:57 -0400300 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
301 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
302 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
303 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
304 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300305
306 genlmsg_end(msg, hdr);
307
308 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
309
310 return 0;
311
312nla_put_failure:
313 genlmsg_cancel(msg, hdr);
314free_msg:
315 nlmsg_free(msg);
316 return -EMSGSIZE;
317}
318
319int nfc_genl_device_removed(struct nfc_dev *dev)
320{
321 struct sk_buff *msg;
322 void *hdr;
323
Thomas Graf58050fc2012-06-28 03:57:45 +0000324 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300325 if (!msg)
326 return -ENOMEM;
327
328 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100329 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300330 if (!hdr)
331 goto free_msg;
332
David S. Miller1e6428d2012-03-29 23:23:57 -0400333 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
334 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300335
336 genlmsg_end(msg, hdr);
337
338 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
339
340 return 0;
341
342nla_put_failure:
343 genlmsg_cancel(msg, hdr);
344free_msg:
345 nlmsg_free(msg);
346 return -EMSGSIZE;
347}
348
349static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000350 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100351 struct netlink_callback *cb,
352 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300353{
354 void *hdr;
355
Eric W. Biederman15e47302012-09-07 20:12:54 +0000356 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100357 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300358 if (!hdr)
359 return -EMSGSIZE;
360
361 if (cb)
362 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
363
David S. Miller1e6428d2012-03-29 23:23:57 -0400364 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
365 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
366 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200367 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
368 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400369 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300370
371 return genlmsg_end(msg, hdr);
372
373nla_put_failure:
374 genlmsg_cancel(msg, hdr);
375 return -EMSGSIZE;
376}
377
378static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100379 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300380{
381 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
382 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
383 bool first_call = false;
384
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300385 if (!iter) {
386 first_call = true;
387 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
388 if (!iter)
389 return -ENOMEM;
390 cb->args[0] = (long) iter;
391 }
392
393 mutex_lock(&nfc_devlist_mutex);
394
395 cb->seq = nfc_devlist_generation;
396
397 if (first_call) {
398 nfc_device_iter_init(iter);
399 dev = nfc_device_iter_next(iter);
400 }
401
402 while (dev) {
403 int rc;
404
Eric W. Biederman15e47302012-09-07 20:12:54 +0000405 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100406 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300407 if (rc < 0)
408 break;
409
410 dev = nfc_device_iter_next(iter);
411 }
412
413 mutex_unlock(&nfc_devlist_mutex);
414
415 cb->args[1] = (long) dev;
416
417 return skb->len;
418}
419
420static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
421{
422 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
423
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300424 nfc_device_iter_exit(iter);
425 kfree(iter);
426
427 return 0;
428}
429
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100430int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100431 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100432{
433 struct sk_buff *msg;
434 void *hdr;
435
436 pr_debug("DEP link is up\n");
437
Thomas Graf58050fc2012-06-28 03:57:45 +0000438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100439 if (!msg)
440 return -ENOMEM;
441
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100442 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100443 if (!hdr)
444 goto free_msg;
445
David S. Miller1e6428d2012-03-29 23:23:57 -0400446 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
447 goto nla_put_failure;
448 if (rf_mode == NFC_RF_INITIATOR &&
449 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
450 goto nla_put_failure;
451 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
452 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
453 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100454
455 genlmsg_end(msg, hdr);
456
457 dev->dep_link_up = true;
458
459 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
460
461 return 0;
462
463nla_put_failure:
464 genlmsg_cancel(msg, hdr);
465free_msg:
466 nlmsg_free(msg);
467 return -EMSGSIZE;
468}
469
470int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
471{
472 struct sk_buff *msg;
473 void *hdr;
474
475 pr_debug("DEP link is down\n");
476
Thomas Graf58050fc2012-06-28 03:57:45 +0000477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100478 if (!msg)
479 return -ENOMEM;
480
481 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100482 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100483 if (!hdr)
484 goto free_msg;
485
David S. Miller1e6428d2012-03-29 23:23:57 -0400486 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
487 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100488
489 genlmsg_end(msg, hdr);
490
491 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
492
493 return 0;
494
495nla_put_failure:
496 genlmsg_cancel(msg, hdr);
497free_msg:
498 nlmsg_free(msg);
499 return -EMSGSIZE;
500}
501
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300502static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
503{
504 struct sk_buff *msg;
505 struct nfc_dev *dev;
506 u32 idx;
507 int rc = -ENOBUFS;
508
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300509 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
510 return -EINVAL;
511
512 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
513
514 dev = nfc_get_device(idx);
515 if (!dev)
516 return -ENODEV;
517
Thomas Graf58050fc2012-06-28 03:57:45 +0000518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300519 if (!msg) {
520 rc = -ENOMEM;
521 goto out_putdev;
522 }
523
Eric W. Biederman15e47302012-09-07 20:12:54 +0000524 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100525 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300526 if (rc < 0)
527 goto out_free;
528
529 nfc_put_device(dev);
530
531 return genlmsg_reply(msg, info);
532
533out_free:
534 nlmsg_free(msg);
535out_putdev:
536 nfc_put_device(dev);
537 return rc;
538}
539
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300540static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
541{
542 struct nfc_dev *dev;
543 int rc;
544 u32 idx;
545
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300546 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
547 return -EINVAL;
548
549 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
550
551 dev = nfc_get_device(idx);
552 if (!dev)
553 return -ENODEV;
554
555 rc = nfc_dev_up(dev);
556
557 nfc_put_device(dev);
558 return rc;
559}
560
561static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
562{
563 struct nfc_dev *dev;
564 int rc;
565 u32 idx;
566
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300567 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
568 return -EINVAL;
569
570 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
571
572 dev = nfc_get_device(idx);
573 if (!dev)
574 return -ENODEV;
575
576 rc = nfc_dev_down(dev);
577
578 nfc_put_device(dev);
579 return rc;
580}
581
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300582static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
583{
584 struct nfc_dev *dev;
585 int rc;
586 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200587 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300588
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100589 pr_debug("Poll start\n");
590
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300591 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200592 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
593 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
594 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300595 return -EINVAL;
596
597 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200598
599 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
600 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200601
602 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
603 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200604 else if (info->attrs[NFC_ATTR_PROTOCOLS])
605 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606
607 dev = nfc_get_device(idx);
608 if (!dev)
609 return -ENODEV;
610
611 mutex_lock(&dev->genl_data.genl_data_mutex);
612
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200613 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300614 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000615 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300616
617 mutex_unlock(&dev->genl_data.genl_data_mutex);
618
619 nfc_put_device(dev);
620 return rc;
621}
622
623static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
624{
625 struct nfc_dev *dev;
626 int rc;
627 u32 idx;
628
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300629 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
630 return -EINVAL;
631
632 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
633
634 dev = nfc_get_device(idx);
635 if (!dev)
636 return -ENODEV;
637
Samuel Ortiza831b912012-06-28 16:41:57 +0200638 device_lock(&dev->dev);
639
640 if (!dev->polling) {
641 device_unlock(&dev->dev);
642 return -EINVAL;
643 }
644
645 device_unlock(&dev->dev);
646
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300647 mutex_lock(&dev->genl_data.genl_data_mutex);
648
Eric W. Biederman15e47302012-09-07 20:12:54 +0000649 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300650 rc = -EBUSY;
651 goto out;
652 }
653
654 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000655 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300656
657out:
658 mutex_unlock(&dev->genl_data.genl_data_mutex);
659 nfc_put_device(dev);
660 return rc;
661}
662
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100663static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
664{
665 struct nfc_dev *dev;
666 int rc, tgt_idx;
667 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100668 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100669
670 pr_debug("DEP link up\n");
671
672 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100673 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100674 return -EINVAL;
675
676 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
677 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
678 tgt_idx = NFC_TARGET_IDX_ANY;
679 else
680 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
681
682 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100683
684 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
685 return -EINVAL;
686
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100687 dev = nfc_get_device(idx);
688 if (!dev)
689 return -ENODEV;
690
Samuel Ortiz47807d32012-03-05 01:03:50 +0100691 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100692
693 nfc_put_device(dev);
694
695 return rc;
696}
697
698static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
699{
700 struct nfc_dev *dev;
701 int rc;
702 u32 idx;
703
704 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
705 return -EINVAL;
706
707 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
708
709 dev = nfc_get_device(idx);
710 if (!dev)
711 return -ENODEV;
712
713 rc = nfc_dep_link_down(dev);
714
715 nfc_put_device(dev);
716 return rc;
717}
718
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300719static struct genl_ops nfc_genl_ops[] = {
720 {
721 .cmd = NFC_CMD_GET_DEVICE,
722 .doit = nfc_genl_get_device,
723 .dumpit = nfc_genl_dump_devices,
724 .done = nfc_genl_dump_devices_done,
725 .policy = nfc_genl_policy,
726 },
727 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300728 .cmd = NFC_CMD_DEV_UP,
729 .doit = nfc_genl_dev_up,
730 .policy = nfc_genl_policy,
731 },
732 {
733 .cmd = NFC_CMD_DEV_DOWN,
734 .doit = nfc_genl_dev_down,
735 .policy = nfc_genl_policy,
736 },
737 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300738 .cmd = NFC_CMD_START_POLL,
739 .doit = nfc_genl_start_poll,
740 .policy = nfc_genl_policy,
741 },
742 {
743 .cmd = NFC_CMD_STOP_POLL,
744 .doit = nfc_genl_stop_poll,
745 .policy = nfc_genl_policy,
746 },
747 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100748 .cmd = NFC_CMD_DEP_LINK_UP,
749 .doit = nfc_genl_dep_link_up,
750 .policy = nfc_genl_policy,
751 },
752 {
753 .cmd = NFC_CMD_DEP_LINK_DOWN,
754 .doit = nfc_genl_dep_link_down,
755 .policy = nfc_genl_policy,
756 },
757 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300758 .cmd = NFC_CMD_GET_TARGET,
759 .dumpit = nfc_genl_dump_targets,
760 .done = nfc_genl_dump_targets_done,
761 .policy = nfc_genl_policy,
762 },
763};
764
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200765
766struct urelease_work {
767 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -0400768 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200769};
770
771static void nfc_urelease_event_work(struct work_struct *work)
772{
773 struct urelease_work *w = container_of(work, struct urelease_work, w);
774 struct class_dev_iter iter;
775 struct nfc_dev *dev;
776
John W. Linvillec4876062012-09-28 11:11:16 -0400777 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200778
779 mutex_lock(&nfc_devlist_mutex);
780
781 nfc_device_iter_init(&iter);
782 dev = nfc_device_iter_next(&iter);
783
784 while (dev) {
785 mutex_lock(&dev->genl_data.genl_data_mutex);
786
John W. Linvillec4876062012-09-28 11:11:16 -0400787 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200788 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -0400789 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200790 }
791
792 mutex_unlock(&dev->genl_data.genl_data_mutex);
793
794 dev = nfc_device_iter_next(&iter);
795 }
796
797 nfc_device_iter_exit(&iter);
798
799 mutex_unlock(&nfc_devlist_mutex);
800
801 kfree(w);
802}
803
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300804static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100805 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300806{
807 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200808 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300809
810 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
811 goto out;
812
Eric W. Biederman15e47302012-09-07 20:12:54 +0000813 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300814
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200815 w = kmalloc(sizeof(*w), GFP_ATOMIC);
816 if (w) {
817 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -0400818 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +0200819 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300820 }
821
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300822out:
823 return NOTIFY_DONE;
824}
825
826void nfc_genl_data_init(struct nfc_genl_data *genl_data)
827{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000828 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300829 mutex_init(&genl_data->genl_data_mutex);
830}
831
832void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
833{
834 mutex_destroy(&genl_data->genl_data_mutex);
835}
836
837static struct notifier_block nl_notifier = {
838 .notifier_call = nfc_genl_rcv_nl_event,
839};
840
841/**
842 * nfc_genl_init() - Initialize netlink interface
843 *
844 * This initialization function registers the nfc netlink family.
845 */
846int __init nfc_genl_init(void)
847{
848 int rc;
849
850 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100851 ARRAY_SIZE(nfc_genl_ops));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300852 if (rc)
853 return rc;
854
855 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
856
857 netlink_register_notifier(&nl_notifier);
858
859 return rc;
860}
861
862/**
863 * nfc_genl_exit() - Deinitialize netlink interface
864 *
865 * This exit function unregisters the nfc netlink family.
866 */
867void nfc_genl_exit(void)
868{
869 netlink_unregister_notifier(&nl_notifier);
870 genl_unregister_family(&nfc_genl_family);
871}