rt2x00: Make use of ieee80211_free_txskb in tx path
[deliverable/linux.git] / net / nfc / core.c
CommitLineData
3e256b8f
LRV
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
ed1e0ad8
JP
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
3e256b8f
LRV
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/slab.h>
30
31#include "nfc.h"
32
33#define VERSION "0.1"
34
35int nfc_devlist_generation;
36DEFINE_MUTEX(nfc_devlist_mutex);
37
8b3fe7b5
IE
38/**
39 * nfc_dev_up - turn on the NFC device
40 *
41 * @dev: The nfc device to be turned on
42 *
43 * The device remains up until the nfc_dev_down function is called.
44 */
45int nfc_dev_up(struct nfc_dev *dev)
46{
47 int rc = 0;
48
20c239c1 49 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
50
51 device_lock(&dev->dev);
52
53 if (!device_is_registered(&dev->dev)) {
54 rc = -ENODEV;
55 goto error;
56 }
57
58 if (dev->dev_up) {
59 rc = -EALREADY;
60 goto error;
61 }
62
63 if (dev->ops->dev_up)
64 rc = dev->ops->dev_up(dev);
65
66 if (!rc)
67 dev->dev_up = true;
68
69error:
70 device_unlock(&dev->dev);
71 return rc;
72}
73
74/**
75 * nfc_dev_down - turn off the NFC device
76 *
77 * @dev: The nfc device to be turned off
78 */
79int nfc_dev_down(struct nfc_dev *dev)
80{
81 int rc = 0;
82
20c239c1 83 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
84
85 device_lock(&dev->dev);
86
87 if (!device_is_registered(&dev->dev)) {
88 rc = -ENODEV;
89 goto error;
90 }
91
92 if (!dev->dev_up) {
93 rc = -EALREADY;
94 goto error;
95 }
96
97 if (dev->polling || dev->remote_activated) {
98 rc = -EBUSY;
99 goto error;
100 }
101
102 if (dev->ops->dev_down)
103 dev->ops->dev_down(dev);
104
105 dev->dev_up = false;
106
107error:
108 device_unlock(&dev->dev);
109 return rc;
110}
111
3e256b8f
LRV
112/**
113 * nfc_start_poll - start polling for nfc targets
114 *
115 * @dev: The nfc device that must start polling
116 * @protocols: bitset of nfc protocols that must be used for polling
117 *
118 * The device remains polling for targets until a target is found or
119 * the nfc_stop_poll function is called.
120 */
121int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
122{
123 int rc;
124
20c239c1
JP
125 pr_debug("dev_name=%s protocols=0x%x\n",
126 dev_name(&dev->dev), protocols);
3e256b8f
LRV
127
128 if (!protocols)
129 return -EINVAL;
130
131 device_lock(&dev->dev);
132
133 if (!device_is_registered(&dev->dev)) {
134 rc = -ENODEV;
135 goto error;
136 }
137
138 if (dev->polling) {
139 rc = -EBUSY;
140 goto error;
141 }
142
143 rc = dev->ops->start_poll(dev, protocols);
144 if (!rc)
145 dev->polling = true;
146
147error:
148 device_unlock(&dev->dev);
149 return rc;
150}
151
152/**
153 * nfc_stop_poll - stop polling for nfc targets
154 *
155 * @dev: The nfc device that must stop polling
156 */
157int nfc_stop_poll(struct nfc_dev *dev)
158{
159 int rc = 0;
160
20c239c1 161 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
162
163 device_lock(&dev->dev);
164
165 if (!device_is_registered(&dev->dev)) {
166 rc = -ENODEV;
167 goto error;
168 }
169
170 if (!dev->polling) {
171 rc = -EINVAL;
172 goto error;
173 }
174
175 dev->ops->stop_poll(dev);
176 dev->polling = false;
177
178error:
179 device_unlock(&dev->dev);
180 return rc;
181}
182
183/**
184 * nfc_activate_target - prepare the target for data exchange
185 *
186 * @dev: The nfc device that found the target
187 * @target_idx: index of the target that must be activated
188 * @protocol: nfc protocol that will be used for data exchange
189 */
190int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
191{
192 int rc;
193
20c239c1
JP
194 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
195 dev_name(&dev->dev), target_idx, protocol);
3e256b8f
LRV
196
197 device_lock(&dev->dev);
198
199 if (!device_is_registered(&dev->dev)) {
200 rc = -ENODEV;
201 goto error;
202 }
203
204 rc = dev->ops->activate_target(dev, target_idx, protocol);
8b3fe7b5
IE
205 if (!rc)
206 dev->remote_activated = true;
3e256b8f
LRV
207
208error:
209 device_unlock(&dev->dev);
210 return rc;
211}
212
213/**
214 * nfc_deactivate_target - deactivate a nfc target
215 *
216 * @dev: The nfc device that found the target
217 * @target_idx: index of the target that must be deactivated
218 */
219int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
220{
221 int rc = 0;
222
20c239c1
JP
223 pr_debug("dev_name=%s target_idx=%u\n",
224 dev_name(&dev->dev), target_idx);
3e256b8f
LRV
225
226 device_lock(&dev->dev);
227
228 if (!device_is_registered(&dev->dev)) {
229 rc = -ENODEV;
230 goto error;
231 }
232
233 dev->ops->deactivate_target(dev, target_idx);
8b3fe7b5 234 dev->remote_activated = false;
3e256b8f
LRV
235
236error:
237 device_unlock(&dev->dev);
238 return rc;
239}
240
241/**
242 * nfc_data_exchange - transceive data
243 *
244 * @dev: The nfc device that found the target
245 * @target_idx: index of the target
246 * @skb: data to be sent
247 * @cb: callback called when the response is received
248 * @cb_context: parameter for the callback function
249 *
250 * The user must wait for the callback before calling this function again.
251 */
252int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
253 struct sk_buff *skb,
254 data_exchange_cb_t cb,
255 void *cb_context)
256{
257 int rc;
258
20c239c1
JP
259 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
260 dev_name(&dev->dev), target_idx, skb->len);
3e256b8f
LRV
261
262 device_lock(&dev->dev);
263
264 if (!device_is_registered(&dev->dev)) {
265 rc = -ENODEV;
266 kfree_skb(skb);
267 goto error;
268 }
269
270 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
271
272error:
273 device_unlock(&dev->dev);
274 return rc;
275}
276
277/**
278 * nfc_alloc_skb - allocate a skb for data exchange responses
279 *
280 * @size: size to allocate
281 * @gfp: gfp flags
282 */
283struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
284{
285 struct sk_buff *skb;
286 unsigned int total_size;
287
288 total_size = size + 1;
289 skb = alloc_skb(total_size, gfp);
290
291 if (skb)
292 skb_reserve(skb, 1);
293
294 return skb;
295}
296EXPORT_SYMBOL(nfc_alloc_skb);
297
4d12b8b1
LRV
298/**
299 * nfc_targets_found - inform that targets were found
300 *
301 * @dev: The nfc device that found the targets
302 * @targets: array of nfc targets found
303 * @ntargets: targets array size
304 *
305 * The device driver must call this function when one or many nfc targets
306 * are found. After calling this function, the device driver must stop
307 * polling for targets.
308 */
309int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
310 int n_targets)
311{
312 int i;
313
20c239c1 314 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
4d12b8b1
LRV
315
316 dev->polling = false;
317
318 for (i = 0; i < n_targets; i++)
319 targets[i].idx = dev->target_idx++;
320
321 spin_lock_bh(&dev->targets_lock);
322
323 dev->targets_generation++;
324
325 kfree(dev->targets);
326 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
327 GFP_ATOMIC);
328
329 if (!dev->targets) {
330 dev->n_targets = 0;
331 spin_unlock_bh(&dev->targets_lock);
332 return -ENOMEM;
333 }
334
335 dev->n_targets = n_targets;
336 spin_unlock_bh(&dev->targets_lock);
337
338 nfc_genl_targets_found(dev);
339
340 return 0;
341}
342EXPORT_SYMBOL(nfc_targets_found);
343
3e256b8f
LRV
344static void nfc_release(struct device *d)
345{
346 struct nfc_dev *dev = to_nfc_dev(d);
347
20c239c1 348 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 349
4d12b8b1
LRV
350 nfc_genl_data_exit(&dev->genl_data);
351 kfree(dev->targets);
3e256b8f
LRV
352 kfree(dev);
353}
354
355struct class nfc_class = {
356 .name = "nfc",
357 .dev_release = nfc_release,
358};
359EXPORT_SYMBOL(nfc_class);
360
361static int match_idx(struct device *d, void *data)
362{
363 struct nfc_dev *dev = to_nfc_dev(d);
364 unsigned *idx = data;
365
366 return dev->idx == *idx;
367}
368
369struct nfc_dev *nfc_get_device(unsigned idx)
370{
371 struct device *d;
372
373 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
374 if (!d)
375 return NULL;
376
377 return to_nfc_dev(d);
378}
379
380/**
381 * nfc_allocate_device - allocate a new nfc device
382 *
383 * @ops: device operations
384 * @supported_protocols: NFC protocols supported by the device
385 */
386struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
e8753043
SO
387 u32 supported_protocols,
388 int tx_headroom,
389 int tx_tailroom)
3e256b8f
LRV
390{
391 static atomic_t dev_no = ATOMIC_INIT(0);
392 struct nfc_dev *dev;
393
394 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
395 !ops->deactivate_target || !ops->data_exchange)
396 return NULL;
397
398 if (!supported_protocols)
399 return NULL;
400
401 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
402 if (!dev)
403 return NULL;
404
405 dev->dev.class = &nfc_class;
406 dev->idx = atomic_inc_return(&dev_no) - 1;
407 dev_set_name(&dev->dev, "nfc%d", dev->idx);
408 device_initialize(&dev->dev);
409
410 dev->ops = ops;
411 dev->supported_protocols = supported_protocols;
e8753043
SO
412 dev->tx_headroom = tx_headroom;
413 dev->tx_tailroom = tx_tailroom;
3e256b8f 414
4d12b8b1
LRV
415 spin_lock_init(&dev->targets_lock);
416 nfc_genl_data_init(&dev->genl_data);
417
418 /* first generation must not be 0 */
419 dev->targets_generation = 1;
420
3e256b8f
LRV
421 return dev;
422}
423EXPORT_SYMBOL(nfc_allocate_device);
424
425/**
426 * nfc_register_device - register a nfc device in the nfc subsystem
427 *
428 * @dev: The nfc device to register
429 */
430int nfc_register_device(struct nfc_dev *dev)
431{
432 int rc;
433
20c239c1 434 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
435
436 mutex_lock(&nfc_devlist_mutex);
437 nfc_devlist_generation++;
438 rc = device_add(&dev->dev);
439 mutex_unlock(&nfc_devlist_mutex);
440
4d12b8b1
LRV
441 if (rc < 0)
442 return rc;
443
444 rc = nfc_genl_device_added(dev);
445 if (rc)
20c239c1
JP
446 pr_debug("The userspace won't be notified that the device %s was added\n",
447 dev_name(&dev->dev));
4d12b8b1
LRV
448
449 return 0;
3e256b8f
LRV
450}
451EXPORT_SYMBOL(nfc_register_device);
452
453/**
454 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
455 *
456 * @dev: The nfc device to unregister
457 */
458void nfc_unregister_device(struct nfc_dev *dev)
459{
4d12b8b1
LRV
460 int rc;
461
20c239c1 462 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
463
464 mutex_lock(&nfc_devlist_mutex);
465 nfc_devlist_generation++;
466
467 /* lock to avoid unregistering a device while an operation
468 is in progress */
469 device_lock(&dev->dev);
470 device_del(&dev->dev);
471 device_unlock(&dev->dev);
472
473 mutex_unlock(&nfc_devlist_mutex);
4d12b8b1
LRV
474
475 rc = nfc_genl_device_removed(dev);
476 if (rc)
20c239c1
JP
477 pr_debug("The userspace won't be notified that the device %s was removed\n",
478 dev_name(&dev->dev));
4d12b8b1 479
3e256b8f
LRV
480}
481EXPORT_SYMBOL(nfc_unregister_device);
482
483static int __init nfc_init(void)
484{
4d12b8b1
LRV
485 int rc;
486
ed1e0ad8 487 pr_info("NFC Core ver %s\n", VERSION);
3e256b8f 488
4d12b8b1
LRV
489 rc = class_register(&nfc_class);
490 if (rc)
491 return rc;
492
493 rc = nfc_genl_init();
494 if (rc)
495 goto err_genl;
496
497 /* the first generation must not be 0 */
498 nfc_devlist_generation = 1;
499
23b7869c
LRV
500 rc = rawsock_init();
501 if (rc)
502 goto err_rawsock;
503
c7fe3b52
AAJ
504 rc = af_nfc_init();
505 if (rc)
506 goto err_af_nfc;
507
4d12b8b1
LRV
508 return 0;
509
c7fe3b52 510err_af_nfc:
23b7869c
LRV
511 rawsock_exit();
512err_rawsock:
c7fe3b52 513 nfc_genl_exit();
4d12b8b1
LRV
514err_genl:
515 class_unregister(&nfc_class);
516 return rc;
3e256b8f
LRV
517}
518
519static void __exit nfc_exit(void)
520{
c7fe3b52 521 af_nfc_exit();
23b7869c 522 rawsock_exit();
4d12b8b1 523 nfc_genl_exit();
3e256b8f
LRV
524 class_unregister(&nfc_class);
525}
526
527subsys_initcall(nfc_init);
528module_exit(nfc_exit);
529
530MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
531MODULE_DESCRIPTION("NFC Core ver " VERSION);
532MODULE_VERSION(VERSION);
533MODULE_LICENSE("GPL");
This page took 0.073533 seconds and 5 git commands to generate.