xref: /linux/net/nfc/core.c (revision 2d87650a3bf1b80f7d0d150ee1af3f8a89e5b7aa)
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 <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/rfkill.h>
29 #include <linux/nfc.h>
30 
31 #include <net/genetlink.h>
32 
33 #include "nfc.h"
34 
35 #define VERSION "0.1"
36 
37 #define NFC_CHECK_PRES_FREQ_MS	2000
38 
39 int nfc_devlist_generation;
40 DEFINE_MUTEX(nfc_devlist_mutex);
41 
42 /* NFC device ID bitmap */
43 static DEFINE_IDA(nfc_index_ida);
44 
45 int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
46 {
47 	int rc = 0;
48 
49 	pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
50 
51 	device_lock(&dev->dev);
52 
53 	if (!device_is_registered(&dev->dev)) {
54 		rc = -ENODEV;
55 		goto error;
56 	}
57 
58 	if (dev->dev_up) {
59 		rc = -EBUSY;
60 		goto error;
61 	}
62 
63 	if (!dev->ops->fw_download) {
64 		rc = -EOPNOTSUPP;
65 		goto error;
66 	}
67 
68 	dev->fw_download_in_progress = true;
69 	rc = dev->ops->fw_download(dev, firmware_name);
70 	if (rc)
71 		dev->fw_download_in_progress = false;
72 
73 error:
74 	device_unlock(&dev->dev);
75 	return rc;
76 }
77 
78 /**
79  * nfc_fw_download_done - inform that a firmware download was completed
80  *
81  * @dev: The nfc device to which firmware was downloaded
82  * @firmware_name: The firmware filename
83  * @result: The positive value of a standard errno value
84  */
85 int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
86 			 u32 result)
87 {
88 	dev->fw_download_in_progress = false;
89 
90 	return nfc_genl_fw_download_done(dev, firmware_name, result);
91 }
92 EXPORT_SYMBOL(nfc_fw_download_done);
93 
94 /**
95  * nfc_dev_up - turn on the NFC device
96  *
97  * @dev: The nfc device to be turned on
98  *
99  * The device remains up until the nfc_dev_down function is called.
100  */
101 int nfc_dev_up(struct nfc_dev *dev)
102 {
103 	int rc = 0;
104 
105 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
106 
107 	device_lock(&dev->dev);
108 
109 	if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
110 		rc = -ERFKILL;
111 		goto error;
112 	}
113 
114 	if (!device_is_registered(&dev->dev)) {
115 		rc = -ENODEV;
116 		goto error;
117 	}
118 
119 	if (dev->fw_download_in_progress) {
120 		rc = -EBUSY;
121 		goto error;
122 	}
123 
124 	if (dev->dev_up) {
125 		rc = -EALREADY;
126 		goto error;
127 	}
128 
129 	if (dev->ops->dev_up)
130 		rc = dev->ops->dev_up(dev);
131 
132 	if (!rc)
133 		dev->dev_up = true;
134 
135 	/* We have to enable the device before discovering SEs */
136 	if (dev->ops->discover_se) {
137 		rc = dev->ops->discover_se(dev);
138 		if (rc)
139 			pr_warn("SE discovery failed\n");
140 	}
141 
142 error:
143 	device_unlock(&dev->dev);
144 	return rc;
145 }
146 
147 /**
148  * nfc_dev_down - turn off the NFC device
149  *
150  * @dev: The nfc device to be turned off
151  */
152 int nfc_dev_down(struct nfc_dev *dev)
153 {
154 	int rc = 0;
155 
156 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
157 
158 	device_lock(&dev->dev);
159 
160 	if (!device_is_registered(&dev->dev)) {
161 		rc = -ENODEV;
162 		goto error;
163 	}
164 
165 	if (!dev->dev_up) {
166 		rc = -EALREADY;
167 		goto error;
168 	}
169 
170 	if (dev->polling || dev->active_target) {
171 		rc = -EBUSY;
172 		goto error;
173 	}
174 
175 	if (dev->ops->dev_down)
176 		dev->ops->dev_down(dev);
177 
178 	dev->dev_up = false;
179 
180 error:
181 	device_unlock(&dev->dev);
182 	return rc;
183 }
184 
185 static int nfc_rfkill_set_block(void *data, bool blocked)
186 {
187 	struct nfc_dev *dev = data;
188 
189 	pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
190 
191 	if (!blocked)
192 		return 0;
193 
194 	nfc_dev_down(dev);
195 
196 	return 0;
197 }
198 
199 static const struct rfkill_ops nfc_rfkill_ops = {
200 	.set_block = nfc_rfkill_set_block,
201 };
202 
203 /**
204  * nfc_start_poll - start polling for nfc targets
205  *
206  * @dev: The nfc device that must start polling
207  * @protocols: bitset of nfc protocols that must be used for polling
208  *
209  * The device remains polling for targets until a target is found or
210  * the nfc_stop_poll function is called.
211  */
212 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
213 {
214 	int rc;
215 
216 	pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
217 		 dev_name(&dev->dev), im_protocols, tm_protocols);
218 
219 	if (!im_protocols && !tm_protocols)
220 		return -EINVAL;
221 
222 	device_lock(&dev->dev);
223 
224 	if (!device_is_registered(&dev->dev)) {
225 		rc = -ENODEV;
226 		goto error;
227 	}
228 
229 	if (!dev->dev_up) {
230 		rc = -ENODEV;
231 		goto error;
232 	}
233 
234 	if (dev->polling) {
235 		rc = -EBUSY;
236 		goto error;
237 	}
238 
239 	rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
240 	if (!rc) {
241 		dev->polling = true;
242 		dev->rf_mode = NFC_RF_NONE;
243 	}
244 
245 error:
246 	device_unlock(&dev->dev);
247 	return rc;
248 }
249 
250 /**
251  * nfc_stop_poll - stop polling for nfc targets
252  *
253  * @dev: The nfc device that must stop polling
254  */
255 int nfc_stop_poll(struct nfc_dev *dev)
256 {
257 	int rc = 0;
258 
259 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
260 
261 	device_lock(&dev->dev);
262 
263 	if (!device_is_registered(&dev->dev)) {
264 		rc = -ENODEV;
265 		goto error;
266 	}
267 
268 	if (!dev->polling) {
269 		rc = -EINVAL;
270 		goto error;
271 	}
272 
273 	dev->ops->stop_poll(dev);
274 	dev->polling = false;
275 	dev->rf_mode = NFC_RF_NONE;
276 
277 error:
278 	device_unlock(&dev->dev);
279 	return rc;
280 }
281 
282 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
283 {
284 	int i;
285 
286 	if (dev->n_targets == 0)
287 		return NULL;
288 
289 	for (i = 0; i < dev->n_targets; i++) {
290 		if (dev->targets[i].idx == target_idx)
291 			return &dev->targets[i];
292 	}
293 
294 	return NULL;
295 }
296 
297 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
298 {
299 	int rc = 0;
300 	u8 *gb;
301 	size_t gb_len;
302 	struct nfc_target *target;
303 
304 	pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
305 
306 	if (!dev->ops->dep_link_up)
307 		return -EOPNOTSUPP;
308 
309 	device_lock(&dev->dev);
310 
311 	if (!device_is_registered(&dev->dev)) {
312 		rc = -ENODEV;
313 		goto error;
314 	}
315 
316 	if (dev->dep_link_up == true) {
317 		rc = -EALREADY;
318 		goto error;
319 	}
320 
321 	gb = nfc_llcp_general_bytes(dev, &gb_len);
322 	if (gb_len > NFC_MAX_GT_LEN) {
323 		rc = -EINVAL;
324 		goto error;
325 	}
326 
327 	target = nfc_find_target(dev, target_index);
328 	if (target == NULL) {
329 		rc = -ENOTCONN;
330 		goto error;
331 	}
332 
333 	rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
334 	if (!rc) {
335 		dev->active_target = target;
336 		dev->rf_mode = NFC_RF_INITIATOR;
337 	}
338 
339 error:
340 	device_unlock(&dev->dev);
341 	return rc;
342 }
343 
344 int nfc_dep_link_down(struct nfc_dev *dev)
345 {
346 	int rc = 0;
347 
348 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
349 
350 	if (!dev->ops->dep_link_down)
351 		return -EOPNOTSUPP;
352 
353 	device_lock(&dev->dev);
354 
355 	if (!device_is_registered(&dev->dev)) {
356 		rc = -ENODEV;
357 		goto error;
358 	}
359 
360 	if (dev->dep_link_up == false) {
361 		rc = -EALREADY;
362 		goto error;
363 	}
364 
365 	rc = dev->ops->dep_link_down(dev);
366 	if (!rc) {
367 		dev->dep_link_up = false;
368 		dev->active_target = NULL;
369 		dev->rf_mode = NFC_RF_NONE;
370 		nfc_llcp_mac_is_down(dev);
371 		nfc_genl_dep_link_down_event(dev);
372 	}
373 
374 error:
375 	device_unlock(&dev->dev);
376 
377 	return rc;
378 }
379 
380 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
381 		       u8 comm_mode, u8 rf_mode)
382 {
383 	dev->dep_link_up = true;
384 
385 	if (!dev->active_target) {
386 		struct nfc_target *target;
387 
388 		target = nfc_find_target(dev, target_idx);
389 		if (target == NULL)
390 			return -ENOTCONN;
391 
392 		dev->active_target = target;
393 	}
394 
395 	dev->polling = false;
396 	dev->rf_mode = rf_mode;
397 
398 	nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
399 
400 	return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
401 }
402 EXPORT_SYMBOL(nfc_dep_link_is_up);
403 
404 /**
405  * nfc_activate_target - prepare the target for data exchange
406  *
407  * @dev: The nfc device that found the target
408  * @target_idx: index of the target that must be activated
409  * @protocol: nfc protocol that will be used for data exchange
410  */
411 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
412 {
413 	int rc;
414 	struct nfc_target *target;
415 
416 	pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
417 		 dev_name(&dev->dev), target_idx, protocol);
418 
419 	device_lock(&dev->dev);
420 
421 	if (!device_is_registered(&dev->dev)) {
422 		rc = -ENODEV;
423 		goto error;
424 	}
425 
426 	if (dev->active_target) {
427 		rc = -EBUSY;
428 		goto error;
429 	}
430 
431 	target = nfc_find_target(dev, target_idx);
432 	if (target == NULL) {
433 		rc = -ENOTCONN;
434 		goto error;
435 	}
436 
437 	rc = dev->ops->activate_target(dev, target, protocol);
438 	if (!rc) {
439 		dev->active_target = target;
440 		dev->rf_mode = NFC_RF_INITIATOR;
441 
442 		if (dev->ops->check_presence && !dev->shutting_down)
443 			mod_timer(&dev->check_pres_timer, jiffies +
444 				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
445 	}
446 
447 error:
448 	device_unlock(&dev->dev);
449 	return rc;
450 }
451 
452 /**
453  * nfc_deactivate_target - deactivate a nfc target
454  *
455  * @dev: The nfc device that found the target
456  * @target_idx: index of the target that must be deactivated
457  */
458 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
459 {
460 	int rc = 0;
461 
462 	pr_debug("dev_name=%s target_idx=%u\n",
463 		 dev_name(&dev->dev), target_idx);
464 
465 	device_lock(&dev->dev);
466 
467 	if (!device_is_registered(&dev->dev)) {
468 		rc = -ENODEV;
469 		goto error;
470 	}
471 
472 	if (dev->active_target == NULL) {
473 		rc = -ENOTCONN;
474 		goto error;
475 	}
476 
477 	if (dev->active_target->idx != target_idx) {
478 		rc = -ENOTCONN;
479 		goto error;
480 	}
481 
482 	if (dev->ops->check_presence)
483 		del_timer_sync(&dev->check_pres_timer);
484 
485 	dev->ops->deactivate_target(dev, dev->active_target);
486 	dev->active_target = NULL;
487 
488 error:
489 	device_unlock(&dev->dev);
490 	return rc;
491 }
492 
493 /**
494  * nfc_data_exchange - transceive data
495  *
496  * @dev: The nfc device that found the target
497  * @target_idx: index of the target
498  * @skb: data to be sent
499  * @cb: callback called when the response is received
500  * @cb_context: parameter for the callback function
501  *
502  * The user must wait for the callback before calling this function again.
503  */
504 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
505 		      data_exchange_cb_t cb, void *cb_context)
506 {
507 	int rc;
508 
509 	pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
510 		 dev_name(&dev->dev), target_idx, skb->len);
511 
512 	device_lock(&dev->dev);
513 
514 	if (!device_is_registered(&dev->dev)) {
515 		rc = -ENODEV;
516 		kfree_skb(skb);
517 		goto error;
518 	}
519 
520 	if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
521 		if (dev->active_target->idx != target_idx) {
522 			rc = -EADDRNOTAVAIL;
523 			kfree_skb(skb);
524 			goto error;
525 		}
526 
527 		if (dev->ops->check_presence)
528 			del_timer_sync(&dev->check_pres_timer);
529 
530 		rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
531 					     cb_context);
532 
533 		if (!rc && dev->ops->check_presence && !dev->shutting_down)
534 			mod_timer(&dev->check_pres_timer, jiffies +
535 				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
536 	} else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
537 		rc = dev->ops->tm_send(dev, skb);
538 	} else {
539 		rc = -ENOTCONN;
540 		kfree_skb(skb);
541 		goto error;
542 	}
543 
544 
545 error:
546 	device_unlock(&dev->dev);
547 	return rc;
548 }
549 
550 struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
551 {
552 	struct nfc_se *se, *n;
553 
554 	list_for_each_entry_safe(se, n, &dev->secure_elements, list)
555 		if (se->idx == se_idx)
556 			return se;
557 
558 	return NULL;
559 }
560 EXPORT_SYMBOL(nfc_find_se);
561 
562 int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
563 {
564 
565 	struct nfc_se *se;
566 	int rc;
567 
568 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
569 
570 	device_lock(&dev->dev);
571 
572 	if (!device_is_registered(&dev->dev)) {
573 		rc = -ENODEV;
574 		goto error;
575 	}
576 
577 	if (!dev->dev_up) {
578 		rc = -ENODEV;
579 		goto error;
580 	}
581 
582 	if (dev->polling) {
583 		rc = -EBUSY;
584 		goto error;
585 	}
586 
587 	if (!dev->ops->enable_se || !dev->ops->disable_se) {
588 		rc = -EOPNOTSUPP;
589 		goto error;
590 	}
591 
592 	se = nfc_find_se(dev, se_idx);
593 	if (!se) {
594 		rc = -EINVAL;
595 		goto error;
596 	}
597 
598 	if (se->state == NFC_SE_ENABLED) {
599 		rc = -EALREADY;
600 		goto error;
601 	}
602 
603 	rc = dev->ops->enable_se(dev, se_idx);
604 	if (rc >= 0)
605 		se->state = NFC_SE_ENABLED;
606 
607 error:
608 	device_unlock(&dev->dev);
609 	return rc;
610 }
611 
612 int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
613 {
614 
615 	struct nfc_se *se;
616 	int rc;
617 
618 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
619 
620 	device_lock(&dev->dev);
621 
622 	if (!device_is_registered(&dev->dev)) {
623 		rc = -ENODEV;
624 		goto error;
625 	}
626 
627 	if (!dev->dev_up) {
628 		rc = -ENODEV;
629 		goto error;
630 	}
631 
632 	if (!dev->ops->enable_se || !dev->ops->disable_se) {
633 		rc = -EOPNOTSUPP;
634 		goto error;
635 	}
636 
637 	se = nfc_find_se(dev, se_idx);
638 	if (!se) {
639 		rc = -EINVAL;
640 		goto error;
641 	}
642 
643 	if (se->state == NFC_SE_DISABLED) {
644 		rc = -EALREADY;
645 		goto error;
646 	}
647 
648 	rc = dev->ops->disable_se(dev, se_idx);
649 	if (rc >= 0)
650 		se->state = NFC_SE_DISABLED;
651 
652 error:
653 	device_unlock(&dev->dev);
654 	return rc;
655 }
656 
657 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
658 {
659 	pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
660 
661 	if (gb_len > NFC_MAX_GT_LEN)
662 		return -EINVAL;
663 
664 	return nfc_llcp_set_remote_gb(dev, gb, gb_len);
665 }
666 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
667 
668 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
669 {
670 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
671 
672 	return nfc_llcp_general_bytes(dev, gb_len);
673 }
674 EXPORT_SYMBOL(nfc_get_local_general_bytes);
675 
676 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
677 {
678 	/* Only LLCP target mode for now */
679 	if (dev->dep_link_up == false) {
680 		kfree_skb(skb);
681 		return -ENOLINK;
682 	}
683 
684 	return nfc_llcp_data_received(dev, skb);
685 }
686 EXPORT_SYMBOL(nfc_tm_data_received);
687 
688 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
689 		     u8 *gb, size_t gb_len)
690 {
691 	int rc;
692 
693 	device_lock(&dev->dev);
694 
695 	dev->polling = false;
696 
697 	if (gb != NULL) {
698 		rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
699 		if (rc < 0)
700 			goto out;
701 	}
702 
703 	dev->rf_mode = NFC_RF_TARGET;
704 
705 	if (protocol == NFC_PROTO_NFC_DEP_MASK)
706 		nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
707 
708 	rc = nfc_genl_tm_activated(dev, protocol);
709 
710 out:
711 	device_unlock(&dev->dev);
712 
713 	return rc;
714 }
715 EXPORT_SYMBOL(nfc_tm_activated);
716 
717 int nfc_tm_deactivated(struct nfc_dev *dev)
718 {
719 	dev->dep_link_up = false;
720 	dev->rf_mode = NFC_RF_NONE;
721 
722 	return nfc_genl_tm_deactivated(dev);
723 }
724 EXPORT_SYMBOL(nfc_tm_deactivated);
725 
726 /**
727  * nfc_alloc_send_skb - allocate a skb for data exchange responses
728  *
729  * @size: size to allocate
730  * @gfp: gfp flags
731  */
732 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
733 				   unsigned int flags, unsigned int size,
734 				   unsigned int *err)
735 {
736 	struct sk_buff *skb;
737 	unsigned int total_size;
738 
739 	total_size = size +
740 		dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
741 
742 	skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
743 	if (skb)
744 		skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
745 
746 	return skb;
747 }
748 
749 /**
750  * nfc_alloc_recv_skb - allocate a skb for data exchange responses
751  *
752  * @size: size to allocate
753  * @gfp: gfp flags
754  */
755 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
756 {
757 	struct sk_buff *skb;
758 	unsigned int total_size;
759 
760 	total_size = size + 1;
761 	skb = alloc_skb(total_size, gfp);
762 
763 	if (skb)
764 		skb_reserve(skb, 1);
765 
766 	return skb;
767 }
768 EXPORT_SYMBOL(nfc_alloc_recv_skb);
769 
770 /**
771  * nfc_targets_found - inform that targets were found
772  *
773  * @dev: The nfc device that found the targets
774  * @targets: array of nfc targets found
775  * @ntargets: targets array size
776  *
777  * The device driver must call this function when one or many nfc targets
778  * are found. After calling this function, the device driver must stop
779  * polling for targets.
780  * NOTE: This function can be called with targets=NULL and n_targets=0 to
781  * notify a driver error, meaning that the polling operation cannot complete.
782  * IMPORTANT: this function must not be called from an atomic context.
783  * In addition, it must also not be called from a context that would prevent
784  * the NFC Core to call other nfc ops entry point concurrently.
785  */
786 int nfc_targets_found(struct nfc_dev *dev,
787 		      struct nfc_target *targets, int n_targets)
788 {
789 	int i;
790 
791 	pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
792 
793 	for (i = 0; i < n_targets; i++)
794 		targets[i].idx = dev->target_next_idx++;
795 
796 	device_lock(&dev->dev);
797 
798 	if (dev->polling == false) {
799 		device_unlock(&dev->dev);
800 		return 0;
801 	}
802 
803 	dev->polling = false;
804 
805 	dev->targets_generation++;
806 
807 	kfree(dev->targets);
808 	dev->targets = NULL;
809 
810 	if (targets) {
811 		dev->targets = kmemdup(targets,
812 				       n_targets * sizeof(struct nfc_target),
813 				       GFP_ATOMIC);
814 
815 		if (!dev->targets) {
816 			dev->n_targets = 0;
817 			device_unlock(&dev->dev);
818 			return -ENOMEM;
819 		}
820 	}
821 
822 	dev->n_targets = n_targets;
823 	device_unlock(&dev->dev);
824 
825 	nfc_genl_targets_found(dev);
826 
827 	return 0;
828 }
829 EXPORT_SYMBOL(nfc_targets_found);
830 
831 /**
832  * nfc_target_lost - inform that an activated target went out of field
833  *
834  * @dev: The nfc device that had the activated target in field
835  * @target_idx: the nfc index of the target
836  *
837  * The device driver must call this function when the activated target
838  * goes out of the field.
839  * IMPORTANT: this function must not be called from an atomic context.
840  * In addition, it must also not be called from a context that would prevent
841  * the NFC Core to call other nfc ops entry point concurrently.
842  */
843 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
844 {
845 	struct nfc_target *tg;
846 	int i;
847 
848 	pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
849 
850 	device_lock(&dev->dev);
851 
852 	for (i = 0; i < dev->n_targets; i++) {
853 		tg = &dev->targets[i];
854 		if (tg->idx == target_idx)
855 			break;
856 	}
857 
858 	if (i == dev->n_targets) {
859 		device_unlock(&dev->dev);
860 		return -EINVAL;
861 	}
862 
863 	dev->targets_generation++;
864 	dev->n_targets--;
865 	dev->active_target = NULL;
866 
867 	if (dev->n_targets) {
868 		memcpy(&dev->targets[i], &dev->targets[i + 1],
869 		       (dev->n_targets - i) * sizeof(struct nfc_target));
870 	} else {
871 		kfree(dev->targets);
872 		dev->targets = NULL;
873 	}
874 
875 	device_unlock(&dev->dev);
876 
877 	nfc_genl_target_lost(dev, target_idx);
878 
879 	return 0;
880 }
881 EXPORT_SYMBOL(nfc_target_lost);
882 
883 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
884 {
885 	nfc_targets_found(dev, NULL, 0);
886 }
887 EXPORT_SYMBOL(nfc_driver_failure);
888 
889 int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
890 {
891 	struct nfc_se *se;
892 	int rc;
893 
894 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
895 
896 	se = nfc_find_se(dev, se_idx);
897 	if (se)
898 		return -EALREADY;
899 
900 	se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
901 	if (!se)
902 		return -ENOMEM;
903 
904 	se->idx = se_idx;
905 	se->type = type;
906 	se->state = NFC_SE_DISABLED;
907 	INIT_LIST_HEAD(&se->list);
908 
909 	list_add(&se->list, &dev->secure_elements);
910 
911 	rc = nfc_genl_se_added(dev, se_idx, type);
912 	if (rc < 0) {
913 		list_del(&se->list);
914 		kfree(se);
915 
916 		return rc;
917 	}
918 
919 	return 0;
920 }
921 EXPORT_SYMBOL(nfc_add_se);
922 
923 int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
924 {
925 	struct nfc_se *se, *n;
926 	int rc;
927 
928 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
929 
930 	list_for_each_entry_safe(se, n, &dev->secure_elements, list)
931 		if (se->idx == se_idx) {
932 			rc = nfc_genl_se_removed(dev, se_idx);
933 			if (rc < 0)
934 				return rc;
935 
936 			list_del(&se->list);
937 			kfree(se);
938 
939 			return 0;
940 		}
941 
942 	return -EINVAL;
943 }
944 EXPORT_SYMBOL(nfc_remove_se);
945 
946 static void nfc_release(struct device *d)
947 {
948 	struct nfc_dev *dev = to_nfc_dev(d);
949 	struct nfc_se *se, *n;
950 
951 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
952 
953 	nfc_genl_data_exit(&dev->genl_data);
954 	kfree(dev->targets);
955 
956 	list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
957 			nfc_genl_se_removed(dev, se->idx);
958 			list_del(&se->list);
959 			kfree(se);
960 	}
961 
962 	kfree(dev);
963 }
964 
965 static void nfc_check_pres_work(struct work_struct *work)
966 {
967 	struct nfc_dev *dev = container_of(work, struct nfc_dev,
968 					   check_pres_work);
969 	int rc;
970 
971 	device_lock(&dev->dev);
972 
973 	if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
974 		rc = dev->ops->check_presence(dev, dev->active_target);
975 		if (rc == -EOPNOTSUPP)
976 			goto exit;
977 		if (rc) {
978 			u32 active_target_idx = dev->active_target->idx;
979 			device_unlock(&dev->dev);
980 			nfc_target_lost(dev, active_target_idx);
981 			return;
982 		}
983 
984 		if (!dev->shutting_down)
985 			mod_timer(&dev->check_pres_timer, jiffies +
986 				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
987 	}
988 
989 exit:
990 	device_unlock(&dev->dev);
991 }
992 
993 static void nfc_check_pres_timeout(unsigned long data)
994 {
995 	struct nfc_dev *dev = (struct nfc_dev *)data;
996 
997 	schedule_work(&dev->check_pres_work);
998 }
999 
1000 struct class nfc_class = {
1001 	.name = "nfc",
1002 	.dev_release = nfc_release,
1003 };
1004 EXPORT_SYMBOL(nfc_class);
1005 
1006 static int match_idx(struct device *d, const void *data)
1007 {
1008 	struct nfc_dev *dev = to_nfc_dev(d);
1009 	const unsigned int *idx = data;
1010 
1011 	return dev->idx == *idx;
1012 }
1013 
1014 struct nfc_dev *nfc_get_device(unsigned int idx)
1015 {
1016 	struct device *d;
1017 
1018 	d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1019 	if (!d)
1020 		return NULL;
1021 
1022 	return to_nfc_dev(d);
1023 }
1024 
1025 /**
1026  * nfc_allocate_device - allocate a new nfc device
1027  *
1028  * @ops: device operations
1029  * @supported_protocols: NFC protocols supported by the device
1030  */
1031 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
1032 				    u32 supported_protocols,
1033 				    int tx_headroom, int tx_tailroom)
1034 {
1035 	struct nfc_dev *dev;
1036 
1037 	if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1038 	    !ops->deactivate_target || !ops->im_transceive)
1039 		return NULL;
1040 
1041 	if (!supported_protocols)
1042 		return NULL;
1043 
1044 	dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1045 	if (!dev)
1046 		return NULL;
1047 
1048 	dev->ops = ops;
1049 	dev->supported_protocols = supported_protocols;
1050 	dev->tx_headroom = tx_headroom;
1051 	dev->tx_tailroom = tx_tailroom;
1052 	INIT_LIST_HEAD(&dev->secure_elements);
1053 
1054 	nfc_genl_data_init(&dev->genl_data);
1055 
1056 	dev->rf_mode = NFC_RF_NONE;
1057 
1058 	/* first generation must not be 0 */
1059 	dev->targets_generation = 1;
1060 
1061 	if (ops->check_presence) {
1062 		init_timer(&dev->check_pres_timer);
1063 		dev->check_pres_timer.data = (unsigned long)dev;
1064 		dev->check_pres_timer.function = nfc_check_pres_timeout;
1065 
1066 		INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1067 	}
1068 
1069 	return dev;
1070 }
1071 EXPORT_SYMBOL(nfc_allocate_device);
1072 
1073 /**
1074  * nfc_register_device - register a nfc device in the nfc subsystem
1075  *
1076  * @dev: The nfc device to register
1077  */
1078 int nfc_register_device(struct nfc_dev *dev)
1079 {
1080 	int rc;
1081 
1082 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1083 
1084 	dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1085 	if (dev->idx < 0)
1086 		return dev->idx;
1087 
1088 	dev->dev.class = &nfc_class;
1089 	dev_set_name(&dev->dev, "nfc%d", dev->idx);
1090 	device_initialize(&dev->dev);
1091 
1092 	mutex_lock(&nfc_devlist_mutex);
1093 	nfc_devlist_generation++;
1094 	rc = device_add(&dev->dev);
1095 	mutex_unlock(&nfc_devlist_mutex);
1096 
1097 	if (rc < 0)
1098 		return rc;
1099 
1100 	rc = nfc_llcp_register_device(dev);
1101 	if (rc)
1102 		pr_err("Could not register llcp device\n");
1103 
1104 	rc = nfc_genl_device_added(dev);
1105 	if (rc)
1106 		pr_debug("The userspace won't be notified that the device %s was added\n",
1107 			 dev_name(&dev->dev));
1108 
1109 	dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1110 				   RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1111 	if (dev->rfkill) {
1112 		if (rfkill_register(dev->rfkill) < 0) {
1113 			rfkill_destroy(dev->rfkill);
1114 			dev->rfkill = NULL;
1115 		}
1116 	}
1117 
1118 	return 0;
1119 }
1120 EXPORT_SYMBOL(nfc_register_device);
1121 
1122 /**
1123  * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1124  *
1125  * @dev: The nfc device to unregister
1126  */
1127 void nfc_unregister_device(struct nfc_dev *dev)
1128 {
1129 	int rc, id;
1130 
1131 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1132 
1133 	id = dev->idx;
1134 
1135 	if (dev->rfkill) {
1136 		rfkill_unregister(dev->rfkill);
1137 		rfkill_destroy(dev->rfkill);
1138 	}
1139 
1140 	if (dev->ops->check_presence) {
1141 		device_lock(&dev->dev);
1142 		dev->shutting_down = true;
1143 		device_unlock(&dev->dev);
1144 		del_timer_sync(&dev->check_pres_timer);
1145 		cancel_work_sync(&dev->check_pres_work);
1146 	}
1147 
1148 	rc = nfc_genl_device_removed(dev);
1149 	if (rc)
1150 		pr_debug("The userspace won't be notified that the device %s "
1151 			 "was removed\n", dev_name(&dev->dev));
1152 
1153 	nfc_llcp_unregister_device(dev);
1154 
1155 	mutex_lock(&nfc_devlist_mutex);
1156 	nfc_devlist_generation++;
1157 	device_del(&dev->dev);
1158 	mutex_unlock(&nfc_devlist_mutex);
1159 
1160 	ida_simple_remove(&nfc_index_ida, id);
1161 }
1162 EXPORT_SYMBOL(nfc_unregister_device);
1163 
1164 static int __init nfc_init(void)
1165 {
1166 	int rc;
1167 
1168 	pr_info("NFC Core ver %s\n", VERSION);
1169 
1170 	rc = class_register(&nfc_class);
1171 	if (rc)
1172 		return rc;
1173 
1174 	rc = nfc_genl_init();
1175 	if (rc)
1176 		goto err_genl;
1177 
1178 	/* the first generation must not be 0 */
1179 	nfc_devlist_generation = 1;
1180 
1181 	rc = rawsock_init();
1182 	if (rc)
1183 		goto err_rawsock;
1184 
1185 	rc = nfc_llcp_init();
1186 	if (rc)
1187 		goto err_llcp_sock;
1188 
1189 	rc = af_nfc_init();
1190 	if (rc)
1191 		goto err_af_nfc;
1192 
1193 	return 0;
1194 
1195 err_af_nfc:
1196 	nfc_llcp_exit();
1197 err_llcp_sock:
1198 	rawsock_exit();
1199 err_rawsock:
1200 	nfc_genl_exit();
1201 err_genl:
1202 	class_unregister(&nfc_class);
1203 	return rc;
1204 }
1205 
1206 static void __exit nfc_exit(void)
1207 {
1208 	af_nfc_exit();
1209 	nfc_llcp_exit();
1210 	rawsock_exit();
1211 	nfc_genl_exit();
1212 	class_unregister(&nfc_class);
1213 }
1214 
1215 subsys_initcall(nfc_init);
1216 module_exit(nfc_exit);
1217 
1218 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1219 MODULE_DESCRIPTION("NFC Core ver " VERSION);
1220 MODULE_VERSION(VERSION);
1221 MODULE_LICENSE("GPL");
1222 MODULE_ALIAS_NETPROTO(PF_NFC);
1223 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);
1224