diff options
author | Stephen Rothwell <sfr@canb.auug.org.au> | 2011-07-25 13:59:46 -0400 |
---|---|---|
committer | Trond Myklebust <Trond.Myklebust@netapp.com> | 2011-07-25 14:53:52 -0400 |
commit | 5f00bcb38ef9a980a33c6dbdc0044964b05f22dd (patch) | |
tree | 3175fb9375aecb50bde1be0bf4fa8aa8155131d6 /net/nfc | |
parent | 34006cee28f7344f9557a4be3816c7891b1bbab1 (diff) | |
parent | b6844e8f64920cdee620157252169ba63afb0c89 (diff) |
Merge branch 'master' into devel and apply fixup from Stephen Rothwell:
vfs/nfs: fixup for nfs_open_context change
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
Diffstat (limited to 'net/nfc')
-rw-r--r-- | net/nfc/Kconfig | 16 | ||||
-rw-r--r-- | net/nfc/Makefile | 7 | ||||
-rw-r--r-- | net/nfc/af_nfc.c | 98 | ||||
-rw-r--r-- | net/nfc/core.c | 468 | ||||
-rw-r--r-- | net/nfc/netlink.c | 537 | ||||
-rw-r--r-- | net/nfc/nfc.h | 117 | ||||
-rw-r--r-- | net/nfc/rawsock.c | 354 |
7 files changed, 1597 insertions, 0 deletions
diff --git a/net/nfc/Kconfig b/net/nfc/Kconfig new file mode 100644 index 000000000000..33e095b124b3 --- /dev/null +++ b/net/nfc/Kconfig | |||
@@ -0,0 +1,16 @@ | |||
1 | # | ||
2 | # NFC sybsystem configuration | ||
3 | # | ||
4 | |||
5 | menuconfig NFC | ||
6 | depends on NET && EXPERIMENTAL | ||
7 | tristate "NFC subsystem support (EXPERIMENTAL)" | ||
8 | default n | ||
9 | help | ||
10 | Say Y here if you want to build support for NFC (Near field | ||
11 | communication) devices. | ||
12 | |||
13 | To compile this support as a module, choose M here: the module will | ||
14 | be called nfc. | ||
15 | |||
16 | source "drivers/nfc/Kconfig" | ||
diff --git a/net/nfc/Makefile b/net/nfc/Makefile new file mode 100644 index 000000000000..16250c353851 --- /dev/null +++ b/net/nfc/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | # | ||
2 | # Makefile for the Linux NFC subsystem. | ||
3 | # | ||
4 | |||
5 | obj-$(CONFIG_NFC) += nfc.o | ||
6 | |||
7 | nfc-objs := core.o netlink.o af_nfc.o rawsock.o | ||
diff --git a/net/nfc/af_nfc.c b/net/nfc/af_nfc.c new file mode 100644 index 000000000000..e982cef8f49d --- /dev/null +++ b/net/nfc/af_nfc.c | |||
@@ -0,0 +1,98 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia | ||
3 | * | ||
4 | * Authors: | ||
5 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> | ||
6 | * Lauro Ramos Venancio <lauro.venancio@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 | #include <linux/nfc.h> | ||
25 | |||
26 | #include "nfc.h" | ||
27 | |||
28 | static DEFINE_RWLOCK(proto_tab_lock); | ||
29 | static const struct nfc_protocol *proto_tab[NFC_SOCKPROTO_MAX]; | ||
30 | |||
31 | static int nfc_sock_create(struct net *net, struct socket *sock, int proto, | ||
32 | int kern) | ||
33 | { | ||
34 | int rc = -EPROTONOSUPPORT; | ||
35 | |||
36 | if (net != &init_net) | ||
37 | return -EAFNOSUPPORT; | ||
38 | |||
39 | if (proto < 0 || proto >= NFC_SOCKPROTO_MAX) | ||
40 | return -EINVAL; | ||
41 | |||
42 | read_lock(&proto_tab_lock); | ||
43 | if (proto_tab[proto] && try_module_get(proto_tab[proto]->owner)) { | ||
44 | rc = proto_tab[proto]->create(net, sock, proto_tab[proto]); | ||
45 | module_put(proto_tab[proto]->owner); | ||
46 | } | ||
47 | read_unlock(&proto_tab_lock); | ||
48 | |||
49 | return rc; | ||
50 | } | ||
51 | |||
52 | static struct net_proto_family nfc_sock_family_ops = { | ||
53 | .owner = THIS_MODULE, | ||
54 | .family = PF_NFC, | ||
55 | .create = nfc_sock_create, | ||
56 | }; | ||
57 | |||
58 | int nfc_proto_register(const struct nfc_protocol *nfc_proto) | ||
59 | { | ||
60 | int rc; | ||
61 | |||
62 | if (nfc_proto->id < 0 || nfc_proto->id >= NFC_SOCKPROTO_MAX) | ||
63 | return -EINVAL; | ||
64 | |||
65 | rc = proto_register(nfc_proto->proto, 0); | ||
66 | if (rc) | ||
67 | return rc; | ||
68 | |||
69 | write_lock(&proto_tab_lock); | ||
70 | if (proto_tab[nfc_proto->id]) | ||
71 | rc = -EBUSY; | ||
72 | else | ||
73 | proto_tab[nfc_proto->id] = nfc_proto; | ||
74 | write_unlock(&proto_tab_lock); | ||
75 | |||
76 | return rc; | ||
77 | } | ||
78 | EXPORT_SYMBOL(nfc_proto_register); | ||
79 | |||
80 | void nfc_proto_unregister(const struct nfc_protocol *nfc_proto) | ||
81 | { | ||
82 | write_lock(&proto_tab_lock); | ||
83 | proto_tab[nfc_proto->id] = NULL; | ||
84 | write_unlock(&proto_tab_lock); | ||
85 | |||
86 | proto_unregister(nfc_proto->proto); | ||
87 | } | ||
88 | EXPORT_SYMBOL(nfc_proto_unregister); | ||
89 | |||
90 | int __init af_nfc_init(void) | ||
91 | { | ||
92 | return sock_register(&nfc_sock_family_ops); | ||
93 | } | ||
94 | |||
95 | void af_nfc_exit(void) | ||
96 | { | ||
97 | sock_unregister(PF_NFC); | ||
98 | } | ||
diff --git a/net/nfc/core.c b/net/nfc/core.c new file mode 100644 index 000000000000..b6fd4e1f2057 --- /dev/null +++ b/net/nfc/core.c | |||
@@ -0,0 +1,468 @@ | |||
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 | #include <linux/init.h> | ||
25 | #include <linux/kernel.h> | ||
26 | #include <linux/module.h> | ||
27 | #include <linux/slab.h> | ||
28 | |||
29 | #include "nfc.h" | ||
30 | |||
31 | #define VERSION "0.1" | ||
32 | |||
33 | int nfc_devlist_generation; | ||
34 | DEFINE_MUTEX(nfc_devlist_mutex); | ||
35 | |||
36 | int nfc_printk(const char *level, const char *format, ...) | ||
37 | { | ||
38 | struct va_format vaf; | ||
39 | va_list args; | ||
40 | int r; | ||
41 | |||
42 | va_start(args, format); | ||
43 | |||
44 | vaf.fmt = format; | ||
45 | vaf.va = &args; | ||
46 | |||
47 | r = printk("%sNFC: %pV\n", level, &vaf); | ||
48 | |||
49 | va_end(args); | ||
50 | |||
51 | return r; | ||
52 | } | ||
53 | EXPORT_SYMBOL(nfc_printk); | ||
54 | |||
55 | /** | ||
56 | * nfc_start_poll - start polling for nfc targets | ||
57 | * | ||
58 | * @dev: The nfc device that must start polling | ||
59 | * @protocols: bitset of nfc protocols that must be used for polling | ||
60 | * | ||
61 | * The device remains polling for targets until a target is found or | ||
62 | * the nfc_stop_poll function is called. | ||
63 | */ | ||
64 | int nfc_start_poll(struct nfc_dev *dev, u32 protocols) | ||
65 | { | ||
66 | int rc; | ||
67 | |||
68 | nfc_dbg("dev_name=%s protocols=0x%x", dev_name(&dev->dev), protocols); | ||
69 | |||
70 | if (!protocols) | ||
71 | return -EINVAL; | ||
72 | |||
73 | device_lock(&dev->dev); | ||
74 | |||
75 | if (!device_is_registered(&dev->dev)) { | ||
76 | rc = -ENODEV; | ||
77 | goto error; | ||
78 | } | ||
79 | |||
80 | if (dev->polling) { | ||
81 | rc = -EBUSY; | ||
82 | goto error; | ||
83 | } | ||
84 | |||
85 | rc = dev->ops->start_poll(dev, protocols); | ||
86 | if (!rc) | ||
87 | dev->polling = true; | ||
88 | |||
89 | error: | ||
90 | device_unlock(&dev->dev); | ||
91 | return rc; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * nfc_stop_poll - stop polling for nfc targets | ||
96 | * | ||
97 | * @dev: The nfc device that must stop polling | ||
98 | */ | ||
99 | int nfc_stop_poll(struct nfc_dev *dev) | ||
100 | { | ||
101 | int rc = 0; | ||
102 | |||
103 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | ||
104 | |||
105 | device_lock(&dev->dev); | ||
106 | |||
107 | if (!device_is_registered(&dev->dev)) { | ||
108 | rc = -ENODEV; | ||
109 | goto error; | ||
110 | } | ||
111 | |||
112 | if (!dev->polling) { | ||
113 | rc = -EINVAL; | ||
114 | goto error; | ||
115 | } | ||
116 | |||
117 | dev->ops->stop_poll(dev); | ||
118 | dev->polling = false; | ||
119 | |||
120 | error: | ||
121 | device_unlock(&dev->dev); | ||
122 | return rc; | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * nfc_activate_target - prepare the target for data exchange | ||
127 | * | ||
128 | * @dev: The nfc device that found the target | ||
129 | * @target_idx: index of the target that must be activated | ||
130 | * @protocol: nfc protocol that will be used for data exchange | ||
131 | */ | ||
132 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) | ||
133 | { | ||
134 | int rc; | ||
135 | |||
136 | nfc_dbg("dev_name=%s target_idx=%u protocol=%u", dev_name(&dev->dev), | ||
137 | target_idx, protocol); | ||
138 | |||
139 | device_lock(&dev->dev); | ||
140 | |||
141 | if (!device_is_registered(&dev->dev)) { | ||
142 | rc = -ENODEV; | ||
143 | goto error; | ||
144 | } | ||
145 | |||
146 | rc = dev->ops->activate_target(dev, target_idx, protocol); | ||
147 | |||
148 | error: | ||
149 | device_unlock(&dev->dev); | ||
150 | return rc; | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * nfc_deactivate_target - deactivate a nfc target | ||
155 | * | ||
156 | * @dev: The nfc device that found the target | ||
157 | * @target_idx: index of the target that must be deactivated | ||
158 | */ | ||
159 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) | ||
160 | { | ||
161 | int rc = 0; | ||
162 | |||
163 | nfc_dbg("dev_name=%s target_idx=%u", dev_name(&dev->dev), target_idx); | ||
164 | |||
165 | device_lock(&dev->dev); | ||
166 | |||
167 | if (!device_is_registered(&dev->dev)) { | ||
168 | rc = -ENODEV; | ||
169 | goto error; | ||
170 | } | ||
171 | |||
172 | dev->ops->deactivate_target(dev, target_idx); | ||
173 | |||
174 | error: | ||
175 | device_unlock(&dev->dev); | ||
176 | return rc; | ||
177 | } | ||
178 | |||
179 | /** | ||
180 | * nfc_data_exchange - transceive data | ||
181 | * | ||
182 | * @dev: The nfc device that found the target | ||
183 | * @target_idx: index of the target | ||
184 | * @skb: data to be sent | ||
185 | * @cb: callback called when the response is received | ||
186 | * @cb_context: parameter for the callback function | ||
187 | * | ||
188 | * The user must wait for the callback before calling this function again. | ||
189 | */ | ||
190 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, | ||
191 | struct sk_buff *skb, | ||
192 | data_exchange_cb_t cb, | ||
193 | void *cb_context) | ||
194 | { | ||
195 | int rc; | ||
196 | |||
197 | nfc_dbg("dev_name=%s target_idx=%u skb->len=%u", dev_name(&dev->dev), | ||
198 | target_idx, skb->len); | ||
199 | |||
200 | device_lock(&dev->dev); | ||
201 | |||
202 | if (!device_is_registered(&dev->dev)) { | ||
203 | rc = -ENODEV; | ||
204 | kfree_skb(skb); | ||
205 | goto error; | ||
206 | } | ||
207 | |||
208 | rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context); | ||
209 | |||
210 | error: | ||
211 | device_unlock(&dev->dev); | ||
212 | return rc; | ||
213 | } | ||
214 | |||
215 | /** | ||
216 | * nfc_alloc_skb - allocate a skb for data exchange responses | ||
217 | * | ||
218 | * @size: size to allocate | ||
219 | * @gfp: gfp flags | ||
220 | */ | ||
221 | struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp) | ||
222 | { | ||
223 | struct sk_buff *skb; | ||
224 | unsigned int total_size; | ||
225 | |||
226 | total_size = size + 1; | ||
227 | skb = alloc_skb(total_size, gfp); | ||
228 | |||
229 | if (skb) | ||
230 | skb_reserve(skb, 1); | ||
231 | |||
232 | return skb; | ||
233 | } | ||
234 | EXPORT_SYMBOL(nfc_alloc_skb); | ||
235 | |||
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 | |||
282 | static void nfc_release(struct device *d) | ||
283 | { | ||
284 | struct nfc_dev *dev = to_nfc_dev(d); | ||
285 | |||
286 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | ||
287 | |||
288 | nfc_genl_data_exit(&dev->genl_data); | ||
289 | kfree(dev->targets); | ||
290 | kfree(dev); | ||
291 | } | ||
292 | |||
293 | struct class nfc_class = { | ||
294 | .name = "nfc", | ||
295 | .dev_release = nfc_release, | ||
296 | }; | ||
297 | EXPORT_SYMBOL(nfc_class); | ||
298 | |||
299 | static int match_idx(struct device *d, void *data) | ||
300 | { | ||
301 | struct nfc_dev *dev = to_nfc_dev(d); | ||
302 | unsigned *idx = data; | ||
303 | |||
304 | return dev->idx == *idx; | ||
305 | } | ||
306 | |||
307 | struct nfc_dev *nfc_get_device(unsigned idx) | ||
308 | { | ||
309 | struct device *d; | ||
310 | |||
311 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); | ||
312 | if (!d) | ||
313 | return NULL; | ||
314 | |||
315 | return to_nfc_dev(d); | ||
316 | } | ||
317 | |||
318 | /** | ||
319 | * nfc_allocate_device - allocate a new nfc device | ||
320 | * | ||
321 | * @ops: device operations | ||
322 | * @supported_protocols: NFC protocols supported by the device | ||
323 | */ | ||
324 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, | ||
325 | u32 supported_protocols) | ||
326 | { | ||
327 | static atomic_t dev_no = ATOMIC_INIT(0); | ||
328 | struct nfc_dev *dev; | ||
329 | |||
330 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || | ||
331 | !ops->deactivate_target || !ops->data_exchange) | ||
332 | return NULL; | ||
333 | |||
334 | if (!supported_protocols) | ||
335 | return NULL; | ||
336 | |||
337 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); | ||
338 | if (!dev) | ||
339 | return NULL; | ||
340 | |||
341 | dev->dev.class = &nfc_class; | ||
342 | dev->idx = atomic_inc_return(&dev_no) - 1; | ||
343 | dev_set_name(&dev->dev, "nfc%d", dev->idx); | ||
344 | device_initialize(&dev->dev); | ||
345 | |||
346 | dev->ops = ops; | ||
347 | dev->supported_protocols = supported_protocols; | ||
348 | |||
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 | |||
355 | return dev; | ||
356 | } | ||
357 | EXPORT_SYMBOL(nfc_allocate_device); | ||
358 | |||
359 | /** | ||
360 | * nfc_register_device - register a nfc device in the nfc subsystem | ||
361 | * | ||
362 | * @dev: The nfc device to register | ||
363 | */ | ||
364 | int nfc_register_device(struct nfc_dev *dev) | ||
365 | { | ||
366 | int rc; | ||
367 | |||
368 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | ||
369 | |||
370 | mutex_lock(&nfc_devlist_mutex); | ||
371 | nfc_devlist_generation++; | ||
372 | rc = device_add(&dev->dev); | ||
373 | mutex_unlock(&nfc_devlist_mutex); | ||
374 | |||
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; | ||
385 | } | ||
386 | EXPORT_SYMBOL(nfc_register_device); | ||
387 | |||
388 | /** | ||
389 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem | ||
390 | * | ||
391 | * @dev: The nfc device to unregister | ||
392 | */ | ||
393 | void nfc_unregister_device(struct nfc_dev *dev) | ||
394 | { | ||
395 | int rc; | ||
396 | |||
397 | nfc_dbg("dev_name=%s", dev_name(&dev->dev)); | ||
398 | |||
399 | mutex_lock(&nfc_devlist_mutex); | ||
400 | nfc_devlist_generation++; | ||
401 | |||
402 | /* lock to avoid unregistering a device while an operation | ||
403 | is in progress */ | ||
404 | device_lock(&dev->dev); | ||
405 | device_del(&dev->dev); | ||
406 | device_unlock(&dev->dev); | ||
407 | |||
408 | 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 | |||
415 | } | ||
416 | EXPORT_SYMBOL(nfc_unregister_device); | ||
417 | |||
418 | static int __init nfc_init(void) | ||
419 | { | ||
420 | int rc; | ||
421 | |||
422 | nfc_info("NFC Core ver %s", VERSION); | ||
423 | |||
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 | rc = rawsock_init(); | ||
436 | if (rc) | ||
437 | goto err_rawsock; | ||
438 | |||
439 | rc = af_nfc_init(); | ||
440 | if (rc) | ||
441 | goto err_af_nfc; | ||
442 | |||
443 | return 0; | ||
444 | |||
445 | err_af_nfc: | ||
446 | rawsock_exit(); | ||
447 | err_rawsock: | ||
448 | nfc_genl_exit(); | ||
449 | err_genl: | ||
450 | class_unregister(&nfc_class); | ||
451 | return rc; | ||
452 | } | ||
453 | |||
454 | static void __exit nfc_exit(void) | ||
455 | { | ||
456 | af_nfc_exit(); | ||
457 | rawsock_exit(); | ||
458 | nfc_genl_exit(); | ||
459 | class_unregister(&nfc_class); | ||
460 | } | ||
461 | |||
462 | subsys_initcall(nfc_init); | ||
463 | module_exit(nfc_exit); | ||
464 | |||
465 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); | ||
466 | MODULE_DESCRIPTION("NFC Core ver " VERSION); | ||
467 | MODULE_VERSION(VERSION); | ||
468 | MODULE_LICENSE("GPL"); | ||
diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c new file mode 100644 index 000000000000..ccdff7953f7d --- /dev/null +++ b/net/nfc/netlink.c | |||
@@ -0,0 +1,537 @@ | |||
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 | #include <net/genetlink.h> | ||
25 | #include <linux/nfc.h> | ||
26 | #include <linux/slab.h> | ||
27 | |||
28 | #include "nfc.h" | ||
29 | |||
30 | static struct genl_multicast_group nfc_genl_event_mcgrp = { | ||
31 | .name = NFC_GENL_MCAST_EVENT_NAME, | ||
32 | }; | ||
33 | |||
34 | struct genl_family nfc_genl_family = { | ||
35 | .id = GENL_ID_GENERATE, | ||
36 | .hdrsize = 0, | ||
37 | .name = NFC_GENL_NAME, | ||
38 | .version = NFC_GENL_VERSION, | ||
39 | .maxattr = NFC_ATTR_MAX, | ||
40 | }; | ||
41 | |||
42 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { | ||
43 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, | ||
44 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, | ||
45 | .len = NFC_DEVICE_NAME_MAXSIZE }, | ||
46 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, | ||
47 | }; | ||
48 | |||
49 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, | ||
50 | struct netlink_callback *cb, int flags) | ||
51 | { | ||
52 | void *hdr; | ||
53 | |||
54 | nfc_dbg("entry"); | ||
55 | |||
56 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, | ||
57 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); | ||
58 | if (!hdr) | ||
59 | return -EMSGSIZE; | ||
60 | |||
61 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | ||
62 | |||
63 | NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx); | ||
64 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, | ||
65 | target->supported_protocols); | ||
66 | NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res); | ||
67 | NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res); | ||
68 | |||
69 | return genlmsg_end(msg, hdr); | ||
70 | |||
71 | nla_put_failure: | ||
72 | genlmsg_cancel(msg, hdr); | ||
73 | return -EMSGSIZE; | ||
74 | } | ||
75 | |||
76 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) | ||
77 | { | ||
78 | struct nfc_dev *dev; | ||
79 | int rc; | ||
80 | u32 idx; | ||
81 | |||
82 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, | ||
83 | nfc_genl_family.attrbuf, | ||
84 | nfc_genl_family.maxattr, | ||
85 | nfc_genl_policy); | ||
86 | if (rc < 0) | ||
87 | return ERR_PTR(rc); | ||
88 | |||
89 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) | ||
90 | return ERR_PTR(-EINVAL); | ||
91 | |||
92 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); | ||
93 | |||
94 | dev = nfc_get_device(idx); | ||
95 | if (!dev) | ||
96 | return ERR_PTR(-ENODEV); | ||
97 | |||
98 | return dev; | ||
99 | } | ||
100 | |||
101 | static int nfc_genl_dump_targets(struct sk_buff *skb, | ||
102 | struct netlink_callback *cb) | ||
103 | { | ||
104 | int i = cb->args[0]; | ||
105 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | ||
106 | int rc; | ||
107 | |||
108 | nfc_dbg("entry"); | ||
109 | |||
110 | if (!dev) { | ||
111 | dev = __get_device_from_cb(cb); | ||
112 | if (IS_ERR(dev)) | ||
113 | return PTR_ERR(dev); | ||
114 | |||
115 | cb->args[1] = (long) dev; | ||
116 | } | ||
117 | |||
118 | spin_lock_bh(&dev->targets_lock); | ||
119 | |||
120 | cb->seq = dev->targets_generation; | ||
121 | |||
122 | while (i < dev->n_targets) { | ||
123 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, | ||
124 | NLM_F_MULTI); | ||
125 | if (rc < 0) | ||
126 | break; | ||
127 | |||
128 | i++; | ||
129 | } | ||
130 | |||
131 | spin_unlock_bh(&dev->targets_lock); | ||
132 | |||
133 | cb->args[0] = i; | ||
134 | |||
135 | return skb->len; | ||
136 | } | ||
137 | |||
138 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) | ||
139 | { | ||
140 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | ||
141 | |||
142 | nfc_dbg("entry"); | ||
143 | |||
144 | if (dev) | ||
145 | nfc_put_device(dev); | ||
146 | |||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | int nfc_genl_targets_found(struct nfc_dev *dev) | ||
151 | { | ||
152 | struct sk_buff *msg; | ||
153 | void *hdr; | ||
154 | |||
155 | nfc_dbg("entry"); | ||
156 | |||
157 | dev->genl_data.poll_req_pid = 0; | ||
158 | |||
159 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); | ||
160 | if (!msg) | ||
161 | return -ENOMEM; | ||
162 | |||
163 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | ||
164 | NFC_EVENT_TARGETS_FOUND); | ||
165 | if (!hdr) | ||
166 | goto free_msg; | ||
167 | |||
168 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | ||
169 | |||
170 | genlmsg_end(msg, hdr); | ||
171 | |||
172 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | ||
173 | |||
174 | nla_put_failure: | ||
175 | genlmsg_cancel(msg, hdr); | ||
176 | free_msg: | ||
177 | nlmsg_free(msg); | ||
178 | return -EMSGSIZE; | ||
179 | } | ||
180 | |||
181 | int nfc_genl_device_added(struct nfc_dev *dev) | ||
182 | { | ||
183 | struct sk_buff *msg; | ||
184 | void *hdr; | ||
185 | |||
186 | nfc_dbg("entry"); | ||
187 | |||
188 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | ||
189 | if (!msg) | ||
190 | return -ENOMEM; | ||
191 | |||
192 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | ||
193 | NFC_EVENT_DEVICE_ADDED); | ||
194 | if (!hdr) | ||
195 | goto free_msg; | ||
196 | |||
197 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); | ||
198 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | ||
199 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); | ||
200 | |||
201 | genlmsg_end(msg, hdr); | ||
202 | |||
203 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | ||
204 | |||
205 | return 0; | ||
206 | |||
207 | nla_put_failure: | ||
208 | genlmsg_cancel(msg, hdr); | ||
209 | free_msg: | ||
210 | nlmsg_free(msg); | ||
211 | return -EMSGSIZE; | ||
212 | } | ||
213 | |||
214 | int nfc_genl_device_removed(struct nfc_dev *dev) | ||
215 | { | ||
216 | struct sk_buff *msg; | ||
217 | void *hdr; | ||
218 | |||
219 | nfc_dbg("entry"); | ||
220 | |||
221 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | ||
222 | if (!msg) | ||
223 | return -ENOMEM; | ||
224 | |||
225 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | ||
226 | NFC_EVENT_DEVICE_REMOVED); | ||
227 | if (!hdr) | ||
228 | goto free_msg; | ||
229 | |||
230 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | ||
231 | |||
232 | genlmsg_end(msg, hdr); | ||
233 | |||
234 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | ||
235 | |||
236 | return 0; | ||
237 | |||
238 | nla_put_failure: | ||
239 | genlmsg_cancel(msg, hdr); | ||
240 | free_msg: | ||
241 | nlmsg_free(msg); | ||
242 | return -EMSGSIZE; | ||
243 | } | ||
244 | |||
245 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, | ||
246 | u32 pid, u32 seq, | ||
247 | struct netlink_callback *cb, | ||
248 | int flags) | ||
249 | { | ||
250 | void *hdr; | ||
251 | |||
252 | nfc_dbg("entry"); | ||
253 | |||
254 | hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags, | ||
255 | NFC_CMD_GET_DEVICE); | ||
256 | if (!hdr) | ||
257 | return -EMSGSIZE; | ||
258 | |||
259 | if (cb) | ||
260 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | ||
261 | |||
262 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); | ||
263 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | ||
264 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); | ||
265 | |||
266 | return genlmsg_end(msg, hdr); | ||
267 | |||
268 | nla_put_failure: | ||
269 | genlmsg_cancel(msg, hdr); | ||
270 | return -EMSGSIZE; | ||
271 | } | ||
272 | |||
273 | static int nfc_genl_dump_devices(struct sk_buff *skb, | ||
274 | struct netlink_callback *cb) | ||
275 | { | ||
276 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | ||
277 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | ||
278 | bool first_call = false; | ||
279 | |||
280 | nfc_dbg("entry"); | ||
281 | |||
282 | if (!iter) { | ||
283 | first_call = true; | ||
284 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | ||
285 | if (!iter) | ||
286 | return -ENOMEM; | ||
287 | cb->args[0] = (long) iter; | ||
288 | } | ||
289 | |||
290 | mutex_lock(&nfc_devlist_mutex); | ||
291 | |||
292 | cb->seq = nfc_devlist_generation; | ||
293 | |||
294 | if (first_call) { | ||
295 | nfc_device_iter_init(iter); | ||
296 | dev = nfc_device_iter_next(iter); | ||
297 | } | ||
298 | |||
299 | while (dev) { | ||
300 | int rc; | ||
301 | |||
302 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid, | ||
303 | cb->nlh->nlmsg_seq, | ||
304 | cb, NLM_F_MULTI); | ||
305 | if (rc < 0) | ||
306 | break; | ||
307 | |||
308 | dev = nfc_device_iter_next(iter); | ||
309 | } | ||
310 | |||
311 | mutex_unlock(&nfc_devlist_mutex); | ||
312 | |||
313 | cb->args[1] = (long) dev; | ||
314 | |||
315 | return skb->len; | ||
316 | } | ||
317 | |||
318 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) | ||
319 | { | ||
320 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | ||
321 | |||
322 | nfc_dbg("entry"); | ||
323 | |||
324 | nfc_device_iter_exit(iter); | ||
325 | kfree(iter); | ||
326 | |||
327 | return 0; | ||
328 | } | ||
329 | |||
330 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) | ||
331 | { | ||
332 | struct sk_buff *msg; | ||
333 | struct nfc_dev *dev; | ||
334 | u32 idx; | ||
335 | int rc = -ENOBUFS; | ||
336 | |||
337 | nfc_dbg("entry"); | ||
338 | |||
339 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | ||
340 | return -EINVAL; | ||
341 | |||
342 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | ||
343 | |||
344 | dev = nfc_get_device(idx); | ||
345 | if (!dev) | ||
346 | return -ENODEV; | ||
347 | |||
348 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | ||
349 | if (!msg) { | ||
350 | rc = -ENOMEM; | ||
351 | goto out_putdev; | ||
352 | } | ||
353 | |||
354 | rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq, | ||
355 | NULL, 0); | ||
356 | if (rc < 0) | ||
357 | goto out_free; | ||
358 | |||
359 | nfc_put_device(dev); | ||
360 | |||
361 | return genlmsg_reply(msg, info); | ||
362 | |||
363 | out_free: | ||
364 | nlmsg_free(msg); | ||
365 | out_putdev: | ||
366 | nfc_put_device(dev); | ||
367 | return rc; | ||
368 | } | ||
369 | |||
370 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) | ||
371 | { | ||
372 | struct nfc_dev *dev; | ||
373 | int rc; | ||
374 | u32 idx; | ||
375 | u32 protocols; | ||
376 | |||
377 | nfc_dbg("entry"); | ||
378 | |||
379 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | ||
380 | !info->attrs[NFC_ATTR_PROTOCOLS]) | ||
381 | return -EINVAL; | ||
382 | |||
383 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | ||
384 | protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); | ||
385 | |||
386 | dev = nfc_get_device(idx); | ||
387 | if (!dev) | ||
388 | return -ENODEV; | ||
389 | |||
390 | mutex_lock(&dev->genl_data.genl_data_mutex); | ||
391 | |||
392 | rc = nfc_start_poll(dev, protocols); | ||
393 | if (!rc) | ||
394 | dev->genl_data.poll_req_pid = info->snd_pid; | ||
395 | |||
396 | mutex_unlock(&dev->genl_data.genl_data_mutex); | ||
397 | |||
398 | nfc_put_device(dev); | ||
399 | return rc; | ||
400 | } | ||
401 | |||
402 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) | ||
403 | { | ||
404 | struct nfc_dev *dev; | ||
405 | int rc; | ||
406 | u32 idx; | ||
407 | |||
408 | nfc_dbg("entry"); | ||
409 | |||
410 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | ||
411 | return -EINVAL; | ||
412 | |||
413 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | ||
414 | |||
415 | dev = nfc_get_device(idx); | ||
416 | if (!dev) | ||
417 | return -ENODEV; | ||
418 | |||
419 | mutex_lock(&dev->genl_data.genl_data_mutex); | ||
420 | |||
421 | if (dev->genl_data.poll_req_pid != info->snd_pid) { | ||
422 | rc = -EBUSY; | ||
423 | goto out; | ||
424 | } | ||
425 | |||
426 | rc = nfc_stop_poll(dev); | ||
427 | dev->genl_data.poll_req_pid = 0; | ||
428 | |||
429 | out: | ||
430 | mutex_unlock(&dev->genl_data.genl_data_mutex); | ||
431 | nfc_put_device(dev); | ||
432 | return rc; | ||
433 | } | ||
434 | |||
435 | static struct genl_ops nfc_genl_ops[] = { | ||
436 | { | ||
437 | .cmd = NFC_CMD_GET_DEVICE, | ||
438 | .doit = nfc_genl_get_device, | ||
439 | .dumpit = nfc_genl_dump_devices, | ||
440 | .done = nfc_genl_dump_devices_done, | ||
441 | .policy = nfc_genl_policy, | ||
442 | }, | ||
443 | { | ||
444 | .cmd = NFC_CMD_START_POLL, | ||
445 | .doit = nfc_genl_start_poll, | ||
446 | .policy = nfc_genl_policy, | ||
447 | }, | ||
448 | { | ||
449 | .cmd = NFC_CMD_STOP_POLL, | ||
450 | .doit = nfc_genl_stop_poll, | ||
451 | .policy = nfc_genl_policy, | ||
452 | }, | ||
453 | { | ||
454 | .cmd = NFC_CMD_GET_TARGET, | ||
455 | .dumpit = nfc_genl_dump_targets, | ||
456 | .done = nfc_genl_dump_targets_done, | ||
457 | .policy = nfc_genl_policy, | ||
458 | }, | ||
459 | }; | ||
460 | |||
461 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, | ||
462 | unsigned long event, void *ptr) | ||
463 | { | ||
464 | struct netlink_notify *n = ptr; | ||
465 | struct class_dev_iter iter; | ||
466 | struct nfc_dev *dev; | ||
467 | |||
468 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) | ||
469 | goto out; | ||
470 | |||
471 | nfc_dbg("NETLINK_URELEASE event from id %d", n->pid); | ||
472 | |||
473 | nfc_device_iter_init(&iter); | ||
474 | dev = nfc_device_iter_next(&iter); | ||
475 | |||
476 | while (dev) { | ||
477 | mutex_lock(&dev->genl_data.genl_data_mutex); | ||
478 | if (dev->genl_data.poll_req_pid == n->pid) { | ||
479 | nfc_stop_poll(dev); | ||
480 | dev->genl_data.poll_req_pid = 0; | ||
481 | } | ||
482 | mutex_unlock(&dev->genl_data.genl_data_mutex); | ||
483 | dev = nfc_device_iter_next(&iter); | ||
484 | } | ||
485 | |||
486 | nfc_device_iter_exit(&iter); | ||
487 | |||
488 | out: | ||
489 | return NOTIFY_DONE; | ||
490 | } | ||
491 | |||
492 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) | ||
493 | { | ||
494 | genl_data->poll_req_pid = 0; | ||
495 | mutex_init(&genl_data->genl_data_mutex); | ||
496 | } | ||
497 | |||
498 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) | ||
499 | { | ||
500 | mutex_destroy(&genl_data->genl_data_mutex); | ||
501 | } | ||
502 | |||
503 | static struct notifier_block nl_notifier = { | ||
504 | .notifier_call = nfc_genl_rcv_nl_event, | ||
505 | }; | ||
506 | |||
507 | /** | ||
508 | * nfc_genl_init() - Initialize netlink interface | ||
509 | * | ||
510 | * This initialization function registers the nfc netlink family. | ||
511 | */ | ||
512 | int __init nfc_genl_init(void) | ||
513 | { | ||
514 | int rc; | ||
515 | |||
516 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, | ||
517 | ARRAY_SIZE(nfc_genl_ops)); | ||
518 | if (rc) | ||
519 | return rc; | ||
520 | |||
521 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); | ||
522 | |||
523 | netlink_register_notifier(&nl_notifier); | ||
524 | |||
525 | return rc; | ||
526 | } | ||
527 | |||
528 | /** | ||
529 | * nfc_genl_exit() - Deinitialize netlink interface | ||
530 | * | ||
531 | * This exit function unregisters the nfc netlink family. | ||
532 | */ | ||
533 | void nfc_genl_exit(void) | ||
534 | { | ||
535 | netlink_unregister_notifier(&nl_notifier); | ||
536 | genl_unregister_family(&nfc_genl_family); | ||
537 | } | ||
diff --git a/net/nfc/nfc.h b/net/nfc/nfc.h new file mode 100644 index 000000000000..aaf9832298f3 --- /dev/null +++ b/net/nfc/nfc.h | |||
@@ -0,0 +1,117 @@ | |||
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 __LOCAL_NFC_H | ||
25 | #define __LOCAL_NFC_H | ||
26 | |||
27 | #include <net/nfc.h> | ||
28 | #include <net/sock.h> | ||
29 | |||
30 | __attribute__((format (printf, 2, 3))) | ||
31 | int nfc_printk(const char *level, const char *fmt, ...); | ||
32 | |||
33 | #define nfc_info(fmt, arg...) nfc_printk(KERN_INFO, fmt, ##arg) | ||
34 | #define nfc_err(fmt, arg...) nfc_printk(KERN_ERR, fmt, ##arg) | ||
35 | #define nfc_dbg(fmt, arg...) pr_debug(fmt "\n", ##arg) | ||
36 | |||
37 | struct nfc_protocol { | ||
38 | int id; | ||
39 | struct proto *proto; | ||
40 | struct module *owner; | ||
41 | int (*create)(struct net *net, struct socket *sock, | ||
42 | const struct nfc_protocol *nfc_proto); | ||
43 | }; | ||
44 | |||
45 | struct nfc_rawsock { | ||
46 | struct sock sk; | ||
47 | struct nfc_dev *dev; | ||
48 | u32 target_idx; | ||
49 | struct work_struct tx_work; | ||
50 | bool tx_work_scheduled; | ||
51 | }; | ||
52 | #define nfc_rawsock(sk) ((struct nfc_rawsock *) sk) | ||
53 | #define to_rawsock_sk(_tx_work) \ | ||
54 | ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work)) | ||
55 | |||
56 | int __init rawsock_init(void); | ||
57 | void rawsock_exit(void); | ||
58 | |||
59 | int __init af_nfc_init(void); | ||
60 | void af_nfc_exit(void); | ||
61 | int nfc_proto_register(const struct nfc_protocol *nfc_proto); | ||
62 | void nfc_proto_unregister(const struct nfc_protocol *nfc_proto); | ||
63 | |||
64 | extern int nfc_devlist_generation; | ||
65 | extern struct mutex nfc_devlist_mutex; | ||
66 | |||
67 | int __init nfc_genl_init(void); | ||
68 | void nfc_genl_exit(void); | ||
69 | |||
70 | void nfc_genl_data_init(struct nfc_genl_data *genl_data); | ||
71 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data); | ||
72 | |||
73 | int nfc_genl_targets_found(struct nfc_dev *dev); | ||
74 | |||
75 | int nfc_genl_device_added(struct nfc_dev *dev); | ||
76 | int nfc_genl_device_removed(struct nfc_dev *dev); | ||
77 | |||
78 | struct nfc_dev *nfc_get_device(unsigned idx); | ||
79 | |||
80 | static inline void nfc_put_device(struct nfc_dev *dev) | ||
81 | { | ||
82 | put_device(&dev->dev); | ||
83 | } | ||
84 | |||
85 | static inline void nfc_device_iter_init(struct class_dev_iter *iter) | ||
86 | { | ||
87 | class_dev_iter_init(iter, &nfc_class, NULL, NULL); | ||
88 | } | ||
89 | |||
90 | static inline struct nfc_dev *nfc_device_iter_next(struct class_dev_iter *iter) | ||
91 | { | ||
92 | struct device *d = class_dev_iter_next(iter); | ||
93 | if (!d) | ||
94 | return NULL; | ||
95 | |||
96 | return to_nfc_dev(d); | ||
97 | } | ||
98 | |||
99 | static inline void nfc_device_iter_exit(struct class_dev_iter *iter) | ||
100 | { | ||
101 | class_dev_iter_exit(iter); | ||
102 | } | ||
103 | |||
104 | int nfc_start_poll(struct nfc_dev *dev, u32 protocols); | ||
105 | |||
106 | int nfc_stop_poll(struct nfc_dev *dev); | ||
107 | |||
108 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol); | ||
109 | |||
110 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx); | ||
111 | |||
112 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, | ||
113 | struct sk_buff *skb, | ||
114 | data_exchange_cb_t cb, | ||
115 | void *cb_context); | ||
116 | |||
117 | #endif /* __LOCAL_NFC_H */ | ||
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c new file mode 100644 index 000000000000..52de84a55115 --- /dev/null +++ b/net/nfc/rawsock.c | |||
@@ -0,0 +1,354 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia | ||
3 | * | ||
4 | * Authors: | ||
5 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> | ||
6 | * Lauro Ramos Venancio <lauro.venancio@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 | #include <net/tcp_states.h> | ||
25 | #include <linux/nfc.h> | ||
26 | |||
27 | #include "nfc.h" | ||
28 | |||
29 | static void rawsock_write_queue_purge(struct sock *sk) | ||
30 | { | ||
31 | nfc_dbg("sk=%p", sk); | ||
32 | |||
33 | spin_lock_bh(&sk->sk_write_queue.lock); | ||
34 | __skb_queue_purge(&sk->sk_write_queue); | ||
35 | nfc_rawsock(sk)->tx_work_scheduled = false; | ||
36 | spin_unlock_bh(&sk->sk_write_queue.lock); | ||
37 | } | ||
38 | |||
39 | static void rawsock_report_error(struct sock *sk, int err) | ||
40 | { | ||
41 | nfc_dbg("sk=%p err=%d", sk, err); | ||
42 | |||
43 | sk->sk_shutdown = SHUTDOWN_MASK; | ||
44 | sk->sk_err = -err; | ||
45 | sk->sk_error_report(sk); | ||
46 | |||
47 | rawsock_write_queue_purge(sk); | ||
48 | } | ||
49 | |||
50 | static int rawsock_release(struct socket *sock) | ||
51 | { | ||
52 | struct sock *sk = sock->sk; | ||
53 | |||
54 | nfc_dbg("sock=%p", sock); | ||
55 | |||
56 | sock_orphan(sk); | ||
57 | sock_put(sk); | ||
58 | |||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | static int rawsock_connect(struct socket *sock, struct sockaddr *_addr, | ||
63 | int len, int flags) | ||
64 | { | ||
65 | struct sock *sk = sock->sk; | ||
66 | struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr; | ||
67 | struct nfc_dev *dev; | ||
68 | int rc = 0; | ||
69 | |||
70 | nfc_dbg("sock=%p sk=%p flags=%d", sock, sk, flags); | ||
71 | |||
72 | if (!addr || len < sizeof(struct sockaddr_nfc) || | ||
73 | addr->sa_family != AF_NFC) | ||
74 | return -EINVAL; | ||
75 | |||
76 | nfc_dbg("addr dev_idx=%u target_idx=%u protocol=%u", addr->dev_idx, | ||
77 | addr->target_idx, addr->nfc_protocol); | ||
78 | |||
79 | lock_sock(sk); | ||
80 | |||
81 | if (sock->state == SS_CONNECTED) { | ||
82 | rc = -EISCONN; | ||
83 | goto error; | ||
84 | } | ||
85 | |||
86 | dev = nfc_get_device(addr->dev_idx); | ||
87 | if (!dev) { | ||
88 | rc = -ENODEV; | ||
89 | goto error; | ||
90 | } | ||
91 | |||
92 | if (addr->target_idx > dev->target_idx - 1 || | ||
93 | addr->target_idx < dev->target_idx - dev->n_targets) { | ||
94 | rc = -EINVAL; | ||
95 | goto error; | ||
96 | } | ||
97 | |||
98 | if (addr->target_idx > dev->target_idx - 1 || | ||
99 | addr->target_idx < dev->target_idx - dev->n_targets) { | ||
100 | rc = -EINVAL; | ||
101 | goto error; | ||
102 | } | ||
103 | |||
104 | rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol); | ||
105 | if (rc) | ||
106 | goto put_dev; | ||
107 | |||
108 | nfc_rawsock(sk)->dev = dev; | ||
109 | nfc_rawsock(sk)->target_idx = addr->target_idx; | ||
110 | sock->state = SS_CONNECTED; | ||
111 | sk->sk_state = TCP_ESTABLISHED; | ||
112 | sk->sk_state_change(sk); | ||
113 | |||
114 | release_sock(sk); | ||
115 | return 0; | ||
116 | |||
117 | put_dev: | ||
118 | nfc_put_device(dev); | ||
119 | error: | ||
120 | release_sock(sk); | ||
121 | return rc; | ||
122 | } | ||
123 | |||
124 | static int rawsock_add_header(struct sk_buff *skb) | ||
125 | { | ||
126 | |||
127 | if (skb_cow_head(skb, 1)) | ||
128 | return -ENOMEM; | ||
129 | |||
130 | *skb_push(skb, 1) = 0; | ||
131 | |||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb, | ||
136 | int err) | ||
137 | { | ||
138 | struct sock *sk = (struct sock *) context; | ||
139 | |||
140 | BUG_ON(in_irq()); | ||
141 | |||
142 | nfc_dbg("sk=%p err=%d", sk, err); | ||
143 | |||
144 | if (err) | ||
145 | goto error; | ||
146 | |||
147 | err = rawsock_add_header(skb); | ||
148 | if (err) | ||
149 | goto error; | ||
150 | |||
151 | err = sock_queue_rcv_skb(sk, skb); | ||
152 | if (err) | ||
153 | goto error; | ||
154 | |||
155 | spin_lock_bh(&sk->sk_write_queue.lock); | ||
156 | if (!skb_queue_empty(&sk->sk_write_queue)) | ||
157 | schedule_work(&nfc_rawsock(sk)->tx_work); | ||
158 | else | ||
159 | nfc_rawsock(sk)->tx_work_scheduled = false; | ||
160 | spin_unlock_bh(&sk->sk_write_queue.lock); | ||
161 | |||
162 | sock_put(sk); | ||
163 | return; | ||
164 | |||
165 | error: | ||
166 | rawsock_report_error(sk, err); | ||
167 | sock_put(sk); | ||
168 | } | ||
169 | |||
170 | static void rawsock_tx_work(struct work_struct *work) | ||
171 | { | ||
172 | struct sock *sk = to_rawsock_sk(work); | ||
173 | struct nfc_dev *dev = nfc_rawsock(sk)->dev; | ||
174 | u32 target_idx = nfc_rawsock(sk)->target_idx; | ||
175 | struct sk_buff *skb; | ||
176 | int rc; | ||
177 | |||
178 | nfc_dbg("sk=%p target_idx=%u", sk, target_idx); | ||
179 | |||
180 | if (sk->sk_shutdown & SEND_SHUTDOWN) { | ||
181 | rawsock_write_queue_purge(sk); | ||
182 | return; | ||
183 | } | ||
184 | |||
185 | skb = skb_dequeue(&sk->sk_write_queue); | ||
186 | |||
187 | sock_hold(sk); | ||
188 | rc = nfc_data_exchange(dev, target_idx, skb, | ||
189 | rawsock_data_exchange_complete, sk); | ||
190 | if (rc) { | ||
191 | rawsock_report_error(sk, rc); | ||
192 | sock_put(sk); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock, | ||
197 | struct msghdr *msg, size_t len) | ||
198 | { | ||
199 | struct sock *sk = sock->sk; | ||
200 | struct sk_buff *skb; | ||
201 | int rc; | ||
202 | |||
203 | nfc_dbg("sock=%p sk=%p len=%zu", sock, sk, len); | ||
204 | |||
205 | if (msg->msg_namelen) | ||
206 | return -EOPNOTSUPP; | ||
207 | |||
208 | if (sock->state != SS_CONNECTED) | ||
209 | return -ENOTCONN; | ||
210 | |||
211 | skb = sock_alloc_send_skb(sk, len, msg->msg_flags & MSG_DONTWAIT, | ||
212 | &rc); | ||
213 | if (!skb) | ||
214 | return rc; | ||
215 | |||
216 | rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); | ||
217 | if (rc < 0) { | ||
218 | kfree_skb(skb); | ||
219 | return rc; | ||
220 | } | ||
221 | |||
222 | spin_lock_bh(&sk->sk_write_queue.lock); | ||
223 | __skb_queue_tail(&sk->sk_write_queue, skb); | ||
224 | if (!nfc_rawsock(sk)->tx_work_scheduled) { | ||
225 | schedule_work(&nfc_rawsock(sk)->tx_work); | ||
226 | nfc_rawsock(sk)->tx_work_scheduled = true; | ||
227 | } | ||
228 | spin_unlock_bh(&sk->sk_write_queue.lock); | ||
229 | |||
230 | return len; | ||
231 | } | ||
232 | |||
233 | static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock, | ||
234 | struct msghdr *msg, size_t len, int flags) | ||
235 | { | ||
236 | int noblock = flags & MSG_DONTWAIT; | ||
237 | struct sock *sk = sock->sk; | ||
238 | struct sk_buff *skb; | ||
239 | int copied; | ||
240 | int rc; | ||
241 | |||
242 | nfc_dbg("sock=%p sk=%p len=%zu flags=%d", sock, sk, len, flags); | ||
243 | |||
244 | skb = skb_recv_datagram(sk, flags, noblock, &rc); | ||
245 | if (!skb) | ||
246 | return rc; | ||
247 | |||
248 | msg->msg_namelen = 0; | ||
249 | |||
250 | copied = skb->len; | ||
251 | if (len < copied) { | ||
252 | msg->msg_flags |= MSG_TRUNC; | ||
253 | copied = len; | ||
254 | } | ||
255 | |||
256 | rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | ||
257 | |||
258 | skb_free_datagram(sk, skb); | ||
259 | |||
260 | return rc ? : copied; | ||
261 | } | ||
262 | |||
263 | |||
264 | static const struct proto_ops rawsock_ops = { | ||
265 | .family = PF_NFC, | ||
266 | .owner = THIS_MODULE, | ||
267 | .release = rawsock_release, | ||
268 | .bind = sock_no_bind, | ||
269 | .connect = rawsock_connect, | ||
270 | .socketpair = sock_no_socketpair, | ||
271 | .accept = sock_no_accept, | ||
272 | .getname = sock_no_getname, | ||
273 | .poll = datagram_poll, | ||
274 | .ioctl = sock_no_ioctl, | ||
275 | .listen = sock_no_listen, | ||
276 | .shutdown = sock_no_shutdown, | ||
277 | .setsockopt = sock_no_setsockopt, | ||
278 | .getsockopt = sock_no_getsockopt, | ||
279 | .sendmsg = rawsock_sendmsg, | ||
280 | .recvmsg = rawsock_recvmsg, | ||
281 | .mmap = sock_no_mmap, | ||
282 | }; | ||
283 | |||
284 | static void rawsock_destruct(struct sock *sk) | ||
285 | { | ||
286 | nfc_dbg("sk=%p", sk); | ||
287 | |||
288 | if (sk->sk_state == TCP_ESTABLISHED) { | ||
289 | nfc_deactivate_target(nfc_rawsock(sk)->dev, | ||
290 | nfc_rawsock(sk)->target_idx); | ||
291 | nfc_put_device(nfc_rawsock(sk)->dev); | ||
292 | } | ||
293 | |||
294 | skb_queue_purge(&sk->sk_receive_queue); | ||
295 | |||
296 | if (!sock_flag(sk, SOCK_DEAD)) { | ||
297 | nfc_err("Freeing alive NFC raw socket %p", sk); | ||
298 | return; | ||
299 | } | ||
300 | } | ||
301 | |||
302 | static int rawsock_create(struct net *net, struct socket *sock, | ||
303 | const struct nfc_protocol *nfc_proto) | ||
304 | { | ||
305 | struct sock *sk; | ||
306 | |||
307 | nfc_dbg("sock=%p", sock); | ||
308 | |||
309 | if (sock->type != SOCK_SEQPACKET) | ||
310 | return -ESOCKTNOSUPPORT; | ||
311 | |||
312 | sock->ops = &rawsock_ops; | ||
313 | |||
314 | sk = sk_alloc(net, PF_NFC, GFP_KERNEL, nfc_proto->proto); | ||
315 | if (!sk) | ||
316 | return -ENOMEM; | ||
317 | |||
318 | sock_init_data(sock, sk); | ||
319 | sk->sk_protocol = nfc_proto->id; | ||
320 | sk->sk_destruct = rawsock_destruct; | ||
321 | sock->state = SS_UNCONNECTED; | ||
322 | |||
323 | INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work); | ||
324 | nfc_rawsock(sk)->tx_work_scheduled = false; | ||
325 | |||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | static struct proto rawsock_proto = { | ||
330 | .name = "NFC_RAW", | ||
331 | .owner = THIS_MODULE, | ||
332 | .obj_size = sizeof(struct nfc_rawsock), | ||
333 | }; | ||
334 | |||
335 | static const struct nfc_protocol rawsock_nfc_proto = { | ||
336 | .id = NFC_SOCKPROTO_RAW, | ||
337 | .proto = &rawsock_proto, | ||
338 | .owner = THIS_MODULE, | ||
339 | .create = rawsock_create | ||
340 | }; | ||
341 | |||
342 | int __init rawsock_init(void) | ||
343 | { | ||
344 | int rc; | ||
345 | |||
346 | rc = nfc_proto_register(&rawsock_nfc_proto); | ||
347 | |||
348 | return rc; | ||
349 | } | ||
350 | |||
351 | void rawsock_exit(void) | ||
352 | { | ||
353 | nfc_proto_unregister(&rawsock_nfc_proto); | ||
354 | } | ||