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 protocols)
128 pr_debug("dev_name=%s protocols=0x%x\n",
129 dev_name(&dev->dev), protocols);
134 device_lock(&dev->dev);
136 if (!device_is_registered(&dev->dev)) {
146 rc = dev->ops->start_poll(dev, protocols);
151 device_unlock(&dev->dev);
156 * nfc_stop_poll - stop polling for nfc targets
158 * @dev: The nfc device that must stop polling
160 int nfc_stop_poll(struct nfc_dev *dev)
164 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
166 device_lock(&dev->dev);
168 if (!device_is_registered(&dev->dev)) {
178 dev->ops->stop_poll(dev);
179 dev->polling = false;
182 device_unlock(&dev->dev);
186 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
190 if (dev->n_targets == 0)
193 for (i = 0; i < dev->n_targets ; i++) {
194 if (dev->targets[i].idx == target_idx)
195 return &dev->targets[i];
201 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
206 struct nfc_target *target;
208 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
210 if (!dev->ops->dep_link_up)
213 device_lock(&dev->dev);
215 if (!device_is_registered(&dev->dev)) {
220 if (dev->dep_link_up == true) {
225 gb = nfc_llcp_general_bytes(dev, &gb_len);
226 if (gb_len > NFC_MAX_GT_LEN) {
231 target = nfc_find_target(dev, target_index);
232 if (target == NULL) {
237 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
239 dev->active_target = target;
242 device_unlock(&dev->dev);
246 int nfc_dep_link_down(struct nfc_dev *dev)
250 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
252 if (!dev->ops->dep_link_down)
255 device_lock(&dev->dev);
257 if (!device_is_registered(&dev->dev)) {
262 if (dev->dep_link_up == false) {
267 if (dev->dep_rf_mode == NFC_RF_TARGET) {
272 rc = dev->ops->dep_link_down(dev);
274 dev->dep_link_up = false;
275 dev->active_target = NULL;
276 nfc_llcp_mac_is_down(dev);
277 nfc_genl_dep_link_down_event(dev);
281 device_unlock(&dev->dev);
285 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
286 u8 comm_mode, u8 rf_mode)
288 dev->dep_link_up = true;
289 dev->dep_rf_mode = rf_mode;
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;
334 if (dev->ops->check_presence)
335 mod_timer(&dev->check_pres_timer, jiffies +
336 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
340 device_unlock(&dev->dev);
345 * nfc_deactivate_target - deactivate a nfc target
347 * @dev: The nfc device that found the target
348 * @target_idx: index of the target that must be deactivated
350 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
354 pr_debug("dev_name=%s target_idx=%u\n",
355 dev_name(&dev->dev), target_idx);
357 device_lock(&dev->dev);
359 if (!device_is_registered(&dev->dev)) {
364 if (dev->active_target == NULL) {
369 if (dev->active_target->idx != target_idx) {
374 if (dev->ops->check_presence)
375 del_timer_sync(&dev->check_pres_timer);
377 dev->ops->deactivate_target(dev, dev->active_target);
378 dev->active_target = NULL;
381 device_unlock(&dev->dev);
386 * nfc_data_exchange - transceive data
388 * @dev: The nfc device that found the target
389 * @target_idx: index of the target
390 * @skb: data to be sent
391 * @cb: callback called when the response is received
392 * @cb_context: parameter for the callback function
394 * The user must wait for the callback before calling this function again.
396 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
397 data_exchange_cb_t cb, void *cb_context)
401 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
402 dev_name(&dev->dev), target_idx, skb->len);
404 device_lock(&dev->dev);
406 if (!device_is_registered(&dev->dev)) {
412 if (dev->active_target == NULL) {
418 if (dev->active_target->idx != target_idx) {
424 if (dev->ops->check_presence)
425 del_timer_sync(&dev->check_pres_timer);
427 rc = dev->ops->data_exchange(dev, dev->active_target, skb, cb,
430 if (!rc && dev->ops->check_presence)
431 mod_timer(&dev->check_pres_timer, jiffies +
432 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
435 device_unlock(&dev->dev);
439 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
441 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
443 if (gb_len > NFC_MAX_GT_LEN)
446 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
448 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
451 * nfc_alloc_send_skb - allocate a skb for data exchange responses
453 * @size: size to allocate
456 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
457 unsigned int flags, unsigned int size,
461 unsigned int total_size;
464 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
466 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
468 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
474 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
476 * @size: size to allocate
479 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
482 unsigned int total_size;
484 total_size = size + 1;
485 skb = alloc_skb(total_size, gfp);
492 EXPORT_SYMBOL(nfc_alloc_recv_skb);
495 * nfc_targets_found - inform that targets were found
497 * @dev: The nfc device that found the targets
498 * @targets: array of nfc targets found
499 * @ntargets: targets array size
501 * The device driver must call this function when one or many nfc targets
502 * are found. After calling this function, the device driver must stop
503 * polling for targets.
504 * IMPORTANT: this function must not be called from an atomic context.
505 * In addition, it must also not be called from a context that would prevent
506 * the NFC Core to call other nfc ops entry point concurrently.
508 int nfc_targets_found(struct nfc_dev *dev,
509 struct nfc_target *targets, int n_targets)
513 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
515 dev->polling = false;
517 for (i = 0; i < n_targets; i++)
518 targets[i].idx = dev->target_next_idx++;
520 device_lock(&dev->dev);
522 dev->targets_generation++;
525 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
530 device_unlock(&dev->dev);
534 dev->n_targets = n_targets;
535 device_unlock(&dev->dev);
537 nfc_genl_targets_found(dev);
541 EXPORT_SYMBOL(nfc_targets_found);
544 * nfc_target_lost - inform that an activated target went out of field
546 * @dev: The nfc device that had the activated target in field
547 * @target_idx: the nfc index of the target
549 * The device driver must call this function when the activated target
550 * goes out of the field.
551 * IMPORTANT: this function must not be called from an atomic context.
552 * In addition, it must also not be called from a context that would prevent
553 * the NFC Core to call other nfc ops entry point concurrently.
555 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
557 struct nfc_target *tg;
560 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
562 device_lock(&dev->dev);
564 for (i = 0; i < dev->n_targets; i++) {
565 tg = &dev->targets[i];
566 if (tg->idx == target_idx)
570 if (i == dev->n_targets) {
571 device_unlock(&dev->dev);
575 dev->targets_generation++;
577 dev->active_target = NULL;
579 if (dev->n_targets) {
580 memcpy(&dev->targets[i], &dev->targets[i + 1],
581 (dev->n_targets - i) * sizeof(struct nfc_target));
587 device_unlock(&dev->dev);
589 nfc_genl_target_lost(dev, target_idx);
593 EXPORT_SYMBOL(nfc_target_lost);
595 static void nfc_release(struct device *d)
597 struct nfc_dev *dev = to_nfc_dev(d);
599 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
601 if (dev->ops->check_presence) {
602 del_timer_sync(&dev->check_pres_timer);
603 destroy_workqueue(dev->check_pres_wq);
606 nfc_genl_data_exit(&dev->genl_data);
611 static void nfc_check_pres_work(struct work_struct *work)
613 struct nfc_dev *dev = container_of(work, struct nfc_dev,
617 device_lock(&dev->dev);
619 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
620 rc = dev->ops->check_presence(dev, dev->active_target);
622 mod_timer(&dev->check_pres_timer, jiffies +
623 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
625 u32 active_target_idx = dev->active_target->idx;
626 device_unlock(&dev->dev);
627 nfc_target_lost(dev, active_target_idx);
632 device_unlock(&dev->dev);
635 static void nfc_check_pres_timeout(unsigned long data)
637 struct nfc_dev *dev = (struct nfc_dev *)data;
639 queue_work(dev->check_pres_wq, &dev->check_pres_work);
642 struct class nfc_class = {
644 .dev_release = nfc_release,
646 EXPORT_SYMBOL(nfc_class);
648 static int match_idx(struct device *d, void *data)
650 struct nfc_dev *dev = to_nfc_dev(d);
651 unsigned int *idx = data;
653 return dev->idx == *idx;
656 struct nfc_dev *nfc_get_device(unsigned int idx)
660 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
664 return to_nfc_dev(d);
668 * nfc_allocate_device - allocate a new nfc device
670 * @ops: device operations
671 * @supported_protocols: NFC protocols supported by the device
673 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
674 u32 supported_protocols,
675 int tx_headroom, int tx_tailroom)
677 static atomic_t dev_no = ATOMIC_INIT(0);
680 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
681 !ops->deactivate_target || !ops->data_exchange)
684 if (!supported_protocols)
687 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
691 dev->dev.class = &nfc_class;
692 dev->idx = atomic_inc_return(&dev_no) - 1;
693 dev_set_name(&dev->dev, "nfc%d", dev->idx);
694 device_initialize(&dev->dev);
697 dev->supported_protocols = supported_protocols;
698 dev->tx_headroom = tx_headroom;
699 dev->tx_tailroom = tx_tailroom;
701 nfc_genl_data_init(&dev->genl_data);
704 /* first generation must not be 0 */
705 dev->targets_generation = 1;
707 if (ops->check_presence) {
709 init_timer(&dev->check_pres_timer);
710 dev->check_pres_timer.data = (unsigned long)dev;
711 dev->check_pres_timer.function = nfc_check_pres_timeout;
713 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
714 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
715 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
718 if (dev->check_pres_wq == NULL) {
726 EXPORT_SYMBOL(nfc_allocate_device);
729 * nfc_register_device - register a nfc device in the nfc subsystem
731 * @dev: The nfc device to register
733 int nfc_register_device(struct nfc_dev *dev)
737 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
739 mutex_lock(&nfc_devlist_mutex);
740 nfc_devlist_generation++;
741 rc = device_add(&dev->dev);
742 mutex_unlock(&nfc_devlist_mutex);
747 rc = nfc_llcp_register_device(dev);
749 pr_err("Could not register llcp device\n");
751 rc = nfc_genl_device_added(dev);
753 pr_debug("The userspace won't be notified that the device %s was added\n",
754 dev_name(&dev->dev));
758 EXPORT_SYMBOL(nfc_register_device);
761 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
763 * @dev: The nfc device to unregister
765 void nfc_unregister_device(struct nfc_dev *dev)
769 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
771 mutex_lock(&nfc_devlist_mutex);
772 nfc_devlist_generation++;
774 /* lock to avoid unregistering a device while an operation
776 device_lock(&dev->dev);
777 device_del(&dev->dev);
778 device_unlock(&dev->dev);
780 mutex_unlock(&nfc_devlist_mutex);
782 nfc_llcp_unregister_device(dev);
784 rc = nfc_genl_device_removed(dev);
786 pr_debug("The userspace won't be notified that the device %s was removed\n",
787 dev_name(&dev->dev));
790 EXPORT_SYMBOL(nfc_unregister_device);
792 static int __init nfc_init(void)
796 pr_info("NFC Core ver %s\n", VERSION);
798 rc = class_register(&nfc_class);
802 rc = nfc_genl_init();
806 /* the first generation must not be 0 */
807 nfc_devlist_generation = 1;
813 rc = nfc_llcp_init();
830 class_unregister(&nfc_class);
834 static void __exit nfc_exit(void)
840 class_unregister(&nfc_class);
843 subsys_initcall(nfc_init);
844 module_exit(nfc_exit);
846 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
847 MODULE_DESCRIPTION("NFC Core ver " VERSION);
848 MODULE_VERSION(VERSION);
849 MODULE_LICENSE("GPL");