1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/rfkill.h>
17 #include <linux/nfc.h>
19 #include <net/genetlink.h>
25 #define NFC_CHECK_PRES_FREQ_MS 2000
27 int nfc_devlist_generation
;
28 DEFINE_MUTEX(nfc_devlist_mutex
);
30 /* NFC device ID bitmap */
31 static DEFINE_IDA(nfc_index_ida
);
33 int nfc_fw_download(struct nfc_dev
*dev
, const char *firmware_name
)
37 pr_debug("%s do firmware %s\n", dev_name(&dev
->dev
), firmware_name
);
39 device_lock(&dev
->dev
);
41 if (!device_is_registered(&dev
->dev
)) {
51 if (!dev
->ops
->fw_download
) {
56 dev
->fw_download_in_progress
= true;
57 rc
= dev
->ops
->fw_download(dev
, firmware_name
);
59 dev
->fw_download_in_progress
= false;
62 device_unlock(&dev
->dev
);
67 * nfc_fw_download_done - inform that a firmware download was completed
69 * @dev: The nfc device to which firmware was downloaded
70 * @firmware_name: The firmware filename
71 * @result: The positive value of a standard errno value
73 int nfc_fw_download_done(struct nfc_dev
*dev
, const char *firmware_name
,
76 dev
->fw_download_in_progress
= false;
78 return nfc_genl_fw_download_done(dev
, firmware_name
, result
);
80 EXPORT_SYMBOL(nfc_fw_download_done
);
83 * nfc_dev_up - turn on the NFC device
85 * @dev: The nfc device to be turned on
87 * The device remains up until the nfc_dev_down function is called.
89 int nfc_dev_up(struct nfc_dev
*dev
)
93 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
95 device_lock(&dev
->dev
);
97 if (dev
->rfkill
&& rfkill_blocked(dev
->rfkill
)) {
102 if (!device_is_registered(&dev
->dev
)) {
107 if (dev
->fw_download_in_progress
) {
117 if (dev
->ops
->dev_up
)
118 rc
= dev
->ops
->dev_up(dev
);
123 /* We have to enable the device before discovering SEs */
124 if (dev
->ops
->discover_se
&& dev
->ops
->discover_se(dev
))
125 pr_err("SE discovery failed\n");
128 device_unlock(&dev
->dev
);
133 * nfc_dev_down - turn off the NFC device
135 * @dev: The nfc device to be turned off
137 int nfc_dev_down(struct nfc_dev
*dev
)
141 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
143 device_lock(&dev
->dev
);
145 if (!device_is_registered(&dev
->dev
)) {
155 if (dev
->polling
|| dev
->active_target
) {
160 if (dev
->ops
->dev_down
)
161 dev
->ops
->dev_down(dev
);
166 device_unlock(&dev
->dev
);
170 static int nfc_rfkill_set_block(void *data
, bool blocked
)
172 struct nfc_dev
*dev
= data
;
174 pr_debug("%s blocked %d", dev_name(&dev
->dev
), blocked
);
184 static const struct rfkill_ops nfc_rfkill_ops
= {
185 .set_block
= nfc_rfkill_set_block
,
189 * nfc_start_poll - start polling for nfc targets
191 * @dev: The nfc device that must start polling
192 * @protocols: bitset of nfc protocols that must be used for polling
194 * The device remains polling for targets until a target is found or
195 * the nfc_stop_poll function is called.
197 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
201 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
202 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
204 if (!im_protocols
&& !tm_protocols
)
207 device_lock(&dev
->dev
);
209 if (!device_is_registered(&dev
->dev
)) {
224 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
227 dev
->rf_mode
= NFC_RF_NONE
;
231 device_unlock(&dev
->dev
);
236 * nfc_stop_poll - stop polling for nfc targets
238 * @dev: The nfc device that must stop polling
240 int nfc_stop_poll(struct nfc_dev
*dev
)
244 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
246 device_lock(&dev
->dev
);
248 if (!device_is_registered(&dev
->dev
)) {
258 dev
->ops
->stop_poll(dev
);
259 dev
->polling
= false;
260 dev
->rf_mode
= NFC_RF_NONE
;
263 device_unlock(&dev
->dev
);
267 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
271 for (i
= 0; i
< dev
->n_targets
; i
++) {
272 if (dev
->targets
[i
].idx
== target_idx
)
273 return &dev
->targets
[i
];
279 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
284 struct nfc_target
*target
;
286 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
288 if (!dev
->ops
->dep_link_up
)
291 device_lock(&dev
->dev
);
293 if (!device_is_registered(&dev
->dev
)) {
298 if (dev
->dep_link_up
== true) {
303 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
304 if (gb_len
> NFC_MAX_GT_LEN
) {
309 target
= nfc_find_target(dev
, target_index
);
310 if (target
== NULL
) {
315 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
317 dev
->active_target
= target
;
318 dev
->rf_mode
= NFC_RF_INITIATOR
;
322 device_unlock(&dev
->dev
);
326 int nfc_dep_link_down(struct nfc_dev
*dev
)
330 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
332 if (!dev
->ops
->dep_link_down
)
335 device_lock(&dev
->dev
);
337 if (!device_is_registered(&dev
->dev
)) {
342 if (dev
->dep_link_up
== false) {
347 rc
= dev
->ops
->dep_link_down(dev
);
349 dev
->dep_link_up
= false;
350 dev
->active_target
= NULL
;
351 dev
->rf_mode
= NFC_RF_NONE
;
352 nfc_llcp_mac_is_down(dev
);
353 nfc_genl_dep_link_down_event(dev
);
357 device_unlock(&dev
->dev
);
362 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
363 u8 comm_mode
, u8 rf_mode
)
365 dev
->dep_link_up
= true;
367 if (!dev
->active_target
&& rf_mode
== NFC_RF_INITIATOR
) {
368 struct nfc_target
*target
;
370 target
= nfc_find_target(dev
, target_idx
);
374 dev
->active_target
= target
;
377 dev
->polling
= false;
378 dev
->rf_mode
= rf_mode
;
380 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
382 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
384 EXPORT_SYMBOL(nfc_dep_link_is_up
);
387 * nfc_activate_target - prepare the target for data exchange
389 * @dev: The nfc device that found the target
390 * @target_idx: index of the target that must be activated
391 * @protocol: nfc protocol that will be used for data exchange
393 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
396 struct nfc_target
*target
;
398 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
399 dev_name(&dev
->dev
), target_idx
, protocol
);
401 device_lock(&dev
->dev
);
403 if (!device_is_registered(&dev
->dev
)) {
408 if (dev
->active_target
) {
413 target
= nfc_find_target(dev
, target_idx
);
414 if (target
== NULL
) {
419 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
421 dev
->active_target
= target
;
422 dev
->rf_mode
= NFC_RF_INITIATOR
;
424 if (dev
->ops
->check_presence
&& !dev
->shutting_down
)
425 mod_timer(&dev
->check_pres_timer
, jiffies
+
426 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
430 device_unlock(&dev
->dev
);
435 * nfc_deactivate_target - deactivate a nfc target
437 * @dev: The nfc device that found the target
438 * @target_idx: index of the target that must be deactivated
440 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
, u8 mode
)
444 pr_debug("dev_name=%s target_idx=%u\n",
445 dev_name(&dev
->dev
), target_idx
);
447 device_lock(&dev
->dev
);
449 if (!device_is_registered(&dev
->dev
)) {
454 if (dev
->active_target
== NULL
) {
459 if (dev
->active_target
->idx
!= target_idx
) {
464 if (dev
->ops
->check_presence
)
465 del_timer_sync(&dev
->check_pres_timer
);
467 dev
->ops
->deactivate_target(dev
, dev
->active_target
, mode
);
468 dev
->active_target
= NULL
;
471 device_unlock(&dev
->dev
);
476 * nfc_data_exchange - transceive data
478 * @dev: The nfc device that found the target
479 * @target_idx: index of the target
480 * @skb: data to be sent
481 * @cb: callback called when the response is received
482 * @cb_context: parameter for the callback function
484 * The user must wait for the callback before calling this function again.
486 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
487 data_exchange_cb_t cb
, void *cb_context
)
491 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
492 dev_name(&dev
->dev
), target_idx
, skb
->len
);
494 device_lock(&dev
->dev
);
496 if (!device_is_registered(&dev
->dev
)) {
502 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
503 if (dev
->active_target
->idx
!= target_idx
) {
509 if (dev
->ops
->check_presence
)
510 del_timer_sync(&dev
->check_pres_timer
);
512 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
515 if (!rc
&& dev
->ops
->check_presence
&& !dev
->shutting_down
)
516 mod_timer(&dev
->check_pres_timer
, jiffies
+
517 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
518 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
519 rc
= dev
->ops
->tm_send(dev
, skb
);
528 device_unlock(&dev
->dev
);
532 struct nfc_se
*nfc_find_se(struct nfc_dev
*dev
, u32 se_idx
)
536 list_for_each_entry(se
, &dev
->secure_elements
, list
)
537 if (se
->idx
== se_idx
)
542 EXPORT_SYMBOL(nfc_find_se
);
544 int nfc_enable_se(struct nfc_dev
*dev
, u32 se_idx
)
549 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
551 device_lock(&dev
->dev
);
553 if (!device_is_registered(&dev
->dev
)) {
568 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
573 se
= nfc_find_se(dev
, se_idx
);
579 if (se
->state
== NFC_SE_ENABLED
) {
584 rc
= dev
->ops
->enable_se(dev
, se_idx
);
586 se
->state
= NFC_SE_ENABLED
;
589 device_unlock(&dev
->dev
);
593 int nfc_disable_se(struct nfc_dev
*dev
, u32 se_idx
)
598 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
600 device_lock(&dev
->dev
);
602 if (!device_is_registered(&dev
->dev
)) {
612 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
617 se
= nfc_find_se(dev
, se_idx
);
623 if (se
->state
== NFC_SE_DISABLED
) {
628 rc
= dev
->ops
->disable_se(dev
, se_idx
);
630 se
->state
= NFC_SE_DISABLED
;
633 device_unlock(&dev
->dev
);
637 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
639 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
641 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
643 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
645 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
647 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
649 return nfc_llcp_general_bytes(dev
, gb_len
);
651 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
653 int nfc_tm_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
655 /* Only LLCP target mode for now */
656 if (dev
->dep_link_up
== false) {
661 return nfc_llcp_data_received(dev
, skb
);
663 EXPORT_SYMBOL(nfc_tm_data_received
);
665 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
666 u8
*gb
, size_t gb_len
)
670 device_lock(&dev
->dev
);
672 dev
->polling
= false;
675 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
680 dev
->rf_mode
= NFC_RF_TARGET
;
682 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
683 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
685 rc
= nfc_genl_tm_activated(dev
, protocol
);
688 device_unlock(&dev
->dev
);
692 EXPORT_SYMBOL(nfc_tm_activated
);
694 int nfc_tm_deactivated(struct nfc_dev
*dev
)
696 dev
->dep_link_up
= false;
697 dev
->rf_mode
= NFC_RF_NONE
;
699 return nfc_genl_tm_deactivated(dev
);
701 EXPORT_SYMBOL(nfc_tm_deactivated
);
704 * nfc_alloc_send_skb - allocate a skb for data exchange responses
706 * @size: size to allocate
709 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
710 unsigned int flags
, unsigned int size
,
714 unsigned int total_size
;
717 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
719 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
721 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
727 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
729 * @size: size to allocate
732 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
735 unsigned int total_size
;
737 total_size
= size
+ 1;
738 skb
= alloc_skb(total_size
, gfp
);
745 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
748 * nfc_targets_found - inform that targets were found
750 * @dev: The nfc device that found the targets
751 * @targets: array of nfc targets found
752 * @ntargets: targets array size
754 * The device driver must call this function when one or many nfc targets
755 * are found. After calling this function, the device driver must stop
756 * polling for targets.
757 * NOTE: This function can be called with targets=NULL and n_targets=0 to
758 * notify a driver error, meaning that the polling operation cannot complete.
759 * IMPORTANT: this function must not be called from an atomic context.
760 * In addition, it must also not be called from a context that would prevent
761 * the NFC Core to call other nfc ops entry point concurrently.
763 int nfc_targets_found(struct nfc_dev
*dev
,
764 struct nfc_target
*targets
, int n_targets
)
768 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
770 for (i
= 0; i
< n_targets
; i
++)
771 targets
[i
].idx
= dev
->target_next_idx
++;
773 device_lock(&dev
->dev
);
775 if (dev
->polling
== false) {
776 device_unlock(&dev
->dev
);
780 dev
->polling
= false;
782 dev
->targets_generation
++;
788 dev
->targets
= kmemdup(targets
,
789 n_targets
* sizeof(struct nfc_target
),
794 device_unlock(&dev
->dev
);
799 dev
->n_targets
= n_targets
;
800 device_unlock(&dev
->dev
);
802 nfc_genl_targets_found(dev
);
806 EXPORT_SYMBOL(nfc_targets_found
);
809 * nfc_target_lost - inform that an activated target went out of field
811 * @dev: The nfc device that had the activated target in field
812 * @target_idx: the nfc index of the target
814 * The device driver must call this function when the activated target
815 * goes out of the field.
816 * IMPORTANT: this function must not be called from an atomic context.
817 * In addition, it must also not be called from a context that would prevent
818 * the NFC Core to call other nfc ops entry point concurrently.
820 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
822 struct nfc_target
*tg
;
825 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
827 device_lock(&dev
->dev
);
829 for (i
= 0; i
< dev
->n_targets
; i
++) {
830 tg
= &dev
->targets
[i
];
831 if (tg
->idx
== target_idx
)
835 if (i
== dev
->n_targets
) {
836 device_unlock(&dev
->dev
);
840 dev
->targets_generation
++;
842 dev
->active_target
= NULL
;
844 if (dev
->n_targets
) {
845 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
846 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
852 device_unlock(&dev
->dev
);
854 nfc_genl_target_lost(dev
, target_idx
);
858 EXPORT_SYMBOL(nfc_target_lost
);
860 inline void nfc_driver_failure(struct nfc_dev
*dev
, int err
)
862 nfc_targets_found(dev
, NULL
, 0);
864 EXPORT_SYMBOL(nfc_driver_failure
);
866 int nfc_add_se(struct nfc_dev
*dev
, u32 se_idx
, u16 type
)
871 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
873 se
= nfc_find_se(dev
, se_idx
);
877 se
= kzalloc(sizeof(struct nfc_se
), GFP_KERNEL
);
883 se
->state
= NFC_SE_DISABLED
;
884 INIT_LIST_HEAD(&se
->list
);
886 list_add(&se
->list
, &dev
->secure_elements
);
888 rc
= nfc_genl_se_added(dev
, se_idx
, type
);
898 EXPORT_SYMBOL(nfc_add_se
);
900 int nfc_remove_se(struct nfc_dev
*dev
, u32 se_idx
)
902 struct nfc_se
*se
, *n
;
905 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
907 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
)
908 if (se
->idx
== se_idx
) {
909 rc
= nfc_genl_se_removed(dev
, se_idx
);
921 EXPORT_SYMBOL(nfc_remove_se
);
923 int nfc_se_transaction(struct nfc_dev
*dev
, u8 se_idx
,
924 struct nfc_evt_transaction
*evt_transaction
)
928 pr_debug("transaction: %x\n", se_idx
);
930 device_lock(&dev
->dev
);
932 if (!evt_transaction
) {
937 rc
= nfc_genl_se_transaction(dev
, se_idx
, evt_transaction
);
939 device_unlock(&dev
->dev
);
942 EXPORT_SYMBOL(nfc_se_transaction
);
944 int nfc_se_connectivity(struct nfc_dev
*dev
, u8 se_idx
)
948 pr_debug("connectivity: %x\n", se_idx
);
950 device_lock(&dev
->dev
);
951 rc
= nfc_genl_se_connectivity(dev
, se_idx
);
952 device_unlock(&dev
->dev
);
955 EXPORT_SYMBOL(nfc_se_connectivity
);
957 static void nfc_release(struct device
*d
)
959 struct nfc_dev
*dev
= to_nfc_dev(d
);
960 struct nfc_se
*se
, *n
;
962 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
964 nfc_genl_data_exit(&dev
->genl_data
);
967 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
968 nfc_genl_se_removed(dev
, se
->idx
);
973 ida_simple_remove(&nfc_index_ida
, dev
->idx
);
978 static void nfc_check_pres_work(struct work_struct
*work
)
980 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
984 device_lock(&dev
->dev
);
986 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
987 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
988 if (rc
== -EOPNOTSUPP
)
991 u32 active_target_idx
= dev
->active_target
->idx
;
992 device_unlock(&dev
->dev
);
993 nfc_target_lost(dev
, active_target_idx
);
997 if (!dev
->shutting_down
)
998 mod_timer(&dev
->check_pres_timer
, jiffies
+
999 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
1003 device_unlock(&dev
->dev
);
1006 static void nfc_check_pres_timeout(struct timer_list
*t
)
1008 struct nfc_dev
*dev
= from_timer(dev
, t
, check_pres_timer
);
1010 schedule_work(&dev
->check_pres_work
);
1013 struct class nfc_class
= {
1015 .dev_release
= nfc_release
,
1017 EXPORT_SYMBOL(nfc_class
);
1019 static int match_idx(struct device
*d
, const void *data
)
1021 struct nfc_dev
*dev
= to_nfc_dev(d
);
1022 const unsigned int *idx
= data
;
1024 return dev
->idx
== *idx
;
1027 struct nfc_dev
*nfc_get_device(unsigned int idx
)
1031 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
1035 return to_nfc_dev(d
);
1039 * nfc_allocate_device - allocate a new nfc device
1041 * @ops: device operations
1042 * @supported_protocols: NFC protocols supported by the device
1044 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
1045 u32 supported_protocols
,
1046 int tx_headroom
, int tx_tailroom
)
1048 struct nfc_dev
*dev
;
1051 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
1052 !ops
->deactivate_target
|| !ops
->im_transceive
)
1055 if (!supported_protocols
)
1058 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
1062 rc
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
1067 dev
->dev
.class = &nfc_class
;
1068 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
1069 device_initialize(&dev
->dev
);
1072 dev
->supported_protocols
= supported_protocols
;
1073 dev
->tx_headroom
= tx_headroom
;
1074 dev
->tx_tailroom
= tx_tailroom
;
1075 INIT_LIST_HEAD(&dev
->secure_elements
);
1077 nfc_genl_data_init(&dev
->genl_data
);
1079 dev
->rf_mode
= NFC_RF_NONE
;
1081 /* first generation must not be 0 */
1082 dev
->targets_generation
= 1;
1084 if (ops
->check_presence
) {
1085 timer_setup(&dev
->check_pres_timer
, nfc_check_pres_timeout
, 0);
1086 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
1096 EXPORT_SYMBOL(nfc_allocate_device
);
1099 * nfc_register_device - register a nfc device in the nfc subsystem
1101 * @dev: The nfc device to register
1103 int nfc_register_device(struct nfc_dev
*dev
)
1107 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1109 mutex_lock(&nfc_devlist_mutex
);
1110 nfc_devlist_generation
++;
1111 rc
= device_add(&dev
->dev
);
1112 mutex_unlock(&nfc_devlist_mutex
);
1117 rc
= nfc_llcp_register_device(dev
);
1119 pr_err("Could not register llcp device\n");
1121 rc
= nfc_genl_device_added(dev
);
1123 pr_debug("The userspace won't be notified that the device %s was added\n",
1124 dev_name(&dev
->dev
));
1126 dev
->rfkill
= rfkill_alloc(dev_name(&dev
->dev
), &dev
->dev
,
1127 RFKILL_TYPE_NFC
, &nfc_rfkill_ops
, dev
);
1129 if (rfkill_register(dev
->rfkill
) < 0) {
1130 rfkill_destroy(dev
->rfkill
);
1137 EXPORT_SYMBOL(nfc_register_device
);
1140 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1142 * @dev: The nfc device to unregister
1144 void nfc_unregister_device(struct nfc_dev
*dev
)
1148 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1151 rfkill_unregister(dev
->rfkill
);
1152 rfkill_destroy(dev
->rfkill
);
1155 if (dev
->ops
->check_presence
) {
1156 device_lock(&dev
->dev
);
1157 dev
->shutting_down
= true;
1158 device_unlock(&dev
->dev
);
1159 del_timer_sync(&dev
->check_pres_timer
);
1160 cancel_work_sync(&dev
->check_pres_work
);
1163 rc
= nfc_genl_device_removed(dev
);
1165 pr_debug("The userspace won't be notified that the device %s "
1166 "was removed\n", dev_name(&dev
->dev
));
1168 nfc_llcp_unregister_device(dev
);
1170 mutex_lock(&nfc_devlist_mutex
);
1171 nfc_devlist_generation
++;
1172 device_del(&dev
->dev
);
1173 mutex_unlock(&nfc_devlist_mutex
);
1175 EXPORT_SYMBOL(nfc_unregister_device
);
1177 static int __init
nfc_init(void)
1181 pr_info("NFC Core ver %s\n", VERSION
);
1183 rc
= class_register(&nfc_class
);
1187 rc
= nfc_genl_init();
1191 /* the first generation must not be 0 */
1192 nfc_devlist_generation
= 1;
1194 rc
= rawsock_init();
1198 rc
= nfc_llcp_init();
1215 class_unregister(&nfc_class
);
1219 static void __exit
nfc_exit(void)
1225 class_unregister(&nfc_class
);
1228 subsys_initcall(nfc_init
);
1229 module_exit(nfc_exit
);
1231 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1232 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
1233 MODULE_VERSION(VERSION
);
1234 MODULE_LICENSE("GPL");
1235 MODULE_ALIAS_NETPROTO(PF_NFC
);
1236 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME
);