Merge branch 'batman-adv/next' of git://git.open-mesh.org/linux-merge
[zen-stable.git] / net / nfc / core.c
blob3ebc6b3aabacc3446fd5e425a78898f149e518d6
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
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.
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.
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.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include "nfc.h"
33 #define VERSION "0.1"
35 int nfc_devlist_generation;
36 DEFINE_MUTEX(nfc_devlist_mutex);
38 /**
39 * nfc_dev_up - turn on the NFC device
41 * @dev: The nfc device to be turned on
43 * The device remains up until the nfc_dev_down function is called.
45 int nfc_dev_up(struct nfc_dev *dev)
47 int rc = 0;
49 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
51 device_lock(&dev->dev);
53 if (!device_is_registered(&dev->dev)) {
54 rc = -ENODEV;
55 goto error;
58 if (dev->dev_up) {
59 rc = -EALREADY;
60 goto error;
63 if (dev->ops->dev_up)
64 rc = dev->ops->dev_up(dev);
66 if (!rc)
67 dev->dev_up = true;
69 error:
70 device_unlock(&dev->dev);
71 return rc;
74 /**
75 * nfc_dev_down - turn off the NFC device
77 * @dev: The nfc device to be turned off
79 int nfc_dev_down(struct nfc_dev *dev)
81 int rc = 0;
83 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
85 device_lock(&dev->dev);
87 if (!device_is_registered(&dev->dev)) {
88 rc = -ENODEV;
89 goto error;
92 if (!dev->dev_up) {
93 rc = -EALREADY;
94 goto error;
97 if (dev->polling || dev->remote_activated) {
98 rc = -EBUSY;
99 goto error;
102 if (dev->ops->dev_down)
103 dev->ops->dev_down(dev);
105 dev->dev_up = false;
107 error:
108 device_unlock(&dev->dev);
109 return rc;
113 * nfc_start_poll - start polling for nfc targets
115 * @dev: The nfc device that must start polling
116 * @protocols: bitset of nfc protocols that must be used for polling
118 * The device remains polling for targets until a target is found or
119 * the nfc_stop_poll function is called.
121 int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
123 int rc;
125 pr_debug("dev_name=%s protocols=0x%x\n",
126 dev_name(&dev->dev), protocols);
128 if (!protocols)
129 return -EINVAL;
131 device_lock(&dev->dev);
133 if (!device_is_registered(&dev->dev)) {
134 rc = -ENODEV;
135 goto error;
138 if (dev->polling) {
139 rc = -EBUSY;
140 goto error;
143 rc = dev->ops->start_poll(dev, protocols);
144 if (!rc)
145 dev->polling = true;
147 error:
148 device_unlock(&dev->dev);
149 return rc;
153 * nfc_stop_poll - stop polling for nfc targets
155 * @dev: The nfc device that must stop polling
157 int nfc_stop_poll(struct nfc_dev *dev)
159 int rc = 0;
161 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
163 device_lock(&dev->dev);
165 if (!device_is_registered(&dev->dev)) {
166 rc = -ENODEV;
167 goto error;
170 if (!dev->polling) {
171 rc = -EINVAL;
172 goto error;
175 dev->ops->stop_poll(dev);
176 dev->polling = false;
178 error:
179 device_unlock(&dev->dev);
180 return rc;
184 * nfc_activate_target - prepare the target for data exchange
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
190 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
192 int rc;
194 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
195 dev_name(&dev->dev), target_idx, protocol);
197 device_lock(&dev->dev);
199 if (!device_is_registered(&dev->dev)) {
200 rc = -ENODEV;
201 goto error;
204 rc = dev->ops->activate_target(dev, target_idx, protocol);
205 if (!rc)
206 dev->remote_activated = true;
208 error:
209 device_unlock(&dev->dev);
210 return rc;
214 * nfc_deactivate_target - deactivate a nfc target
216 * @dev: The nfc device that found the target
217 * @target_idx: index of the target that must be deactivated
219 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
221 int rc = 0;
223 pr_debug("dev_name=%s target_idx=%u\n",
224 dev_name(&dev->dev), target_idx);
226 device_lock(&dev->dev);
228 if (!device_is_registered(&dev->dev)) {
229 rc = -ENODEV;
230 goto error;
233 dev->ops->deactivate_target(dev, target_idx);
234 dev->remote_activated = false;
236 error:
237 device_unlock(&dev->dev);
238 return rc;
242 * nfc_data_exchange - transceive data
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
250 * The user must wait for the callback before calling this function again.
252 int 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)
257 int rc;
259 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
260 dev_name(&dev->dev), target_idx, skb->len);
262 device_lock(&dev->dev);
264 if (!device_is_registered(&dev->dev)) {
265 rc = -ENODEV;
266 kfree_skb(skb);
267 goto error;
270 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
272 error:
273 device_unlock(&dev->dev);
274 return rc;
278 * nfc_alloc_skb - allocate a skb for data exchange responses
280 * @size: size to allocate
281 * @gfp: gfp flags
283 struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
285 struct sk_buff *skb;
286 unsigned int total_size;
288 total_size = size + 1;
289 skb = alloc_skb(total_size, gfp);
291 if (skb)
292 skb_reserve(skb, 1);
294 return skb;
296 EXPORT_SYMBOL(nfc_alloc_skb);
299 * nfc_targets_found - inform that targets were found
301 * @dev: The nfc device that found the targets
302 * @targets: array of nfc targets found
303 * @ntargets: targets array size
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.
309 int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
310 int n_targets)
312 int i;
314 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
316 dev->polling = false;
318 for (i = 0; i < n_targets; i++)
319 targets[i].idx = dev->target_idx++;
321 spin_lock_bh(&dev->targets_lock);
323 dev->targets_generation++;
325 kfree(dev->targets);
326 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
327 GFP_ATOMIC);
329 if (!dev->targets) {
330 dev->n_targets = 0;
331 spin_unlock_bh(&dev->targets_lock);
332 return -ENOMEM;
335 dev->n_targets = n_targets;
336 spin_unlock_bh(&dev->targets_lock);
338 nfc_genl_targets_found(dev);
340 return 0;
342 EXPORT_SYMBOL(nfc_targets_found);
344 static void nfc_release(struct device *d)
346 struct nfc_dev *dev = to_nfc_dev(d);
348 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
350 nfc_genl_data_exit(&dev->genl_data);
351 kfree(dev->targets);
352 kfree(dev);
355 struct class nfc_class = {
356 .name = "nfc",
357 .dev_release = nfc_release,
359 EXPORT_SYMBOL(nfc_class);
361 static int match_idx(struct device *d, void *data)
363 struct nfc_dev *dev = to_nfc_dev(d);
364 unsigned *idx = data;
366 return dev->idx == *idx;
369 struct nfc_dev *nfc_get_device(unsigned idx)
371 struct device *d;
373 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
374 if (!d)
375 return NULL;
377 return to_nfc_dev(d);
381 * nfc_allocate_device - allocate a new nfc device
383 * @ops: device operations
384 * @supported_protocols: NFC protocols supported by the device
386 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
387 u32 supported_protocols,
388 int tx_headroom,
389 int tx_tailroom)
391 static atomic_t dev_no = ATOMIC_INIT(0);
392 struct nfc_dev *dev;
394 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
395 !ops->deactivate_target || !ops->data_exchange)
396 return NULL;
398 if (!supported_protocols)
399 return NULL;
401 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
402 if (!dev)
403 return NULL;
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);
410 dev->ops = ops;
411 dev->supported_protocols = supported_protocols;
412 dev->tx_headroom = tx_headroom;
413 dev->tx_tailroom = tx_tailroom;
415 spin_lock_init(&dev->targets_lock);
416 nfc_genl_data_init(&dev->genl_data);
418 /* first generation must not be 0 */
419 dev->targets_generation = 1;
421 return dev;
423 EXPORT_SYMBOL(nfc_allocate_device);
426 * nfc_register_device - register a nfc device in the nfc subsystem
428 * @dev: The nfc device to register
430 int nfc_register_device(struct nfc_dev *dev)
432 int rc;
434 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
436 mutex_lock(&nfc_devlist_mutex);
437 nfc_devlist_generation++;
438 rc = device_add(&dev->dev);
439 mutex_unlock(&nfc_devlist_mutex);
441 if (rc < 0)
442 return rc;
444 rc = nfc_genl_device_added(dev);
445 if (rc)
446 pr_debug("The userspace won't be notified that the device %s was added\n",
447 dev_name(&dev->dev));
449 return 0;
451 EXPORT_SYMBOL(nfc_register_device);
454 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
456 * @dev: The nfc device to unregister
458 void nfc_unregister_device(struct nfc_dev *dev)
460 int rc;
462 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
464 mutex_lock(&nfc_devlist_mutex);
465 nfc_devlist_generation++;
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);
473 mutex_unlock(&nfc_devlist_mutex);
475 rc = nfc_genl_device_removed(dev);
476 if (rc)
477 pr_debug("The userspace won't be notified that the device %s was removed\n",
478 dev_name(&dev->dev));
481 EXPORT_SYMBOL(nfc_unregister_device);
483 static int __init nfc_init(void)
485 int rc;
487 pr_info("NFC Core ver %s\n", VERSION);
489 rc = class_register(&nfc_class);
490 if (rc)
491 return rc;
493 rc = nfc_genl_init();
494 if (rc)
495 goto err_genl;
497 /* the first generation must not be 0 */
498 nfc_devlist_generation = 1;
500 rc = rawsock_init();
501 if (rc)
502 goto err_rawsock;
504 rc = af_nfc_init();
505 if (rc)
506 goto err_af_nfc;
508 return 0;
510 err_af_nfc:
511 rawsock_exit();
512 err_rawsock:
513 nfc_genl_exit();
514 err_genl:
515 class_unregister(&nfc_class);
516 return rc;
519 static void __exit nfc_exit(void)
521 af_nfc_exit();
522 rawsock_exit();
523 nfc_genl_exit();
524 class_unregister(&nfc_class);
527 subsys_initcall(nfc_init);
528 module_exit(nfc_exit);
530 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
531 MODULE_DESCRIPTION("NFC Core ver " VERSION);
532 MODULE_VERSION(VERSION);
533 MODULE_LICENSE("GPL");