]> Pileus Git - ~andy/linux/blobdiff - net/nfc/core.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / net / nfc / core.c
index 295d129864d246cb6c1a3b993dfbc54ebc826bad..9f6ce011d35d17135dc0780e75da036e520f7288 100644 (file)
@@ -33,6 +33,8 @@
 
 #define VERSION "0.1"
 
+#define NFC_CHECK_PRES_FREQ_MS 2000
+
 int nfc_devlist_generation;
 DEFINE_MUTEX(nfc_devlist_mutex);
 
@@ -95,7 +97,7 @@ int nfc_dev_down(struct nfc_dev *dev)
                goto error;
        }
 
-       if (dev->polling || dev->remote_activated) {
+       if (dev->polling || dev->active_target) {
                rc = -EBUSY;
                goto error;
        }
@@ -181,11 +183,27 @@ error:
        return rc;
 }
 
+static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
+{
+       int i;
+
+       if (dev->n_targets == 0)
+               return NULL;
+
+       for (i = 0; i < dev->n_targets ; i++) {
+               if (dev->targets[i].idx == target_idx)
+                       return &dev->targets[i];
+       }
+
+       return NULL;
+}
+
 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
 {
        int rc = 0;
        u8 *gb;
        size_t gb_len;
+       struct nfc_target *target;
 
        pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
 
@@ -210,7 +228,15 @@ int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
                goto error;
        }
 
-       rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len);
+       target = nfc_find_target(dev, target_index);
+       if (target == NULL) {
+               rc = -ENOTCONN;
+               goto error;
+       }
+
+       rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
+       if (!rc)
+               dev->active_target = target;
 
 error:
        device_unlock(&dev->dev);
@@ -246,6 +272,7 @@ int nfc_dep_link_down(struct nfc_dev *dev)
        rc = dev->ops->dep_link_down(dev);
        if (!rc) {
                dev->dep_link_up = false;
+               dev->active_target = NULL;
                nfc_llcp_mac_is_down(dev);
                nfc_genl_dep_link_down_event(dev);
        }
@@ -277,6 +304,7 @@ EXPORT_SYMBOL(nfc_dep_link_is_up);
 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
 {
        int rc;
+       struct nfc_target *target;
 
        pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
                 dev_name(&dev->dev), target_idx, protocol);
@@ -288,9 +316,25 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
                goto error;
        }
 
-       rc = dev->ops->activate_target(dev, target_idx, protocol);
-       if (!rc)
-               dev->remote_activated = true;
+       if (dev->active_target) {
+               rc = -EBUSY;
+               goto error;
+       }
+
+       target = nfc_find_target(dev, target_idx);
+       if (target == NULL) {
+               rc = -ENOTCONN;
+               goto error;
+       }
+
+       rc = dev->ops->activate_target(dev, target, protocol);
+       if (!rc) {
+               dev->active_target = target;
+
+               if (dev->ops->check_presence)
+                       mod_timer(&dev->check_pres_timer, jiffies +
+                                 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
+       }
 
 error:
        device_unlock(&dev->dev);
@@ -317,8 +361,21 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
                goto error;
        }
 
-       dev->ops->deactivate_target(dev, target_idx);
-       dev->remote_activated = false;
+       if (dev->active_target == NULL) {
+               rc = -ENOTCONN;
+               goto error;
+       }
+
+       if (dev->active_target->idx != target_idx) {
+               rc = -ENOTCONN;
+               goto error;
+       }
+
+       if (dev->ops->check_presence)
+               del_timer_sync(&dev->check_pres_timer);
+
+       dev->ops->deactivate_target(dev, dev->active_target);
+       dev->active_target = NULL;
 
 error:
        device_unlock(&dev->dev);
@@ -352,7 +409,27 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
                goto error;
        }
 
-       rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
+       if (dev->active_target == NULL) {
+               rc = -ENOTCONN;
+               kfree_skb(skb);
+               goto error;
+       }
+
+       if (dev->active_target->idx != target_idx) {
+               rc = -EADDRNOTAVAIL;
+               kfree_skb(skb);
+               goto error;
+       }
+
+       if (dev->ops->check_presence)
+               del_timer_sync(&dev->check_pres_timer);
+
+       rc = dev->ops->data_exchange(dev, dev->active_target, skb, cb,
+                                    cb_context);
+
+       if (!rc && dev->ops->check_presence)
+               mod_timer(&dev->check_pres_timer, jiffies +
+                         msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
 
 error:
        device_unlock(&dev->dev);
@@ -424,15 +501,23 @@ EXPORT_SYMBOL(nfc_alloc_recv_skb);
  * The device driver must call this function when one or many nfc targets
  * are found. After calling this function, the device driver must stop
  * polling for targets.
+ * IMPORTANT: this function must not be called from an atomic context.
+ * In addition, it must also not be called from a context that would prevent
+ * the NFC Core to call other nfc ops entry point concurrently.
  */
 int nfc_targets_found(struct nfc_dev *dev,
                      struct nfc_target *targets, int n_targets)
 {
+       int i;
+
        pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
 
        dev->polling = false;
 
-       spin_lock_bh(&dev->targets_lock);
+       for (i = 0; i < n_targets; i++)
+               targets[i].idx = dev->target_next_idx++;
+
+       device_lock(&dev->dev);
 
        dev->targets_generation++;
 
@@ -442,12 +527,12 @@ int nfc_targets_found(struct nfc_dev *dev,
 
        if (!dev->targets) {
                dev->n_targets = 0;
-               spin_unlock_bh(&dev->targets_lock);
+               device_unlock(&dev->dev);
                return -ENOMEM;
        }
 
        dev->n_targets = n_targets;
-       spin_unlock_bh(&dev->targets_lock);
+       device_unlock(&dev->dev);
 
        nfc_genl_targets_found(dev);
 
@@ -455,17 +540,105 @@ int nfc_targets_found(struct nfc_dev *dev,
 }
 EXPORT_SYMBOL(nfc_targets_found);
 
+/**
+ * nfc_target_lost - inform that an activated target went out of field
+ *
+ * @dev: The nfc device that had the activated target in field
+ * @target_idx: the nfc index of the target
+ *
+ * The device driver must call this function when the activated target
+ * goes out of the field.
+ * IMPORTANT: this function must not be called from an atomic context.
+ * In addition, it must also not be called from a context that would prevent
+ * the NFC Core to call other nfc ops entry point concurrently.
+ */
+int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
+{
+       struct nfc_target *tg;
+       int i;
+
+       pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
+
+       device_lock(&dev->dev);
+
+       for (i = 0; i < dev->n_targets; i++) {
+               tg = &dev->targets[i];
+               if (tg->idx == target_idx)
+                       break;
+       }
+
+       if (i == dev->n_targets) {
+               device_unlock(&dev->dev);
+               return -EINVAL;
+       }
+
+       dev->targets_generation++;
+       dev->n_targets--;
+       dev->active_target = NULL;
+
+       if (dev->n_targets) {
+               memcpy(&dev->targets[i], &dev->targets[i + 1],
+                      (dev->n_targets - i) * sizeof(struct nfc_target));
+       } else {
+               kfree(dev->targets);
+               dev->targets = NULL;
+       }
+
+       device_unlock(&dev->dev);
+
+       nfc_genl_target_lost(dev, target_idx);
+
+       return 0;
+}
+EXPORT_SYMBOL(nfc_target_lost);
+
 static void nfc_release(struct device *d)
 {
        struct nfc_dev *dev = to_nfc_dev(d);
 
        pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 
+       if (dev->ops->check_presence) {
+               del_timer_sync(&dev->check_pres_timer);
+               destroy_workqueue(dev->check_pres_wq);
+       }
+
        nfc_genl_data_exit(&dev->genl_data);
        kfree(dev->targets);
        kfree(dev);
 }
 
+static void nfc_check_pres_work(struct work_struct *work)
+{
+       struct nfc_dev *dev = container_of(work, struct nfc_dev,
+                                          check_pres_work);
+       int rc;
+
+       device_lock(&dev->dev);
+
+       if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
+               rc = dev->ops->check_presence(dev, dev->active_target);
+               if (!rc) {
+                       mod_timer(&dev->check_pres_timer, jiffies +
+                                 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
+               } else {
+                       u32 active_target_idx = dev->active_target->idx;
+                       device_unlock(&dev->dev);
+                       nfc_target_lost(dev, active_target_idx);
+                       return;
+               }
+       }
+
+       device_unlock(&dev->dev);
+}
+
+static void nfc_check_pres_timeout(unsigned long data)
+{
+       struct nfc_dev *dev = (struct nfc_dev *)data;
+
+       queue_work(dev->check_pres_wq, &dev->check_pres_work);
+}
+
 struct class nfc_class = {
        .name = "nfc",
        .dev_release = nfc_release,
@@ -475,12 +648,12 @@ EXPORT_SYMBOL(nfc_class);
 static int match_idx(struct device *d, void *data)
 {
        struct nfc_dev *dev = to_nfc_dev(d);
-       unsigned *idx = data;
+       unsigned int *idx = data;
 
        return dev->idx == *idx;
 }
 
-struct nfc_dev *nfc_get_device(unsigned idx)
+struct nfc_dev *nfc_get_device(unsigned int idx)
 {
        struct device *d;
 
@@ -525,12 +698,29 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
        dev->tx_headroom = tx_headroom;
        dev->tx_tailroom = tx_tailroom;
 
-       spin_lock_init(&dev->targets_lock);
        nfc_genl_data_init(&dev->genl_data);
 
+
        /* first generation must not be 0 */
        dev->targets_generation = 1;
 
+       if (ops->check_presence) {
+               char name[32];
+               init_timer(&dev->check_pres_timer);
+               dev->check_pres_timer.data = (unsigned long)dev;
+               dev->check_pres_timer.function = nfc_check_pres_timeout;
+
+               INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
+               snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
+               dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
+                                                    WQ_UNBOUND |
+                                                    WQ_MEM_RECLAIM, 1);
+               if (dev->check_pres_wq == NULL) {
+                       kfree(dev);
+                       return NULL;
+               }
+       }
+
        return dev;
 }
 EXPORT_SYMBOL(nfc_allocate_device);