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_download(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_download
) {
70 dev
->fw_download_in_progress
= true;
71 rc
= dev
->ops
->fw_download(dev
, firmware_name
);
73 dev
->fw_download_in_progress
= false;
76 device_unlock(&dev
->dev
);
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
,
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
);
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
)
107 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
109 device_lock(&dev
->dev
);
111 if (dev
->rfkill
&& rfkill_blocked(dev
->rfkill
)) {
116 if (!device_is_registered(&dev
->dev
)) {
121 if (dev
->fw_download_in_progress
) {
131 if (dev
->ops
->dev_up
)
132 rc
= dev
->ops
->dev_up(dev
);
137 /* We have to enable the device before discovering SEs */
138 if (dev
->ops
->discover_se
) {
139 rc
= dev
->ops
->discover_se(dev
);
141 pr_warn("SE discovery failed\n");
145 device_unlock(&dev
->dev
);
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
)
158 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
160 device_lock(&dev
->dev
);
162 if (!device_is_registered(&dev
->dev
)) {
172 if (dev
->polling
|| dev
->active_target
) {
177 if (dev
->ops
->dev_down
)
178 dev
->ops
->dev_down(dev
);
183 device_unlock(&dev
->dev
);
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
);
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
)
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
)
224 device_lock(&dev
->dev
);
226 if (!device_is_registered(&dev
->dev
)) {
241 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
244 dev
->rf_mode
= NFC_RF_NONE
;
248 device_unlock(&dev
->dev
);
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
)
261 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
263 device_lock(&dev
->dev
);
265 if (!device_is_registered(&dev
->dev
)) {
275 dev
->ops
->stop_poll(dev
);
276 dev
->polling
= false;
277 dev
->rf_mode
= NFC_RF_NONE
;
280 device_unlock(&dev
->dev
);
284 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
288 if (dev
->n_targets
== 0)
291 for (i
= 0; i
< dev
->n_targets
; i
++) {
292 if (dev
->targets
[i
].idx
== target_idx
)
293 return &dev
->targets
[i
];
299 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
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
)
311 device_lock(&dev
->dev
);
313 if (!device_is_registered(&dev
->dev
)) {
318 if (dev
->dep_link_up
== true) {
323 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
324 if (gb_len
> NFC_MAX_GT_LEN
) {
329 target
= nfc_find_target(dev
, target_index
);
330 if (target
== NULL
) {
335 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
337 dev
->active_target
= target
;
338 dev
->rf_mode
= NFC_RF_INITIATOR
;
342 device_unlock(&dev
->dev
);
346 int nfc_dep_link_down(struct nfc_dev
*dev
)
350 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
352 if (!dev
->ops
->dep_link_down
)
355 device_lock(&dev
->dev
);
357 if (!device_is_registered(&dev
->dev
)) {
362 if (dev
->dep_link_up
== false) {
367 rc
= dev
->ops
->dep_link_down(dev
);
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
);
377 device_unlock(&dev
->dev
);
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
);
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
)
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
)) {
428 if (dev
->active_target
) {
433 target
= nfc_find_target(dev
, target_idx
);
434 if (target
== NULL
) {
439 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
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
));
450 device_unlock(&dev
->dev
);
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
)
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
)) {
474 if (dev
->active_target
== NULL
) {
479 if (dev
->active_target
->idx
!= target_idx
) {
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
;
491 device_unlock(&dev
->dev
);
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
)
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
)) {
522 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
523 if (dev
->active_target
->idx
!= target_idx
) {
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
,
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
);
548 device_unlock(&dev
->dev
);
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
)
562 EXPORT_SYMBOL(nfc_find_se
);
564 int nfc_enable_se(struct nfc_dev
*dev
, u32 se_idx
)
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
)) {
589 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
594 se
= nfc_find_se(dev
, se_idx
);
600 if (se
->state
== NFC_SE_ENABLED
) {
605 rc
= dev
->ops
->enable_se(dev
, se_idx
);
607 se
->state
= NFC_SE_ENABLED
;
610 device_unlock(&dev
->dev
);
614 int nfc_disable_se(struct nfc_dev
*dev
, u32 se_idx
)
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
)) {
634 if (!dev
->ops
->enable_se
|| !dev
->ops
->disable_se
) {
639 se
= nfc_find_se(dev
, se_idx
);
645 if (se
->state
== NFC_SE_DISABLED
) {
650 rc
= dev
->ops
->disable_se(dev
, se_idx
);
652 se
->state
= NFC_SE_DISABLED
;
655 device_unlock(&dev
->dev
);
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
)
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) {
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
)
695 device_lock(&dev
->dev
);
697 dev
->polling
= false;
700 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
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
);
713 device_unlock(&dev
->dev
);
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
734 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
735 unsigned int flags
, unsigned int size
,
739 unsigned int total_size
;
742 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
744 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
746 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
752 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
754 * @size: size to allocate
757 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
760 unsigned int total_size
;
762 total_size
= size
+ 1;
763 skb
= alloc_skb(total_size
, gfp
);
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
)
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
);
805 dev
->polling
= false;
807 dev
->targets_generation
++;
813 dev
->targets
= kmemdup(targets
,
814 n_targets
* sizeof(struct nfc_target
),
819 device_unlock(&dev
->dev
);
824 dev
->n_targets
= n_targets
;
825 device_unlock(&dev
->dev
);
827 nfc_genl_targets_found(dev
);
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
;
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
)
860 if (i
== dev
->n_targets
) {
861 device_unlock(&dev
->dev
);
865 dev
->targets_generation
++;
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
));
877 device_unlock(&dev
->dev
);
879 nfc_genl_target_lost(dev
, target_idx
);
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
)
896 pr_debug("%s se index %d\n", dev_name(&dev
->dev
), se_idx
);
898 se
= nfc_find_se(dev
, se_idx
);
902 se
= kzalloc(sizeof(struct nfc_se
), GFP_KERNEL
);
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
);
923 EXPORT_SYMBOL(nfc_add_se
);
925 int nfc_remove_se(struct nfc_dev
*dev
, u32 se_idx
)
927 struct nfc_se
*se
, *n
;
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
);
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
);
958 list_for_each_entry_safe(se
, n
, &dev
->secure_elements
, list
) {
959 nfc_genl_se_removed(dev
, se
->idx
);
967 static void nfc_check_pres_work(struct work_struct
*work
)
969 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
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
)
980 u32 active_target_idx
= dev
->active_target
->idx
;
981 device_unlock(&dev
->dev
);
982 nfc_target_lost(dev
, active_target_idx
);
986 if (!dev
->shutting_down
)
987 mod_timer(&dev
->check_pres_timer
, jiffies
+
988 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
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
= {
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
)
1020 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
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
)
1043 if (!supported_protocols
)
1046 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
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
);
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
)
1084 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
1086 dev
->idx
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
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
);
1102 rc
= nfc_llcp_register_device(dev
);
1104 pr_err("Could not register llcp device\n");
1106 rc
= nfc_genl_device_added(dev
);
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
);
1114 if (rfkill_register(dev
->rfkill
) < 0) {
1115 rfkill_destroy(dev
->rfkill
);
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
)
1133 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
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
);
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)
1170 pr_info("NFC Core ver %s\n", VERSION
);
1172 rc
= class_register(&nfc_class
);
1176 rc
= nfc_genl_init();
1180 /* the first generation must not be 0 */
1181 nfc_devlist_generation
= 1;
1183 rc
= rawsock_init();
1187 rc
= nfc_llcp_init();
1204 class_unregister(&nfc_class
);
1208 static void __exit
nfc_exit(void)
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
);