Skip to content

Commit 8b3fe7b

Browse files
Ilan Eliaslinvjw
authored andcommitted
NFC: Add dev_up and dev_down control operations
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 <[email protected]> Signed-off-by: John W. Linville <[email protected]>
1 parent a7ce1c9 commit 8b3fe7b

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

drivers/nfc/pn533.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,8 @@ static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
14321432
}
14331433

14341434
struct nfc_ops pn533_nfc_ops = {
1435+
.dev_up = NULL,
1436+
.dev_down = NULL,
14351437
.start_poll = pn533_start_poll,
14361438
.stop_poll = pn533_stop_poll,
14371439
.activate_target = pn533_activate_target,

include/linux/nfc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
*
4040
* @NFC_CMD_GET_DEVICE: request information about a device (requires
4141
* %NFC_ATTR_DEVICE_INDEX) or dump request to get a list of all nfc devices
42+
* @NFC_CMD_DEV_UP: turn on the nfc device
43+
* (requires %NFC_ATTR_DEVICE_INDEX)
44+
* @NFC_CMD_DEV_DOWN: turn off the nfc device
45+
* (requires %NFC_ATTR_DEVICE_INDEX)
4246
* @NFC_CMD_START_POLL: start polling for targets using the given protocols
4347
* (requires %NFC_ATTR_DEVICE_INDEX and %NFC_ATTR_PROTOCOLS)
4448
* @NFC_CMD_STOP_POLL: stop polling for targets (requires
@@ -56,6 +60,8 @@
5660
enum nfc_commands {
5761
NFC_CMD_UNSPEC,
5862
NFC_CMD_GET_DEVICE,
63+
NFC_CMD_DEV_UP,
64+
NFC_CMD_DEV_DOWN,
5965
NFC_CMD_START_POLL,
6066
NFC_CMD_STOP_POLL,
6167
NFC_CMD_GET_TARGET,

include/net/nfc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
4848
int err);
4949

5050
struct nfc_ops {
51+
int (*dev_up)(struct nfc_dev *dev);
52+
int (*dev_down)(struct nfc_dev *dev);
5153
int (*start_poll)(struct nfc_dev *dev, u32 protocols);
5254
void (*stop_poll)(struct nfc_dev *dev);
5355
int (*activate_target)(struct nfc_dev *dev, u32 target_idx,
@@ -78,7 +80,9 @@ struct nfc_dev {
7880
int targets_generation;
7981
spinlock_t targets_lock;
8082
struct device dev;
83+
bool dev_up;
8184
bool polling;
85+
bool remote_activated;
8286
struct nfc_genl_data genl_data;
8387
u32 supported_protocols;
8488

net/nfc/core.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,80 @@ int nfc_printk(const char *level, const char *format, ...)
5252
}
5353
EXPORT_SYMBOL(nfc_printk);
5454

55+
/**
56+
* nfc_dev_up - turn on the NFC device
57+
*
58+
* @dev: The nfc device to be turned on
59+
*
60+
* The device remains up until the nfc_dev_down function is called.
61+
*/
62+
int nfc_dev_up(struct nfc_dev *dev)
63+
{
64+
int rc = 0;
65+
66+
nfc_dbg("dev_name=%s", dev_name(&dev->dev));
67+
68+
device_lock(&dev->dev);
69+
70+
if (!device_is_registered(&dev->dev)) {
71+
rc = -ENODEV;
72+
goto error;
73+
}
74+
75+
if (dev->dev_up) {
76+
rc = -EALREADY;
77+
goto error;
78+
}
79+
80+
if (dev->ops->dev_up)
81+
rc = dev->ops->dev_up(dev);
82+
83+
if (!rc)
84+
dev->dev_up = true;
85+
86+
error:
87+
device_unlock(&dev->dev);
88+
return rc;
89+
}
90+
91+
/**
92+
* nfc_dev_down - turn off the NFC device
93+
*
94+
* @dev: The nfc device to be turned off
95+
*/
96+
int nfc_dev_down(struct nfc_dev *dev)
97+
{
98+
int rc = 0;
99+
100+
nfc_dbg("dev_name=%s", dev_name(&dev->dev));
101+
102+
device_lock(&dev->dev);
103+
104+
if (!device_is_registered(&dev->dev)) {
105+
rc = -ENODEV;
106+
goto error;
107+
}
108+
109+
if (!dev->dev_up) {
110+
rc = -EALREADY;
111+
goto error;
112+
}
113+
114+
if (dev->polling || dev->remote_activated) {
115+
rc = -EBUSY;
116+
goto error;
117+
}
118+
119+
if (dev->ops->dev_down)
120+
dev->ops->dev_down(dev);
121+
122+
dev->dev_up = false;
123+
124+
error:
125+
device_unlock(&dev->dev);
126+
return rc;
127+
}
128+
55129
/**
56130
* nfc_start_poll - start polling for nfc targets
57131
*
@@ -144,6 +218,8 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
144218
}
145219

146220
rc = dev->ops->activate_target(dev, target_idx, protocol);
221+
if (!rc)
222+
dev->remote_activated = true;
147223

148224
error:
149225
device_unlock(&dev->dev);
@@ -170,6 +246,7 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
170246
}
171247

172248
dev->ops->deactivate_target(dev, target_idx);
249+
dev->remote_activated = false;
173250

174251
error:
175252
device_unlock(&dev->dev);

net/nfc/netlink.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,52 @@ static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
367367
return rc;
368368
}
369369

370+
static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
371+
{
372+
struct nfc_dev *dev;
373+
int rc;
374+
u32 idx;
375+
376+
nfc_dbg("entry");
377+
378+
if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
379+
return -EINVAL;
380+
381+
idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
382+
383+
dev = nfc_get_device(idx);
384+
if (!dev)
385+
return -ENODEV;
386+
387+
rc = nfc_dev_up(dev);
388+
389+
nfc_put_device(dev);
390+
return rc;
391+
}
392+
393+
static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
394+
{
395+
struct nfc_dev *dev;
396+
int rc;
397+
u32 idx;
398+
399+
nfc_dbg("entry");
400+
401+
if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
402+
return -EINVAL;
403+
404+
idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
405+
406+
dev = nfc_get_device(idx);
407+
if (!dev)
408+
return -ENODEV;
409+
410+
rc = nfc_dev_down(dev);
411+
412+
nfc_put_device(dev);
413+
return rc;
414+
}
415+
370416
static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
371417
{
372418
struct nfc_dev *dev;
@@ -440,6 +486,16 @@ static struct genl_ops nfc_genl_ops[] = {
440486
.done = nfc_genl_dump_devices_done,
441487
.policy = nfc_genl_policy,
442488
},
489+
{
490+
.cmd = NFC_CMD_DEV_UP,
491+
.doit = nfc_genl_dev_up,
492+
.policy = nfc_genl_policy,
493+
},
494+
{
495+
.cmd = NFC_CMD_DEV_DOWN,
496+
.doit = nfc_genl_dev_down,
497+
.policy = nfc_genl_policy,
498+
},
443499
{
444500
.cmd = NFC_CMD_START_POLL,
445501
.doit = nfc_genl_start_poll,

net/nfc/nfc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter)
101101
class_dev_iter_exit(iter);
102102
}
103103

104+
int nfc_dev_up(struct nfc_dev *dev);
105+
106+
int nfc_dev_down(struct nfc_dev *dev);
107+
104108
int nfc_start_poll(struct nfc_dev *dev, u32 protocols);
105109

106110
int nfc_stop_poll(struct nfc_dev *dev);

0 commit comments

Comments
 (0)