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/nfc.h>
36 #define NFC_CHECK_PRES_FREQ_MS 2000
38 int nfc_devlist_generation
;
39 DEFINE_MUTEX(nfc_devlist_mutex
);
42 * nfc_dev_up - turn on the NFC device
44 * @dev: The nfc device to be turned on
46 * The device remains up until the nfc_dev_down function is called.
48 int nfc_dev_up(struct nfc_dev
*dev
)
52 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
54 device_lock(&dev
->dev
);
56 if (!device_is_registered(&dev
->dev
)) {
67 rc
= dev
->ops
->dev_up(dev
);
73 device_unlock(&dev
->dev
);
78 * nfc_dev_down - turn off the NFC device
80 * @dev: The nfc device to be turned off
82 int nfc_dev_down(struct nfc_dev
*dev
)
86 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
88 device_lock(&dev
->dev
);
90 if (!device_is_registered(&dev
->dev
)) {
100 if (dev
->polling
|| dev
->active_target
) {
105 if (dev
->ops
->dev_down
)
106 dev
->ops
->dev_down(dev
);
111 device_unlock(&dev
->dev
);
116 * nfc_start_poll - start polling for nfc targets
118 * @dev: The nfc device that must start polling
119 * @protocols: bitset of nfc protocols that must be used for polling
121 * The device remains polling for targets until a target is found or
122 * the nfc_stop_poll function is called.
124 int nfc_start_poll(struct nfc_dev
*dev
, u32 im_protocols
, u32 tm_protocols
)
128 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
129 dev_name(&dev
->dev
), im_protocols
, tm_protocols
);
131 if (!im_protocols
&& !tm_protocols
)
134 device_lock(&dev
->dev
);
136 if (!device_is_registered(&dev
->dev
)) {
146 rc
= dev
->ops
->start_poll(dev
, im_protocols
, tm_protocols
);
149 dev
->rf_mode
= NFC_RF_NONE
;
153 device_unlock(&dev
->dev
);
158 * nfc_stop_poll - stop polling for nfc targets
160 * @dev: The nfc device that must stop polling
162 int nfc_stop_poll(struct nfc_dev
*dev
)
166 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
168 device_lock(&dev
->dev
);
170 if (!device_is_registered(&dev
->dev
)) {
180 dev
->ops
->stop_poll(dev
);
181 dev
->polling
= false;
184 device_unlock(&dev
->dev
);
188 static struct nfc_target
*nfc_find_target(struct nfc_dev
*dev
, u32 target_idx
)
192 if (dev
->n_targets
== 0)
195 for (i
= 0; i
< dev
->n_targets
; i
++) {
196 if (dev
->targets
[i
].idx
== target_idx
)
197 return &dev
->targets
[i
];
203 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
208 struct nfc_target
*target
;
210 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
212 if (!dev
->ops
->dep_link_up
)
215 device_lock(&dev
->dev
);
217 if (!device_is_registered(&dev
->dev
)) {
222 if (dev
->dep_link_up
== true) {
227 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
228 if (gb_len
> NFC_MAX_GT_LEN
) {
233 target
= nfc_find_target(dev
, target_index
);
234 if (target
== NULL
) {
239 rc
= dev
->ops
->dep_link_up(dev
, target
, comm_mode
, gb
, gb_len
);
241 dev
->active_target
= target
;
242 dev
->rf_mode
= NFC_RF_INITIATOR
;
246 device_unlock(&dev
->dev
);
250 int nfc_dep_link_down(struct nfc_dev
*dev
)
254 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
256 if (!dev
->ops
->dep_link_down
)
259 device_lock(&dev
->dev
);
261 if (!device_is_registered(&dev
->dev
)) {
266 if (dev
->dep_link_up
== false) {
271 if (dev
->rf_mode
== NFC_RF_TARGET
) {
276 rc
= dev
->ops
->dep_link_down(dev
);
278 dev
->dep_link_up
= false;
279 dev
->active_target
= NULL
;
280 nfc_llcp_mac_is_down(dev
);
281 nfc_genl_dep_link_down_event(dev
);
285 device_unlock(&dev
->dev
);
289 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
290 u8 comm_mode
, u8 rf_mode
)
292 dev
->dep_link_up
= true;
294 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
296 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
298 EXPORT_SYMBOL(nfc_dep_link_is_up
);
301 * nfc_activate_target - prepare the target for data exchange
303 * @dev: The nfc device that found the target
304 * @target_idx: index of the target that must be activated
305 * @protocol: nfc protocol that will be used for data exchange
307 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
310 struct nfc_target
*target
;
312 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
313 dev_name(&dev
->dev
), target_idx
, protocol
);
315 device_lock(&dev
->dev
);
317 if (!device_is_registered(&dev
->dev
)) {
322 if (dev
->active_target
) {
327 target
= nfc_find_target(dev
, target_idx
);
328 if (target
== NULL
) {
333 rc
= dev
->ops
->activate_target(dev
, target
, protocol
);
335 dev
->active_target
= target
;
336 dev
->rf_mode
= NFC_RF_INITIATOR
;
338 if (dev
->ops
->check_presence
)
339 mod_timer(&dev
->check_pres_timer
, jiffies
+
340 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
344 device_unlock(&dev
->dev
);
349 * nfc_deactivate_target - deactivate a nfc target
351 * @dev: The nfc device that found the target
352 * @target_idx: index of the target that must be deactivated
354 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
358 pr_debug("dev_name=%s target_idx=%u\n",
359 dev_name(&dev
->dev
), target_idx
);
361 device_lock(&dev
->dev
);
363 if (!device_is_registered(&dev
->dev
)) {
368 if (dev
->active_target
== NULL
) {
373 if (dev
->active_target
->idx
!= target_idx
) {
378 if (dev
->ops
->check_presence
)
379 del_timer_sync(&dev
->check_pres_timer
);
381 dev
->ops
->deactivate_target(dev
, dev
->active_target
);
382 dev
->active_target
= NULL
;
385 device_unlock(&dev
->dev
);
390 * nfc_data_exchange - transceive data
392 * @dev: The nfc device that found the target
393 * @target_idx: index of the target
394 * @skb: data to be sent
395 * @cb: callback called when the response is received
396 * @cb_context: parameter for the callback function
398 * The user must wait for the callback before calling this function again.
400 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
401 data_exchange_cb_t cb
, void *cb_context
)
405 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
406 dev_name(&dev
->dev
), target_idx
, skb
->len
);
408 device_lock(&dev
->dev
);
410 if (!device_is_registered(&dev
->dev
)) {
416 if (dev
->rf_mode
== NFC_RF_INITIATOR
&& dev
->active_target
!= NULL
) {
417 if (dev
->active_target
->idx
!= target_idx
) {
423 if (dev
->ops
->check_presence
)
424 del_timer_sync(&dev
->check_pres_timer
);
426 rc
= dev
->ops
->im_transceive(dev
, dev
->active_target
, skb
, cb
,
429 if (!rc
&& dev
->ops
->check_presence
)
430 mod_timer(&dev
->check_pres_timer
, jiffies
+
431 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
432 } else if (dev
->rf_mode
== NFC_RF_TARGET
&& dev
->ops
->tm_send
!= NULL
) {
433 rc
= dev
->ops
->tm_send(dev
, skb
);
442 device_unlock(&dev
->dev
);
446 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
448 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
450 if (gb_len
> NFC_MAX_GT_LEN
)
453 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
455 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
457 u8
*nfc_get_local_general_bytes(struct nfc_dev
*dev
, size_t *gb_len
)
459 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
461 return nfc_llcp_general_bytes(dev
, gb_len
);
463 EXPORT_SYMBOL(nfc_get_local_general_bytes
);
465 int nfc_tm_activated(struct nfc_dev
*dev
, u32 protocol
, u8 comm_mode
,
466 u8
*gb
, size_t gb_len
)
470 device_lock(&dev
->dev
);
472 dev
->polling
= false;
475 rc
= nfc_set_remote_general_bytes(dev
, gb
, gb_len
);
480 dev
->rf_mode
= NFC_RF_TARGET
;
482 if (protocol
== NFC_PROTO_NFC_DEP_MASK
)
483 nfc_dep_link_is_up(dev
, 0, comm_mode
, NFC_RF_TARGET
);
485 rc
= nfc_genl_tm_activated(dev
, protocol
);
488 device_unlock(&dev
->dev
);
492 EXPORT_SYMBOL(nfc_tm_activated
);
494 int nfc_tm_deactivated(struct nfc_dev
*dev
)
496 dev
->dep_link_up
= false;
498 return nfc_genl_tm_deactivated(dev
);
500 EXPORT_SYMBOL(nfc_tm_deactivated
);
503 * nfc_alloc_send_skb - allocate a skb for data exchange responses
505 * @size: size to allocate
508 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
509 unsigned int flags
, unsigned int size
,
513 unsigned int total_size
;
516 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
518 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
520 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
526 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
528 * @size: size to allocate
531 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
534 unsigned int total_size
;
536 total_size
= size
+ 1;
537 skb
= alloc_skb(total_size
, gfp
);
544 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
547 * nfc_targets_found - inform that targets were found
549 * @dev: The nfc device that found the targets
550 * @targets: array of nfc targets found
551 * @ntargets: targets array size
553 * The device driver must call this function when one or many nfc targets
554 * are found. After calling this function, the device driver must stop
555 * polling for targets.
556 * IMPORTANT: this function must not be called from an atomic context.
557 * In addition, it must also not be called from a context that would prevent
558 * the NFC Core to call other nfc ops entry point concurrently.
560 int nfc_targets_found(struct nfc_dev
*dev
,
561 struct nfc_target
*targets
, int n_targets
)
565 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
567 dev
->polling
= false;
569 for (i
= 0; i
< n_targets
; i
++)
570 targets
[i
].idx
= dev
->target_next_idx
++;
572 device_lock(&dev
->dev
);
574 dev
->targets_generation
++;
577 dev
->targets
= kmemdup(targets
, n_targets
* sizeof(struct nfc_target
),
582 device_unlock(&dev
->dev
);
586 dev
->n_targets
= n_targets
;
587 device_unlock(&dev
->dev
);
589 nfc_genl_targets_found(dev
);
593 EXPORT_SYMBOL(nfc_targets_found
);
596 * nfc_target_lost - inform that an activated target went out of field
598 * @dev: The nfc device that had the activated target in field
599 * @target_idx: the nfc index of the target
601 * The device driver must call this function when the activated target
602 * goes out of the field.
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_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
609 struct nfc_target
*tg
;
612 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
614 device_lock(&dev
->dev
);
616 for (i
= 0; i
< dev
->n_targets
; i
++) {
617 tg
= &dev
->targets
[i
];
618 if (tg
->idx
== target_idx
)
622 if (i
== dev
->n_targets
) {
623 device_unlock(&dev
->dev
);
627 dev
->targets_generation
++;
629 dev
->active_target
= NULL
;
631 if (dev
->n_targets
) {
632 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
633 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
639 device_unlock(&dev
->dev
);
641 nfc_genl_target_lost(dev
, target_idx
);
645 EXPORT_SYMBOL(nfc_target_lost
);
647 static void nfc_release(struct device
*d
)
649 struct nfc_dev
*dev
= to_nfc_dev(d
);
651 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
653 if (dev
->ops
->check_presence
) {
654 del_timer_sync(&dev
->check_pres_timer
);
655 destroy_workqueue(dev
->check_pres_wq
);
658 nfc_genl_data_exit(&dev
->genl_data
);
663 static void nfc_check_pres_work(struct work_struct
*work
)
665 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
669 device_lock(&dev
->dev
);
671 if (dev
->active_target
&& timer_pending(&dev
->check_pres_timer
) == 0) {
672 rc
= dev
->ops
->check_presence(dev
, dev
->active_target
);
674 mod_timer(&dev
->check_pres_timer
, jiffies
+
675 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
677 u32 active_target_idx
= dev
->active_target
->idx
;
678 device_unlock(&dev
->dev
);
679 nfc_target_lost(dev
, active_target_idx
);
684 device_unlock(&dev
->dev
);
687 static void nfc_check_pres_timeout(unsigned long data
)
689 struct nfc_dev
*dev
= (struct nfc_dev
*)data
;
691 queue_work(dev
->check_pres_wq
, &dev
->check_pres_work
);
694 struct class nfc_class
= {
696 .dev_release
= nfc_release
,
698 EXPORT_SYMBOL(nfc_class
);
700 static int match_idx(struct device
*d
, void *data
)
702 struct nfc_dev
*dev
= to_nfc_dev(d
);
703 unsigned int *idx
= data
;
705 return dev
->idx
== *idx
;
708 struct nfc_dev
*nfc_get_device(unsigned int idx
)
712 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
716 return to_nfc_dev(d
);
720 * nfc_allocate_device - allocate a new nfc device
722 * @ops: device operations
723 * @supported_protocols: NFC protocols supported by the device
725 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
726 u32 supported_protocols
,
727 int tx_headroom
, int tx_tailroom
)
729 static atomic_t dev_no
= ATOMIC_INIT(0);
732 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
733 !ops
->deactivate_target
|| !ops
->im_transceive
)
736 if (!supported_protocols
)
739 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
743 dev
->dev
.class = &nfc_class
;
744 dev
->idx
= atomic_inc_return(&dev_no
) - 1;
745 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
746 device_initialize(&dev
->dev
);
749 dev
->supported_protocols
= supported_protocols
;
750 dev
->tx_headroom
= tx_headroom
;
751 dev
->tx_tailroom
= tx_tailroom
;
753 nfc_genl_data_init(&dev
->genl_data
);
756 /* first generation must not be 0 */
757 dev
->targets_generation
= 1;
759 if (ops
->check_presence
) {
761 init_timer(&dev
->check_pres_timer
);
762 dev
->check_pres_timer
.data
= (unsigned long)dev
;
763 dev
->check_pres_timer
.function
= nfc_check_pres_timeout
;
765 INIT_WORK(&dev
->check_pres_work
, nfc_check_pres_work
);
766 snprintf(name
, sizeof(name
), "nfc%d_check_pres_wq", dev
->idx
);
767 dev
->check_pres_wq
= alloc_workqueue(name
, WQ_NON_REENTRANT
|
770 if (dev
->check_pres_wq
== NULL
) {
778 EXPORT_SYMBOL(nfc_allocate_device
);
781 * nfc_register_device - register a nfc device in the nfc subsystem
783 * @dev: The nfc device to register
785 int nfc_register_device(struct nfc_dev
*dev
)
789 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
791 mutex_lock(&nfc_devlist_mutex
);
792 nfc_devlist_generation
++;
793 rc
= device_add(&dev
->dev
);
794 mutex_unlock(&nfc_devlist_mutex
);
799 rc
= nfc_llcp_register_device(dev
);
801 pr_err("Could not register llcp device\n");
803 rc
= nfc_genl_device_added(dev
);
805 pr_debug("The userspace won't be notified that the device %s was added\n",
806 dev_name(&dev
->dev
));
810 EXPORT_SYMBOL(nfc_register_device
);
813 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
815 * @dev: The nfc device to unregister
817 void nfc_unregister_device(struct nfc_dev
*dev
)
821 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
823 mutex_lock(&nfc_devlist_mutex
);
824 nfc_devlist_generation
++;
826 /* lock to avoid unregistering a device while an operation
828 device_lock(&dev
->dev
);
829 device_del(&dev
->dev
);
830 device_unlock(&dev
->dev
);
832 mutex_unlock(&nfc_devlist_mutex
);
834 nfc_llcp_unregister_device(dev
);
836 rc
= nfc_genl_device_removed(dev
);
838 pr_debug("The userspace won't be notified that the device %s was removed\n",
839 dev_name(&dev
->dev
));
842 EXPORT_SYMBOL(nfc_unregister_device
);
844 static int __init
nfc_init(void)
848 pr_info("NFC Core ver %s\n", VERSION
);
850 rc
= class_register(&nfc_class
);
854 rc
= nfc_genl_init();
858 /* the first generation must not be 0 */
859 nfc_devlist_generation
= 1;
865 rc
= nfc_llcp_init();
882 class_unregister(&nfc_class
);
886 static void __exit
nfc_exit(void)
892 class_unregister(&nfc_class
);
895 subsys_initcall(nfc_init
);
896 module_exit(nfc_exit
);
898 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
899 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
900 MODULE_VERSION(VERSION
);
901 MODULE_LICENSE("GPL");