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
);
130 device_unlock(&dev
->dev
);
135 * nfc_dev_down - turn off the NFC device
137 * @dev: The nfc device to be turned off
139 int nfc_dev_down(struct nfc_dev
*dev
)
143 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
145 device_lock(&dev
->dev
);
147 if (!device_is_registered(&dev
->dev
)) {
157 if (dev
->polling
|| dev
->active_target
) {
162 if (dev
->ops
->dev_down
)
163 dev
->ops
->dev_down(dev
);
168 device_unlock(&dev
->dev
);
172 static int nfc_rfkill_set_block(void *data
, bool blocked
)
174 struct nfc_dev
*dev
= data
;
176 pr_debug("%s blocked %d", dev_name(&dev
->dev
), blocked
);
186 static const struct rfkill_ops nfc_rfkill_ops
= {
187 .set_block
= nfc_rfkill_set_block
,
191 * nfc_start_poll - start polling for nfc targets
193 * @dev: The nfc device that must start polling
194 * @protocols: bitset of nfc protocols that must be used for polling
196 * The device remains polling for targets until a target is found or
197 * the nfc_stop_poll function is called.
199 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
203 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
204 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
206 if (!im_protocols
&& !tm_protocols
)
209 device_lock(&dev
->dev
);
211 if (!device_is_registered(&dev
->dev
)) {
226 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
229 dev
->rf_mode
= NFC_RF_NONE
;
233 device_unlock(&dev
->dev
);
238 * nfc_stop_poll - stop polling for nfc targets
240 * @dev: The nfc device that must stop polling
242 int nfc_stop_poll(struct nfc_dev
*dev
)
246 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
248 device_lock(&dev
->dev
);
250 if (!device_is_registered(&dev
->dev
)) {
260 dev
->ops
->stop_poll(dev
);
261 dev
->polling
= false;
262 dev
->rf_mode
= NFC_RF_NONE
;
265 device_unlock(&dev
->dev
);
269 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
273 if (dev
->n_targets
== 0)
276 for (i
= 0; i
< dev
->n_targets
; i
++) {
277 if (dev
->targets
[i
].idx
== target_idx
)
278 return &dev
->targets
[i
];
284 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
289 struct nfc_target
*target
;
291 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
293 if (!dev
->ops
->dep_link_up
)
296 device_lock(&dev
->dev
);
298 if (!device_is_registered(&dev
->dev
)) {
303 if (dev
->dep_link_up
== true) {
308 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
309 if (gb_len
> NFC_MAX_GT_LEN
) {
314 target
= nfc_find_target(dev
, target_index
);
315 if (target
== NULL
) {
320 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
322 dev
->active_target
= target
;
323 dev
->rf_mode
= NFC_RF_INITIATOR
;
327 device_unlock(&dev
->dev
);
331 int nfc_dep_link_down(struct nfc_dev
*dev
)
335 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
337 if (!dev
->ops
->dep_link_down
)
340 device_lock(&dev
->dev
);
342 if (!device_is_registered(&dev
->dev
)) {
347 if (dev
->dep_link_up
== false) {
352 rc
= dev
->ops
->dep_link_down(dev
);
354 dev
->dep_link_up
= false;
355 dev
->active_target
= NULL
;
356 dev
->rf_mode
= NFC_RF_NONE
;
357 nfc_llcp_mac_is_down(dev
);
358 nfc_genl_dep_link_down_event(dev
);
362 device_unlock(&dev
->dev
);
367 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
368 u8 comm_mode
, u8 rf_mode
)
370 dev
->dep_link_up
= true;
372 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
374 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
376 EXPORT_SYMBOL(nfc_dep_link_is_up
);
379 * nfc_activate_target - prepare the target for data exchange
381 * @dev: The nfc device that found the target
382 * @target_idx: index of the target that must be activated
383 * @protocol: nfc protocol that will be used for data exchange
385 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
388 struct nfc_target
*target
;
390 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
391 dev_name(&dev
->dev
), target_idx
, protocol
);
393 device_lock(&dev
->dev
);
395 if (!device_is_registered(&dev
->dev
)) {
400 if (dev
->active_target
) {
405 target
= nfc_find_target(dev
, target_idx
);
406 if (target
== NULL
) {
411 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
413 dev
->active_target
= target
;
414 dev
->rf_mode
= NFC_RF_INITIATOR
;
416 if (dev
->ops
->check_presence
&& !dev
->shutting_down
)
417 mod_timer(&dev
->check_pres_timer
, jiffies
+
418 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
422 device_unlock(&dev
->dev
);
427 * nfc_deactivate_target - deactivate a nfc target
429 * @dev: The nfc device that found the target
430 * @target_idx: index of the target that must be deactivated
432 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
436 pr_debug("dev_name=%s target_idx=%u\n",
437 dev_name(&dev
->dev
), target_idx
);
439 device_lock(&dev
->dev
);
441 if (!device_is_registered(&dev
->dev
)) {
446 if (dev
->active_target
== NULL
) {
451 if (dev
->active_target
->idx
!= target_idx
) {
456 if (dev
->ops
->check_presence
)
457 del_timer_sync(&dev
->check_pres_timer
);
459 dev
->ops
->deactivate_target(dev
, dev
->active_target
);
460 dev
->active_target
= NULL
;
463 device_unlock(&dev
->dev
);
468 * nfc_data_exchange - transceive data
470 * @dev: The nfc device that found the target
471 * @target_idx: index of the target
472 * @skb: data to be sent
473 * @cb: callback called when the response is received
474 * @cb_context: parameter for the callback function
476 * The user must wait for the callback before calling this function again.
478 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
479 data_exchange_cb_t cb
, void *cb_context
)
483 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
484 dev_name(&dev
->dev
), target_idx
, skb
->len
);
486 device_lock(&dev
->dev
);
488 if (!device_is_registered(&dev
->dev
)) {
494 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
495 if (dev
->active_target
->idx
!= target_idx
) {
501 if (dev
->ops
->check_presence
)
502 del_timer_sync(&dev
->check_pres_timer
);
504 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
507 if (!rc
&& dev
->ops
->check_presence
&& !dev
->shutting_down
)
508 mod_timer(&dev
->check_pres_timer
, jiffies
+
509 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
510 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
511 rc
= dev
->ops
->tm_send(dev
, skb
);
520 device_unlock(&dev
->dev
);
524 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
526 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
528 if (gb_len
> NFC_MAX_GT_LEN
)
531 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
533 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
535 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
537 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
539 return nfc_llcp_general_bytes(dev
, gb_len
);
541 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
543 int nfc_tm_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
545 /* Only LLCP target mode for now */
546 if (dev
->dep_link_up
== false) {
551 return nfc_llcp_data_received(dev
, skb
);
553 EXPORT_SYMBOL(nfc_tm_data_received
);
555 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
556 u8
*gb
, size_t gb_len
)
560 device_lock(&dev
->dev
);
562 dev
->polling
= false;
565 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
570 dev
->rf_mode
= NFC_RF_TARGET
;
572 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
573 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
575 rc
= nfc_genl_tm_activated(dev
, protocol
);
578 device_unlock(&dev
->dev
);
582 EXPORT_SYMBOL(nfc_tm_activated
);
584 int nfc_tm_deactivated(struct nfc_dev
*dev
)
586 dev
->dep_link_up
= false;
587 dev
->rf_mode
= NFC_RF_NONE
;
589 return nfc_genl_tm_deactivated(dev
);
591 EXPORT_SYMBOL(nfc_tm_deactivated
);
594 * nfc_alloc_send_skb - allocate a skb for data exchange responses
596 * @size: size to allocate
599 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
600 unsigned int flags
, unsigned int size
,
604 unsigned int total_size
;
607 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
609 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
611 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
617 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
619 * @size: size to allocate
622 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
625 unsigned int total_size
;
627 total_size
= size
+ 1;
628 skb
= alloc_skb(total_size
, gfp
);
635 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
638 * nfc_targets_found - inform that targets were found
640 * @dev: The nfc device that found the targets
641 * @targets: array of nfc targets found
642 * @ntargets: targets array size
644 * The device driver must call this function when one or many nfc targets
645 * are found. After calling this function, the device driver must stop
646 * polling for targets.
647 * NOTE: This function can be called with targets=NULL and n_targets=0 to
648 * notify a driver error, meaning that the polling operation cannot complete.
649 * IMPORTANT: this function must not be called from an atomic context.
650 * In addition, it must also not be called from a context that would prevent
651 * the NFC Core to call other nfc ops entry point concurrently.
653 int nfc_targets_found(struct nfc_dev
*dev
,
654 struct nfc_target
*targets
, int n_targets
)
658 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
660 for (i
= 0; i
< n_targets
; i
++)
661 targets
[i
].idx
= dev
->target_next_idx
++;
663 device_lock(&dev
->dev
);
665 if (dev
->polling
== false) {
666 device_unlock(&dev
->dev
);
670 dev
->polling
= false;
672 dev
->targets_generation
++;
678 dev
->targets
= kmemdup(targets
,
679 n_targets
* sizeof(struct nfc_target
),
684 device_unlock(&dev
->dev
);
689 dev
->n_targets
= n_targets
;
690 device_unlock(&dev
->dev
);
692 nfc_genl_targets_found(dev
);
696 EXPORT_SYMBOL(nfc_targets_found
);
699 * nfc_target_lost - inform that an activated target went out of field
701 * @dev: The nfc device that had the activated target in field
702 * @target_idx: the nfc index of the target
704 * The device driver must call this function when the activated target
705 * goes out of the field.
706 * IMPORTANT: this function must not be called from an atomic context.
707 * In addition, it must also not be called from a context that would prevent
708 * the NFC Core to call other nfc ops entry point concurrently.
710 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
712 struct nfc_target
*tg
;
715 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
717 device_lock(&dev
->dev
);
719 for (i
= 0; i
< dev
->n_targets
; i
++) {
720 tg
= &dev
->targets
[i
];
721 if (tg
->idx
== target_idx
)
725 if (i
== dev
->n_targets
) {
726 device_unlock(&dev
->dev
);
730 dev
->targets_generation
++;
732 dev
->active_target
= NULL
;
734 if (dev
->n_targets
) {
735 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
736 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
742 device_unlock(&dev
->dev
);
744 nfc_genl_target_lost(dev
, target_idx
);
748 EXPORT_SYMBOL(nfc_target_lost
);
750 inline void nfc_driver_failure(struct nfc_dev
*dev
, int err
)
752 nfc_targets_found(dev
, NULL
, 0);
754 EXPORT_SYMBOL(nfc_driver_failure
);
756 static void nfc_release(struct device
*d
)
758 struct nfc_dev
*dev
= to_nfc_dev(d
);
760 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
762 nfc_genl_data_exit(&dev
->genl_data
);
767 static void nfc_check_pres_work(struct work_struct
*work
)
769 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
773 device_lock(&dev
->dev
);
775 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
776 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
777 if (rc
== -EOPNOTSUPP
)
780 u32 active_target_idx
= dev
->active_target
->idx
;
781 device_unlock(&dev
->dev
);
782 nfc_target_lost(dev
, active_target_idx
);
786 if (!dev
->shutting_down
)
787 mod_timer(&dev
->check_pres_timer
, jiffies
+
788 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
792 device_unlock(&dev
->dev
);
795 static void nfc_check_pres_timeout(unsigned long data
)
797 struct nfc_dev
*dev
= (struct nfc_dev
*)data
;
799 schedule_work(&dev
->check_pres_work
);
802 struct class nfc_class
= {
804 .dev_release
= nfc_release
,
806 EXPORT_SYMBOL(nfc_class
);
808 static int match_idx(struct device
*d
, const void *data
)
810 struct nfc_dev
*dev
= to_nfc_dev(d
);
811 const unsigned int *idx
= data
;
813 return dev
->idx
== *idx
;
816 struct nfc_dev
*nfc_get_device(unsigned int idx
)
820 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
824 return to_nfc_dev(d
);
828 * nfc_allocate_device - allocate a new nfc device
830 * @ops: device operations
831 * @supported_protocols: NFC protocols supported by the device
833 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
834 u32 supported_protocols
,
836 int tx_headroom
, int tx_tailroom
)
840 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
841 !ops
->deactivate_target
|| !ops
->im_transceive
)
844 if (!supported_protocols
)
847 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
852 dev
->supported_protocols
= supported_protocols
;
853 dev
->supported_se
= supported_se
;
854 dev
->active_se
= NFC_SE_NONE
;
855 dev
->tx_headroom
= tx_headroom
;
856 dev
->tx_tailroom
= tx_tailroom
;
858 nfc_genl_data_init(&dev
->genl_data
);
860 dev
->rf_mode
= NFC_RF_NONE
;
862 /* first generation must not be 0 */
863 dev
->targets_generation
= 1;
865 if (ops
->check_presence
) {
866 init_timer(&dev
->check_pres_timer
);
867 dev
->check_pres_timer
.data
= (unsigned long)dev
;
868 dev
->check_pres_timer
.function
= nfc_check_pres_timeout
;
870 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
875 EXPORT_SYMBOL(nfc_allocate_device
);
878 * nfc_register_device - register a nfc device in the nfc subsystem
880 * @dev: The nfc device to register
882 int nfc_register_device(struct nfc_dev
*dev
)
886 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
888 dev
->idx
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
892 dev
->dev
.class = &nfc_class
;
893 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
894 device_initialize(&dev
->dev
);
896 mutex_lock(&nfc_devlist_mutex
);
897 nfc_devlist_generation
++;
898 rc
= device_add(&dev
->dev
);
899 mutex_unlock(&nfc_devlist_mutex
);
904 rc
= nfc_llcp_register_device(dev
);
906 pr_err("Could not register llcp device\n");
908 rc
= nfc_genl_device_added(dev
);
910 pr_debug("The userspace won't be notified that the device %s was added\n",
911 dev_name(&dev
->dev
));
913 dev
->rfkill
= rfkill_alloc(dev_name(&dev
->dev
), &dev
->dev
,
914 RFKILL_TYPE_NFC
, &nfc_rfkill_ops
, dev
);
916 if (rfkill_register(dev
->rfkill
) < 0) {
917 rfkill_destroy(dev
->rfkill
);
924 EXPORT_SYMBOL(nfc_register_device
);
927 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
929 * @dev: The nfc device to unregister
931 void nfc_unregister_device(struct nfc_dev
*dev
)
935 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
940 rfkill_unregister(dev
->rfkill
);
941 rfkill_destroy(dev
->rfkill
);
944 if (dev
->ops
->check_presence
) {
945 device_lock(&dev
->dev
);
946 dev
->shutting_down
= true;
947 device_unlock(&dev
->dev
);
948 del_timer_sync(&dev
->check_pres_timer
);
949 cancel_work_sync(&dev
->check_pres_work
);
952 rc
= nfc_genl_device_removed(dev
);
954 pr_debug("The userspace won't be notified that the device %s "
955 "was removed\n", dev_name(&dev
->dev
));
957 nfc_llcp_unregister_device(dev
);
959 mutex_lock(&nfc_devlist_mutex
);
960 nfc_devlist_generation
++;
961 device_del(&dev
->dev
);
962 mutex_unlock(&nfc_devlist_mutex
);
964 ida_simple_remove(&nfc_index_ida
, id
);
966 EXPORT_SYMBOL(nfc_unregister_device
);
968 static int __init
nfc_init(void)
972 pr_info("NFC Core ver %s\n", VERSION
);
974 rc
= class_register(&nfc_class
);
978 rc
= nfc_genl_init();
982 /* the first generation must not be 0 */
983 nfc_devlist_generation
= 1;
989 rc
= nfc_llcp_init();
1006 class_unregister(&nfc_class
);
1010 static void __exit
nfc_exit(void)
1016 class_unregister(&nfc_class
);
1019 subsys_initcall(nfc_init
);
1020 module_exit(nfc_exit
);
1022 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1023 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
1024 MODULE_VERSION(VERSION
);
1025 MODULE_LICENSE("GPL");
1026 MODULE_ALIAS_NETPROTO(PF_NFC
);
1027 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME
);