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