Skip to content

Commit 4d12b8b

Browse files
Lauro Ramos Venanciolinvjw
authored andcommitted
NFC: add nfc generic netlink interface
The NFC generic netlink interface exports the NFC control operations to the user space. Signed-off-by: Lauro Ramos Venancio <lauro.venancio@openbossa.org> Signed-off-by: Aloisio Almeida Jr <aloisio.almeida@openbossa.org> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com> Reviewed-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 3e256b8 commit 4d12b8b

File tree

6 files changed

+773
-3
lines changed

6 files changed

+773
-3
lines changed

include/linux/nfc.h

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (C) 2011 Instituto Nokia de Tecnologia
3+
*
4+
* Authors:
5+
* Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6+
* Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7+
*
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.
12+
*
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.
17+
*
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.
22+
*/
23+
24+
#ifndef __LINUX_NFC_H
25+
#define __LINUX_NFC_H
26+
27+
#define NFC_GENL_NAME "nfc"
28+
#define NFC_GENL_VERSION 1
29+
30+
#define NFC_GENL_MCAST_EVENT_NAME "events"
31+
32+
/**
33+
* enum nfc_commands - supported nfc commands
34+
*
35+
* @NFC_CMD_UNSPEC: unspecified command
36+
*
37+
* @NFC_CMD_GET_DEVICE: request information about a device (requires
38+
* %NFC_ATTR_DEVICE_INDEX) or dump request to get a list of all nfc devices
39+
* @NFC_CMD_START_POLL: start polling for targets using the given protocols
40+
* (requires %NFC_ATTR_DEVICE_INDEX and %NFC_ATTR_PROTOCOLS)
41+
* @NFC_CMD_STOP_POLL: stop polling for targets (requires
42+
* %NFC_ATTR_DEVICE_INDEX)
43+
* @NFC_CMD_GET_TARGET: dump all targets found by the previous poll (requires
44+
* %NFC_ATTR_DEVICE_INDEX)
45+
* @NFC_EVENT_TARGETS_FOUND: event emitted when a new target is found
46+
* (it sends %NFC_ATTR_DEVICE_INDEX)
47+
* @NFC_EVENT_DEVICE_ADDED: event emitted when a new device is registred
48+
* (it sends %NFC_ATTR_DEVICE_NAME, %NFC_ATTR_DEVICE_INDEX and
49+
* %NFC_ATTR_PROTOCOLS)
50+
* @NFC_EVENT_DEVICE_REMOVED: event emitted when a device is removed
51+
* (it sends %NFC_ATTR_DEVICE_INDEX)
52+
*/
53+
enum nfc_commands {
54+
NFC_CMD_UNSPEC,
55+
NFC_CMD_GET_DEVICE,
56+
NFC_CMD_START_POLL,
57+
NFC_CMD_STOP_POLL,
58+
NFC_CMD_GET_TARGET,
59+
NFC_EVENT_TARGETS_FOUND,
60+
NFC_EVENT_DEVICE_ADDED,
61+
NFC_EVENT_DEVICE_REMOVED,
62+
/* private: internal use only */
63+
__NFC_CMD_AFTER_LAST
64+
};
65+
#define NFC_CMD_MAX (__NFC_CMD_AFTER_LAST - 1)
66+
67+
/**
68+
* enum nfc_attrs - supported nfc attributes
69+
*
70+
* @NFC_ATTR_UNSPEC: unspecified attribute
71+
*
72+
* @NFC_ATTR_DEVICE_INDEX: index of nfc device
73+
* @NFC_ATTR_DEVICE_NAME: device name, max 8 chars
74+
* @NFC_ATTR_PROTOCOLS: nfc protocols - bitwise or-ed combination from
75+
* NFC_PROTO_*_MASK constants
76+
* @NFC_ATTR_TARGET_INDEX: index of the nfc target
77+
* @NFC_ATTR_TARGET_SENS_RES: NFC-A targets extra information such as NFCID
78+
* @NFC_ATTR_TARGET_SEL_RES: NFC-A targets extra information (useful if the
79+
* target is not NFC-Forum compliant)
80+
*/
81+
enum nfc_attrs {
82+
NFC_ATTR_UNSPEC,
83+
NFC_ATTR_DEVICE_INDEX,
84+
NFC_ATTR_DEVICE_NAME,
85+
NFC_ATTR_PROTOCOLS,
86+
NFC_ATTR_TARGET_INDEX,
87+
NFC_ATTR_TARGET_SENS_RES,
88+
NFC_ATTR_TARGET_SEL_RES,
89+
/* private: internal use only */
90+
__NFC_ATTR_AFTER_LAST
91+
};
92+
#define NFC_ATTR_MAX (__NFC_ATTR_AFTER_LAST - 1)
93+
94+
#define NFC_DEVICE_NAME_MAXSIZE 8
95+
96+
/* NFC protocols */
97+
#define NFC_PROTO_JEWEL 1
98+
#define NFC_PROTO_MIFARE 2
99+
#define NFC_PROTO_FELICA 3
100+
#define NFC_PROTO_ISO14443 4
101+
#define NFC_PROTO_NFC_DEP 5
102+
103+
#define NFC_PROTO_MAX 6
104+
105+
/* NFC protocols masks used in bitsets */
106+
#define NFC_PROTO_JEWEL_MASK (1 << NFC_PROTO_JEWEL)
107+
#define NFC_PROTO_MIFARE_MASK (1 << NFC_PROTO_MIFARE)
108+
#define NFC_PROTO_FELICA_MASK (1 << NFC_PROTO_FELICA)
109+
#define NFC_PROTO_ISO14443_MASK (1 << NFC_PROTO_ISO14443)
110+
#define NFC_PROTO_NFC_DEP_MASK (1 << NFC_PROTO_NFC_DEP)
111+
112+
#endif /*__LINUX_NFC_H */

include/net/nfc.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,28 @@ struct nfc_ops {
5858
void *cb_context);
5959
};
6060

61+
struct nfc_target {
62+
u32 idx;
63+
u32 supported_protocols;
64+
u16 sens_res;
65+
u8 sel_res;
66+
};
67+
68+
struct nfc_genl_data {
69+
u32 poll_req_pid;
70+
struct mutex genl_data_mutex;
71+
};
72+
6173
struct nfc_dev {
6274
unsigned idx;
75+
unsigned target_idx;
76+
struct nfc_target *targets;
77+
int n_targets;
78+
int targets_generation;
79+
spinlock_t targets_lock;
6380
struct device dev;
6481
bool polling;
82+
struct nfc_genl_data genl_data;
6583
u32 supported_protocols;
6684

6785
struct nfc_ops *ops;
@@ -132,4 +150,7 @@ static inline const char *nfc_device_name(struct nfc_dev *dev)
132150

133151
struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp);
134152

153+
int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
154+
int ntargets);
155+
135156
#endif /* __NET_NFC_H */

net/nfc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
obj-$(CONFIG_NFC) += nfc.o
66

7-
nfc-objs := core.o
7+
nfc-objs := core.o netlink.o

net/nfc/core.c

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,60 @@ struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
233233
}
234234
EXPORT_SYMBOL(nfc_alloc_skb);
235235

236+
/**
237+
* nfc_targets_found - inform that targets were found
238+
*
239+
* @dev: The nfc device that found the targets
240+
* @targets: array of nfc targets found
241+
* @ntargets: targets array size
242+
*
243+
* The device driver must call this function when one or many nfc targets
244+
* are found. After calling this function, the device driver must stop
245+
* polling for targets.
246+
*/
247+
int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
248+
int n_targets)
249+
{
250+
int i;
251+
252+
nfc_dbg("dev_name=%s n_targets=%d", dev_name(&dev->dev), n_targets);
253+
254+
dev->polling = false;
255+
256+
for (i = 0; i < n_targets; i++)
257+
targets[i].idx = dev->target_idx++;
258+
259+
spin_lock_bh(&dev->targets_lock);
260+
261+
dev->targets_generation++;
262+
263+
kfree(dev->targets);
264+
dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
265+
GFP_ATOMIC);
266+
267+
if (!dev->targets) {
268+
dev->n_targets = 0;
269+
spin_unlock_bh(&dev->targets_lock);
270+
return -ENOMEM;
271+
}
272+
273+
dev->n_targets = n_targets;
274+
spin_unlock_bh(&dev->targets_lock);
275+
276+
nfc_genl_targets_found(dev);
277+
278+
return 0;
279+
}
280+
EXPORT_SYMBOL(nfc_targets_found);
281+
236282
static void nfc_release(struct device *d)
237283
{
238284
struct nfc_dev *dev = to_nfc_dev(d);
239285

240286
nfc_dbg("dev_name=%s", dev_name(&dev->dev));
241287

288+
nfc_genl_data_exit(&dev->genl_data);
289+
kfree(dev->targets);
242290
kfree(dev);
243291
}
244292

@@ -298,6 +346,12 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
298346
dev->ops = ops;
299347
dev->supported_protocols = supported_protocols;
300348

349+
spin_lock_init(&dev->targets_lock);
350+
nfc_genl_data_init(&dev->genl_data);
351+
352+
/* first generation must not be 0 */
353+
dev->targets_generation = 1;
354+
301355
return dev;
302356
}
303357
EXPORT_SYMBOL(nfc_allocate_device);
@@ -318,7 +372,16 @@ int nfc_register_device(struct nfc_dev *dev)
318372
rc = device_add(&dev->dev);
319373
mutex_unlock(&nfc_devlist_mutex);
320374

321-
return rc;
375+
if (rc < 0)
376+
return rc;
377+
378+
rc = nfc_genl_device_added(dev);
379+
if (rc)
380+
nfc_dbg("The userspace won't be notified that the device %s was"
381+
" added", dev_name(&dev->dev));
382+
383+
384+
return 0;
322385
}
323386
EXPORT_SYMBOL(nfc_register_device);
324387

@@ -329,6 +392,8 @@ EXPORT_SYMBOL(nfc_register_device);
329392
*/
330393
void nfc_unregister_device(struct nfc_dev *dev)
331394
{
395+
int rc;
396+
332397
nfc_dbg("dev_name=%s", dev_name(&dev->dev));
333398

334399
mutex_lock(&nfc_devlist_mutex);
@@ -341,18 +406,42 @@ void nfc_unregister_device(struct nfc_dev *dev)
341406
device_unlock(&dev->dev);
342407

343408
mutex_unlock(&nfc_devlist_mutex);
409+
410+
rc = nfc_genl_device_removed(dev);
411+
if (rc)
412+
nfc_dbg("The userspace won't be notified that the device %s"
413+
" was removed", dev_name(&dev->dev));
414+
344415
}
345416
EXPORT_SYMBOL(nfc_unregister_device);
346417

347418
static int __init nfc_init(void)
348419
{
420+
int rc;
421+
349422
nfc_info("NFC Core ver %s", VERSION);
350423

351-
return class_register(&nfc_class);
424+
rc = class_register(&nfc_class);
425+
if (rc)
426+
return rc;
427+
428+
rc = nfc_genl_init();
429+
if (rc)
430+
goto err_genl;
431+
432+
/* the first generation must not be 0 */
433+
nfc_devlist_generation = 1;
434+
435+
return 0;
436+
437+
err_genl:
438+
class_unregister(&nfc_class);
439+
return rc;
352440
}
353441

354442
static void __exit nfc_exit(void)
355443
{
444+
nfc_genl_exit();
356445
class_unregister(&nfc_class);
357446
}
358447

0 commit comments

Comments
 (0)