2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
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>
32 #include <net/genetlink.h>
38 #define NFC_CHECK_PRES_FREQ_MS 2000
40 int nfc_devlist_generation;
41 DEFINE_MUTEX(nfc_devlist_mutex);
43 /* NFC device ID bitmap */
44 static DEFINE_IDA(nfc_index_ida);
47 * nfc_dev_up - turn on the NFC device
49 * @dev: The nfc device to be turned on
51 * The device remains up until the nfc_dev_down function is called.
53 int nfc_dev_up(struct nfc_dev *dev)
57 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
59 device_lock(&dev->dev);
61 if (!device_is_registered(&dev->dev)) {
72 rc = dev->ops->dev_up(dev);
78 device_unlock(&dev->dev);
83 * nfc_dev_down - turn off the NFC device
85 * @dev: The nfc device to be turned off
87 int nfc_dev_down(struct nfc_dev *dev)
91 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
93 device_lock(&dev->dev);
95 if (!device_is_registered(&dev->dev)) {
105 if (dev->polling || dev->active_target) {
110 if (dev->ops->dev_down)
111 dev->ops->dev_down(dev);
116 device_unlock(&dev->dev);
121 * nfc_start_poll - start polling for nfc targets
123 * @dev: The nfc device that must start polling
124 * @protocols: bitset of nfc protocols that must be used for polling
126 * The device remains polling for targets until a target is found or
127 * the nfc_stop_poll function is called.
129 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
133 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
134 dev_name(&dev->dev), im_protocols, tm_protocols);
136 if (!im_protocols && !tm_protocols)
139 device_lock(&dev->dev);
141 if (!device_is_registered(&dev->dev)) {
151 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
154 dev->rf_mode = NFC_RF_NONE;
158 device_unlock(&dev->dev);
163 * nfc_stop_poll - stop polling for nfc targets
165 * @dev: The nfc device that must stop polling
167 int nfc_stop_poll(struct nfc_dev *dev)
171 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
173 device_lock(&dev->dev);
175 if (!device_is_registered(&dev->dev)) {
185 dev->ops->stop_poll(dev);
186 dev->polling = false;
187 dev->rf_mode = NFC_RF_NONE;
190 device_unlock(&dev->dev);
194 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
198 if (dev->n_targets == 0)
201 for (i = 0; i < dev->n_targets; i++) {
202 if (dev->targets[i].idx == target_idx)
203 return &dev->targets[i];
209 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
214 struct nfc_target *target;
216 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
218 if (!dev->ops->dep_link_up)
221 device_lock(&dev->dev);
223 if (!device_is_registered(&dev->dev)) {
228 if (dev->dep_link_up == true) {
233 gb = nfc_llcp_general_bytes(dev, &gb_len);
234 if (gb_len > NFC_MAX_GT_LEN) {
239 target = nfc_find_target(dev, target_index);
240 if (target == NULL) {
245 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
247 dev->active_target = target;
248 dev->rf_mode = NFC_RF_INITIATOR;
252 device_unlock(&dev->dev);
256 int nfc_dep_link_down(struct nfc_dev *dev)
260 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
262 if (!dev->ops->dep_link_down)
265 device_lock(&dev->dev);
267 if (!device_is_registered(&dev->dev)) {
272 if (dev->dep_link_up == false) {
277 rc = dev->ops->dep_link_down(dev);
279 dev->dep_link_up = false;
280 dev->active_target = NULL;
281 dev->rf_mode = NFC_RF_NONE;
282 nfc_llcp_mac_is_down(dev);
283 nfc_genl_dep_link_down_event(dev);
287 device_unlock(&dev->dev);
292 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
293 u8 comm_mode, u8 rf_mode)
295 dev->dep_link_up = true;
297 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
299 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
301 EXPORT_SYMBOL(nfc_dep_link_is_up);
304 * nfc_activate_target - prepare the target for data exchange
306 * @dev: The nfc device that found the target
307 * @target_idx: index of the target that must be activated
308 * @protocol: nfc protocol that will be used for data exchange
310 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
313 struct nfc_target *target;
315 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
316 dev_name(&dev->dev), target_idx, protocol);
318 device_lock(&dev->dev);
320 if (!device_is_registered(&dev->dev)) {
325 if (dev->active_target) {
330 target = nfc_find_target(dev, target_idx);
331 if (target == NULL) {
336 rc = dev->ops->activate_target(dev, target, protocol);
338 dev->active_target = target;
339 dev->rf_mode = NFC_RF_INITIATOR;
341 if (dev->ops->check_presence && !dev->shutting_down)
342 mod_timer(&dev->check_pres_timer, jiffies +
343 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
347 device_unlock(&dev->dev);
352 * nfc_deactivate_target - deactivate a nfc target
354 * @dev: The nfc device that found the target
355 * @target_idx: index of the target that must be deactivated
357 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
361 pr_debug("dev_name=%s target_idx=%u\n",
362 dev_name(&dev->dev), target_idx);
364 device_lock(&dev->dev);
366 if (!device_is_registered(&dev->dev)) {
371 if (dev->active_target == NULL) {
376 if (dev->active_target->idx != target_idx) {
381 if (dev->ops->check_presence)
382 del_timer_sync(&dev->check_pres_timer);
384 dev->ops->deactivate_target(dev, dev->active_target);
385 dev->active_target = NULL;
388 device_unlock(&dev->dev);
393 * nfc_data_exchange - transceive data
395 * @dev: The nfc device that found the target
396 * @target_idx: index of the target
397 * @skb: data to be sent
398 * @cb: callback called when the response is received
399 * @cb_context: parameter for the callback function
401 * The user must wait for the callback before calling this function again.
403 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
404 data_exchange_cb_t cb, void *cb_context)
408 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
409 dev_name(&dev->dev), target_idx, skb->len);
411 device_lock(&dev->dev);
413 if (!device_is_registered(&dev->dev)) {
419 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
420 if (dev->active_target->idx != target_idx) {
426 if (dev->ops->check_presence)
427 del_timer_sync(&dev->check_pres_timer);
429 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
432 if (!rc && dev->ops->check_presence && !dev->shutting_down)
433 mod_timer(&dev->check_pres_timer, jiffies +
434 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
435 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
436 rc = dev->ops->tm_send(dev, skb);
445 device_unlock(&dev->dev);
449 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
451 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
453 if (gb_len > NFC_MAX_GT_LEN)
456 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
458 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
460 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
462 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
464 return nfc_llcp_general_bytes(dev, gb_len);
466 EXPORT_SYMBOL(nfc_get_local_general_bytes);
468 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
470 /* Only LLCP target mode for now */
471 if (dev->dep_link_up == false) {
476 return nfc_llcp_data_received(dev, skb);
478 EXPORT_SYMBOL(nfc_tm_data_received);
480 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
481 u8 *gb, size_t gb_len)
485 device_lock(&dev->dev);
487 dev->polling = false;
490 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
495 dev->rf_mode = NFC_RF_TARGET;
497 if (protocol == NFC_PROTO_NFC_DEP_MASK)
498 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
500 rc = nfc_genl_tm_activated(dev, protocol);
503 device_unlock(&dev->dev);
507 EXPORT_SYMBOL(nfc_tm_activated);
509 int nfc_tm_deactivated(struct nfc_dev *dev)
511 dev->dep_link_up = false;
512 dev->rf_mode = NFC_RF_NONE;
514 return nfc_genl_tm_deactivated(dev);
516 EXPORT_SYMBOL(nfc_tm_deactivated);
519 * nfc_alloc_send_skb - allocate a skb for data exchange responses
521 * @size: size to allocate
524 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
525 unsigned int flags, unsigned int size,
529 unsigned int total_size;
532 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
534 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
536 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
542 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
544 * @size: size to allocate
547 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
550 unsigned int total_size;
552 total_size = size + 1;
553 skb = alloc_skb(total_size, gfp);
560 EXPORT_SYMBOL(nfc_alloc_recv_skb);
563 * nfc_targets_found - inform that targets were found
565 * @dev: The nfc device that found the targets
566 * @targets: array of nfc targets found
567 * @ntargets: targets array size
569 * The device driver must call this function when one or many nfc targets
570 * are found. After calling this function, the device driver must stop
571 * polling for targets.
572 * NOTE: This function can be called with targets=NULL and n_targets=0 to
573 * notify a driver error, meaning that the polling operation cannot complete.
574 * IMPORTANT: this function must not be called from an atomic context.
575 * In addition, it must also not be called from a context that would prevent
576 * the NFC Core to call other nfc ops entry point concurrently.
578 int nfc_targets_found(struct nfc_dev *dev,
579 struct nfc_target *targets, int n_targets)
583 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
585 for (i = 0; i < n_targets; i++)
586 targets[i].idx = dev->target_next_idx++;
588 device_lock(&dev->dev);
590 if (dev->polling == false) {
591 device_unlock(&dev->dev);
595 dev->polling = false;
597 dev->targets_generation++;
603 dev->targets = kmemdup(targets,
604 n_targets * sizeof(struct nfc_target),
609 device_unlock(&dev->dev);
614 dev->n_targets = n_targets;
615 device_unlock(&dev->dev);
617 nfc_genl_targets_found(dev);
621 EXPORT_SYMBOL(nfc_targets_found);
624 * nfc_target_lost - inform that an activated target went out of field
626 * @dev: The nfc device that had the activated target in field
627 * @target_idx: the nfc index of the target
629 * The device driver must call this function when the activated target
630 * goes out of the field.
631 * IMPORTANT: this function must not be called from an atomic context.
632 * In addition, it must also not be called from a context that would prevent
633 * the NFC Core to call other nfc ops entry point concurrently.
635 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
637 struct nfc_target *tg;
640 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
642 device_lock(&dev->dev);
644 for (i = 0; i < dev->n_targets; i++) {
645 tg = &dev->targets[i];
646 if (tg->idx == target_idx)
650 if (i == dev->n_targets) {
651 device_unlock(&dev->dev);
655 dev->targets_generation++;
657 dev->active_target = NULL;
659 if (dev->n_targets) {
660 memcpy(&dev->targets[i], &dev->targets[i + 1],
661 (dev->n_targets - i) * sizeof(struct nfc_target));
667 device_unlock(&dev->dev);
669 nfc_genl_target_lost(dev, target_idx);
673 EXPORT_SYMBOL(nfc_target_lost);
675 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
677 nfc_targets_found(dev, NULL, 0);
679 EXPORT_SYMBOL(nfc_driver_failure);
681 static void nfc_release(struct device *d)
683 struct nfc_dev *dev = to_nfc_dev(d);
685 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
687 nfc_genl_data_exit(&dev->genl_data);
692 static void nfc_check_pres_work(struct work_struct *work)
694 struct nfc_dev *dev = container_of(work, struct nfc_dev,
698 device_lock(&dev->dev);
700 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
701 rc = dev->ops->check_presence(dev, dev->active_target);
702 if (rc == -EOPNOTSUPP)
705 u32 active_target_idx = dev->active_target->idx;
706 device_unlock(&dev->dev);
707 nfc_target_lost(dev, active_target_idx);
711 if (!dev->shutting_down)
712 mod_timer(&dev->check_pres_timer, jiffies +
713 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
717 device_unlock(&dev->dev);
720 static void nfc_check_pres_timeout(unsigned long data)
722 struct nfc_dev *dev = (struct nfc_dev *)data;
724 schedule_work(&dev->check_pres_work);
727 struct class nfc_class = {
729 .dev_release = nfc_release,
731 EXPORT_SYMBOL(nfc_class);
733 static int match_idx(struct device *d, const void *data)
735 struct nfc_dev *dev = to_nfc_dev(d);
736 const unsigned int *idx = data;
738 return dev->idx == *idx;
741 struct nfc_dev *nfc_get_device(unsigned int idx)
745 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
749 return to_nfc_dev(d);
753 * nfc_allocate_device - allocate a new nfc device
755 * @ops: device operations
756 * @supported_protocols: NFC protocols supported by the device
758 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
759 u32 supported_protocols,
761 int tx_headroom, int tx_tailroom)
765 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
766 !ops->deactivate_target || !ops->im_transceive)
769 if (!supported_protocols)
772 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
777 dev->supported_protocols = supported_protocols;
778 dev->supported_se = supported_se;
779 dev->active_se = NFC_SE_NONE;
780 dev->tx_headroom = tx_headroom;
781 dev->tx_tailroom = tx_tailroom;
783 nfc_genl_data_init(&dev->genl_data);
785 dev->rf_mode = NFC_RF_NONE;
787 /* first generation must not be 0 */
788 dev->targets_generation = 1;
790 if (ops->check_presence) {
791 init_timer(&dev->check_pres_timer);
792 dev->check_pres_timer.data = (unsigned long)dev;
793 dev->check_pres_timer.function = nfc_check_pres_timeout;
795 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
800 EXPORT_SYMBOL(nfc_allocate_device);
803 * nfc_register_device - register a nfc device in the nfc subsystem
805 * @dev: The nfc device to register
807 int nfc_register_device(struct nfc_dev *dev)
811 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
813 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
817 dev->dev.class = &nfc_class;
818 dev_set_name(&dev->dev, "nfc%d", dev->idx);
819 device_initialize(&dev->dev);
821 mutex_lock(&nfc_devlist_mutex);
822 nfc_devlist_generation++;
823 rc = device_add(&dev->dev);
824 mutex_unlock(&nfc_devlist_mutex);
829 rc = nfc_llcp_register_device(dev);
831 pr_err("Could not register llcp device\n");
833 rc = nfc_genl_device_added(dev);
835 pr_debug("The userspace won't be notified that the device %s was added\n",
836 dev_name(&dev->dev));
840 EXPORT_SYMBOL(nfc_register_device);
843 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
845 * @dev: The nfc device to unregister
847 void nfc_unregister_device(struct nfc_dev *dev)
851 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
855 if (dev->ops->check_presence) {
856 device_lock(&dev->dev);
857 dev->shutting_down = true;
858 device_unlock(&dev->dev);
859 del_timer_sync(&dev->check_pres_timer);
860 cancel_work_sync(&dev->check_pres_work);
863 rc = nfc_genl_device_removed(dev);
865 pr_debug("The userspace won't be notified that the device %s "
866 "was removed\n", dev_name(&dev->dev));
868 nfc_llcp_unregister_device(dev);
870 mutex_lock(&nfc_devlist_mutex);
871 nfc_devlist_generation++;
872 device_del(&dev->dev);
873 mutex_unlock(&nfc_devlist_mutex);
875 ida_simple_remove(&nfc_index_ida, id);
877 EXPORT_SYMBOL(nfc_unregister_device);
879 static int __init nfc_init(void)
883 pr_info("NFC Core ver %s\n", VERSION);
885 rc = class_register(&nfc_class);
889 rc = nfc_genl_init();
893 /* the first generation must not be 0 */
894 nfc_devlist_generation = 1;
900 rc = nfc_llcp_init();
917 class_unregister(&nfc_class);
921 static void __exit nfc_exit(void)
927 class_unregister(&nfc_class);
930 subsys_initcall(nfc_init);
931 module_exit(nfc_exit);
934 MODULE_DESCRIPTION("NFC Core ver " VERSION);
935 MODULE_VERSION(VERSION);
936 MODULE_LICENSE("GPL");
937 MODULE_ALIAS_NETPROTO(PF_NFC);
938 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);