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