xref: /openbmc/linux/net/nfc/netlink.c (revision 8e9356c6)
1 /*
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, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
23 
24 #include <net/genetlink.h>
25 #include <linux/nfc.h>
26 #include <linux/slab.h>
27 
28 #include "nfc.h"
29 #include "llcp.h"
30 
31 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 	{ .name = NFC_GENL_MCAST_EVENT_NAME, },
33 };
34 
35 static struct genl_family nfc_genl_family = {
36 	.id = GENL_ID_GENERATE,
37 	.hdrsize = 0,
38 	.name = NFC_GENL_NAME,
39 	.version = NFC_GENL_VERSION,
40 	.maxattr = NFC_ATTR_MAX,
41 };
42 
43 static 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 },
48 	[NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 	[NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
50 	[NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
51 	[NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 	[NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
53 	[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 },
56 	[NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
57 	[NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 				     .len = NFC_FIRMWARE_NAME_MAXSIZE },
59 	[NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
60 };
61 
62 static 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 },
65 };
66 
67 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
68 				struct netlink_callback *cb, int flags)
69 {
70 	void *hdr;
71 
72 	hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
73 			  &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
74 	if (!hdr)
75 		return -EMSGSIZE;
76 
77 	genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78 
79 	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;
96 
97 	return genlmsg_end(msg, hdr);
98 
99 nla_put_failure:
100 	genlmsg_cancel(msg, hdr);
101 	return -EMSGSIZE;
102 }
103 
104 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
105 {
106 	struct nfc_dev *dev;
107 	int rc;
108 	u32 idx;
109 
110 	rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
111 			 nfc_genl_family.attrbuf,
112 			 nfc_genl_family.maxattr,
113 			 nfc_genl_policy);
114 	if (rc < 0)
115 		return ERR_PTR(rc);
116 
117 	if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
118 		return ERR_PTR(-EINVAL);
119 
120 	idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
121 
122 	dev = nfc_get_device(idx);
123 	if (!dev)
124 		return ERR_PTR(-ENODEV);
125 
126 	return dev;
127 }
128 
129 static int nfc_genl_dump_targets(struct sk_buff *skb,
130 				 struct netlink_callback *cb)
131 {
132 	int i = cb->args[0];
133 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
134 	int rc;
135 
136 	if (!dev) {
137 		dev = __get_device_from_cb(cb);
138 		if (IS_ERR(dev))
139 			return PTR_ERR(dev);
140 
141 		cb->args[1] = (long) dev;
142 	}
143 
144 	device_lock(&dev->dev);
145 
146 	cb->seq = dev->targets_generation;
147 
148 	while (i < dev->n_targets) {
149 		rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
150 					  NLM_F_MULTI);
151 		if (rc < 0)
152 			break;
153 
154 		i++;
155 	}
156 
157 	device_unlock(&dev->dev);
158 
159 	cb->args[0] = i;
160 
161 	return skb->len;
162 }
163 
164 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
165 {
166 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
167 
168 	if (dev)
169 		nfc_put_device(dev);
170 
171 	return 0;
172 }
173 
174 int nfc_genl_targets_found(struct nfc_dev *dev)
175 {
176 	struct sk_buff *msg;
177 	void *hdr;
178 
179 	dev->genl_data.poll_req_portid = 0;
180 
181 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
182 	if (!msg)
183 		return -ENOMEM;
184 
185 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
186 			  NFC_EVENT_TARGETS_FOUND);
187 	if (!hdr)
188 		goto free_msg;
189 
190 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
191 		goto nla_put_failure;
192 
193 	genlmsg_end(msg, hdr);
194 
195 	return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
196 
197 nla_put_failure:
198 	genlmsg_cancel(msg, hdr);
199 free_msg:
200 	nlmsg_free(msg);
201 	return -EMSGSIZE;
202 }
203 
204 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
205 {
206 	struct sk_buff *msg;
207 	void *hdr;
208 
209 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
210 	if (!msg)
211 		return -ENOMEM;
212 
213 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
214 			  NFC_EVENT_TARGET_LOST);
215 	if (!hdr)
216 		goto free_msg;
217 
218 	if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
219 	    nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
220 		goto nla_put_failure;
221 
222 	genlmsg_end(msg, hdr);
223 
224 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
225 
226 	return 0;
227 
228 nla_put_failure:
229 	genlmsg_cancel(msg, hdr);
230 free_msg:
231 	nlmsg_free(msg);
232 	return -EMSGSIZE;
233 }
234 
235 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
236 {
237 	struct sk_buff *msg;
238 	void *hdr;
239 
240 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
241 	if (!msg)
242 		return -ENOMEM;
243 
244 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
245 			  NFC_EVENT_TM_ACTIVATED);
246 	if (!hdr)
247 		goto free_msg;
248 
249 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
250 		goto nla_put_failure;
251 	if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
252 		goto nla_put_failure;
253 
254 	genlmsg_end(msg, hdr);
255 
256 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
257 
258 	return 0;
259 
260 nla_put_failure:
261 	genlmsg_cancel(msg, hdr);
262 free_msg:
263 	nlmsg_free(msg);
264 	return -EMSGSIZE;
265 }
266 
267 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
268 {
269 	struct sk_buff *msg;
270 	void *hdr;
271 
272 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
273 	if (!msg)
274 		return -ENOMEM;
275 
276 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
277 			  NFC_EVENT_TM_DEACTIVATED);
278 	if (!hdr)
279 		goto free_msg;
280 
281 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
282 		goto nla_put_failure;
283 
284 	genlmsg_end(msg, hdr);
285 
286 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
287 
288 	return 0;
289 
290 nla_put_failure:
291 	genlmsg_cancel(msg, hdr);
292 free_msg:
293 	nlmsg_free(msg);
294 	return -EMSGSIZE;
295 }
296 
297 int nfc_genl_device_added(struct nfc_dev *dev)
298 {
299 	struct sk_buff *msg;
300 	void *hdr;
301 
302 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
303 	if (!msg)
304 		return -ENOMEM;
305 
306 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
307 			  NFC_EVENT_DEVICE_ADDED);
308 	if (!hdr)
309 		goto free_msg;
310 
311 	if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
312 	    nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
313 	    nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
314 	    nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
315 		goto nla_put_failure;
316 
317 	genlmsg_end(msg, hdr);
318 
319 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
320 
321 	return 0;
322 
323 nla_put_failure:
324 	genlmsg_cancel(msg, hdr);
325 free_msg:
326 	nlmsg_free(msg);
327 	return -EMSGSIZE;
328 }
329 
330 int nfc_genl_device_removed(struct nfc_dev *dev)
331 {
332 	struct sk_buff *msg;
333 	void *hdr;
334 
335 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
336 	if (!msg)
337 		return -ENOMEM;
338 
339 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
340 			  NFC_EVENT_DEVICE_REMOVED);
341 	if (!hdr)
342 		goto free_msg;
343 
344 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
345 		goto nla_put_failure;
346 
347 	genlmsg_end(msg, hdr);
348 
349 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
350 
351 	return 0;
352 
353 nla_put_failure:
354 	genlmsg_cancel(msg, hdr);
355 free_msg:
356 	nlmsg_free(msg);
357 	return -EMSGSIZE;
358 }
359 
360 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
361 {
362 	struct sk_buff *msg;
363 	struct nlattr *sdp_attr, *uri_attr;
364 	struct nfc_llcp_sdp_tlv *sdres;
365 	struct hlist_node *n;
366 	void *hdr;
367 	int rc = -EMSGSIZE;
368 	int i;
369 
370 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
371 	if (!msg)
372 		return -ENOMEM;
373 
374 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
375 			  NFC_EVENT_LLC_SDRES);
376 	if (!hdr)
377 		goto free_msg;
378 
379 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
380 		goto nla_put_failure;
381 
382 	sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
383 	if (sdp_attr == NULL) {
384 		rc = -ENOMEM;
385 		goto nla_put_failure;
386 	}
387 
388 	i = 1;
389 	hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
390 		pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
391 
392 		uri_attr = nla_nest_start(msg, i++);
393 		if (uri_attr == NULL) {
394 			rc = -ENOMEM;
395 			goto nla_put_failure;
396 		}
397 
398 		if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
399 			goto nla_put_failure;
400 
401 		if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
402 			goto nla_put_failure;
403 
404 		nla_nest_end(msg, uri_attr);
405 
406 		hlist_del(&sdres->node);
407 
408 		nfc_llcp_free_sdp_tlv(sdres);
409 	}
410 
411 	nla_nest_end(msg, sdp_attr);
412 
413 	genlmsg_end(msg, hdr);
414 
415 	return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
416 
417 nla_put_failure:
418 	genlmsg_cancel(msg, hdr);
419 
420 free_msg:
421 	nlmsg_free(msg);
422 
423 	nfc_llcp_free_sdp_tlv_list(sdres_list);
424 
425 	return rc;
426 }
427 
428 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
429 {
430 	struct sk_buff *msg;
431 	void *hdr;
432 
433 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
434 	if (!msg)
435 		return -ENOMEM;
436 
437 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
438 			  NFC_EVENT_SE_ADDED);
439 	if (!hdr)
440 		goto free_msg;
441 
442 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
443 	    nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
444 	    nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
445 		goto nla_put_failure;
446 
447 	genlmsg_end(msg, hdr);
448 
449 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
450 
451 	return 0;
452 
453 nla_put_failure:
454 	genlmsg_cancel(msg, hdr);
455 free_msg:
456 	nlmsg_free(msg);
457 	return -EMSGSIZE;
458 }
459 
460 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
461 {
462 	struct sk_buff *msg;
463 	void *hdr;
464 
465 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
466 	if (!msg)
467 		return -ENOMEM;
468 
469 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
470 			  NFC_EVENT_SE_REMOVED);
471 	if (!hdr)
472 		goto free_msg;
473 
474 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
475 	    nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
476 		goto nla_put_failure;
477 
478 	genlmsg_end(msg, hdr);
479 
480 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
481 
482 	return 0;
483 
484 nla_put_failure:
485 	genlmsg_cancel(msg, hdr);
486 free_msg:
487 	nlmsg_free(msg);
488 	return -EMSGSIZE;
489 }
490 
491 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
492 				u32 portid, u32 seq,
493 				struct netlink_callback *cb,
494 				int flags)
495 {
496 	void *hdr;
497 
498 	hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
499 			  NFC_CMD_GET_DEVICE);
500 	if (!hdr)
501 		return -EMSGSIZE;
502 
503 	if (cb)
504 		genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
505 
506 	if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
507 	    nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
508 	    nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
509 	    nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
510 	    nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
511 		goto nla_put_failure;
512 
513 	return genlmsg_end(msg, hdr);
514 
515 nla_put_failure:
516 	genlmsg_cancel(msg, hdr);
517 	return -EMSGSIZE;
518 }
519 
520 static int nfc_genl_dump_devices(struct sk_buff *skb,
521 				 struct netlink_callback *cb)
522 {
523 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
524 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
525 	bool first_call = false;
526 
527 	if (!iter) {
528 		first_call = true;
529 		iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
530 		if (!iter)
531 			return -ENOMEM;
532 		cb->args[0] = (long) iter;
533 	}
534 
535 	mutex_lock(&nfc_devlist_mutex);
536 
537 	cb->seq = nfc_devlist_generation;
538 
539 	if (first_call) {
540 		nfc_device_iter_init(iter);
541 		dev = nfc_device_iter_next(iter);
542 	}
543 
544 	while (dev) {
545 		int rc;
546 
547 		rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
548 					  cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
549 		if (rc < 0)
550 			break;
551 
552 		dev = nfc_device_iter_next(iter);
553 	}
554 
555 	mutex_unlock(&nfc_devlist_mutex);
556 
557 	cb->args[1] = (long) dev;
558 
559 	return skb->len;
560 }
561 
562 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
563 {
564 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
565 
566 	nfc_device_iter_exit(iter);
567 	kfree(iter);
568 
569 	return 0;
570 }
571 
572 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
573 			       u8 comm_mode, u8 rf_mode)
574 {
575 	struct sk_buff *msg;
576 	void *hdr;
577 
578 	pr_debug("DEP link is up\n");
579 
580 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
581 	if (!msg)
582 		return -ENOMEM;
583 
584 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
585 	if (!hdr)
586 		goto free_msg;
587 
588 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
589 		goto nla_put_failure;
590 	if (rf_mode == NFC_RF_INITIATOR &&
591 	    nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
592 		goto nla_put_failure;
593 	if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
594 	    nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
595 		goto nla_put_failure;
596 
597 	genlmsg_end(msg, hdr);
598 
599 	dev->dep_link_up = true;
600 
601 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
602 
603 	return 0;
604 
605 nla_put_failure:
606 	genlmsg_cancel(msg, hdr);
607 free_msg:
608 	nlmsg_free(msg);
609 	return -EMSGSIZE;
610 }
611 
612 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
613 {
614 	struct sk_buff *msg;
615 	void *hdr;
616 
617 	pr_debug("DEP link is down\n");
618 
619 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
620 	if (!msg)
621 		return -ENOMEM;
622 
623 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
624 			  NFC_CMD_DEP_LINK_DOWN);
625 	if (!hdr)
626 		goto free_msg;
627 
628 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
629 		goto nla_put_failure;
630 
631 	genlmsg_end(msg, hdr);
632 
633 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
634 
635 	return 0;
636 
637 nla_put_failure:
638 	genlmsg_cancel(msg, hdr);
639 free_msg:
640 	nlmsg_free(msg);
641 	return -EMSGSIZE;
642 }
643 
644 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
645 {
646 	struct sk_buff *msg;
647 	struct nfc_dev *dev;
648 	u32 idx;
649 	int rc = -ENOBUFS;
650 
651 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
652 		return -EINVAL;
653 
654 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
655 
656 	dev = nfc_get_device(idx);
657 	if (!dev)
658 		return -ENODEV;
659 
660 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
661 	if (!msg) {
662 		rc = -ENOMEM;
663 		goto out_putdev;
664 	}
665 
666 	rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
667 				  NULL, 0);
668 	if (rc < 0)
669 		goto out_free;
670 
671 	nfc_put_device(dev);
672 
673 	return genlmsg_reply(msg, info);
674 
675 out_free:
676 	nlmsg_free(msg);
677 out_putdev:
678 	nfc_put_device(dev);
679 	return rc;
680 }
681 
682 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
683 {
684 	struct nfc_dev *dev;
685 	int rc;
686 	u32 idx;
687 
688 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
689 		return -EINVAL;
690 
691 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
692 
693 	dev = nfc_get_device(idx);
694 	if (!dev)
695 		return -ENODEV;
696 
697 	rc = nfc_dev_up(dev);
698 
699 	nfc_put_device(dev);
700 	return rc;
701 }
702 
703 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
704 {
705 	struct nfc_dev *dev;
706 	int rc;
707 	u32 idx;
708 
709 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
710 		return -EINVAL;
711 
712 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713 
714 	dev = nfc_get_device(idx);
715 	if (!dev)
716 		return -ENODEV;
717 
718 	rc = nfc_dev_down(dev);
719 
720 	nfc_put_device(dev);
721 	return rc;
722 }
723 
724 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
725 {
726 	struct nfc_dev *dev;
727 	int rc;
728 	u32 idx;
729 	u32 im_protocols = 0, tm_protocols = 0;
730 
731 	pr_debug("Poll start\n");
732 
733 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
734 	    ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
735 	      !info->attrs[NFC_ATTR_PROTOCOLS]) &&
736 	      !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
737 		return -EINVAL;
738 
739 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
740 
741 	if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
742 		tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
743 
744 	if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
745 		im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
746 	else if (info->attrs[NFC_ATTR_PROTOCOLS])
747 		im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
748 
749 	dev = nfc_get_device(idx);
750 	if (!dev)
751 		return -ENODEV;
752 
753 	mutex_lock(&dev->genl_data.genl_data_mutex);
754 
755 	rc = nfc_start_poll(dev, im_protocols, tm_protocols);
756 	if (!rc)
757 		dev->genl_data.poll_req_portid = info->snd_portid;
758 
759 	mutex_unlock(&dev->genl_data.genl_data_mutex);
760 
761 	nfc_put_device(dev);
762 	return rc;
763 }
764 
765 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
766 {
767 	struct nfc_dev *dev;
768 	int rc;
769 	u32 idx;
770 
771 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
772 		return -EINVAL;
773 
774 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
775 
776 	dev = nfc_get_device(idx);
777 	if (!dev)
778 		return -ENODEV;
779 
780 	device_lock(&dev->dev);
781 
782 	if (!dev->polling) {
783 		device_unlock(&dev->dev);
784 		return -EINVAL;
785 	}
786 
787 	device_unlock(&dev->dev);
788 
789 	mutex_lock(&dev->genl_data.genl_data_mutex);
790 
791 	if (dev->genl_data.poll_req_portid != info->snd_portid) {
792 		rc = -EBUSY;
793 		goto out;
794 	}
795 
796 	rc = nfc_stop_poll(dev);
797 	dev->genl_data.poll_req_portid = 0;
798 
799 out:
800 	mutex_unlock(&dev->genl_data.genl_data_mutex);
801 	nfc_put_device(dev);
802 	return rc;
803 }
804 
805 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
806 {
807 	struct nfc_dev *dev;
808 	int rc, tgt_idx;
809 	u32 idx;
810 	u8 comm;
811 
812 	pr_debug("DEP link up\n");
813 
814 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
815 	    !info->attrs[NFC_ATTR_COMM_MODE])
816 		return -EINVAL;
817 
818 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
819 	if (!info->attrs[NFC_ATTR_TARGET_INDEX])
820 		tgt_idx = NFC_TARGET_IDX_ANY;
821 	else
822 		tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
823 
824 	comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
825 
826 	if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
827 		return -EINVAL;
828 
829 	dev = nfc_get_device(idx);
830 	if (!dev)
831 		return -ENODEV;
832 
833 	rc = nfc_dep_link_up(dev, tgt_idx, comm);
834 
835 	nfc_put_device(dev);
836 
837 	return rc;
838 }
839 
840 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
841 {
842 	struct nfc_dev *dev;
843 	int rc;
844 	u32 idx;
845 
846 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
847 		return -EINVAL;
848 
849 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
850 
851 	dev = nfc_get_device(idx);
852 	if (!dev)
853 		return -ENODEV;
854 
855 	rc = nfc_dep_link_down(dev);
856 
857 	nfc_put_device(dev);
858 	return rc;
859 }
860 
861 static int nfc_genl_send_params(struct sk_buff *msg,
862 				struct nfc_llcp_local *local,
863 				u32 portid, u32 seq)
864 {
865 	void *hdr;
866 
867 	hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
868 			  NFC_CMD_LLC_GET_PARAMS);
869 	if (!hdr)
870 		return -EMSGSIZE;
871 
872 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
873 	    nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
874 	    nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
875 	    nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
876 		goto nla_put_failure;
877 
878 	return genlmsg_end(msg, hdr);
879 
880 nla_put_failure:
881 
882 	genlmsg_cancel(msg, hdr);
883 	return -EMSGSIZE;
884 }
885 
886 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
887 {
888 	struct nfc_dev *dev;
889 	struct nfc_llcp_local *local;
890 	int rc = 0;
891 	struct sk_buff *msg = NULL;
892 	u32 idx;
893 
894 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
895 		return -EINVAL;
896 
897 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
898 
899 	dev = nfc_get_device(idx);
900 	if (!dev)
901 		return -ENODEV;
902 
903 	device_lock(&dev->dev);
904 
905 	local = nfc_llcp_find_local(dev);
906 	if (!local) {
907 		rc = -ENODEV;
908 		goto exit;
909 	}
910 
911 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
912 	if (!msg) {
913 		rc = -ENOMEM;
914 		goto exit;
915 	}
916 
917 	rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
918 
919 exit:
920 	device_unlock(&dev->dev);
921 
922 	nfc_put_device(dev);
923 
924 	if (rc < 0) {
925 		if (msg)
926 			nlmsg_free(msg);
927 
928 		return rc;
929 	}
930 
931 	return genlmsg_reply(msg, info);
932 }
933 
934 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
935 {
936 	struct nfc_dev *dev;
937 	struct nfc_llcp_local *local;
938 	u8 rw = 0;
939 	u16 miux = 0;
940 	u32 idx;
941 	int rc = 0;
942 
943 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
944 	    (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
945 	     !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
946 	     !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
947 		return -EINVAL;
948 
949 	if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
950 		rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
951 
952 		if (rw > LLCP_MAX_RW)
953 			return -EINVAL;
954 	}
955 
956 	if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
957 		miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
958 
959 		if (miux > LLCP_MAX_MIUX)
960 			return -EINVAL;
961 	}
962 
963 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
964 
965 	dev = nfc_get_device(idx);
966 	if (!dev)
967 		return -ENODEV;
968 
969 	device_lock(&dev->dev);
970 
971 	local = nfc_llcp_find_local(dev);
972 	if (!local) {
973 		nfc_put_device(dev);
974 		rc = -ENODEV;
975 		goto exit;
976 	}
977 
978 	if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
979 		if (dev->dep_link_up) {
980 			rc = -EINPROGRESS;
981 			goto exit;
982 		}
983 
984 		local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
985 	}
986 
987 	if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
988 		local->rw = rw;
989 
990 	if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
991 		local->miux = cpu_to_be16(miux);
992 
993 exit:
994 	device_unlock(&dev->dev);
995 
996 	nfc_put_device(dev);
997 
998 	return rc;
999 }
1000 
1001 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1002 {
1003 	struct nfc_dev *dev;
1004 	struct nfc_llcp_local *local;
1005 	struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1006 	u32 idx;
1007 	u8 tid;
1008 	char *uri;
1009 	int rc = 0, rem;
1010 	size_t uri_len, tlvs_len;
1011 	struct hlist_head sdreq_list;
1012 	struct nfc_llcp_sdp_tlv *sdreq;
1013 
1014 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1015 	    !info->attrs[NFC_ATTR_LLC_SDP])
1016 		return -EINVAL;
1017 
1018 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1019 
1020 	dev = nfc_get_device(idx);
1021 	if (!dev) {
1022 		rc = -ENODEV;
1023 		goto exit;
1024 	}
1025 
1026 	device_lock(&dev->dev);
1027 
1028 	if (dev->dep_link_up == false) {
1029 		rc = -ENOLINK;
1030 		goto exit;
1031 	}
1032 
1033 	local = nfc_llcp_find_local(dev);
1034 	if (!local) {
1035 		nfc_put_device(dev);
1036 		rc = -ENODEV;
1037 		goto exit;
1038 	}
1039 
1040 	INIT_HLIST_HEAD(&sdreq_list);
1041 
1042 	tlvs_len = 0;
1043 
1044 	nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1045 		rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1046 				      nfc_sdp_genl_policy);
1047 
1048 		if (rc != 0) {
1049 			rc = -EINVAL;
1050 			goto exit;
1051 		}
1052 
1053 		if (!sdp_attrs[NFC_SDP_ATTR_URI])
1054 			continue;
1055 
1056 		uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1057 		if (uri_len == 0)
1058 			continue;
1059 
1060 		uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1061 		if (uri == NULL || *uri == 0)
1062 			continue;
1063 
1064 		tid = local->sdreq_next_tid++;
1065 
1066 		sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1067 		if (sdreq == NULL) {
1068 			rc = -ENOMEM;
1069 			goto exit;
1070 		}
1071 
1072 		tlvs_len += sdreq->tlv_len;
1073 
1074 		hlist_add_head(&sdreq->node, &sdreq_list);
1075 	}
1076 
1077 	if (hlist_empty(&sdreq_list)) {
1078 		rc = -EINVAL;
1079 		goto exit;
1080 	}
1081 
1082 	rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1083 exit:
1084 	device_unlock(&dev->dev);
1085 
1086 	nfc_put_device(dev);
1087 
1088 	return rc;
1089 }
1090 
1091 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1092 {
1093 	struct nfc_dev *dev;
1094 	int rc;
1095 	u32 idx;
1096 	char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1097 
1098 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1099 		return -EINVAL;
1100 
1101 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1102 
1103 	dev = nfc_get_device(idx);
1104 	if (!dev)
1105 		return -ENODEV;
1106 
1107 	nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1108 		    sizeof(firmware_name));
1109 
1110 	rc = nfc_fw_download(dev, firmware_name);
1111 
1112 	nfc_put_device(dev);
1113 	return rc;
1114 }
1115 
1116 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1117 			      u32 result)
1118 {
1119 	struct sk_buff *msg;
1120 	void *hdr;
1121 
1122 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1123 	if (!msg)
1124 		return -ENOMEM;
1125 
1126 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1127 			  NFC_CMD_FW_DOWNLOAD);
1128 	if (!hdr)
1129 		goto free_msg;
1130 
1131 	if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1132 	    nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1133 	    nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1134 		goto nla_put_failure;
1135 
1136 	genlmsg_end(msg, hdr);
1137 
1138 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1139 
1140 	return 0;
1141 
1142 nla_put_failure:
1143 	genlmsg_cancel(msg, hdr);
1144 free_msg:
1145 	nlmsg_free(msg);
1146 	return -EMSGSIZE;
1147 }
1148 
1149 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1150 {
1151 	struct nfc_dev *dev;
1152 	int rc;
1153 	u32 idx, se_idx;
1154 
1155 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1156 	    !info->attrs[NFC_ATTR_SE_INDEX])
1157 		return -EINVAL;
1158 
1159 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1160 	se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1161 
1162 	dev = nfc_get_device(idx);
1163 	if (!dev)
1164 		return -ENODEV;
1165 
1166 	rc = nfc_enable_se(dev, se_idx);
1167 
1168 	nfc_put_device(dev);
1169 	return rc;
1170 }
1171 
1172 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1173 {
1174 	struct nfc_dev *dev;
1175 	int rc;
1176 	u32 idx, se_idx;
1177 
1178 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1179 	    !info->attrs[NFC_ATTR_SE_INDEX])
1180 		return -EINVAL;
1181 
1182 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1183 	se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1184 
1185 	dev = nfc_get_device(idx);
1186 	if (!dev)
1187 		return -ENODEV;
1188 
1189 	rc = nfc_disable_se(dev, se_idx);
1190 
1191 	nfc_put_device(dev);
1192 	return rc;
1193 }
1194 
1195 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1196 				u32 portid, u32 seq,
1197 				struct netlink_callback *cb,
1198 				int flags)
1199 {
1200 	void *hdr;
1201 	struct nfc_se *se, *n;
1202 
1203 	list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1204 		hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1205 				  NFC_CMD_GET_SE);
1206 		if (!hdr)
1207 			goto nla_put_failure;
1208 
1209 		if (cb)
1210 			genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1211 
1212 		if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1213 		    nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1214 		    nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1215 			goto nla_put_failure;
1216 
1217 		if (genlmsg_end(msg, hdr) < 0)
1218 			goto nla_put_failure;
1219 	}
1220 
1221 	return 0;
1222 
1223 nla_put_failure:
1224 	genlmsg_cancel(msg, hdr);
1225 	return -EMSGSIZE;
1226 }
1227 
1228 static int nfc_genl_dump_ses(struct sk_buff *skb,
1229 				 struct netlink_callback *cb)
1230 {
1231 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1232 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1233 	bool first_call = false;
1234 
1235 	if (!iter) {
1236 		first_call = true;
1237 		iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1238 		if (!iter)
1239 			return -ENOMEM;
1240 		cb->args[0] = (long) iter;
1241 	}
1242 
1243 	mutex_lock(&nfc_devlist_mutex);
1244 
1245 	cb->seq = nfc_devlist_generation;
1246 
1247 	if (first_call) {
1248 		nfc_device_iter_init(iter);
1249 		dev = nfc_device_iter_next(iter);
1250 	}
1251 
1252 	while (dev) {
1253 		int rc;
1254 
1255 		rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1256 					  cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1257 		if (rc < 0)
1258 			break;
1259 
1260 		dev = nfc_device_iter_next(iter);
1261 	}
1262 
1263 	mutex_unlock(&nfc_devlist_mutex);
1264 
1265 	cb->args[1] = (long) dev;
1266 
1267 	return skb->len;
1268 }
1269 
1270 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1271 {
1272 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1273 
1274 	nfc_device_iter_exit(iter);
1275 	kfree(iter);
1276 
1277 	return 0;
1278 }
1279 
1280 struct se_io_ctx {
1281 	u32 dev_idx;
1282 	u32 se_idx;
1283 };
1284 
1285 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1286 {
1287 	struct se_io_ctx *ctx = context;
1288 	struct sk_buff *msg;
1289 	void *hdr;
1290 
1291 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1292 	if (!msg) {
1293 		kfree(ctx);
1294 		return;
1295 	}
1296 
1297 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1298 			  NFC_CMD_SE_IO);
1299 	if (!hdr)
1300 		goto free_msg;
1301 
1302 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1303 	    nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1304 	    nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1305 		goto nla_put_failure;
1306 
1307 	genlmsg_end(msg, hdr);
1308 
1309 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1310 
1311 	kfree(ctx);
1312 
1313 	return;
1314 
1315 nla_put_failure:
1316 	genlmsg_cancel(msg, hdr);
1317 free_msg:
1318 	nlmsg_free(msg);
1319 	kfree(ctx);
1320 
1321 	return;
1322 }
1323 
1324 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1325 {
1326 	struct nfc_dev *dev;
1327 	struct se_io_ctx *ctx;
1328 	u32 dev_idx, se_idx;
1329 	u8 *apdu;
1330 	size_t apdu_len;
1331 
1332 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1333 	    !info->attrs[NFC_ATTR_SE_INDEX] ||
1334 	    !info->attrs[NFC_ATTR_SE_APDU])
1335 		return -EINVAL;
1336 
1337 	dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1338 	se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1339 
1340 	dev = nfc_get_device(dev_idx);
1341 	if (!dev)
1342 		return -ENODEV;
1343 
1344 	if (!dev->ops || !dev->ops->se_io)
1345 		return -ENOTSUPP;
1346 
1347 	apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1348 	if (apdu_len == 0)
1349 		return -EINVAL;
1350 
1351 	apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1352 	if (!apdu)
1353 		return -EINVAL;
1354 
1355 	ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1356 	if (!ctx)
1357 		return -ENOMEM;
1358 
1359 	ctx->dev_idx = dev_idx;
1360 	ctx->se_idx = se_idx;
1361 
1362 	return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1363 }
1364 
1365 static const struct genl_ops nfc_genl_ops[] = {
1366 	{
1367 		.cmd = NFC_CMD_GET_DEVICE,
1368 		.doit = nfc_genl_get_device,
1369 		.dumpit = nfc_genl_dump_devices,
1370 		.done = nfc_genl_dump_devices_done,
1371 		.policy = nfc_genl_policy,
1372 	},
1373 	{
1374 		.cmd = NFC_CMD_DEV_UP,
1375 		.doit = nfc_genl_dev_up,
1376 		.policy = nfc_genl_policy,
1377 	},
1378 	{
1379 		.cmd = NFC_CMD_DEV_DOWN,
1380 		.doit = nfc_genl_dev_down,
1381 		.policy = nfc_genl_policy,
1382 	},
1383 	{
1384 		.cmd = NFC_CMD_START_POLL,
1385 		.doit = nfc_genl_start_poll,
1386 		.policy = nfc_genl_policy,
1387 	},
1388 	{
1389 		.cmd = NFC_CMD_STOP_POLL,
1390 		.doit = nfc_genl_stop_poll,
1391 		.policy = nfc_genl_policy,
1392 	},
1393 	{
1394 		.cmd = NFC_CMD_DEP_LINK_UP,
1395 		.doit = nfc_genl_dep_link_up,
1396 		.policy = nfc_genl_policy,
1397 	},
1398 	{
1399 		.cmd = NFC_CMD_DEP_LINK_DOWN,
1400 		.doit = nfc_genl_dep_link_down,
1401 		.policy = nfc_genl_policy,
1402 	},
1403 	{
1404 		.cmd = NFC_CMD_GET_TARGET,
1405 		.dumpit = nfc_genl_dump_targets,
1406 		.done = nfc_genl_dump_targets_done,
1407 		.policy = nfc_genl_policy,
1408 	},
1409 	{
1410 		.cmd = NFC_CMD_LLC_GET_PARAMS,
1411 		.doit = nfc_genl_llc_get_params,
1412 		.policy = nfc_genl_policy,
1413 	},
1414 	{
1415 		.cmd = NFC_CMD_LLC_SET_PARAMS,
1416 		.doit = nfc_genl_llc_set_params,
1417 		.policy = nfc_genl_policy,
1418 	},
1419 	{
1420 		.cmd = NFC_CMD_LLC_SDREQ,
1421 		.doit = nfc_genl_llc_sdreq,
1422 		.policy = nfc_genl_policy,
1423 	},
1424 	{
1425 		.cmd = NFC_CMD_FW_DOWNLOAD,
1426 		.doit = nfc_genl_fw_download,
1427 		.policy = nfc_genl_policy,
1428 	},
1429 	{
1430 		.cmd = NFC_CMD_ENABLE_SE,
1431 		.doit = nfc_genl_enable_se,
1432 		.policy = nfc_genl_policy,
1433 	},
1434 	{
1435 		.cmd = NFC_CMD_DISABLE_SE,
1436 		.doit = nfc_genl_disable_se,
1437 		.policy = nfc_genl_policy,
1438 	},
1439 	{
1440 		.cmd = NFC_CMD_GET_SE,
1441 		.dumpit = nfc_genl_dump_ses,
1442 		.done = nfc_genl_dump_ses_done,
1443 		.policy = nfc_genl_policy,
1444 	},
1445 	{
1446 		.cmd = NFC_CMD_SE_IO,
1447 		.doit = nfc_genl_se_io,
1448 		.policy = nfc_genl_policy,
1449 	},
1450 };
1451 
1452 
1453 struct urelease_work {
1454 	struct	work_struct w;
1455 	int	portid;
1456 };
1457 
1458 static void nfc_urelease_event_work(struct work_struct *work)
1459 {
1460 	struct urelease_work *w = container_of(work, struct urelease_work, w);
1461 	struct class_dev_iter iter;
1462 	struct nfc_dev *dev;
1463 
1464 	pr_debug("portid %d\n", w->portid);
1465 
1466 	mutex_lock(&nfc_devlist_mutex);
1467 
1468 	nfc_device_iter_init(&iter);
1469 	dev = nfc_device_iter_next(&iter);
1470 
1471 	while (dev) {
1472 		mutex_lock(&dev->genl_data.genl_data_mutex);
1473 
1474 		if (dev->genl_data.poll_req_portid == w->portid) {
1475 			nfc_stop_poll(dev);
1476 			dev->genl_data.poll_req_portid = 0;
1477 		}
1478 
1479 		mutex_unlock(&dev->genl_data.genl_data_mutex);
1480 
1481 		dev = nfc_device_iter_next(&iter);
1482 	}
1483 
1484 	nfc_device_iter_exit(&iter);
1485 
1486 	mutex_unlock(&nfc_devlist_mutex);
1487 
1488 	kfree(w);
1489 }
1490 
1491 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1492 				 unsigned long event, void *ptr)
1493 {
1494 	struct netlink_notify *n = ptr;
1495 	struct urelease_work *w;
1496 
1497 	if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1498 		goto out;
1499 
1500 	pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1501 
1502 	w = kmalloc(sizeof(*w), GFP_ATOMIC);
1503 	if (w) {
1504 		INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1505 		w->portid = n->portid;
1506 		schedule_work((struct work_struct *) w);
1507 	}
1508 
1509 out:
1510 	return NOTIFY_DONE;
1511 }
1512 
1513 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1514 {
1515 	genl_data->poll_req_portid = 0;
1516 	mutex_init(&genl_data->genl_data_mutex);
1517 }
1518 
1519 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1520 {
1521 	mutex_destroy(&genl_data->genl_data_mutex);
1522 }
1523 
1524 static struct notifier_block nl_notifier = {
1525 	.notifier_call  = nfc_genl_rcv_nl_event,
1526 };
1527 
1528 /**
1529  * nfc_genl_init() - Initialize netlink interface
1530  *
1531  * This initialization function registers the nfc netlink family.
1532  */
1533 int __init nfc_genl_init(void)
1534 {
1535 	int rc;
1536 
1537 	rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1538 						  nfc_genl_ops,
1539 						  nfc_genl_mcgrps);
1540 	if (rc)
1541 		return rc;
1542 
1543 	netlink_register_notifier(&nl_notifier);
1544 
1545 	return 0;
1546 }
1547 
1548 /**
1549  * nfc_genl_exit() - Deinitialize netlink interface
1550  *
1551  * This exit function unregisters the nfc netlink family.
1552  */
1553 void nfc_genl_exit(void)
1554 {
1555 	netlink_unregister_notifier(&nl_notifier);
1556 	genl_unregister_family(&nfc_genl_family);
1557 }
1558