sched/topology: Rename topology_thread_cpumask() to topology_sibling_cpumask()
[linux/fpc-iii.git] / net / nfc / core.c
blobcff3f1614ad4557ff178804acb55bacbad328149
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
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.
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.
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/>.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
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>
31 #include <net/genetlink.h>
33 #include "nfc.h"
35 #define VERSION "0.1"
37 #define NFC_CHECK_PRES_FREQ_MS 2000
39 int nfc_devlist_generation;
40 DEFINE_MUTEX(nfc_devlist_mutex);
42 /* NFC device ID bitmap */
43 static DEFINE_IDA(nfc_index_ida);
45 int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
47 int rc = 0;
49 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
51 device_lock(&dev->dev);
53 if (!device_is_registered(&dev->dev)) {
54 rc = -ENODEV;
55 goto error;
58 if (dev->dev_up) {
59 rc = -EBUSY;
60 goto error;
63 if (!dev->ops->fw_download) {
64 rc = -EOPNOTSUPP;
65 goto error;
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;
73 error:
74 device_unlock(&dev->dev);
75 return rc;
78 /**
79 * nfc_fw_download_done - inform that a firmware download was completed
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
85 int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
86 u32 result)
88 dev->fw_download_in_progress = false;
90 return nfc_genl_fw_download_done(dev, firmware_name, result);
92 EXPORT_SYMBOL(nfc_fw_download_done);
94 /**
95 * nfc_dev_up - turn on the NFC device
97 * @dev: The nfc device to be turned on
99 * The device remains up until the nfc_dev_down function is called.
101 int nfc_dev_up(struct nfc_dev *dev)
103 int rc = 0;
105 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
107 device_lock(&dev->dev);
109 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
110 rc = -ERFKILL;
111 goto error;
114 if (!device_is_registered(&dev->dev)) {
115 rc = -ENODEV;
116 goto error;
119 if (dev->fw_download_in_progress) {
120 rc = -EBUSY;
121 goto error;
124 if (dev->dev_up) {
125 rc = -EALREADY;
126 goto error;
129 if (dev->ops->dev_up)
130 rc = dev->ops->dev_up(dev);
132 if (!rc)
133 dev->dev_up = true;
135 /* We have to enable the device before discovering SEs */
136 if (dev->ops->discover_se && dev->ops->discover_se(dev))
137 pr_err("SE discovery failed\n");
139 error:
140 device_unlock(&dev->dev);
141 return rc;
145 * nfc_dev_down - turn off the NFC device
147 * @dev: The nfc device to be turned off
149 int nfc_dev_down(struct nfc_dev *dev)
151 int rc = 0;
153 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
155 device_lock(&dev->dev);
157 if (!device_is_registered(&dev->dev)) {
158 rc = -ENODEV;
159 goto error;
162 if (!dev->dev_up) {
163 rc = -EALREADY;
164 goto error;
167 if (dev->polling || dev->active_target) {
168 rc = -EBUSY;
169 goto error;
172 if (dev->ops->dev_down)
173 dev->ops->dev_down(dev);
175 dev->dev_up = false;
177 error:
178 device_unlock(&dev->dev);
179 return rc;
182 static int nfc_rfkill_set_block(void *data, bool blocked)
184 struct nfc_dev *dev = data;
186 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
188 if (!blocked)
189 return 0;
191 nfc_dev_down(dev);
193 return 0;
196 static const struct rfkill_ops nfc_rfkill_ops = {
197 .set_block = nfc_rfkill_set_block,
201 * nfc_start_poll - start polling for nfc targets
203 * @dev: The nfc device that must start polling
204 * @protocols: bitset of nfc protocols that must be used for polling
206 * The device remains polling for targets until a target is found or
207 * the nfc_stop_poll function is called.
209 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
211 int rc;
213 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
214 dev_name(&dev->dev), im_protocols, tm_protocols);
216 if (!im_protocols && !tm_protocols)
217 return -EINVAL;
219 device_lock(&dev->dev);
221 if (!device_is_registered(&dev->dev)) {
222 rc = -ENODEV;
223 goto error;
226 if (!dev->dev_up) {
227 rc = -ENODEV;
228 goto error;
231 if (dev->polling) {
232 rc = -EBUSY;
233 goto error;
236 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
237 if (!rc) {
238 dev->polling = true;
239 dev->rf_mode = NFC_RF_NONE;
242 error:
243 device_unlock(&dev->dev);
244 return rc;
248 * nfc_stop_poll - stop polling for nfc targets
250 * @dev: The nfc device that must stop polling
252 int nfc_stop_poll(struct nfc_dev *dev)
254 int rc = 0;
256 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
258 device_lock(&dev->dev);
260 if (!device_is_registered(&dev->dev)) {
261 rc = -ENODEV;
262 goto error;
265 if (!dev->polling) {
266 rc = -EINVAL;
267 goto error;
270 dev->ops->stop_poll(dev);
271 dev->polling = false;
272 dev->rf_mode = NFC_RF_NONE;
274 error:
275 device_unlock(&dev->dev);
276 return rc;
279 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
281 int i;
283 for (i = 0; i < dev->n_targets; i++) {
284 if (dev->targets[i].idx == target_idx)
285 return &dev->targets[i];
288 return NULL;
291 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
293 int rc = 0;
294 u8 *gb;
295 size_t gb_len;
296 struct nfc_target *target;
298 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
300 if (!dev->ops->dep_link_up)
301 return -EOPNOTSUPP;
303 device_lock(&dev->dev);
305 if (!device_is_registered(&dev->dev)) {
306 rc = -ENODEV;
307 goto error;
310 if (dev->dep_link_up == true) {
311 rc = -EALREADY;
312 goto error;
315 gb = nfc_llcp_general_bytes(dev, &gb_len);
316 if (gb_len > NFC_MAX_GT_LEN) {
317 rc = -EINVAL;
318 goto error;
321 target = nfc_find_target(dev, target_index);
322 if (target == NULL) {
323 rc = -ENOTCONN;
324 goto error;
327 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
328 if (!rc) {
329 dev->active_target = target;
330 dev->rf_mode = NFC_RF_INITIATOR;
333 error:
334 device_unlock(&dev->dev);
335 return rc;
338 int nfc_dep_link_down(struct nfc_dev *dev)
340 int rc = 0;
342 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
344 if (!dev->ops->dep_link_down)
345 return -EOPNOTSUPP;
347 device_lock(&dev->dev);
349 if (!device_is_registered(&dev->dev)) {
350 rc = -ENODEV;
351 goto error;
354 if (dev->dep_link_up == false) {
355 rc = -EALREADY;
356 goto error;
359 rc = dev->ops->dep_link_down(dev);
360 if (!rc) {
361 dev->dep_link_up = false;
362 dev->active_target = NULL;
363 dev->rf_mode = NFC_RF_NONE;
364 nfc_llcp_mac_is_down(dev);
365 nfc_genl_dep_link_down_event(dev);
368 error:
369 device_unlock(&dev->dev);
371 return rc;
374 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
375 u8 comm_mode, u8 rf_mode)
377 dev->dep_link_up = true;
379 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
380 struct nfc_target *target;
382 target = nfc_find_target(dev, target_idx);
383 if (target == NULL)
384 return -ENOTCONN;
386 dev->active_target = target;
389 dev->polling = false;
390 dev->rf_mode = rf_mode;
392 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
394 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
396 EXPORT_SYMBOL(nfc_dep_link_is_up);
399 * nfc_activate_target - prepare the target for data exchange
401 * @dev: The nfc device that found the target
402 * @target_idx: index of the target that must be activated
403 * @protocol: nfc protocol that will be used for data exchange
405 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
407 int rc;
408 struct nfc_target *target;
410 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
411 dev_name(&dev->dev), target_idx, protocol);
413 device_lock(&dev->dev);
415 if (!device_is_registered(&dev->dev)) {
416 rc = -ENODEV;
417 goto error;
420 if (dev->active_target) {
421 rc = -EBUSY;
422 goto error;
425 target = nfc_find_target(dev, target_idx);
426 if (target == NULL) {
427 rc = -ENOTCONN;
428 goto error;
431 rc = dev->ops->activate_target(dev, target, protocol);
432 if (!rc) {
433 dev->active_target = target;
434 dev->rf_mode = NFC_RF_INITIATOR;
436 if (dev->ops->check_presence && !dev->shutting_down)
437 mod_timer(&dev->check_pres_timer, jiffies +
438 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
441 error:
442 device_unlock(&dev->dev);
443 return rc;
447 * nfc_deactivate_target - deactivate a nfc target
449 * @dev: The nfc device that found the target
450 * @target_idx: index of the target that must be deactivated
452 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
454 int rc = 0;
456 pr_debug("dev_name=%s target_idx=%u\n",
457 dev_name(&dev->dev), target_idx);
459 device_lock(&dev->dev);
461 if (!device_is_registered(&dev->dev)) {
462 rc = -ENODEV;
463 goto error;
466 if (dev->active_target == NULL) {
467 rc = -ENOTCONN;
468 goto error;
471 if (dev->active_target->idx != target_idx) {
472 rc = -ENOTCONN;
473 goto error;
476 if (dev->ops->check_presence)
477 del_timer_sync(&dev->check_pres_timer);
479 dev->ops->deactivate_target(dev, dev->active_target);
480 dev->active_target = NULL;
482 error:
483 device_unlock(&dev->dev);
484 return rc;
488 * nfc_data_exchange - transceive data
490 * @dev: The nfc device that found the target
491 * @target_idx: index of the target
492 * @skb: data to be sent
493 * @cb: callback called when the response is received
494 * @cb_context: parameter for the callback function
496 * The user must wait for the callback before calling this function again.
498 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
499 data_exchange_cb_t cb, void *cb_context)
501 int rc;
503 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
504 dev_name(&dev->dev), target_idx, skb->len);
506 device_lock(&dev->dev);
508 if (!device_is_registered(&dev->dev)) {
509 rc = -ENODEV;
510 kfree_skb(skb);
511 goto error;
514 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
515 if (dev->active_target->idx != target_idx) {
516 rc = -EADDRNOTAVAIL;
517 kfree_skb(skb);
518 goto error;
521 if (dev->ops->check_presence)
522 del_timer_sync(&dev->check_pres_timer);
524 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
525 cb_context);
527 if (!rc && dev->ops->check_presence && !dev->shutting_down)
528 mod_timer(&dev->check_pres_timer, jiffies +
529 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
530 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
531 rc = dev->ops->tm_send(dev, skb);
532 } else {
533 rc = -ENOTCONN;
534 kfree_skb(skb);
535 goto error;
539 error:
540 device_unlock(&dev->dev);
541 return rc;
544 struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
546 struct nfc_se *se;
548 list_for_each_entry(se, &dev->secure_elements, list)
549 if (se->idx == se_idx)
550 return se;
552 return NULL;
554 EXPORT_SYMBOL(nfc_find_se);
556 int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
558 struct nfc_se *se;
559 int rc;
561 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
563 device_lock(&dev->dev);
565 if (!device_is_registered(&dev->dev)) {
566 rc = -ENODEV;
567 goto error;
570 if (!dev->dev_up) {
571 rc = -ENODEV;
572 goto error;
575 if (dev->polling) {
576 rc = -EBUSY;
577 goto error;
580 if (!dev->ops->enable_se || !dev->ops->disable_se) {
581 rc = -EOPNOTSUPP;
582 goto error;
585 se = nfc_find_se(dev, se_idx);
586 if (!se) {
587 rc = -EINVAL;
588 goto error;
591 if (se->state == NFC_SE_ENABLED) {
592 rc = -EALREADY;
593 goto error;
596 rc = dev->ops->enable_se(dev, se_idx);
597 if (rc >= 0)
598 se->state = NFC_SE_ENABLED;
600 error:
601 device_unlock(&dev->dev);
602 return rc;
605 int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
607 struct nfc_se *se;
608 int rc;
610 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
612 device_lock(&dev->dev);
614 if (!device_is_registered(&dev->dev)) {
615 rc = -ENODEV;
616 goto error;
619 if (!dev->dev_up) {
620 rc = -ENODEV;
621 goto error;
624 if (!dev->ops->enable_se || !dev->ops->disable_se) {
625 rc = -EOPNOTSUPP;
626 goto error;
629 se = nfc_find_se(dev, se_idx);
630 if (!se) {
631 rc = -EINVAL;
632 goto error;
635 if (se->state == NFC_SE_DISABLED) {
636 rc = -EALREADY;
637 goto error;
640 rc = dev->ops->disable_se(dev, se_idx);
641 if (rc >= 0)
642 se->state = NFC_SE_DISABLED;
644 error:
645 device_unlock(&dev->dev);
646 return rc;
649 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
651 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
653 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
655 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
657 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
659 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
661 return nfc_llcp_general_bytes(dev, gb_len);
663 EXPORT_SYMBOL(nfc_get_local_general_bytes);
665 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
667 /* Only LLCP target mode for now */
668 if (dev->dep_link_up == false) {
669 kfree_skb(skb);
670 return -ENOLINK;
673 return nfc_llcp_data_received(dev, skb);
675 EXPORT_SYMBOL(nfc_tm_data_received);
677 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
678 u8 *gb, size_t gb_len)
680 int rc;
682 device_lock(&dev->dev);
684 dev->polling = false;
686 if (gb != NULL) {
687 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
688 if (rc < 0)
689 goto out;
692 dev->rf_mode = NFC_RF_TARGET;
694 if (protocol == NFC_PROTO_NFC_DEP_MASK)
695 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
697 rc = nfc_genl_tm_activated(dev, protocol);
699 out:
700 device_unlock(&dev->dev);
702 return rc;
704 EXPORT_SYMBOL(nfc_tm_activated);
706 int nfc_tm_deactivated(struct nfc_dev *dev)
708 dev->dep_link_up = false;
709 dev->rf_mode = NFC_RF_NONE;
711 return nfc_genl_tm_deactivated(dev);
713 EXPORT_SYMBOL(nfc_tm_deactivated);
716 * nfc_alloc_send_skb - allocate a skb for data exchange responses
718 * @size: size to allocate
719 * @gfp: gfp flags
721 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
722 unsigned int flags, unsigned int size,
723 unsigned int *err)
725 struct sk_buff *skb;
726 unsigned int total_size;
728 total_size = size +
729 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
731 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
732 if (skb)
733 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
735 return skb;
739 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
741 * @size: size to allocate
742 * @gfp: gfp flags
744 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
746 struct sk_buff *skb;
747 unsigned int total_size;
749 total_size = size + 1;
750 skb = alloc_skb(total_size, gfp);
752 if (skb)
753 skb_reserve(skb, 1);
755 return skb;
757 EXPORT_SYMBOL(nfc_alloc_recv_skb);
760 * nfc_targets_found - inform that targets were found
762 * @dev: The nfc device that found the targets
763 * @targets: array of nfc targets found
764 * @ntargets: targets array size
766 * The device driver must call this function when one or many nfc targets
767 * are found. After calling this function, the device driver must stop
768 * polling for targets.
769 * NOTE: This function can be called with targets=NULL and n_targets=0 to
770 * notify a driver error, meaning that the polling operation cannot complete.
771 * IMPORTANT: this function must not be called from an atomic context.
772 * In addition, it must also not be called from a context that would prevent
773 * the NFC Core to call other nfc ops entry point concurrently.
775 int nfc_targets_found(struct nfc_dev *dev,
776 struct nfc_target *targets, int n_targets)
778 int i;
780 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
782 for (i = 0; i < n_targets; i++)
783 targets[i].idx = dev->target_next_idx++;
785 device_lock(&dev->dev);
787 if (dev->polling == false) {
788 device_unlock(&dev->dev);
789 return 0;
792 dev->polling = false;
794 dev->targets_generation++;
796 kfree(dev->targets);
797 dev->targets = NULL;
799 if (targets) {
800 dev->targets = kmemdup(targets,
801 n_targets * sizeof(struct nfc_target),
802 GFP_ATOMIC);
804 if (!dev->targets) {
805 dev->n_targets = 0;
806 device_unlock(&dev->dev);
807 return -ENOMEM;
811 dev->n_targets = n_targets;
812 device_unlock(&dev->dev);
814 nfc_genl_targets_found(dev);
816 return 0;
818 EXPORT_SYMBOL(nfc_targets_found);
821 * nfc_target_lost - inform that an activated target went out of field
823 * @dev: The nfc device that had the activated target in field
824 * @target_idx: the nfc index of the target
826 * The device driver must call this function when the activated target
827 * goes out of the field.
828 * IMPORTANT: this function must not be called from an atomic context.
829 * In addition, it must also not be called from a context that would prevent
830 * the NFC Core to call other nfc ops entry point concurrently.
832 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
834 struct nfc_target *tg;
835 int i;
837 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
839 device_lock(&dev->dev);
841 for (i = 0; i < dev->n_targets; i++) {
842 tg = &dev->targets[i];
843 if (tg->idx == target_idx)
844 break;
847 if (i == dev->n_targets) {
848 device_unlock(&dev->dev);
849 return -EINVAL;
852 dev->targets_generation++;
853 dev->n_targets--;
854 dev->active_target = NULL;
856 if (dev->n_targets) {
857 memcpy(&dev->targets[i], &dev->targets[i + 1],
858 (dev->n_targets - i) * sizeof(struct nfc_target));
859 } else {
860 kfree(dev->targets);
861 dev->targets = NULL;
864 device_unlock(&dev->dev);
866 nfc_genl_target_lost(dev, target_idx);
868 return 0;
870 EXPORT_SYMBOL(nfc_target_lost);
872 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
874 nfc_targets_found(dev, NULL, 0);
876 EXPORT_SYMBOL(nfc_driver_failure);
878 int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
880 struct nfc_se *se;
881 int rc;
883 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
885 se = nfc_find_se(dev, se_idx);
886 if (se)
887 return -EALREADY;
889 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
890 if (!se)
891 return -ENOMEM;
893 se->idx = se_idx;
894 se->type = type;
895 se->state = NFC_SE_DISABLED;
896 INIT_LIST_HEAD(&se->list);
898 list_add(&se->list, &dev->secure_elements);
900 rc = nfc_genl_se_added(dev, se_idx, type);
901 if (rc < 0) {
902 list_del(&se->list);
903 kfree(se);
905 return rc;
908 return 0;
910 EXPORT_SYMBOL(nfc_add_se);
912 int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
914 struct nfc_se *se, *n;
915 int rc;
917 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
919 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
920 if (se->idx == se_idx) {
921 rc = nfc_genl_se_removed(dev, se_idx);
922 if (rc < 0)
923 return rc;
925 list_del(&se->list);
926 kfree(se);
928 return 0;
931 return -EINVAL;
933 EXPORT_SYMBOL(nfc_remove_se);
935 int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
936 struct nfc_evt_transaction *evt_transaction)
938 int rc;
940 pr_debug("transaction: %x\n", se_idx);
942 device_lock(&dev->dev);
944 if (!evt_transaction) {
945 rc = -EPROTO;
946 goto out;
949 rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
950 out:
951 device_unlock(&dev->dev);
952 return rc;
954 EXPORT_SYMBOL(nfc_se_transaction);
956 static void nfc_release(struct device *d)
958 struct nfc_dev *dev = to_nfc_dev(d);
959 struct nfc_se *se, *n;
961 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
963 nfc_genl_data_exit(&dev->genl_data);
964 kfree(dev->targets);
966 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
967 nfc_genl_se_removed(dev, se->idx);
968 list_del(&se->list);
969 kfree(se);
972 kfree(dev);
975 static void nfc_check_pres_work(struct work_struct *work)
977 struct nfc_dev *dev = container_of(work, struct nfc_dev,
978 check_pres_work);
979 int rc;
981 device_lock(&dev->dev);
983 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
984 rc = dev->ops->check_presence(dev, dev->active_target);
985 if (rc == -EOPNOTSUPP)
986 goto exit;
987 if (rc) {
988 u32 active_target_idx = dev->active_target->idx;
989 device_unlock(&dev->dev);
990 nfc_target_lost(dev, active_target_idx);
991 return;
994 if (!dev->shutting_down)
995 mod_timer(&dev->check_pres_timer, jiffies +
996 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
999 exit:
1000 device_unlock(&dev->dev);
1003 static void nfc_check_pres_timeout(unsigned long data)
1005 struct nfc_dev *dev = (struct nfc_dev *)data;
1007 schedule_work(&dev->check_pres_work);
1010 struct class nfc_class = {
1011 .name = "nfc",
1012 .dev_release = nfc_release,
1014 EXPORT_SYMBOL(nfc_class);
1016 static int match_idx(struct device *d, const void *data)
1018 struct nfc_dev *dev = to_nfc_dev(d);
1019 const unsigned int *idx = data;
1021 return dev->idx == *idx;
1024 struct nfc_dev *nfc_get_device(unsigned int idx)
1026 struct device *d;
1028 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1029 if (!d)
1030 return NULL;
1032 return to_nfc_dev(d);
1036 * nfc_allocate_device - allocate a new nfc device
1038 * @ops: device operations
1039 * @supported_protocols: NFC protocols supported by the device
1041 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
1042 u32 supported_protocols,
1043 int tx_headroom, int tx_tailroom)
1045 struct nfc_dev *dev;
1047 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1048 !ops->deactivate_target || !ops->im_transceive)
1049 return NULL;
1051 if (!supported_protocols)
1052 return NULL;
1054 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1055 if (!dev)
1056 return NULL;
1058 dev->ops = ops;
1059 dev->supported_protocols = supported_protocols;
1060 dev->tx_headroom = tx_headroom;
1061 dev->tx_tailroom = tx_tailroom;
1062 INIT_LIST_HEAD(&dev->secure_elements);
1064 nfc_genl_data_init(&dev->genl_data);
1066 dev->rf_mode = NFC_RF_NONE;
1068 /* first generation must not be 0 */
1069 dev->targets_generation = 1;
1071 if (ops->check_presence) {
1072 init_timer(&dev->check_pres_timer);
1073 dev->check_pres_timer.data = (unsigned long)dev;
1074 dev->check_pres_timer.function = nfc_check_pres_timeout;
1076 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1079 return dev;
1081 EXPORT_SYMBOL(nfc_allocate_device);
1084 * nfc_register_device - register a nfc device in the nfc subsystem
1086 * @dev: The nfc device to register
1088 int nfc_register_device(struct nfc_dev *dev)
1090 int rc;
1092 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1094 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1095 if (dev->idx < 0)
1096 return dev->idx;
1098 dev->dev.class = &nfc_class;
1099 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1100 device_initialize(&dev->dev);
1102 mutex_lock(&nfc_devlist_mutex);
1103 nfc_devlist_generation++;
1104 rc = device_add(&dev->dev);
1105 mutex_unlock(&nfc_devlist_mutex);
1107 if (rc < 0)
1108 return rc;
1110 rc = nfc_llcp_register_device(dev);
1111 if (rc)
1112 pr_err("Could not register llcp device\n");
1114 rc = nfc_genl_device_added(dev);
1115 if (rc)
1116 pr_debug("The userspace won't be notified that the device %s was added\n",
1117 dev_name(&dev->dev));
1119 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1120 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1121 if (dev->rfkill) {
1122 if (rfkill_register(dev->rfkill) < 0) {
1123 rfkill_destroy(dev->rfkill);
1124 dev->rfkill = NULL;
1128 return 0;
1130 EXPORT_SYMBOL(nfc_register_device);
1133 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1135 * @dev: The nfc device to unregister
1137 void nfc_unregister_device(struct nfc_dev *dev)
1139 int rc, id;
1141 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1143 id = dev->idx;
1145 if (dev->rfkill) {
1146 rfkill_unregister(dev->rfkill);
1147 rfkill_destroy(dev->rfkill);
1150 if (dev->ops->check_presence) {
1151 device_lock(&dev->dev);
1152 dev->shutting_down = true;
1153 device_unlock(&dev->dev);
1154 del_timer_sync(&dev->check_pres_timer);
1155 cancel_work_sync(&dev->check_pres_work);
1158 rc = nfc_genl_device_removed(dev);
1159 if (rc)
1160 pr_debug("The userspace won't be notified that the device %s "
1161 "was removed\n", dev_name(&dev->dev));
1163 nfc_llcp_unregister_device(dev);
1165 mutex_lock(&nfc_devlist_mutex);
1166 nfc_devlist_generation++;
1167 device_del(&dev->dev);
1168 mutex_unlock(&nfc_devlist_mutex);
1170 ida_simple_remove(&nfc_index_ida, id);
1172 EXPORT_SYMBOL(nfc_unregister_device);
1174 static int __init nfc_init(void)
1176 int rc;
1178 pr_info("NFC Core ver %s\n", VERSION);
1180 rc = class_register(&nfc_class);
1181 if (rc)
1182 return rc;
1184 rc = nfc_genl_init();
1185 if (rc)
1186 goto err_genl;
1188 /* the first generation must not be 0 */
1189 nfc_devlist_generation = 1;
1191 rc = rawsock_init();
1192 if (rc)
1193 goto err_rawsock;
1195 rc = nfc_llcp_init();
1196 if (rc)
1197 goto err_llcp_sock;
1199 rc = af_nfc_init();
1200 if (rc)
1201 goto err_af_nfc;
1203 return 0;
1205 err_af_nfc:
1206 nfc_llcp_exit();
1207 err_llcp_sock:
1208 rawsock_exit();
1209 err_rawsock:
1210 nfc_genl_exit();
1211 err_genl:
1212 class_unregister(&nfc_class);
1213 return rc;
1216 static void __exit nfc_exit(void)
1218 af_nfc_exit();
1219 nfc_llcp_exit();
1220 rawsock_exit();
1221 nfc_genl_exit();
1222 class_unregister(&nfc_class);
1225 subsys_initcall(nfc_init);
1226 module_exit(nfc_exit);
1228 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1229 MODULE_DESCRIPTION("NFC Core ver " VERSION);
1230 MODULE_VERSION(VERSION);
1231 MODULE_LICENSE("GPL");
1232 MODULE_ALIAS_NETPROTO(PF_NFC);
1233 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);