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