2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
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>
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_upload(struct nfc_dev
*dev
, const char *firmware_name
)
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
)) {
65 if (!dev
->ops
->fw_upload
) {
70 dev
->fw_upload_in_progress
= true;
71 rc
= dev
->ops
->fw_upload(dev
, firmware_name
);
73 dev
->fw_upload_in_progress
= false;
76 device_unlock(&dev
->dev
);
80 int nfc_fw_upload_done(struct nfc_dev
*dev
, const char *firmware_name
)
82 dev
->fw_upload_in_progress
= false;
84 return nfc_genl_fw_upload_done(dev
, firmware_name
);
86 EXPORT_SYMBOL(nfc_fw_upload_done
);
89 * nfc_dev_up - turn on the NFC device
91 * @dev: The nfc device to be turned on
93 * The device remains up until the nfc_dev_down function is called.
95 int nfc_dev_up(struct nfc_dev
*dev
)
99 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
101 device_lock(&dev
->dev
);
103 if (dev
->rfkill
&& rfkill_blocked(dev
->rfkill
)) {
108 if (!device_is_registered(&dev
->dev
)) {
113 if (dev
->fw_upload_in_progress
) {
123 if (dev
->ops
->dev_up
)
124 rc
= dev
->ops
->dev_up(dev
);
129 /* We have to enable the device before discovering SEs */
130 if (dev
->ops
->discover_se
) {
131 rc
= dev
->ops
->discover_se(dev
);
133 pr_warn("SE discovery failed\n");
137 device_unlock(&dev
->dev
);
142 * nfc_dev_down - turn off the NFC device
144 * @dev: The nfc device to be turned off
146 int nfc_dev_down(struct nfc_dev
*dev
)
150 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
152 device_lock(&dev
->dev
);
154 if (!device_is_registered(&dev
->dev
)) {
164 if (dev
->polling
|| dev
->active_target
) {
169 if (dev
->ops
->dev_down
)
170 dev
->ops
->dev_down(dev
);
175 device_unlock(&dev
->dev
);
179 static int nfc_rfkill_set_block(void *data
, bool blocked
)
181 struct nfc_dev
*dev
= data
;
183 pr_debug("%s blocked %d", dev_name(&dev
->dev
), blocked
);
193 static const struct rfkill_ops nfc_rfkill_ops
= {
194 .set_block
= nfc_rfkill_set_block
,
198 * nfc_start_poll - start polling for nfc targets
200 * @dev: The nfc device that must start polling
201 * @protocols: bitset of nfc protocols that must be used for polling
203 * The device remains polling for targets until a target is found or
204 * the nfc_stop_poll function is called.
206 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
210 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
211 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
213 if (!im_protocols
&& !tm_protocols
)
216 device_lock(&dev
->dev
);
218 if (!device_is_registered(&dev
->dev
)) {
233 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
236 dev
->rf_mode
= NFC_RF_NONE
;
240 device_unlock(&dev
->dev
);
245 * nfc_stop_poll - stop polling for nfc targets
247 * @dev: The nfc device that must stop polling
249 int nfc_stop_poll(struct nfc_dev
*dev
)
253 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
255 device_lock(&dev
->dev
);
257 if (!device_is_registered(&dev
->dev
)) {
267 dev
->ops
->stop_poll(dev
);
268 dev
->polling
= false;
269 dev
->rf_mode
= NFC_RF_NONE
;
272 device_unlock(&dev
->dev
);
276 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
280 if (dev
->n_targets
== 0)
283 for (i
= 0; i
< dev
->n_targets
; i
++) {
284 if (dev
->targets
[i
].idx
== target_idx
)
285 return &dev
->targets
[i
];
291 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
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
)
303 device_lock(&dev
->dev
);
305 if (!device_is_registered(&dev
->dev
)) {
310 if (dev
->dep_link_up
== true) {
315 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
316 if (gb_len
> NFC_MAX_GT_LEN
) {
321 target
= nfc_find_target(dev
, target_index
);
322 if (target
== NULL
) {
327 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
329 dev
->active_target
= target
;
330 dev
->rf_mode
= NFC_RF_INITIATOR
;
334 device_unlock(&dev
->dev
);
338 int nfc_dep_link_down(struct nfc_dev
*dev
)
342 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
344 if (!dev
->ops
->dep_link_down
)
347 device_lock(&dev
->dev
);
349 if (!device_is_registered(&dev
->dev
)) {
354 if (dev
->dep_link_up
== false) {
359 rc
= dev
->ops
->dep_link_down(dev
);
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
);
369 device_unlock(&dev
->dev
);
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 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
381 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
383 EXPORT_SYMBOL(nfc_dep_link_is_up
);
386 * nfc_activate_target - prepare the target for data exchange
388 * @dev: The nfc device that found the target
389 * @target_idx: index of the target that must be activated
390 * @protocol: nfc protocol that will be used for data exchange
392 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
395 struct nfc_target
*target
;
397 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
398 dev_name(&dev
->dev
), target_idx
, protocol
);
400 device_lock(&dev
->dev
);
402 if (!device_is_registered(&dev
->dev
)) {
407 if (dev
->active_target
) {
412 target
= nfc_find_target(dev
, target_idx
);
413 if (target
== NULL
) {
418 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
420 dev
->active_target
= target
;
421 dev
->rf_mode
= NFC_RF_INITIATOR
;
423 if (dev
->ops
->check_presence
&& !dev
->shutting_down
)
424 mod_timer(&dev
->check_pres_timer
, jiffies
+
425 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
429 device_unlock(&dev
->dev
);
434 * nfc_deactivate_target - deactivate a nfc target
436 * @dev: The nfc device that found the target
437 * @target_idx: index of the target that must be deactivated
439 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
443 pr_debug("dev_name=%s target_idx=%u\n",
444 dev_name(&dev
->dev
), target_idx
);
446 device_lock(&dev
->dev
);
448 if (!device_is_registered(&dev
->dev
)) {
453 if (dev
->active_target
== NULL
) {
458 if (dev
->active_target
->idx
!= target_idx
) {
463 if (dev
->ops
->check_presence
)
464 del_timer_sync(&dev
->check_pres_timer
);
466 dev
->ops
->deactivate_target(dev
, dev
->active_target
);
467 dev
->active_target
= NULL
;
470 device_unlock(&dev
->dev
);
475 * nfc_data_exchange - transceive data
477 * @dev: The nfc device that found the target
478 * @target_idx: index of the target
479 * @skb: data to be sent
480 * @cb: callback called when the response is received
481 * @cb_context: parameter for the callback function
483 * The user must wait for the callback before calling this function again.
485 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
486 data_exchange_cb_t cb
, void *cb_context
)
490 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
491 dev_name(&dev
->dev
), target_idx
, skb
->len
);
493 device_lock(&dev
->dev
);
495 if (!device_is_registered(&dev
->dev
)) {
501 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
502 if (dev
->active_target
->idx
!= target_idx
) {
508 if (dev
->ops
->check_presence
)
509 del_timer_sync(&dev
->check_pres_timer
);
511 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
514 if (!rc
&& dev
->ops
->check_presence
&& !dev
->shutting_down
)
515 mod_timer(&dev
->check_pres_timer
, jiffies
+
516 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
517 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
518 rc
= dev
->ops
->tm_send(dev
, skb
);
527 device_unlock(&dev
->dev
);
531 static struct nfc_se
*find_se(struct nfc_dev
*dev
, u32 se_idx
)
533 struct nfc_se
*se
, *n
;
535 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
)
536 if (se
->idx
== se_idx
)
542 int nfc_enable_se(struct nfc_dev
*dev
, u32 se_idx
)
548 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
550 device_lock(&dev
->dev
);
552 if (!device_is_registered(&dev
->dev
)) {
567 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
572 se
= find_se(dev
, se_idx
);
578 if (se
->type
== NFC_SE_ENABLED
) {
583 rc
= dev
->ops
->enable_se(dev
, se_idx
);
586 device_unlock(&dev
->dev
);
590 int nfc_disable_se(struct nfc_dev
*dev
, u32 se_idx
)
596 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
598 device_lock(&dev
->dev
);
600 if (!device_is_registered(&dev
->dev
)) {
610 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
615 se
= find_se(dev
, se_idx
);
621 if (se
->type
== NFC_SE_DISABLED
) {
626 rc
= dev
->ops
->disable_se(dev
, se_idx
);
629 device_unlock(&dev
->dev
);
633 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
635 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
637 if (gb_len
> NFC_MAX_GT_LEN
)
640 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
642 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
644 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
646 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
648 return nfc_llcp_general_bytes(dev
, gb_len
);
650 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
652 int nfc_tm_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
654 /* Only LLCP target mode for now */
655 if (dev
->dep_link_up
== false) {
660 return nfc_llcp_data_received(dev
, skb
);
662 EXPORT_SYMBOL(nfc_tm_data_received
);
664 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
665 u8
*gb
, size_t gb_len
)
669 device_lock(&dev
->dev
);
671 dev
->polling
= false;
674 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
679 dev
->rf_mode
= NFC_RF_TARGET
;
681 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
682 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
684 rc
= nfc_genl_tm_activated(dev
, protocol
);
687 device_unlock(&dev
->dev
);
691 EXPORT_SYMBOL(nfc_tm_activated
);
693 int nfc_tm_deactivated(struct nfc_dev
*dev
)
695 dev
->dep_link_up
= false;
696 dev
->rf_mode
= NFC_RF_NONE
;
698 return nfc_genl_tm_deactivated(dev
);
700 EXPORT_SYMBOL(nfc_tm_deactivated
);
703 * nfc_alloc_send_skb - allocate a skb for data exchange responses
705 * @size: size to allocate
708 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
709 unsigned int flags
, unsigned int size
,
713 unsigned int total_size
;
716 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
718 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
720 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
726 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
728 * @size: size to allocate
731 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
734 unsigned int total_size
;
736 total_size
= size
+ 1;
737 skb
= alloc_skb(total_size
, gfp
);
744 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
747 * nfc_targets_found - inform that targets were found
749 * @dev: The nfc device that found the targets
750 * @targets: array of nfc targets found
751 * @ntargets: targets array size
753 * The device driver must call this function when one or many nfc targets
754 * are found. After calling this function, the device driver must stop
755 * polling for targets.
756 * NOTE: This function can be called with targets=NULL and n_targets=0 to
757 * notify a driver error, meaning that the polling operation cannot complete.
758 * IMPORTANT: this function must not be called from an atomic context.
759 * In addition, it must also not be called from a context that would prevent
760 * the NFC Core to call other nfc ops entry point concurrently.
762 int nfc_targets_found(struct nfc_dev
*dev
,
763 struct nfc_target
*targets
, int n_targets
)
767 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
769 for (i
= 0; i
< n_targets
; i
++)
770 targets
[i
].idx
= dev
->target_next_idx
++;
772 device_lock(&dev
->dev
);
774 if (dev
->polling
== false) {
775 device_unlock(&dev
->dev
);
779 dev
->polling
= false;
781 dev
->targets_generation
++;
787 dev
->targets
= kmemdup(targets
,
788 n_targets
* sizeof(struct nfc_target
),
793 device_unlock(&dev
->dev
);
798 dev
->n_targets
= n_targets
;
799 device_unlock(&dev
->dev
);
801 nfc_genl_targets_found(dev
);
805 EXPORT_SYMBOL(nfc_targets_found
);
808 * nfc_target_lost - inform that an activated target went out of field
810 * @dev: The nfc device that had the activated target in field
811 * @target_idx: the nfc index of the target
813 * The device driver must call this function when the activated target
814 * goes out of the field.
815 * IMPORTANT: this function must not be called from an atomic context.
816 * In addition, it must also not be called from a context that would prevent
817 * the NFC Core to call other nfc ops entry point concurrently.
819 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
821 struct nfc_target
*tg
;
824 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
826 device_lock(&dev
->dev
);
828 for (i
= 0; i
< dev
->n_targets
; i
++) {
829 tg
= &dev
->targets
[i
];
830 if (tg
->idx
== target_idx
)
834 if (i
== dev
->n_targets
) {
835 device_unlock(&dev
->dev
);
839 dev
->targets_generation
++;
841 dev
->active_target
= NULL
;
843 if (dev
->n_targets
) {
844 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
845 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
851 device_unlock(&dev
->dev
);
853 nfc_genl_target_lost(dev
, target_idx
);
857 EXPORT_SYMBOL(nfc_target_lost
);
859 inline void nfc_driver_failure(struct nfc_dev
*dev
, int err
)
861 nfc_targets_found(dev
, NULL
, 0);
863 EXPORT_SYMBOL(nfc_driver_failure
);
865 int nfc_add_se(struct nfc_dev
*dev
, u32 se_idx
, u16 type
)
870 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
872 se
= find_se(dev
, se_idx
);
876 se
= kzalloc(sizeof(struct nfc_se
), GFP_KERNEL
);
882 se
->state
= NFC_SE_DISABLED
;
883 INIT_LIST_HEAD(&se
->list
);
885 list_add(&se
->list
, &dev
->secure_elements
);
887 rc
= nfc_genl_se_added(dev
, se_idx
, type
);
897 EXPORT_SYMBOL(nfc_add_se
);
899 int nfc_remove_se(struct nfc_dev
*dev
, u32 se_idx
)
901 struct nfc_se
*se
, *n
;
904 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
906 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
)
907 if (se
->idx
== se_idx
) {
908 rc
= nfc_genl_se_removed(dev
, se_idx
);
920 EXPORT_SYMBOL(nfc_remove_se
);
922 static void nfc_release(struct device
*d
)
924 struct nfc_dev
*dev
= to_nfc_dev(d
);
925 struct nfc_se
*se
, *n
;
927 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
929 nfc_genl_data_exit(&dev
->genl_data
);
932 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
933 nfc_genl_se_removed(dev
, se
->idx
);
941 static void nfc_check_pres_work(struct work_struct
*work
)
943 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
947 device_lock(&dev
->dev
);
949 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
950 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
951 if (rc
== -EOPNOTSUPP
)
954 u32 active_target_idx
= dev
->active_target
->idx
;
955 device_unlock(&dev
->dev
);
956 nfc_target_lost(dev
, active_target_idx
);
960 if (!dev
->shutting_down
)
961 mod_timer(&dev
->check_pres_timer
, jiffies
+
962 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
966 device_unlock(&dev
->dev
);
969 static void nfc_check_pres_timeout(unsigned long data
)
971 struct nfc_dev
*dev
= (struct nfc_dev
*)data
;
973 schedule_work(&dev
->check_pres_work
);
976 struct class nfc_class
= {
978 .dev_release
= nfc_release
,
980 EXPORT_SYMBOL(nfc_class
);
982 static int match_idx(struct device
*d
, const void *data
)
984 struct nfc_dev
*dev
= to_nfc_dev(d
);
985 const unsigned int *idx
= data
;
987 return dev
->idx
== *idx
;
990 struct nfc_dev
*nfc_get_device(unsigned int idx
)
994 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
998 return to_nfc_dev(d
);
1002 * nfc_allocate_device - allocate a new nfc device
1004 * @ops: device operations
1005 * @supported_protocols: NFC protocols supported by the device
1007 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
1008 u32 supported_protocols
,
1009 int tx_headroom
, int tx_tailroom
)
1011 struct nfc_dev
*dev
;
1013 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
1014 !ops
->deactivate_target
|| !ops
->im_transceive
)
1017 if (!supported_protocols
)
1020 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
1025 dev
->supported_protocols
= supported_protocols
;
1026 dev
->tx_headroom
= tx_headroom
;
1027 dev
->tx_tailroom
= tx_tailroom
;
1028 INIT_LIST_HEAD(&dev
->secure_elements
);
1030 nfc_genl_data_init(&dev
->genl_data
);
1032 dev
->rf_mode
= NFC_RF_NONE
;
1034 /* first generation must not be 0 */
1035 dev
->targets_generation
= 1;
1037 if (ops
->check_presence
) {
1038 init_timer(&dev
->check_pres_timer
);
1039 dev
->check_pres_timer
.data
= (unsigned long)dev
;
1040 dev
->check_pres_timer
.function
= nfc_check_pres_timeout
;
1042 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
1047 EXPORT_SYMBOL(nfc_allocate_device
);
1050 * nfc_register_device - register a nfc device in the nfc subsystem
1052 * @dev: The nfc device to register
1054 int nfc_register_device(struct nfc_dev
*dev
)
1058 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1060 dev
->idx
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
1064 dev
->dev
.class = &nfc_class
;
1065 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
1066 device_initialize(&dev
->dev
);
1068 mutex_lock(&nfc_devlist_mutex
);
1069 nfc_devlist_generation
++;
1070 rc
= device_add(&dev
->dev
);
1071 mutex_unlock(&nfc_devlist_mutex
);
1076 rc
= nfc_llcp_register_device(dev
);
1078 pr_err("Could not register llcp device\n");
1080 rc
= nfc_genl_device_added(dev
);
1082 pr_debug("The userspace won't be notified that the device %s was added\n",
1083 dev_name(&dev
->dev
));
1085 dev
->rfkill
= rfkill_alloc(dev_name(&dev
->dev
), &dev
->dev
,
1086 RFKILL_TYPE_NFC
, &nfc_rfkill_ops
, dev
);
1088 if (rfkill_register(dev
->rfkill
) < 0) {
1089 rfkill_destroy(dev
->rfkill
);
1096 EXPORT_SYMBOL(nfc_register_device
);
1099 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1101 * @dev: The nfc device to unregister
1103 void nfc_unregister_device(struct nfc_dev
*dev
)
1107 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1112 rfkill_unregister(dev
->rfkill
);
1113 rfkill_destroy(dev
->rfkill
);
1116 if (dev
->ops
->check_presence
) {
1117 device_lock(&dev
->dev
);
1118 dev
->shutting_down
= true;
1119 device_unlock(&dev
->dev
);
1120 del_timer_sync(&dev
->check_pres_timer
);
1121 cancel_work_sync(&dev
->check_pres_work
);
1124 rc
= nfc_genl_device_removed(dev
);
1126 pr_debug("The userspace won't be notified that the device %s "
1127 "was removed\n", dev_name(&dev
->dev
));
1129 nfc_llcp_unregister_device(dev
);
1131 mutex_lock(&nfc_devlist_mutex
);
1132 nfc_devlist_generation
++;
1133 device_del(&dev
->dev
);
1134 mutex_unlock(&nfc_devlist_mutex
);
1136 ida_simple_remove(&nfc_index_ida
, id
);
1138 EXPORT_SYMBOL(nfc_unregister_device
);
1140 static int __init
nfc_init(void)
1144 pr_info("NFC Core ver %s\n", VERSION
);
1146 rc
= class_register(&nfc_class
);
1150 rc
= nfc_genl_init();
1154 /* the first generation must not be 0 */
1155 nfc_devlist_generation
= 1;
1157 rc
= rawsock_init();
1161 rc
= nfc_llcp_init();
1178 class_unregister(&nfc_class
);
1182 static void __exit
nfc_exit(void)
1188 class_unregister(&nfc_class
);
1191 subsys_initcall(nfc_init
);
1192 module_exit(nfc_exit
);
1194 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1195 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
1196 MODULE_VERSION(VERSION
);
1197 MODULE_LICENSE("GPL");
1198 MODULE_ALIAS_NETPROTO(PF_NFC
);
1199 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME
);