NFC: Add dev_up and dev_down control operations
authorIlan Elias <ilane@ti.com>
Sun, 18 Sep 2011 08:19:33 +0000 (11:19 +0300)
committerJohn W. Linville <linville@tuxdriver.com>
Tue, 20 Sep 2011 18:43:49 +0000 (14:43 -0400)
Add 2 new nfc control operations:
dev_up to turn on the nfc device
dev_down to turn off the nfc device

Signed-off-by: Ilan Elias <ilane@ti.com>
Signed-off-by: John W. Linville <linville@tuxdriver.com>
drivers/nfc/pn533.c
include/linux/nfc.h
include/net/nfc.h
net/nfc/core.c
net/nfc/netlink.c
net/nfc/nfc.h

index f81a93e5b59dfa865e413454a82ab01539c6457f..c78eb6afd0cb7745adb73ed5ccadf60259a59882 100644 (file)
@@ -1432,6 +1432,8 @@ static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
 }
 
 struct nfc_ops pn533_nfc_ops = {
+       .dev_up = NULL,
+       .dev_down = NULL,
        .start_poll = pn533_start_poll,
        .stop_poll = pn533_stop_poll,
        .activate_target = pn533_activate_target,
index c525e0b5876b9286d110a5f4dc14de7e4b93f010..36cb955b05cc517fc09ffcb0126571f7ccb5bcc0 100644 (file)
  *
  * @NFC_CMD_GET_DEVICE: request information about a device (requires
  *     %NFC_ATTR_DEVICE_INDEX) or dump request to get a list of all nfc devices
+ * @NFC_CMD_DEV_UP: turn on the nfc device
+ *     (requires %NFC_ATTR_DEVICE_INDEX)
+ * @NFC_CMD_DEV_DOWN: turn off the nfc device
+ *     (requires %NFC_ATTR_DEVICE_INDEX)
  * @NFC_CMD_START_POLL: start polling for targets using the given protocols
  *     (requires %NFC_ATTR_DEVICE_INDEX and %NFC_ATTR_PROTOCOLS)
  * @NFC_CMD_STOP_POLL: stop polling for targets (requires
@@ -56,6 +60,8 @@
 enum nfc_commands {
        NFC_CMD_UNSPEC,
        NFC_CMD_GET_DEVICE,
+       NFC_CMD_DEV_UP,
+       NFC_CMD_DEV_DOWN,
        NFC_CMD_START_POLL,
        NFC_CMD_STOP_POLL,
        NFC_CMD_GET_TARGET,
index 87b51fe15b707c14ad1c662af04726b07fff45e1..6a7f602aa841d87794fa175c7e3ca7d2d934d4c0 100644 (file)
@@ -48,6 +48,8 @@ typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
                                                                int err);
 
 struct nfc_ops {
+       int (*dev_up)(struct nfc_dev *dev);
+       int (*dev_down)(struct nfc_dev *dev);
        int (*start_poll)(struct nfc_dev *dev, u32 protocols);
        void (*stop_poll)(struct nfc_dev *dev);
        int (*activate_target)(struct nfc_dev *dev, u32 target_idx,
@@ -78,7 +80,9 @@ struct nfc_dev {
        int targets_generation;
        spinlock_t targets_lock;
        struct device dev;
+       bool dev_up;
        bool polling;
+       bool remote_activated;
        struct nfc_genl_data genl_data;
        u32 supported_protocols;
 
index 284e2f6a14ff490f287354bdd60058b87123d475..47e02c1b8c02eea81a67f4432e6d45311ba6bd73 100644 (file)
@@ -52,6 +52,80 @@ int nfc_printk(const char *level, const char *format, ...)
 }
 EXPORT_SYMBOL(nfc_printk);
 
+/**
+ * nfc_dev_up - turn on the NFC device
+ *
+ * @dev: The nfc device to be turned on
+ *
+ * The device remains up until the nfc_dev_down function is called.
+ */
+int nfc_dev_up(struct nfc_dev *dev)
+{
+       int rc = 0;
+
+       nfc_dbg("dev_name=%s", dev_name(&dev->dev));
+
+       device_lock(&dev->dev);
+
+       if (!device_is_registered(&dev->dev)) {
+               rc = -ENODEV;
+               goto error;
+       }
+
+       if (dev->dev_up) {
+               rc = -EALREADY;
+               goto error;
+       }
+
+       if (dev->ops->dev_up)
+               rc = dev->ops->dev_up(dev);
+
+       if (!rc)
+               dev->dev_up = true;
+
+error:
+       device_unlock(&dev->dev);
+       return rc;
+}
+
+/**
+ * nfc_dev_down - turn off the NFC device
+ *
+ * @dev: The nfc device to be turned off
+ */
+int nfc_dev_down(struct nfc_dev *dev)
+{
+       int rc = 0;
+
+       nfc_dbg("dev_name=%s", dev_name(&dev->dev));
+
+       device_lock(&dev->dev);
+
+       if (!device_is_registered(&dev->dev)) {
+               rc = -ENODEV;
+               goto error;
+       }
+
+       if (!dev->dev_up) {
+               rc = -EALREADY;
+               goto error;
+       }
+
+       if (dev->polling || dev->remote_activated) {
+               rc = -EBUSY;
+               goto error;
+       }
+
+       if (dev->ops->dev_down)
+               dev->ops->dev_down(dev);
+
+       dev->dev_up = false;
+
+error:
+       device_unlock(&dev->dev);
+       return rc;
+}
+
 /**
  * nfc_start_poll - start polling for nfc targets
  *
@@ -144,6 +218,8 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
        }
 
        rc = dev->ops->activate_target(dev, target_idx, protocol);
+       if (!rc)
+               dev->remote_activated = true;
 
 error:
        device_unlock(&dev->dev);
@@ -170,6 +246,7 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
        }
 
        dev->ops->deactivate_target(dev, target_idx);
+       dev->remote_activated = false;
 
 error:
        device_unlock(&dev->dev);
index ccdff7953f7d888973d2fcb99c7554170e67f01b..03f8818e1f166a2b0eefe11862523df7e582ee33 100644 (file)
@@ -367,6 +367,52 @@ out_putdev:
        return rc;
 }
 
+static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
+{
+       struct nfc_dev *dev;
+       int rc;
+       u32 idx;
+
+       nfc_dbg("entry");
+
+       if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
+               return -EINVAL;
+
+       idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
+
+       dev = nfc_get_device(idx);
+       if (!dev)
+               return -ENODEV;
+
+       rc = nfc_dev_up(dev);
+
+       nfc_put_device(dev);
+       return rc;
+}
+
+static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
+{
+       struct nfc_dev *dev;
+       int rc;
+       u32 idx;
+
+       nfc_dbg("entry");
+
+       if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
+               return -EINVAL;
+
+       idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
+
+       dev = nfc_get_device(idx);
+       if (!dev)
+               return -ENODEV;
+
+       rc = nfc_dev_down(dev);
+
+       nfc_put_device(dev);
+       return rc;
+}
+
 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
 {
        struct nfc_dev *dev;
@@ -440,6 +486,16 @@ static struct genl_ops nfc_genl_ops[] = {
                .done = nfc_genl_dump_devices_done,
                .policy = nfc_genl_policy,
        },
+       {
+               .cmd = NFC_CMD_DEV_UP,
+               .doit = nfc_genl_dev_up,
+               .policy = nfc_genl_policy,
+       },
+       {
+               .cmd = NFC_CMD_DEV_DOWN,
+               .doit = nfc_genl_dev_down,
+               .policy = nfc_genl_policy,
+       },
        {
                .cmd = NFC_CMD_START_POLL,
                .doit = nfc_genl_start_poll,
index aaf9832298f34d9c932c39060a4ca94fe1f5e208..1a877de8e23011d4f080d94da6c42d8a1f822093 100644 (file)
@@ -101,6 +101,10 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter)
        class_dev_iter_exit(iter);
 }
 
+int nfc_dev_up(struct nfc_dev *dev);
+
+int nfc_dev_down(struct nfc_dev *dev);
+
 int nfc_start_poll(struct nfc_dev *dev, u32 protocols);
 
 int nfc_stop_poll(struct nfc_dev *dev);