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
);
48 * nfc_dev_up - turn on the NFC device
50 * @dev: The nfc device to be turned on
52 * The device remains up until the nfc_dev_down function is called.
54 int nfc_dev_up(struct nfc_dev
*dev
)
58 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
60 device_lock(&dev
->dev
);
62 if (dev
->rfkill
&& rfkill_blocked(dev
->rfkill
)) {
67 if (!device_is_registered(&dev
->dev
)) {
78 rc
= dev
->ops
->dev_up(dev
);
84 device_unlock(&dev
->dev
);
89 * nfc_dev_down - turn off the NFC device
91 * @dev: The nfc device to be turned off
93 int nfc_dev_down(struct nfc_dev
*dev
)
97 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
99 device_lock(&dev
->dev
);
101 if (!device_is_registered(&dev
->dev
)) {
111 if (dev
->polling
|| dev
->active_target
) {
116 if (dev
->ops
->dev_down
)
117 dev
->ops
->dev_down(dev
);
122 device_unlock(&dev
->dev
);
126 static int nfc_rfkill_set_block(void *data
, bool blocked
)
128 struct nfc_dev
*dev
= data
;
130 pr_debug("%s blocked %d", dev_name(&dev
->dev
), blocked
);
140 static const struct rfkill_ops nfc_rfkill_ops
= {
141 .set_block
= nfc_rfkill_set_block
,
145 * nfc_start_poll - start polling for nfc targets
147 * @dev: The nfc device that must start polling
148 * @protocols: bitset of nfc protocols that must be used for polling
150 * The device remains polling for targets until a target is found or
151 * the nfc_stop_poll function is called.
153 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
157 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
158 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
160 if (!im_protocols
&& !tm_protocols
)
163 device_lock(&dev
->dev
);
165 if (!device_is_registered(&dev
->dev
)) {
180 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
183 dev
->rf_mode
= NFC_RF_NONE
;
187 device_unlock(&dev
->dev
);
192 * nfc_stop_poll - stop polling for nfc targets
194 * @dev: The nfc device that must stop polling
196 int nfc_stop_poll(struct nfc_dev
*dev
)
200 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
202 device_lock(&dev
->dev
);
204 if (!device_is_registered(&dev
->dev
)) {
214 dev
->ops
->stop_poll(dev
);
215 dev
->polling
= false;
216 dev
->rf_mode
= NFC_RF_NONE
;
219 device_unlock(&dev
->dev
);
223 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
227 if (dev
->n_targets
== 0)
230 for (i
= 0; i
< dev
->n_targets
; i
++) {
231 if (dev
->targets
[i
].idx
== target_idx
)
232 return &dev
->targets
[i
];
238 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
243 struct nfc_target
*target
;
245 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
247 if (!dev
->ops
->dep_link_up
)
250 device_lock(&dev
->dev
);
252 if (!device_is_registered(&dev
->dev
)) {
257 if (dev
->dep_link_up
== true) {
262 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
263 if (gb_len
> NFC_MAX_GT_LEN
) {
268 target
= nfc_find_target(dev
, target_index
);
269 if (target
== NULL
) {
274 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
276 dev
->active_target
= target
;
277 dev
->rf_mode
= NFC_RF_INITIATOR
;
281 device_unlock(&dev
->dev
);
285 int nfc_dep_link_down(struct nfc_dev
*dev
)
289 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
291 if (!dev
->ops
->dep_link_down
)
294 device_lock(&dev
->dev
);
296 if (!device_is_registered(&dev
->dev
)) {
301 if (dev
->dep_link_up
== false) {
306 rc
= dev
->ops
->dep_link_down(dev
);
308 dev
->dep_link_up
= false;
309 dev
->active_target
= NULL
;
310 dev
->rf_mode
= NFC_RF_NONE
;
311 nfc_llcp_mac_is_down(dev
);
312 nfc_genl_dep_link_down_event(dev
);
316 device_unlock(&dev
->dev
);
321 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
322 u8 comm_mode
, u8 rf_mode
)
324 dev
->dep_link_up
= true;
326 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
328 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
330 EXPORT_SYMBOL(nfc_dep_link_is_up
);
333 * nfc_activate_target - prepare the target for data exchange
335 * @dev: The nfc device that found the target
336 * @target_idx: index of the target that must be activated
337 * @protocol: nfc protocol that will be used for data exchange
339 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
342 struct nfc_target
*target
;
344 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
345 dev_name(&dev
->dev
), target_idx
, protocol
);
347 device_lock(&dev
->dev
);
349 if (!device_is_registered(&dev
->dev
)) {
354 if (dev
->active_target
) {
359 target
= nfc_find_target(dev
, target_idx
);
360 if (target
== NULL
) {
365 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
367 dev
->active_target
= target
;
368 dev
->rf_mode
= NFC_RF_INITIATOR
;
370 if (dev
->ops
->check_presence
&& !dev
->shutting_down
)
371 mod_timer(&dev
->check_pres_timer
, jiffies
+
372 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
376 device_unlock(&dev
->dev
);
381 * nfc_deactivate_target - deactivate a nfc target
383 * @dev: The nfc device that found the target
384 * @target_idx: index of the target that must be deactivated
386 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
390 pr_debug("dev_name=%s target_idx=%u\n",
391 dev_name(&dev
->dev
), target_idx
);
393 device_lock(&dev
->dev
);
395 if (!device_is_registered(&dev
->dev
)) {
400 if (dev
->active_target
== NULL
) {
405 if (dev
->active_target
->idx
!= target_idx
) {
410 if (dev
->ops
->check_presence
)
411 del_timer_sync(&dev
->check_pres_timer
);
413 dev
->ops
->deactivate_target(dev
, dev
->active_target
);
414 dev
->active_target
= NULL
;
417 device_unlock(&dev
->dev
);
422 * nfc_data_exchange - transceive data
424 * @dev: The nfc device that found the target
425 * @target_idx: index of the target
426 * @skb: data to be sent
427 * @cb: callback called when the response is received
428 * @cb_context: parameter for the callback function
430 * The user must wait for the callback before calling this function again.
432 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
433 data_exchange_cb_t cb
, void *cb_context
)
437 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
438 dev_name(&dev
->dev
), target_idx
, skb
->len
);
440 device_lock(&dev
->dev
);
442 if (!device_is_registered(&dev
->dev
)) {
448 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
449 if (dev
->active_target
->idx
!= target_idx
) {
455 if (dev
->ops
->check_presence
)
456 del_timer_sync(&dev
->check_pres_timer
);
458 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
461 if (!rc
&& dev
->ops
->check_presence
&& !dev
->shutting_down
)
462 mod_timer(&dev
->check_pres_timer
, jiffies
+
463 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
464 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
465 rc
= dev
->ops
->tm_send(dev
, skb
);
474 device_unlock(&dev
->dev
);
478 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
480 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
482 if (gb_len
> NFC_MAX_GT_LEN
)
485 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
487 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
489 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
491 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
493 return nfc_llcp_general_bytes(dev
, gb_len
);
495 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
497 int nfc_tm_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
499 /* Only LLCP target mode for now */
500 if (dev
->dep_link_up
== false) {
505 return nfc_llcp_data_received(dev
, skb
);
507 EXPORT_SYMBOL(nfc_tm_data_received
);
509 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
510 u8
*gb
, size_t gb_len
)
514 device_lock(&dev
->dev
);
516 dev
->polling
= false;
519 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
524 dev
->rf_mode
= NFC_RF_TARGET
;
526 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
527 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
529 rc
= nfc_genl_tm_activated(dev
, protocol
);
532 device_unlock(&dev
->dev
);
536 EXPORT_SYMBOL(nfc_tm_activated
);
538 int nfc_tm_deactivated(struct nfc_dev
*dev
)
540 dev
->dep_link_up
= false;
541 dev
->rf_mode
= NFC_RF_NONE
;
543 return nfc_genl_tm_deactivated(dev
);
545 EXPORT_SYMBOL(nfc_tm_deactivated
);
548 * nfc_alloc_send_skb - allocate a skb for data exchange responses
550 * @size: size to allocate
553 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
554 unsigned int flags
, unsigned int size
,
558 unsigned int total_size
;
561 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
563 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
565 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
571 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
573 * @size: size to allocate
576 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
579 unsigned int total_size
;
581 total_size
= size
+ 1;
582 skb
= alloc_skb(total_size
, gfp
);
589 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
592 * nfc_targets_found - inform that targets were found
594 * @dev: The nfc device that found the targets
595 * @targets: array of nfc targets found
596 * @ntargets: targets array size
598 * The device driver must call this function when one or many nfc targets
599 * are found. After calling this function, the device driver must stop
600 * polling for targets.
601 * NOTE: This function can be called with targets=NULL and n_targets=0 to
602 * notify a driver error, meaning that the polling operation cannot complete.
603 * IMPORTANT: this function must not be called from an atomic context.
604 * In addition, it must also not be called from a context that would prevent
605 * the NFC Core to call other nfc ops entry point concurrently.
607 int nfc_targets_found(struct nfc_dev
*dev
,
608 struct nfc_target
*targets
, int n_targets
)
612 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
614 for (i
= 0; i
< n_targets
; i
++)
615 targets
[i
].idx
= dev
->target_next_idx
++;
617 device_lock(&dev
->dev
);
619 if (dev
->polling
== false) {
620 device_unlock(&dev
->dev
);
624 dev
->polling
= false;
626 dev
->targets_generation
++;
632 dev
->targets
= kmemdup(targets
,
633 n_targets
* sizeof(struct nfc_target
),
638 device_unlock(&dev
->dev
);
643 dev
->n_targets
= n_targets
;
644 device_unlock(&dev
->dev
);
646 nfc_genl_targets_found(dev
);
650 EXPORT_SYMBOL(nfc_targets_found
);
653 * nfc_target_lost - inform that an activated target went out of field
655 * @dev: The nfc device that had the activated target in field
656 * @target_idx: the nfc index of the target
658 * The device driver must call this function when the activated target
659 * goes out of the field.
660 * IMPORTANT: this function must not be called from an atomic context.
661 * In addition, it must also not be called from a context that would prevent
662 * the NFC Core to call other nfc ops entry point concurrently.
664 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
666 struct nfc_target
*tg
;
669 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
671 device_lock(&dev
->dev
);
673 for (i
= 0; i
< dev
->n_targets
; i
++) {
674 tg
= &dev
->targets
[i
];
675 if (tg
->idx
== target_idx
)
679 if (i
== dev
->n_targets
) {
680 device_unlock(&dev
->dev
);
684 dev
->targets_generation
++;
686 dev
->active_target
= NULL
;
688 if (dev
->n_targets
) {
689 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
690 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
696 device_unlock(&dev
->dev
);
698 nfc_genl_target_lost(dev
, target_idx
);
702 EXPORT_SYMBOL(nfc_target_lost
);
704 inline void nfc_driver_failure(struct nfc_dev
*dev
, int err
)
706 nfc_targets_found(dev
, NULL
, 0);
708 EXPORT_SYMBOL(nfc_driver_failure
);
710 static void nfc_release(struct device
*d
)
712 struct nfc_dev
*dev
= to_nfc_dev(d
);
714 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
716 nfc_genl_data_exit(&dev
->genl_data
);
721 static void nfc_check_pres_work(struct work_struct
*work
)
723 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
727 device_lock(&dev
->dev
);
729 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
730 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
731 if (rc
== -EOPNOTSUPP
)
734 u32 active_target_idx
= dev
->active_target
->idx
;
735 device_unlock(&dev
->dev
);
736 nfc_target_lost(dev
, active_target_idx
);
740 if (!dev
->shutting_down
)
741 mod_timer(&dev
->check_pres_timer
, jiffies
+
742 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
746 device_unlock(&dev
->dev
);
749 static void nfc_check_pres_timeout(unsigned long data
)
751 struct nfc_dev
*dev
= (struct nfc_dev
*)data
;
753 schedule_work(&dev
->check_pres_work
);
756 struct class nfc_class
= {
758 .dev_release
= nfc_release
,
760 EXPORT_SYMBOL(nfc_class
);
762 static int match_idx(struct device
*d
, const void *data
)
764 struct nfc_dev
*dev
= to_nfc_dev(d
);
765 const unsigned int *idx
= data
;
767 return dev
->idx
== *idx
;
770 struct nfc_dev
*nfc_get_device(unsigned int idx
)
774 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
778 return to_nfc_dev(d
);
782 * nfc_allocate_device - allocate a new nfc device
784 * @ops: device operations
785 * @supported_protocols: NFC protocols supported by the device
787 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
788 u32 supported_protocols
,
790 int tx_headroom
, int tx_tailroom
)
794 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
795 !ops
->deactivate_target
|| !ops
->im_transceive
)
798 if (!supported_protocols
)
801 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
806 dev
->supported_protocols
= supported_protocols
;
807 dev
->supported_se
= supported_se
;
808 dev
->active_se
= NFC_SE_NONE
;
809 dev
->tx_headroom
= tx_headroom
;
810 dev
->tx_tailroom
= tx_tailroom
;
812 nfc_genl_data_init(&dev
->genl_data
);
814 dev
->rf_mode
= NFC_RF_NONE
;
816 /* first generation must not be 0 */
817 dev
->targets_generation
= 1;
819 if (ops
->check_presence
) {
820 init_timer(&dev
->check_pres_timer
);
821 dev
->check_pres_timer
.data
= (unsigned long)dev
;
822 dev
->check_pres_timer
.function
= nfc_check_pres_timeout
;
824 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
829 EXPORT_SYMBOL(nfc_allocate_device
);
832 * nfc_register_device - register a nfc device in the nfc subsystem
834 * @dev: The nfc device to register
836 int nfc_register_device(struct nfc_dev
*dev
)
840 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
842 dev
->idx
= ida_simple_get(&nfc_index_ida
, 0, 0, GFP_KERNEL
);
846 dev
->dev
.class = &nfc_class
;
847 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
848 device_initialize(&dev
->dev
);
850 mutex_lock(&nfc_devlist_mutex
);
851 nfc_devlist_generation
++;
852 rc
= device_add(&dev
->dev
);
853 mutex_unlock(&nfc_devlist_mutex
);
858 rc
= nfc_llcp_register_device(dev
);
860 pr_err("Could not register llcp device\n");
862 rc
= nfc_genl_device_added(dev
);
864 pr_debug("The userspace won't be notified that the device %s was added\n",
865 dev_name(&dev
->dev
));
867 dev
->rfkill
= rfkill_alloc(dev_name(&dev
->dev
), &dev
->dev
,
868 RFKILL_TYPE_NFC
, &nfc_rfkill_ops
, dev
);
870 if (rfkill_register(dev
->rfkill
) < 0) {
871 rfkill_destroy(dev
->rfkill
);
878 EXPORT_SYMBOL(nfc_register_device
);
881 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
883 * @dev: The nfc device to unregister
885 void nfc_unregister_device(struct nfc_dev
*dev
)
889 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
894 rfkill_unregister(dev
->rfkill
);
895 rfkill_destroy(dev
->rfkill
);
898 if (dev
->ops
->check_presence
) {
899 device_lock(&dev
->dev
);
900 dev
->shutting_down
= true;
901 device_unlock(&dev
->dev
);
902 del_timer_sync(&dev
->check_pres_timer
);
903 cancel_work_sync(&dev
->check_pres_work
);
906 rc
= nfc_genl_device_removed(dev
);
908 pr_debug("The userspace won't be notified that the device %s "
909 "was removed\n", dev_name(&dev
->dev
));
911 nfc_llcp_unregister_device(dev
);
913 mutex_lock(&nfc_devlist_mutex
);
914 nfc_devlist_generation
++;
915 device_del(&dev
->dev
);
916 mutex_unlock(&nfc_devlist_mutex
);
918 ida_simple_remove(&nfc_index_ida
, id
);
920 EXPORT_SYMBOL(nfc_unregister_device
);
922 static int __init
nfc_init(void)
926 pr_info("NFC Core ver %s\n", VERSION
);
928 rc
= class_register(&nfc_class
);
932 rc
= nfc_genl_init();
936 /* the first generation must not be 0 */
937 nfc_devlist_generation
= 1;
943 rc
= nfc_llcp_init();
960 class_unregister(&nfc_class
);
964 static void __exit
nfc_exit(void)
970 class_unregister(&nfc_class
);
973 subsys_initcall(nfc_init
);
974 module_exit(nfc_exit
);
976 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
977 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
978 MODULE_VERSION(VERSION
);
979 MODULE_LICENSE("GPL");
980 MODULE_ALIAS_NETPROTO(PF_NFC
);
981 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME
);