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 rc = dev->ops->dep_link_down(dev);
273 dev->dep_link_up = false;
274 dev->active_target = NULL;
275 nfc_llcp_mac_is_down(dev);
276 nfc_genl_dep_link_down_event(dev);
280 device_unlock(&dev->dev);
284 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
285 u8 comm_mode, u8 rf_mode)
287 dev->dep_link_up = true;
289 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
291 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
293 EXPORT_SYMBOL(nfc_dep_link_is_up);
296 * nfc_activate_target - prepare the target for data exchange
298 * @dev: The nfc device that found the target
299 * @target_idx: index of the target that must be activated
300 * @protocol: nfc protocol that will be used for data exchange
302 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
305 struct nfc_target *target;
307 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
308 dev_name(&dev->dev), target_idx, protocol);
310 device_lock(&dev->dev);
312 if (!device_is_registered(&dev->dev)) {
317 if (dev->active_target) {
322 target = nfc_find_target(dev, target_idx);
323 if (target == NULL) {
328 rc = dev->ops->activate_target(dev, target, protocol);
330 dev->active_target = target;
331 dev->rf_mode = NFC_RF_INITIATOR;
333 if (dev->ops->check_presence)
334 mod_timer(&dev->check_pres_timer, jiffies +
335 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
339 device_unlock(&dev->dev);
344 * nfc_deactivate_target - deactivate a nfc target
346 * @dev: The nfc device that found the target
347 * @target_idx: index of the target that must be deactivated
349 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
353 pr_debug("dev_name=%s target_idx=%u\n",
354 dev_name(&dev->dev), target_idx);
356 device_lock(&dev->dev);
358 if (!device_is_registered(&dev->dev)) {
363 if (dev->active_target == NULL) {
368 if (dev->active_target->idx != target_idx) {
373 if (dev->ops->check_presence)
374 del_timer_sync(&dev->check_pres_timer);
376 dev->ops->deactivate_target(dev, dev->active_target);
377 dev->active_target = NULL;
380 device_unlock(&dev->dev);
385 * nfc_data_exchange - transceive data
387 * @dev: The nfc device that found the target
388 * @target_idx: index of the target
389 * @skb: data to be sent
390 * @cb: callback called when the response is received
391 * @cb_context: parameter for the callback function
393 * The user must wait for the callback before calling this function again.
395 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
396 data_exchange_cb_t cb, void *cb_context)
400 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
401 dev_name(&dev->dev), target_idx, skb->len);
403 device_lock(&dev->dev);
405 if (!device_is_registered(&dev->dev)) {
411 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
412 if (dev->active_target->idx != target_idx) {
418 if (dev->ops->check_presence)
419 del_timer_sync(&dev->check_pres_timer);
421 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
424 if (!rc && dev->ops->check_presence)
425 mod_timer(&dev->check_pres_timer, jiffies +
426 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
427 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
428 rc = dev->ops->tm_send(dev, skb);
437 device_unlock(&dev->dev);
441 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
443 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
445 if (gb_len > NFC_MAX_GT_LEN)
448 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
450 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
452 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
454 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
456 return nfc_llcp_general_bytes(dev, gb_len);
458 EXPORT_SYMBOL(nfc_get_local_general_bytes);
460 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
462 /* Only LLCP target mode for now */
463 if (dev->dep_link_up == false) {
468 return nfc_llcp_data_received(dev, skb);
470 EXPORT_SYMBOL(nfc_tm_data_received);
472 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
473 u8 *gb, size_t gb_len)
477 device_lock(&dev->dev);
479 dev->polling = false;
482 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
487 dev->rf_mode = NFC_RF_TARGET;
489 if (protocol == NFC_PROTO_NFC_DEP_MASK)
490 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
492 rc = nfc_genl_tm_activated(dev, protocol);
495 device_unlock(&dev->dev);
499 EXPORT_SYMBOL(nfc_tm_activated);
501 int nfc_tm_deactivated(struct nfc_dev *dev)
503 dev->dep_link_up = false;
505 return nfc_genl_tm_deactivated(dev);
507 EXPORT_SYMBOL(nfc_tm_deactivated);
510 * nfc_alloc_send_skb - allocate a skb for data exchange responses
512 * @size: size to allocate
515 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
516 unsigned int flags, unsigned int size,
520 unsigned int total_size;
523 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
525 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
527 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
533 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
535 * @size: size to allocate
538 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
541 unsigned int total_size;
543 total_size = size + 1;
544 skb = alloc_skb(total_size, gfp);
551 EXPORT_SYMBOL(nfc_alloc_recv_skb);
554 * nfc_targets_found - inform that targets were found
556 * @dev: The nfc device that found the targets
557 * @targets: array of nfc targets found
558 * @ntargets: targets array size
560 * The device driver must call this function when one or many nfc targets
561 * are found. After calling this function, the device driver must stop
562 * polling for targets.
563 * IMPORTANT: this function must not be called from an atomic context.
564 * In addition, it must also not be called from a context that would prevent
565 * the NFC Core to call other nfc ops entry point concurrently.
567 int nfc_targets_found(struct nfc_dev *dev,
568 struct nfc_target *targets, int n_targets)
572 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
574 dev->polling = false;
576 for (i = 0; i < n_targets; i++)
577 targets[i].idx = dev->target_next_idx++;
579 device_lock(&dev->dev);
581 dev->targets_generation++;
584 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
589 device_unlock(&dev->dev);
593 dev->n_targets = n_targets;
594 device_unlock(&dev->dev);
596 nfc_genl_targets_found(dev);
600 EXPORT_SYMBOL(nfc_targets_found);
603 * nfc_target_lost - inform that an activated target went out of field
605 * @dev: The nfc device that had the activated target in field
606 * @target_idx: the nfc index of the target
608 * The device driver must call this function when the activated target
609 * goes out of the field.
610 * IMPORTANT: this function must not be called from an atomic context.
611 * In addition, it must also not be called from a context that would prevent
612 * the NFC Core to call other nfc ops entry point concurrently.
614 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
616 struct nfc_target *tg;
619 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
621 device_lock(&dev->dev);
623 for (i = 0; i < dev->n_targets; i++) {
624 tg = &dev->targets[i];
625 if (tg->idx == target_idx)
629 if (i == dev->n_targets) {
630 device_unlock(&dev->dev);
634 dev->targets_generation++;
636 dev->active_target = NULL;
638 if (dev->n_targets) {
639 memcpy(&dev->targets[i], &dev->targets[i + 1],
640 (dev->n_targets - i) * sizeof(struct nfc_target));
646 device_unlock(&dev->dev);
648 nfc_genl_target_lost(dev, target_idx);
652 EXPORT_SYMBOL(nfc_target_lost);
654 static void nfc_release(struct device *d)
656 struct nfc_dev *dev = to_nfc_dev(d);
658 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
660 if (dev->ops->check_presence) {
661 del_timer_sync(&dev->check_pres_timer);
662 destroy_workqueue(dev->check_pres_wq);
665 nfc_genl_data_exit(&dev->genl_data);
670 static void nfc_check_pres_work(struct work_struct *work)
672 struct nfc_dev *dev = container_of(work, struct nfc_dev,
676 device_lock(&dev->dev);
678 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
679 rc = dev->ops->check_presence(dev, dev->active_target);
681 mod_timer(&dev->check_pres_timer, jiffies +
682 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
684 u32 active_target_idx = dev->active_target->idx;
685 device_unlock(&dev->dev);
686 nfc_target_lost(dev, active_target_idx);
691 device_unlock(&dev->dev);
694 static void nfc_check_pres_timeout(unsigned long data)
696 struct nfc_dev *dev = (struct nfc_dev *)data;
698 queue_work(dev->check_pres_wq, &dev->check_pres_work);
701 struct class nfc_class = {
703 .dev_release = nfc_release,
705 EXPORT_SYMBOL(nfc_class);
707 static int match_idx(struct device *d, void *data)
709 struct nfc_dev *dev = to_nfc_dev(d);
710 unsigned int *idx = data;
712 return dev->idx == *idx;
715 struct nfc_dev *nfc_get_device(unsigned int idx)
719 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
723 return to_nfc_dev(d);
727 * nfc_allocate_device - allocate a new nfc device
729 * @ops: device operations
730 * @supported_protocols: NFC protocols supported by the device
732 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
733 u32 supported_protocols,
734 int tx_headroom, int tx_tailroom)
736 static atomic_t dev_no = ATOMIC_INIT(0);
739 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
740 !ops->deactivate_target || !ops->im_transceive)
743 if (!supported_protocols)
746 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
750 dev->dev.class = &nfc_class;
751 dev->idx = atomic_inc_return(&dev_no) - 1;
752 dev_set_name(&dev->dev, "nfc%d", dev->idx);
753 device_initialize(&dev->dev);
756 dev->supported_protocols = supported_protocols;
757 dev->tx_headroom = tx_headroom;
758 dev->tx_tailroom = tx_tailroom;
760 nfc_genl_data_init(&dev->genl_data);
763 /* first generation must not be 0 */
764 dev->targets_generation = 1;
766 if (ops->check_presence) {
768 init_timer(&dev->check_pres_timer);
769 dev->check_pres_timer.data = (unsigned long)dev;
770 dev->check_pres_timer.function = nfc_check_pres_timeout;
772 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
773 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
774 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
777 if (dev->check_pres_wq == NULL) {
785 EXPORT_SYMBOL(nfc_allocate_device);
788 * nfc_register_device - register a nfc device in the nfc subsystem
790 * @dev: The nfc device to register
792 int nfc_register_device(struct nfc_dev *dev)
796 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
798 mutex_lock(&nfc_devlist_mutex);
799 nfc_devlist_generation++;
800 rc = device_add(&dev->dev);
801 mutex_unlock(&nfc_devlist_mutex);
806 rc = nfc_llcp_register_device(dev);
808 pr_err("Could not register llcp device\n");
810 rc = nfc_genl_device_added(dev);
812 pr_debug("The userspace won't be notified that the device %s was added\n",
813 dev_name(&dev->dev));
817 EXPORT_SYMBOL(nfc_register_device);
820 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
822 * @dev: The nfc device to unregister
824 void nfc_unregister_device(struct nfc_dev *dev)
828 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
830 mutex_lock(&nfc_devlist_mutex);
831 nfc_devlist_generation++;
833 /* lock to avoid unregistering a device while an operation
835 device_lock(&dev->dev);
836 device_del(&dev->dev);
837 device_unlock(&dev->dev);
839 mutex_unlock(&nfc_devlist_mutex);
841 nfc_llcp_unregister_device(dev);
843 rc = nfc_genl_device_removed(dev);
845 pr_debug("The userspace won't be notified that the device %s was removed\n",
846 dev_name(&dev->dev));
849 EXPORT_SYMBOL(nfc_unregister_device);
851 static int __init nfc_init(void)
855 pr_info("NFC Core ver %s\n", VERSION);
857 rc = class_register(&nfc_class);
861 rc = nfc_genl_init();
865 /* the first generation must not be 0 */
866 nfc_devlist_generation = 1;
872 rc = nfc_llcp_init();
889 class_unregister(&nfc_class);
893 static void __exit nfc_exit(void)
899 class_unregister(&nfc_class);
902 subsys_initcall(nfc_init);
903 module_exit(nfc_exit);
905 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
906 MODULE_DESCRIPTION("NFC Core ver " VERSION);
907 MODULE_VERSION(VERSION);
908 MODULE_LICENSE("GPL");