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);
44 * nfc_dev_up - turn on the NFC device
46 * @dev: The nfc device to be turned on
48 * The device remains up until the nfc_dev_down function is called.
50 int nfc_dev_up(struct nfc_dev *dev)
54 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
56 device_lock(&dev->dev);
58 if (!device_is_registered(&dev->dev)) {
69 rc = dev->ops->dev_up(dev);
75 device_unlock(&dev->dev);
80 * nfc_dev_down - turn off the NFC device
82 * @dev: The nfc device to be turned off
84 int nfc_dev_down(struct nfc_dev *dev)
88 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
90 device_lock(&dev->dev);
92 if (!device_is_registered(&dev->dev)) {
102 if (dev->polling || dev->active_target) {
107 if (dev->ops->dev_down)
108 dev->ops->dev_down(dev);
113 device_unlock(&dev->dev);
118 * nfc_start_poll - start polling for nfc targets
120 * @dev: The nfc device that must start polling
121 * @protocols: bitset of nfc protocols that must be used for polling
123 * The device remains polling for targets until a target is found or
124 * the nfc_stop_poll function is called.
126 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
130 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
131 dev_name(&dev->dev), im_protocols, tm_protocols);
133 if (!im_protocols && !tm_protocols)
136 device_lock(&dev->dev);
138 if (!device_is_registered(&dev->dev)) {
148 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
151 dev->rf_mode = NFC_RF_NONE;
155 device_unlock(&dev->dev);
160 * nfc_stop_poll - stop polling for nfc targets
162 * @dev: The nfc device that must stop polling
164 int nfc_stop_poll(struct nfc_dev *dev)
168 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
170 device_lock(&dev->dev);
172 if (!device_is_registered(&dev->dev)) {
182 dev->ops->stop_poll(dev);
183 dev->polling = false;
186 device_unlock(&dev->dev);
190 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
194 if (dev->n_targets == 0)
197 for (i = 0; i < dev->n_targets ; i++) {
198 if (dev->targets[i].idx == target_idx)
199 return &dev->targets[i];
205 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
210 struct nfc_target *target;
212 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
214 if (!dev->ops->dep_link_up)
217 device_lock(&dev->dev);
219 if (!device_is_registered(&dev->dev)) {
224 if (dev->dep_link_up == true) {
229 gb = nfc_llcp_general_bytes(dev, &gb_len);
230 if (gb_len > NFC_MAX_GT_LEN) {
235 target = nfc_find_target(dev, target_index);
236 if (target == NULL) {
241 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
243 dev->active_target = target;
244 dev->rf_mode = NFC_RF_INITIATOR;
248 device_unlock(&dev->dev);
252 int nfc_dep_link_down(struct nfc_dev *dev)
256 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
258 if (!dev->ops->dep_link_down)
261 device_lock(&dev->dev);
263 if (!device_is_registered(&dev->dev)) {
268 if (dev->dep_link_up == false) {
273 rc = dev->ops->dep_link_down(dev);
275 dev->dep_link_up = false;
276 dev->active_target = NULL;
277 nfc_llcp_mac_is_down(dev);
278 nfc_genl_dep_link_down_event(dev);
282 device_unlock(&dev->dev);
286 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
287 u8 comm_mode, u8 rf_mode)
289 dev->dep_link_up = true;
291 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
293 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
295 EXPORT_SYMBOL(nfc_dep_link_is_up);
298 * nfc_activate_target - prepare the target for data exchange
300 * @dev: The nfc device that found the target
301 * @target_idx: index of the target that must be activated
302 * @protocol: nfc protocol that will be used for data exchange
304 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
307 struct nfc_target *target;
309 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
310 dev_name(&dev->dev), target_idx, protocol);
312 device_lock(&dev->dev);
314 if (!device_is_registered(&dev->dev)) {
319 if (dev->active_target) {
324 target = nfc_find_target(dev, target_idx);
325 if (target == NULL) {
330 rc = dev->ops->activate_target(dev, target, protocol);
332 dev->active_target = target;
333 dev->rf_mode = NFC_RF_INITIATOR;
335 if (dev->ops->check_presence)
336 mod_timer(&dev->check_pres_timer, jiffies +
337 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
341 device_unlock(&dev->dev);
346 * nfc_deactivate_target - deactivate a nfc target
348 * @dev: The nfc device that found the target
349 * @target_idx: index of the target that must be deactivated
351 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
355 pr_debug("dev_name=%s target_idx=%u\n",
356 dev_name(&dev->dev), target_idx);
358 device_lock(&dev->dev);
360 if (!device_is_registered(&dev->dev)) {
365 if (dev->active_target == NULL) {
370 if (dev->active_target->idx != target_idx) {
375 if (dev->ops->check_presence)
376 del_timer_sync(&dev->check_pres_timer);
378 dev->ops->deactivate_target(dev, dev->active_target);
379 dev->active_target = NULL;
382 device_unlock(&dev->dev);
387 * nfc_data_exchange - transceive data
389 * @dev: The nfc device that found the target
390 * @target_idx: index of the target
391 * @skb: data to be sent
392 * @cb: callback called when the response is received
393 * @cb_context: parameter for the callback function
395 * The user must wait for the callback before calling this function again.
397 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
398 data_exchange_cb_t cb, void *cb_context)
402 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
403 dev_name(&dev->dev), target_idx, skb->len);
405 device_lock(&dev->dev);
407 if (!device_is_registered(&dev->dev)) {
413 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
414 if (dev->active_target->idx != target_idx) {
420 if (dev->ops->check_presence)
421 del_timer_sync(&dev->check_pres_timer);
423 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
426 if (!rc && dev->ops->check_presence)
427 mod_timer(&dev->check_pres_timer, jiffies +
428 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
429 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
430 rc = dev->ops->tm_send(dev, skb);
439 device_unlock(&dev->dev);
443 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
445 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
447 if (gb_len > NFC_MAX_GT_LEN)
450 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
452 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
454 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
456 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
458 return nfc_llcp_general_bytes(dev, gb_len);
460 EXPORT_SYMBOL(nfc_get_local_general_bytes);
462 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
464 /* Only LLCP target mode for now */
465 if (dev->dep_link_up == false) {
470 return nfc_llcp_data_received(dev, skb);
472 EXPORT_SYMBOL(nfc_tm_data_received);
474 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
475 u8 *gb, size_t gb_len)
479 device_lock(&dev->dev);
481 dev->polling = false;
484 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
489 dev->rf_mode = NFC_RF_TARGET;
491 if (protocol == NFC_PROTO_NFC_DEP_MASK)
492 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
494 rc = nfc_genl_tm_activated(dev, protocol);
497 device_unlock(&dev->dev);
501 EXPORT_SYMBOL(nfc_tm_activated);
503 int nfc_tm_deactivated(struct nfc_dev *dev)
505 dev->dep_link_up = false;
507 return nfc_genl_tm_deactivated(dev);
509 EXPORT_SYMBOL(nfc_tm_deactivated);
512 * nfc_alloc_send_skb - allocate a skb for data exchange responses
514 * @size: size to allocate
517 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
518 unsigned int flags, unsigned int size,
522 unsigned int total_size;
525 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
527 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
529 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
535 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
537 * @size: size to allocate
540 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
543 unsigned int total_size;
545 total_size = size + 1;
546 skb = alloc_skb(total_size, gfp);
553 EXPORT_SYMBOL(nfc_alloc_recv_skb);
556 * nfc_targets_found - inform that targets were found
558 * @dev: The nfc device that found the targets
559 * @targets: array of nfc targets found
560 * @ntargets: targets array size
562 * The device driver must call this function when one or many nfc targets
563 * are found. After calling this function, the device driver must stop
564 * polling for targets.
565 * NOTE: This function can be called with targets=NULL and n_targets=0 to
566 * notify a driver error, meaning that the polling operation cannot complete.
567 * IMPORTANT: this function must not be called from an atomic context.
568 * In addition, it must also not be called from a context that would prevent
569 * the NFC Core to call other nfc ops entry point concurrently.
571 int nfc_targets_found(struct nfc_dev *dev,
572 struct nfc_target *targets, int n_targets)
576 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
578 for (i = 0; i < n_targets; i++)
579 targets[i].idx = dev->target_next_idx++;
581 device_lock(&dev->dev);
583 if (dev->polling == false) {
584 device_unlock(&dev->dev);
588 dev->polling = false;
590 dev->targets_generation++;
596 dev->targets = kmemdup(targets,
597 n_targets * sizeof(struct nfc_target),
602 device_unlock(&dev->dev);
607 dev->n_targets = n_targets;
608 device_unlock(&dev->dev);
610 nfc_genl_targets_found(dev);
614 EXPORT_SYMBOL(nfc_targets_found);
617 * nfc_target_lost - inform that an activated target went out of field
619 * @dev: The nfc device that had the activated target in field
620 * @target_idx: the nfc index of the target
622 * The device driver must call this function when the activated target
623 * goes out of the field.
624 * IMPORTANT: this function must not be called from an atomic context.
625 * In addition, it must also not be called from a context that would prevent
626 * the NFC Core to call other nfc ops entry point concurrently.
628 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
630 struct nfc_target *tg;
633 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
635 device_lock(&dev->dev);
637 for (i = 0; i < dev->n_targets; i++) {
638 tg = &dev->targets[i];
639 if (tg->idx == target_idx)
643 if (i == dev->n_targets) {
644 device_unlock(&dev->dev);
648 dev->targets_generation++;
650 dev->active_target = NULL;
652 if (dev->n_targets) {
653 memcpy(&dev->targets[i], &dev->targets[i + 1],
654 (dev->n_targets - i) * sizeof(struct nfc_target));
660 device_unlock(&dev->dev);
662 nfc_genl_target_lost(dev, target_idx);
666 EXPORT_SYMBOL(nfc_target_lost);
668 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
670 nfc_targets_found(dev, NULL, 0);
672 EXPORT_SYMBOL(nfc_driver_failure);
674 static void nfc_release(struct device *d)
676 struct nfc_dev *dev = to_nfc_dev(d);
678 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
680 if (dev->ops->check_presence) {
681 del_timer_sync(&dev->check_pres_timer);
682 cancel_work_sync(&dev->check_pres_work);
685 nfc_genl_data_exit(&dev->genl_data);
690 static void nfc_check_pres_work(struct work_struct *work)
692 struct nfc_dev *dev = container_of(work, struct nfc_dev,
696 device_lock(&dev->dev);
698 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
699 rc = dev->ops->check_presence(dev, dev->active_target);
701 mod_timer(&dev->check_pres_timer, jiffies +
702 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
704 u32 active_target_idx = dev->active_target->idx;
705 device_unlock(&dev->dev);
706 nfc_target_lost(dev, active_target_idx);
711 device_unlock(&dev->dev);
714 static void nfc_check_pres_timeout(unsigned long data)
716 struct nfc_dev *dev = (struct nfc_dev *)data;
718 schedule_work(&dev->check_pres_work);
721 struct class nfc_class = {
723 .dev_release = nfc_release,
725 EXPORT_SYMBOL(nfc_class);
727 static int match_idx(struct device *d, void *data)
729 struct nfc_dev *dev = to_nfc_dev(d);
730 unsigned int *idx = data;
732 return dev->idx == *idx;
735 struct nfc_dev *nfc_get_device(unsigned int idx)
739 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
743 return to_nfc_dev(d);
747 * nfc_allocate_device - allocate a new nfc device
749 * @ops: device operations
750 * @supported_protocols: NFC protocols supported by the device
752 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
753 u32 supported_protocols,
754 int tx_headroom, int tx_tailroom)
756 static atomic_t dev_no = ATOMIC_INIT(0);
759 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
760 !ops->deactivate_target || !ops->im_transceive)
763 if (!supported_protocols)
766 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
770 dev->dev.class = &nfc_class;
771 dev->idx = atomic_inc_return(&dev_no) - 1;
772 dev_set_name(&dev->dev, "nfc%d", dev->idx);
773 device_initialize(&dev->dev);
776 dev->supported_protocols = supported_protocols;
777 dev->tx_headroom = tx_headroom;
778 dev->tx_tailroom = tx_tailroom;
780 nfc_genl_data_init(&dev->genl_data);
783 /* first generation must not be 0 */
784 dev->targets_generation = 1;
786 if (ops->check_presence) {
787 init_timer(&dev->check_pres_timer);
788 dev->check_pres_timer.data = (unsigned long)dev;
789 dev->check_pres_timer.function = nfc_check_pres_timeout;
791 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
796 EXPORT_SYMBOL(nfc_allocate_device);
799 * nfc_register_device - register a nfc device in the nfc subsystem
801 * @dev: The nfc device to register
803 int nfc_register_device(struct nfc_dev *dev)
807 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
809 mutex_lock(&nfc_devlist_mutex);
810 nfc_devlist_generation++;
811 rc = device_add(&dev->dev);
812 mutex_unlock(&nfc_devlist_mutex);
817 rc = nfc_llcp_register_device(dev);
819 pr_err("Could not register llcp device\n");
821 rc = nfc_genl_device_added(dev);
823 pr_debug("The userspace won't be notified that the device %s was added\n",
824 dev_name(&dev->dev));
828 EXPORT_SYMBOL(nfc_register_device);
831 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
833 * @dev: The nfc device to unregister
835 void nfc_unregister_device(struct nfc_dev *dev)
839 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
841 mutex_lock(&nfc_devlist_mutex);
842 nfc_devlist_generation++;
844 /* lock to avoid unregistering a device while an operation
846 device_lock(&dev->dev);
847 device_del(&dev->dev);
848 device_unlock(&dev->dev);
850 mutex_unlock(&nfc_devlist_mutex);
852 nfc_llcp_unregister_device(dev);
854 rc = nfc_genl_device_removed(dev);
856 pr_debug("The userspace won't be notified that the device %s was removed\n",
857 dev_name(&dev->dev));
860 EXPORT_SYMBOL(nfc_unregister_device);
862 static int __init nfc_init(void)
866 pr_info("NFC Core ver %s\n", VERSION);
868 rc = class_register(&nfc_class);
872 rc = nfc_genl_init();
876 /* the first generation must not be 0 */
877 nfc_devlist_generation = 1;
883 rc = nfc_llcp_init();
900 class_unregister(&nfc_class);
904 static void __exit nfc_exit(void)
910 class_unregister(&nfc_class);
913 subsys_initcall(nfc_init);
914 module_exit(nfc_exit);
917 MODULE_DESCRIPTION("NFC Core ver " VERSION);
918 MODULE_VERSION(VERSION);
919 MODULE_LICENSE("GPL");
920 MODULE_ALIAS_NETPROTO(PF_NFC);
921 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);