liblockdep: Add pthread_mutex_t test suite
[linux-2.6/btrfs-unstable.git] / net / nfc / core.c
blob872529105abc7c3a2e41b9d579e934e019e8e5ff
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, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
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>
33 #include <net/genetlink.h>
35 #include "nfc.h"
37 #define VERSION "0.1"
39 #define NFC_CHECK_PRES_FREQ_MS 2000
41 int nfc_devlist_generation;
42 DEFINE_MUTEX(nfc_devlist_mutex);
44 /* NFC device ID bitmap */
45 static DEFINE_IDA(nfc_index_ida);
47 int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
49 int rc = 0;
51 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
53 device_lock(&dev->dev);
55 if (!device_is_registered(&dev->dev)) {
56 rc = -ENODEV;
57 goto error;
60 if (dev->dev_up) {
61 rc = -EBUSY;
62 goto error;
65 if (!dev->ops->fw_download) {
66 rc = -EOPNOTSUPP;
67 goto error;
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;
75 error:
76 device_unlock(&dev->dev);
77 return rc;
80 /**
81 * nfc_fw_download_done - inform that a firmware download was completed
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
87 int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
88 u32 result)
90 dev->fw_download_in_progress = false;
92 return nfc_genl_fw_download_done(dev, firmware_name, result);
94 EXPORT_SYMBOL(nfc_fw_download_done);
96 /**
97 * nfc_dev_up - turn on the NFC device
99 * @dev: The nfc device to be turned on
101 * The device remains up until the nfc_dev_down function is called.
103 int nfc_dev_up(struct nfc_dev *dev)
105 int rc = 0;
107 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
109 device_lock(&dev->dev);
111 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
112 rc = -ERFKILL;
113 goto error;
116 if (!device_is_registered(&dev->dev)) {
117 rc = -ENODEV;
118 goto error;
121 if (dev->fw_download_in_progress) {
122 rc = -EBUSY;
123 goto error;
126 if (dev->dev_up) {
127 rc = -EALREADY;
128 goto error;
131 if (dev->ops->dev_up)
132 rc = dev->ops->dev_up(dev);
134 if (!rc)
135 dev->dev_up = true;
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");
144 error:
145 device_unlock(&dev->dev);
146 return rc;
150 * nfc_dev_down - turn off the NFC device
152 * @dev: The nfc device to be turned off
154 int nfc_dev_down(struct nfc_dev *dev)
156 int rc = 0;
158 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
160 device_lock(&dev->dev);
162 if (!device_is_registered(&dev->dev)) {
163 rc = -ENODEV;
164 goto error;
167 if (!dev->dev_up) {
168 rc = -EALREADY;
169 goto error;
172 if (dev->polling || dev->active_target) {
173 rc = -EBUSY;
174 goto error;
177 if (dev->ops->dev_down)
178 dev->ops->dev_down(dev);
180 dev->dev_up = false;
182 error:
183 device_unlock(&dev->dev);
184 return rc;
187 static int nfc_rfkill_set_block(void *data, bool blocked)
189 struct nfc_dev *dev = data;
191 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
193 if (!blocked)
194 return 0;
196 nfc_dev_down(dev);
198 return 0;
201 static const struct rfkill_ops nfc_rfkill_ops = {
202 .set_block = nfc_rfkill_set_block,
206 * nfc_start_poll - start polling for nfc targets
208 * @dev: The nfc device that must start polling
209 * @protocols: bitset of nfc protocols that must be used for polling
211 * The device remains polling for targets until a target is found or
212 * the nfc_stop_poll function is called.
214 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
216 int rc;
218 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
219 dev_name(&dev->dev), im_protocols, tm_protocols);
221 if (!im_protocols && !tm_protocols)
222 return -EINVAL;
224 device_lock(&dev->dev);
226 if (!device_is_registered(&dev->dev)) {
227 rc = -ENODEV;
228 goto error;
231 if (!dev->dev_up) {
232 rc = -ENODEV;
233 goto error;
236 if (dev->polling) {
237 rc = -EBUSY;
238 goto error;
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;
247 error:
248 device_unlock(&dev->dev);
249 return rc;
253 * nfc_stop_poll - stop polling for nfc targets
255 * @dev: The nfc device that must stop polling
257 int nfc_stop_poll(struct nfc_dev *dev)
259 int rc = 0;
261 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
263 device_lock(&dev->dev);
265 if (!device_is_registered(&dev->dev)) {
266 rc = -ENODEV;
267 goto error;
270 if (!dev->polling) {
271 rc = -EINVAL;
272 goto error;
275 dev->ops->stop_poll(dev);
276 dev->polling = false;
277 dev->rf_mode = NFC_RF_NONE;
279 error:
280 device_unlock(&dev->dev);
281 return rc;
284 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
286 int i;
288 if (dev->n_targets == 0)
289 return NULL;
291 for (i = 0; i < dev->n_targets; i++) {
292 if (dev->targets[i].idx == target_idx)
293 return &dev->targets[i];
296 return NULL;
299 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
301 int rc = 0;
302 u8 *gb;
303 size_t gb_len;
304 struct nfc_target *target;
306 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
308 if (!dev->ops->dep_link_up)
309 return -EOPNOTSUPP;
311 device_lock(&dev->dev);
313 if (!device_is_registered(&dev->dev)) {
314 rc = -ENODEV;
315 goto error;
318 if (dev->dep_link_up == true) {
319 rc = -EALREADY;
320 goto error;
323 gb = nfc_llcp_general_bytes(dev, &gb_len);
324 if (gb_len > NFC_MAX_GT_LEN) {
325 rc = -EINVAL;
326 goto error;
329 target = nfc_find_target(dev, target_index);
330 if (target == NULL) {
331 rc = -ENOTCONN;
332 goto error;
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;
341 error:
342 device_unlock(&dev->dev);
343 return rc;
346 int nfc_dep_link_down(struct nfc_dev *dev)
348 int rc = 0;
350 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
352 if (!dev->ops->dep_link_down)
353 return -EOPNOTSUPP;
355 device_lock(&dev->dev);
357 if (!device_is_registered(&dev->dev)) {
358 rc = -ENODEV;
359 goto error;
362 if (dev->dep_link_up == false) {
363 rc = -EALREADY;
364 goto error;
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);
376 error:
377 device_unlock(&dev->dev);
379 return rc;
382 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
383 u8 comm_mode, u8 rf_mode)
385 dev->dep_link_up = true;
387 if (!dev->active_target) {
388 struct nfc_target *target;
390 target = nfc_find_target(dev, target_idx);
391 if (target == NULL)
392 return -ENOTCONN;
394 dev->active_target = target;
397 dev->polling = false;
398 dev->rf_mode = rf_mode;
400 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
402 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
404 EXPORT_SYMBOL(nfc_dep_link_is_up);
407 * nfc_activate_target - prepare the target for data exchange
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
413 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
415 int rc;
416 struct nfc_target *target;
418 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
419 dev_name(&dev->dev), target_idx, protocol);
421 device_lock(&dev->dev);
423 if (!device_is_registered(&dev->dev)) {
424 rc = -ENODEV;
425 goto error;
428 if (dev->active_target) {
429 rc = -EBUSY;
430 goto error;
433 target = nfc_find_target(dev, target_idx);
434 if (target == NULL) {
435 rc = -ENOTCONN;
436 goto error;
439 rc = dev->ops->activate_target(dev, target, protocol);
440 if (!rc) {
441 dev->active_target = target;
442 dev->rf_mode = NFC_RF_INITIATOR;
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));
449 error:
450 device_unlock(&dev->dev);
451 return rc;
455 * nfc_deactivate_target - deactivate a nfc target
457 * @dev: The nfc device that found the target
458 * @target_idx: index of the target that must be deactivated
460 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
462 int rc = 0;
464 pr_debug("dev_name=%s target_idx=%u\n",
465 dev_name(&dev->dev), target_idx);
467 device_lock(&dev->dev);
469 if (!device_is_registered(&dev->dev)) {
470 rc = -ENODEV;
471 goto error;
474 if (dev->active_target == NULL) {
475 rc = -ENOTCONN;
476 goto error;
479 if (dev->active_target->idx != target_idx) {
480 rc = -ENOTCONN;
481 goto error;
484 if (dev->ops->check_presence)
485 del_timer_sync(&dev->check_pres_timer);
487 dev->ops->deactivate_target(dev, dev->active_target);
488 dev->active_target = NULL;
490 error:
491 device_unlock(&dev->dev);
492 return rc;
496 * nfc_data_exchange - transceive data
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
504 * The user must wait for the callback before calling this function again.
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)
509 int rc;
511 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
512 dev_name(&dev->dev), target_idx, skb->len);
514 device_lock(&dev->dev);
516 if (!device_is_registered(&dev->dev)) {
517 rc = -ENODEV;
518 kfree_skb(skb);
519 goto error;
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;
529 if (dev->ops->check_presence)
530 del_timer_sync(&dev->check_pres_timer);
532 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
533 cb_context);
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;
547 error:
548 device_unlock(&dev->dev);
549 return rc;
552 struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
554 struct nfc_se *se, *n;
556 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
557 if (se->idx == se_idx)
558 return se;
560 return NULL;
562 EXPORT_SYMBOL(nfc_find_se);
564 int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
567 struct nfc_se *se;
568 int rc;
570 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
572 device_lock(&dev->dev);
574 if (!device_is_registered(&dev->dev)) {
575 rc = -ENODEV;
576 goto error;
579 if (!dev->dev_up) {
580 rc = -ENODEV;
581 goto error;
584 if (dev->polling) {
585 rc = -EBUSY;
586 goto error;
589 if (!dev->ops->enable_se || !dev->ops->disable_se) {
590 rc = -EOPNOTSUPP;
591 goto error;
594 se = nfc_find_se(dev, se_idx);
595 if (!se) {
596 rc = -EINVAL;
597 goto error;
600 if (se->state == NFC_SE_ENABLED) {
601 rc = -EALREADY;
602 goto error;
605 rc = dev->ops->enable_se(dev, se_idx);
606 if (rc >= 0)
607 se->state = NFC_SE_ENABLED;
609 error:
610 device_unlock(&dev->dev);
611 return rc;
614 int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
617 struct nfc_se *se;
618 int rc;
620 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
622 device_lock(&dev->dev);
624 if (!device_is_registered(&dev->dev)) {
625 rc = -ENODEV;
626 goto error;
629 if (!dev->dev_up) {
630 rc = -ENODEV;
631 goto error;
634 if (!dev->ops->enable_se || !dev->ops->disable_se) {
635 rc = -EOPNOTSUPP;
636 goto error;
639 se = nfc_find_se(dev, se_idx);
640 if (!se) {
641 rc = -EINVAL;
642 goto error;
645 if (se->state == NFC_SE_DISABLED) {
646 rc = -EALREADY;
647 goto error;
650 rc = dev->ops->disable_se(dev, se_idx);
651 if (rc >= 0)
652 se->state = NFC_SE_DISABLED;
654 error:
655 device_unlock(&dev->dev);
656 return rc;
659 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
661 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
663 if (gb_len > NFC_MAX_GT_LEN)
664 return -EINVAL;
666 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
668 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
670 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
672 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
674 return nfc_llcp_general_bytes(dev, gb_len);
676 EXPORT_SYMBOL(nfc_get_local_general_bytes);
678 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
680 /* Only LLCP target mode for now */
681 if (dev->dep_link_up == false) {
682 kfree_skb(skb);
683 return -ENOLINK;
686 return nfc_llcp_data_received(dev, skb);
688 EXPORT_SYMBOL(nfc_tm_data_received);
690 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
691 u8 *gb, size_t gb_len)
693 int rc;
695 device_lock(&dev->dev);
697 dev->polling = false;
699 if (gb != NULL) {
700 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
701 if (rc < 0)
702 goto out;
705 dev->rf_mode = NFC_RF_TARGET;
707 if (protocol == NFC_PROTO_NFC_DEP_MASK)
708 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
710 rc = nfc_genl_tm_activated(dev, protocol);
712 out:
713 device_unlock(&dev->dev);
715 return rc;
717 EXPORT_SYMBOL(nfc_tm_activated);
719 int nfc_tm_deactivated(struct nfc_dev *dev)
721 dev->dep_link_up = false;
722 dev->rf_mode = NFC_RF_NONE;
724 return nfc_genl_tm_deactivated(dev);
726 EXPORT_SYMBOL(nfc_tm_deactivated);
729 * nfc_alloc_send_skb - allocate a skb for data exchange responses
731 * @size: size to allocate
732 * @gfp: gfp flags
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)
738 struct sk_buff *skb;
739 unsigned int total_size;
741 total_size = size +
742 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
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);
748 return skb;
752 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
754 * @size: size to allocate
755 * @gfp: gfp flags
757 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
759 struct sk_buff *skb;
760 unsigned int total_size;
762 total_size = size + 1;
763 skb = alloc_skb(total_size, gfp);
765 if (skb)
766 skb_reserve(skb, 1);
768 return skb;
770 EXPORT_SYMBOL(nfc_alloc_recv_skb);
773 * nfc_targets_found - inform that targets were found
775 * @dev: The nfc device that found the targets
776 * @targets: array of nfc targets found
777 * @ntargets: targets array size
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.
788 int nfc_targets_found(struct nfc_dev *dev,
789 struct nfc_target *targets, int n_targets)
791 int i;
793 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
795 for (i = 0; i < n_targets; i++)
796 targets[i].idx = dev->target_next_idx++;
798 device_lock(&dev->dev);
800 if (dev->polling == false) {
801 device_unlock(&dev->dev);
802 return 0;
805 dev->polling = false;
807 dev->targets_generation++;
809 kfree(dev->targets);
810 dev->targets = NULL;
812 if (targets) {
813 dev->targets = kmemdup(targets,
814 n_targets * sizeof(struct nfc_target),
815 GFP_ATOMIC);
817 if (!dev->targets) {
818 dev->n_targets = 0;
819 device_unlock(&dev->dev);
820 return -ENOMEM;
824 dev->n_targets = n_targets;
825 device_unlock(&dev->dev);
827 nfc_genl_targets_found(dev);
829 return 0;
831 EXPORT_SYMBOL(nfc_targets_found);
834 * nfc_target_lost - inform that an activated target went out of field
836 * @dev: The nfc device that had the activated target in field
837 * @target_idx: the nfc index of the target
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.
845 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
847 struct nfc_target *tg;
848 int i;
850 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
852 device_lock(&dev->dev);
854 for (i = 0; i < dev->n_targets; i++) {
855 tg = &dev->targets[i];
856 if (tg->idx == target_idx)
857 break;
860 if (i == dev->n_targets) {
861 device_unlock(&dev->dev);
862 return -EINVAL;
865 dev->targets_generation++;
866 dev->n_targets--;
867 dev->active_target = NULL;
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;
877 device_unlock(&dev->dev);
879 nfc_genl_target_lost(dev, target_idx);
881 return 0;
883 EXPORT_SYMBOL(nfc_target_lost);
885 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
887 nfc_targets_found(dev, NULL, 0);
889 EXPORT_SYMBOL(nfc_driver_failure);
891 int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
893 struct nfc_se *se;
894 int rc;
896 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
898 se = nfc_find_se(dev, se_idx);
899 if (se)
900 return -EALREADY;
902 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
903 if (!se)
904 return -ENOMEM;
906 se->idx = se_idx;
907 se->type = type;
908 se->state = NFC_SE_DISABLED;
909 INIT_LIST_HEAD(&se->list);
911 list_add(&se->list, &dev->secure_elements);
913 rc = nfc_genl_se_added(dev, se_idx, type);
914 if (rc < 0) {
915 list_del(&se->list);
916 kfree(se);
918 return rc;
921 return 0;
923 EXPORT_SYMBOL(nfc_add_se);
925 int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
927 struct nfc_se *se, *n;
928 int rc;
930 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
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;
938 list_del(&se->list);
939 kfree(se);
941 return 0;
944 return -EINVAL;
946 EXPORT_SYMBOL(nfc_remove_se);
948 static void nfc_release(struct device *d)
950 struct nfc_dev *dev = to_nfc_dev(d);
951 struct nfc_se *se, *n;
953 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
955 nfc_genl_data_exit(&dev->genl_data);
956 kfree(dev->targets);
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);
964 kfree(dev);
967 static void nfc_check_pres_work(struct work_struct *work)
969 struct nfc_dev *dev = container_of(work, struct nfc_dev,
970 check_pres_work);
971 int rc;
973 device_lock(&dev->dev);
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;
986 if (!dev->shutting_down)
987 mod_timer(&dev->check_pres_timer, jiffies +
988 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
991 exit:
992 device_unlock(&dev->dev);
995 static void nfc_check_pres_timeout(unsigned long data)
997 struct nfc_dev *dev = (struct nfc_dev *)data;
999 schedule_work(&dev->check_pres_work);
1002 struct class nfc_class = {
1003 .name = "nfc",
1004 .dev_release = nfc_release,
1006 EXPORT_SYMBOL(nfc_class);
1008 static int match_idx(struct device *d, const void *data)
1010 struct nfc_dev *dev = to_nfc_dev(d);
1011 const unsigned int *idx = data;
1013 return dev->idx == *idx;
1016 struct nfc_dev *nfc_get_device(unsigned int idx)
1018 struct device *d;
1020 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1021 if (!d)
1022 return NULL;
1024 return to_nfc_dev(d);
1028 * nfc_allocate_device - allocate a new nfc device
1030 * @ops: device operations
1031 * @supported_protocols: NFC protocols supported by the device
1033 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
1034 u32 supported_protocols,
1035 int tx_headroom, int tx_tailroom)
1037 struct nfc_dev *dev;
1039 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1040 !ops->deactivate_target || !ops->im_transceive)
1041 return NULL;
1043 if (!supported_protocols)
1044 return NULL;
1046 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1047 if (!dev)
1048 return NULL;
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);
1056 nfc_genl_data_init(&dev->genl_data);
1058 dev->rf_mode = NFC_RF_NONE;
1060 /* first generation must not be 0 */
1061 dev->targets_generation = 1;
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;
1068 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1071 return dev;
1073 EXPORT_SYMBOL(nfc_allocate_device);
1076 * nfc_register_device - register a nfc device in the nfc subsystem
1078 * @dev: The nfc device to register
1080 int nfc_register_device(struct nfc_dev *dev)
1082 int rc;
1084 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1086 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1087 if (dev->idx < 0)
1088 return dev->idx;
1090 dev->dev.class = &nfc_class;
1091 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1092 device_initialize(&dev->dev);
1094 mutex_lock(&nfc_devlist_mutex);
1095 nfc_devlist_generation++;
1096 rc = device_add(&dev->dev);
1097 mutex_unlock(&nfc_devlist_mutex);
1099 if (rc < 0)
1100 return rc;
1102 rc = nfc_llcp_register_device(dev);
1103 if (rc)
1104 pr_err("Could not register llcp device\n");
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));
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;
1120 return 0;
1122 EXPORT_SYMBOL(nfc_register_device);
1125 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1127 * @dev: The nfc device to unregister
1129 void nfc_unregister_device(struct nfc_dev *dev)
1131 int rc, id;
1133 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1135 id = dev->idx;
1137 if (dev->rfkill) {
1138 rfkill_unregister(dev->rfkill);
1139 rfkill_destroy(dev->rfkill);
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);
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));
1155 nfc_llcp_unregister_device(dev);
1157 mutex_lock(&nfc_devlist_mutex);
1158 nfc_devlist_generation++;
1159 device_del(&dev->dev);
1160 mutex_unlock(&nfc_devlist_mutex);
1162 ida_simple_remove(&nfc_index_ida, id);
1164 EXPORT_SYMBOL(nfc_unregister_device);
1166 static int __init nfc_init(void)
1168 int rc;
1170 pr_info("NFC Core ver %s\n", VERSION);
1172 rc = class_register(&nfc_class);
1173 if (rc)
1174 return rc;
1176 rc = nfc_genl_init();
1177 if (rc)
1178 goto err_genl;
1180 /* the first generation must not be 0 */
1181 nfc_devlist_generation = 1;
1183 rc = rawsock_init();
1184 if (rc)
1185 goto err_rawsock;
1187 rc = nfc_llcp_init();
1188 if (rc)
1189 goto err_llcp_sock;
1191 rc = af_nfc_init();
1192 if (rc)
1193 goto err_af_nfc;
1195 return 0;
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;
1208 static void __exit nfc_exit(void)
1210 af_nfc_exit();
1211 nfc_llcp_exit();
1212 rawsock_exit();
1213 nfc_genl_exit();
1214 class_unregister(&nfc_class);
1217 subsys_initcall(nfc_init);
1218 module_exit(nfc_exit);
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);