Skip to content

Commit be055b2

Browse files
author
Samuel Ortiz
committed
NFC: RFKILL support
All NFC devices will now get proper RFKILL support as long as they provide some dev_up and dev_down hooks. Rfkilling an NFC device will bring it down while it is left to userspace to bring it back up when being rfkill unblocked. This is very similar to what Bluetooth does. Acked-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
1 parent 44b3dec commit be055b2

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎include/net/nfc/nfc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ struct nfc_dev {
122122

123123
bool shutting_down;
124124

125+
struct rfkill *rfkill;
126+
125127
struct nfc_ops *ops;
126128
};
127129
#define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev)

‎net/nfc/core.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/kernel.h>
2828
#include <linux/module.h>
2929
#include <linux/slab.h>
30+
#include <linux/rfkill.h>
3031
#include <linux/nfc.h>
3132

3233
#include <net/genetlink.h>
@@ -58,6 +59,11 @@ int nfc_dev_up(struct nfc_dev *dev)
5859

5960
device_lock(&dev->dev);
6061

62+
if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
63+
rc = -ERFKILL;
64+
goto error;
65+
}
66+
6167
if (!device_is_registered(&dev->dev)) {
6268
rc = -ENODEV;
6369
goto error;
@@ -117,6 +123,24 @@ int nfc_dev_down(struct nfc_dev *dev)
117123
return rc;
118124
}
119125

126+
static int nfc_rfkill_set_block(void *data, bool blocked)
127+
{
128+
struct nfc_dev *dev = data;
129+
130+
pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
131+
132+
if (!blocked)
133+
return 0;
134+
135+
nfc_dev_down(dev);
136+
137+
return 0;
138+
}
139+
140+
static const struct rfkill_ops nfc_rfkill_ops = {
141+
.set_block = nfc_rfkill_set_block,
142+
};
143+
120144
/**
121145
* nfc_start_poll - start polling for nfc targets
122146
*
@@ -840,6 +864,15 @@ int nfc_register_device(struct nfc_dev *dev)
840864
pr_debug("The userspace won't be notified that the device %s was added\n",
841865
dev_name(&dev->dev));
842866

867+
dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
868+
RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
869+
if (dev->rfkill) {
870+
if (rfkill_register(dev->rfkill) < 0) {
871+
rfkill_destroy(dev->rfkill);
872+
dev->rfkill = NULL;
873+
}
874+
}
875+
843876
return 0;
844877
}
845878
EXPORT_SYMBOL(nfc_register_device);
@@ -857,6 +890,11 @@ void nfc_unregister_device(struct nfc_dev *dev)
857890

858891
id = dev->idx;
859892

893+
if (dev->rfkill) {
894+
rfkill_unregister(dev->rfkill);
895+
rfkill_destroy(dev->rfkill);
896+
}
897+
860898
if (dev->ops->check_presence) {
861899
device_lock(&dev->dev);
862900
dev->shutting_down = true;

0 commit comments

Comments
 (0)