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 * @im_protocols: bitset of nfc initiator protocols to be used for polling
193 * @tm_protocols: bitset of nfc transport protocols to be used for polling
195 * The device remains polling for targets until a target is found or
196 * the nfc_stop_poll function is called.
198 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
202 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
203 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
205 if (!im_protocols
&& !tm_protocols
)
208 device_lock(&dev
->dev
);
210 if (!device_is_registered(&dev
->dev
)) {
225 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
228 dev
->rf_mode
= NFC_RF_NONE
;
232 device_unlock(&dev
->dev
);
237 * nfc_stop_poll - stop polling for nfc targets
239 * @dev: The nfc device that must stop polling
241 int nfc_stop_poll(struct nfc_dev
*dev
)
245 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
247 device_lock(&dev
->dev
);
249 if (!device_is_registered(&dev
->dev
)) {
259 dev
->ops
->stop_poll(dev
);
260 dev
->polling
= false;
261 dev
->rf_mode
= NFC_RF_NONE
;
264 device_unlock(&dev
->dev
);
268 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
272 for (i
= 0; i
< dev
->n_targets
; i
++) {
273 if (dev
->targets
[i
].idx
== target_idx
)
274 return &dev
->targets
[i
];
280 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
285 struct nfc_target
*target
;
287 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
289 if (!dev
->ops
->dep_link_up
)
292 device_lock(&dev
->dev
);
294 if (!device_is_registered(&dev
->dev
)) {
299 if (dev
->dep_link_up
== true) {
304 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
305 if (gb_len
> NFC_MAX_GT_LEN
) {
310 target
= nfc_find_target(dev
, target_index
);
311 if (target
== NULL
) {
316 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
318 dev
->active_target
= target
;
319 dev
->rf_mode
= NFC_RF_INITIATOR
;
323 device_unlock(&dev
->dev
);
327 int nfc_dep_link_down(struct nfc_dev
*dev
)
331 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
333 if (!dev
->ops
->dep_link_down
)
336 device_lock(&dev
->dev
);
338 if (!device_is_registered(&dev
->dev
)) {
343 if (dev
->dep_link_up
== false) {
348 rc
= dev
->ops
->dep_link_down(dev
);
350 dev
->dep_link_up
= false;
351 dev
->active_target
= NULL
;
352 dev
->rf_mode
= NFC_RF_NONE
;
353 nfc_llcp_mac_is_down(dev
);
354 nfc_genl_dep_link_down_event(dev
);
358 device_unlock(&dev
->dev
);
363 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
364 u8 comm_mode
, u8 rf_mode
)
366 dev
->dep_link_up
= true;
368 if (!dev
->active_target
&& rf_mode
== NFC_RF_INITIATOR
) {
369 struct nfc_target
*target
;
371 target
= nfc_find_target(dev
, target_idx
);
375 dev
->active_target
= target
;
378 dev
->polling
= false;
379 dev
->rf_mode
= rf_mode
;
381 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
383 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
385 EXPORT_SYMBOL(nfc_dep_link_is_up
);
388 * nfc_activate_target - prepare the target for data exchange
390 * @dev: The nfc device that found the target
391 * @target_idx: index of the target that must be activated
392 * @protocol: nfc protocol that will be used for data exchange
394 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
397 struct nfc_target
*target
;
399 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
400 dev_name(&dev
->dev
), target_idx
, protocol
);
402 device_lock(&dev
->dev
);
404 if (!device_is_registered(&dev
->dev
)) {
409 if (dev
->active_target
) {
414 target
= nfc_find_target(dev
, target_idx
);
415 if (target
== NULL
) {
420 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
422 dev
->active_target
= target
;
423 dev
->rf_mode
= NFC_RF_INITIATOR
;
425 if (dev
->ops
->check_presence
&& !dev
->shutting_down
)
426 mod_timer(&dev
->check_pres_timer
, jiffies
+
427 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
431 device_unlock(&dev
->dev
);
436 * nfc_deactivate_target - deactivate a nfc target
438 * @dev: The nfc device that found the target
439 * @target_idx: index of the target that must be deactivated
440 * @mode: idle or sleep?
442 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
, u8 mode
)
446 pr_debug("dev_name=%s target_idx=%u\n",
447 dev_name(&dev
->dev
), target_idx
);
449 device_lock(&dev
->dev
);
451 if (!device_is_registered(&dev
->dev
)) {
456 if (dev
->active_target
== NULL
) {
461 if (dev
->active_target
->idx
!= target_idx
) {
466 if (dev
->ops
->check_presence
)
467 del_timer_sync(&dev
->check_pres_timer
);
469 dev
->ops
->deactivate_target(dev
, dev
->active_target
, mode
);
470 dev
->active_target
= NULL
;
473 device_unlock(&dev
->dev
);
478 * nfc_data_exchange - transceive data
480 * @dev: The nfc device that found the target
481 * @target_idx: index of the target
482 * @skb: data to be sent
483 * @cb: callback called when the response is received
484 * @cb_context: parameter for the callback function
486 * The user must wait for the callback before calling this function again.
488 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
489 data_exchange_cb_t cb
, void *cb_context
)
493 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
494 dev_name(&dev
->dev
), target_idx
, skb
->len
);
496 device_lock(&dev
->dev
);
498 if (!device_is_registered(&dev
->dev
)) {
504 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
505 if (dev
->active_target
->idx
!= target_idx
) {
511 if (dev
->ops
->check_presence
)
512 del_timer_sync(&dev
->check_pres_timer
);
514 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
517 if (!rc
&& dev
->ops
->check_presence
&& !dev
->shutting_down
)
518 mod_timer(&dev
->check_pres_timer
, jiffies
+
519 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
520 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
521 rc
= dev
->ops
->tm_send(dev
, skb
);
530 device_unlock(&dev
->dev
);
534 struct nfc_se
*nfc_find_se(struct nfc_dev
*dev
, u32 se_idx
)
538 list_for_each_entry(se
, &dev
->secure_elements
, list
)
539 if (se
->idx
== se_idx
)
544 EXPORT_SYMBOL(nfc_find_se
);
546 int nfc_enable_se(struct nfc_dev
*dev
, u32 se_idx
)
551 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
553 device_lock(&dev
->dev
);
555 if (!device_is_registered(&dev
->dev
)) {
570 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
575 se
= nfc_find_se(dev
, se_idx
);
581 if (se
->state
== NFC_SE_ENABLED
) {
586 rc
= dev
->ops
->enable_se(dev
, se_idx
);
588 se
->state
= NFC_SE_ENABLED
;
591 device_unlock(&dev
->dev
);
595 int nfc_disable_se(struct nfc_dev
*dev
, u32 se_idx
)
600 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
602 device_lock(&dev
->dev
);
604 if (!device_is_registered(&dev
->dev
)) {
614 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
619 se
= nfc_find_se(dev
, se_idx
);
625 if (se
->state
== NFC_SE_DISABLED
) {
630 rc
= dev
->ops
->disable_se(dev
, se_idx
);
632 se
->state
= NFC_SE_DISABLED
;
635 device_unlock(&dev
->dev
);
639 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
641 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
643 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
645 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
647 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
649 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
651 return nfc_llcp_general_bytes(dev
, gb_len
);
653 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
655 int nfc_tm_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
657 /* Only LLCP target mode for now */
658 if (dev
->dep_link_up
== false) {
663 return nfc_llcp_data_received(dev
, skb
);
665 EXPORT_SYMBOL(nfc_tm_data_received
);
667 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
668 u8
*gb
, size_t gb_len
)
672 device_lock(&dev
->dev
);
674 dev
->polling
= false;
677 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
682 dev
->rf_mode
= NFC_RF_TARGET
;
684 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
685 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
687 rc
= nfc_genl_tm_activated(dev
, protocol
);
690 device_unlock(&dev
->dev
);
694 EXPORT_SYMBOL(nfc_tm_activated
);
696 int nfc_tm_deactivated(struct nfc_dev
*dev
)
698 dev
->dep_link_up
= false;
699 dev
->rf_mode
= NFC_RF_NONE
;
701 return nfc_genl_tm_deactivated(dev
);
703 EXPORT_SYMBOL(nfc_tm_deactivated
);
706 * nfc_alloc_send_skb - allocate a skb for data exchange responses
708 * @dev: device sending the response
709 * @sk: socket sending the response
710 * @flags: MSG_DONTWAIT flag
711 * @size: size to allocate
712 * @err: pointer to memory to store the error code
714 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
715 unsigned int flags
, unsigned int size
,
719 unsigned int total_size
;
722 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
724 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
726 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
732 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
734 * @size: size to allocate
737 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
740 unsigned int total_size
;
742 total_size
= size
+ 1;
743 skb
= alloc_skb(total_size
, gfp
);
750 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
753 * nfc_targets_found - inform that targets were found
755 * @dev: The nfc device that found the targets
756 * @targets: array of nfc targets found
757 * @n_targets: targets array size
759 * The device driver must call this function when one or many nfc targets
760 * are found. After calling this function, the device driver must stop
761 * polling for targets.
762 * NOTE: This function can be called with targets=NULL and n_targets=0 to
763 * notify a driver error, meaning that the polling operation cannot complete.
764 * IMPORTANT: this function must not be called from an atomic context.
765 * In addition, it must also not be called from a context that would prevent
766 * the NFC Core to call other nfc ops entry point concurrently.
768 int nfc_targets_found(struct nfc_dev
*dev
,
769 struct nfc_target
*targets
, int n_targets
)
773 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
775 for (i
= 0; i
< n_targets
; i
++)
776 targets
[i
].idx
= dev
->target_next_idx
++;
778 device_lock(&dev
->dev
);
780 if (dev
->polling
== false) {
781 device_unlock(&dev
->dev
);
785 dev
->polling
= false;
787 dev
->targets_generation
++;
793 dev
->targets
= kmemdup(targets
,
794 n_targets
* sizeof(struct nfc_target
),
799 device_unlock(&dev
->dev
);
804 dev
->n_targets
= n_targets
;
805 device_unlock(&dev
->dev
);
807 nfc_genl_targets_found(dev
);
811 EXPORT_SYMBOL(nfc_targets_found
);
814 * nfc_target_lost - inform that an activated target went out of field
816 * @dev: The nfc device that had the activated target in field
817 * @target_idx: the nfc index of the target
819 * The device driver must call this function when the activated target
820 * goes out of the field.
821 * IMPORTANT: this function must not be called from an atomic context.
822 * In addition, it must also not be called from a context that would prevent
823 * the NFC Core to call other nfc ops entry point concurrently.
825 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
827 struct nfc_target
*tg
;
830 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
832 device_lock(&dev
->dev
);
834 for (i
= 0; i
< dev
->n_targets
; i
++) {
835 tg
= &dev
->targets
[i
];
836 if (tg
->idx
== target_idx
)
840 if (i
== dev
->n_targets
) {
841 device_unlock(&dev
->dev
);
845 dev
->targets_generation
++;
847 dev
->active_target
= NULL
;
849 if (dev
->n_targets
) {
850 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
851 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
857 device_unlock(&dev
->dev
);
859 nfc_genl_target_lost(dev
, target_idx
);
863 EXPORT_SYMBOL(nfc_target_lost
);
865 inline void nfc_driver_failure(struct nfc_dev
*dev
, int err
)
867 nfc_targets_found(dev
, NULL
, 0);
869 EXPORT_SYMBOL(nfc_driver_failure
);
871 int nfc_add_se(struct nfc_dev
*dev
, u32 se_idx
, u16 type
)
876 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
878 se
= nfc_find_se(dev
, se_idx
);
882 se
= kzalloc(sizeof(struct nfc_se
), GFP_KERNEL
);
888 se
->state
= NFC_SE_DISABLED
;
889 INIT_LIST_HEAD(&se
->list
);
891 list_add(&se
->list
, &dev
->secure_elements
);
893 rc
= nfc_genl_se_added(dev
, se_idx
, type
);
903 EXPORT_SYMBOL(nfc_add_se
);
905 int nfc_remove_se(struct nfc_dev
*dev
, u32 se_idx
)
907 struct nfc_se
*se
, *n
;
910 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
912 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
)
913 if (se
->idx
== se_idx
) {
914 rc
= nfc_genl_se_removed(dev
, se_idx
);
926 EXPORT_SYMBOL(nfc_remove_se
);
928 int nfc_se_transaction(struct nfc_dev
*dev
, u8 se_idx
,
929 struct nfc_evt_transaction
*evt_transaction
)
933 pr_debug("transaction: %x\n", se_idx
);
935 device_lock(&dev
->dev
);
937 if (!evt_transaction
) {
942 rc
= nfc_genl_se_transaction(dev
, se_idx
, evt_transaction
);
944 device_unlock(&dev
->dev
);
947 EXPORT_SYMBOL(nfc_se_transaction
);
949 int nfc_se_connectivity(struct nfc_dev
*dev
, u8 se_idx
)
953 pr_debug("connectivity: %x\n", se_idx
);
955 device_lock(&dev
->dev
);
956 rc
= nfc_genl_se_connectivity(dev
, se_idx
);
957 device_unlock(&dev
->dev
);
960 EXPORT_SYMBOL(nfc_se_connectivity
);
962 static void nfc_release(struct device
*d
)
964 struct nfc_dev
*dev
= to_nfc_dev(d
);
965 struct nfc_se
*se
, *n
;
967 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
969 nfc_genl_data_exit(&dev
->genl_data
);
972 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
973 nfc_genl_se_removed(dev
, se
->idx
);
978 ida_simple_remove(&nfc_index_ida
, dev
->idx
);
983 static void nfc_check_pres_work(struct work_struct
*work
)
985 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
989 device_lock(&dev
->dev
);
991 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
992 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
993 if (rc
== -EOPNOTSUPP
)
996 u32 active_target_idx
= dev
->active_target
->idx
;
997 device_unlock(&dev
->dev
);
998 nfc_target_lost(dev
, active_target_idx
);
1002 if (!dev
->shutting_down
)
1003 mod_timer(&dev
->check_pres_timer
, jiffies
+
1004 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
1008 device_unlock(&dev
->dev
);
1011 static void nfc_check_pres_timeout(struct timer_list
*t
)
1013 struct nfc_dev
*dev
= from_timer(dev
, t
, check_pres_timer
);
1015 schedule_work(&dev
->check_pres_work
);
1018 struct class nfc_class
= {
1020 .dev_release
= nfc_release
,
1022 EXPORT_SYMBOL(nfc_class
);
1024 static int match_idx(struct device
*d
, const void *data
)
1026 struct nfc_dev
*dev
= to_nfc_dev(d
);
1027 const unsigned int *idx
= data
;
1029 return dev
->idx
== *idx
;
1032 struct nfc_dev
*nfc_get_device(unsigned int idx
)
1036 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
1040 return to_nfc_dev(d
);
1044 * nfc_allocate_device - allocate a new nfc device
1046 * @ops: device operations
1047 * @supported_protocols: NFC protocols supported by the device
1048 * @tx_headroom: reserved space at beginning of skb
1049 * @tx_tailroom: reserved space at end of skb
1051 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
1052 u32 supported_protocols
,
1053 int tx_headroom
, int tx_tailroom
)
1055 struct nfc_dev
*dev
;
1058 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
1059 !ops
->deactivate_target
|| !ops
->im_transceive
)
1062 if (!supported_protocols
)
1065 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
1069 rc
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
1074 dev
->dev
.class = &nfc_class
;
1075 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
1076 device_initialize(&dev
->dev
);
1079 dev
->supported_protocols
= supported_protocols
;
1080 dev
->tx_headroom
= tx_headroom
;
1081 dev
->tx_tailroom
= tx_tailroom
;
1082 INIT_LIST_HEAD(&dev
->secure_elements
);
1084 nfc_genl_data_init(&dev
->genl_data
);
1086 dev
->rf_mode
= NFC_RF_NONE
;
1088 /* first generation must not be 0 */
1089 dev
->targets_generation
= 1;
1091 if (ops
->check_presence
) {
1092 timer_setup(&dev
->check_pres_timer
, nfc_check_pres_timeout
, 0);
1093 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
1103 EXPORT_SYMBOL(nfc_allocate_device
);
1106 * nfc_register_device - register a nfc device in the nfc subsystem
1108 * @dev: The nfc device to register
1110 int nfc_register_device(struct nfc_dev
*dev
)
1114 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1116 mutex_lock(&nfc_devlist_mutex
);
1117 nfc_devlist_generation
++;
1118 rc
= device_add(&dev
->dev
);
1119 mutex_unlock(&nfc_devlist_mutex
);
1124 rc
= nfc_llcp_register_device(dev
);
1126 pr_err("Could not register llcp device\n");
1128 rc
= nfc_genl_device_added(dev
);
1130 pr_debug("The userspace won't be notified that the device %s was added\n",
1131 dev_name(&dev
->dev
));
1133 dev
->rfkill
= rfkill_alloc(dev_name(&dev
->dev
), &dev
->dev
,
1134 RFKILL_TYPE_NFC
, &nfc_rfkill_ops
, dev
);
1136 if (rfkill_register(dev
->rfkill
) < 0) {
1137 rfkill_destroy(dev
->rfkill
);
1144 EXPORT_SYMBOL(nfc_register_device
);
1147 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1149 * @dev: The nfc device to unregister
1151 void nfc_unregister_device(struct nfc_dev
*dev
)
1155 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1158 rfkill_unregister(dev
->rfkill
);
1159 rfkill_destroy(dev
->rfkill
);
1162 if (dev
->ops
->check_presence
) {
1163 device_lock(&dev
->dev
);
1164 dev
->shutting_down
= true;
1165 device_unlock(&dev
->dev
);
1166 del_timer_sync(&dev
->check_pres_timer
);
1167 cancel_work_sync(&dev
->check_pres_work
);
1170 rc
= nfc_genl_device_removed(dev
);
1172 pr_debug("The userspace won't be notified that the device %s "
1173 "was removed\n", dev_name(&dev
->dev
));
1175 nfc_llcp_unregister_device(dev
);
1177 mutex_lock(&nfc_devlist_mutex
);
1178 nfc_devlist_generation
++;
1179 device_del(&dev
->dev
);
1180 mutex_unlock(&nfc_devlist_mutex
);
1182 EXPORT_SYMBOL(nfc_unregister_device
);
1184 static int __init
nfc_init(void)
1188 pr_info("NFC Core ver %s\n", VERSION
);
1190 rc
= class_register(&nfc_class
);
1194 rc
= nfc_genl_init();
1198 /* the first generation must not be 0 */
1199 nfc_devlist_generation
= 1;
1201 rc
= rawsock_init();
1205 rc
= nfc_llcp_init();
1222 class_unregister(&nfc_class
);
1226 static void __exit
nfc_exit(void)
1232 class_unregister(&nfc_class
);
1235 subsys_initcall(nfc_init
);
1236 module_exit(nfc_exit
);
1238 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1239 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
1240 MODULE_VERSION(VERSION
);
1241 MODULE_LICENSE("GPL");
1242 MODULE_ALIAS_NETPROTO(PF_NFC
);
1243 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME
);