nfc: Convert nfc_dbg to pr_debug
[pohmelfs.git] / net / nfc / core.c
blobc922adb9e651cd35e468b50611f9fd6d5169bf08
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 int nfc_printk(const char *level, const char *format, ...)
40 struct va_format vaf;
41 va_list args;
42 int r;
44 va_start(args, format);
46 vaf.fmt = format;
47 vaf.va = &args;
49 r = printk("%sNFC: %pV\n", level, &vaf);
51 va_end(args);
53 return r;
55 EXPORT_SYMBOL(nfc_printk);
57 /**
58 * nfc_dev_up - turn on the NFC device
60 * @dev: The nfc device to be turned on
62 * The device remains up until the nfc_dev_down function is called.
64 int nfc_dev_up(struct nfc_dev *dev)
66 int rc = 0;
68 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
70 device_lock(&dev->dev);
72 if (!device_is_registered(&dev->dev)) {
73 rc = -ENODEV;
74 goto error;
77 if (dev->dev_up) {
78 rc = -EALREADY;
79 goto error;
82 if (dev->ops->dev_up)
83 rc = dev->ops->dev_up(dev);
85 if (!rc)
86 dev->dev_up = true;
88 error:
89 device_unlock(&dev->dev);
90 return rc;
93 /**
94 * nfc_dev_down - turn off the NFC device
96 * @dev: The nfc device to be turned off
98 int nfc_dev_down(struct nfc_dev *dev)
100 int rc = 0;
102 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
104 device_lock(&dev->dev);
106 if (!device_is_registered(&dev->dev)) {
107 rc = -ENODEV;
108 goto error;
111 if (!dev->dev_up) {
112 rc = -EALREADY;
113 goto error;
116 if (dev->polling || dev->remote_activated) {
117 rc = -EBUSY;
118 goto error;
121 if (dev->ops->dev_down)
122 dev->ops->dev_down(dev);
124 dev->dev_up = false;
126 error:
127 device_unlock(&dev->dev);
128 return rc;
132 * nfc_start_poll - start polling for nfc targets
134 * @dev: The nfc device that must start polling
135 * @protocols: bitset of nfc protocols that must be used for polling
137 * The device remains polling for targets until a target is found or
138 * the nfc_stop_poll function is called.
140 int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
142 int rc;
144 pr_debug("dev_name=%s protocols=0x%x\n",
145 dev_name(&dev->dev), protocols);
147 if (!protocols)
148 return -EINVAL;
150 device_lock(&dev->dev);
152 if (!device_is_registered(&dev->dev)) {
153 rc = -ENODEV;
154 goto error;
157 if (dev->polling) {
158 rc = -EBUSY;
159 goto error;
162 rc = dev->ops->start_poll(dev, protocols);
163 if (!rc)
164 dev->polling = true;
166 error:
167 device_unlock(&dev->dev);
168 return rc;
172 * nfc_stop_poll - stop polling for nfc targets
174 * @dev: The nfc device that must stop polling
176 int nfc_stop_poll(struct nfc_dev *dev)
178 int rc = 0;
180 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
182 device_lock(&dev->dev);
184 if (!device_is_registered(&dev->dev)) {
185 rc = -ENODEV;
186 goto error;
189 if (!dev->polling) {
190 rc = -EINVAL;
191 goto error;
194 dev->ops->stop_poll(dev);
195 dev->polling = false;
197 error:
198 device_unlock(&dev->dev);
199 return rc;
203 * nfc_activate_target - prepare the target for data exchange
205 * @dev: The nfc device that found the target
206 * @target_idx: index of the target that must be activated
207 * @protocol: nfc protocol that will be used for data exchange
209 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
211 int rc;
213 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
214 dev_name(&dev->dev), target_idx, protocol);
216 device_lock(&dev->dev);
218 if (!device_is_registered(&dev->dev)) {
219 rc = -ENODEV;
220 goto error;
223 rc = dev->ops->activate_target(dev, target_idx, protocol);
224 if (!rc)
225 dev->remote_activated = true;
227 error:
228 device_unlock(&dev->dev);
229 return rc;
233 * nfc_deactivate_target - deactivate a nfc target
235 * @dev: The nfc device that found the target
236 * @target_idx: index of the target that must be deactivated
238 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
240 int rc = 0;
242 pr_debug("dev_name=%s target_idx=%u\n",
243 dev_name(&dev->dev), target_idx);
245 device_lock(&dev->dev);
247 if (!device_is_registered(&dev->dev)) {
248 rc = -ENODEV;
249 goto error;
252 dev->ops->deactivate_target(dev, target_idx);
253 dev->remote_activated = false;
255 error:
256 device_unlock(&dev->dev);
257 return rc;
261 * nfc_data_exchange - transceive data
263 * @dev: The nfc device that found the target
264 * @target_idx: index of the target
265 * @skb: data to be sent
266 * @cb: callback called when the response is received
267 * @cb_context: parameter for the callback function
269 * The user must wait for the callback before calling this function again.
271 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
272 struct sk_buff *skb,
273 data_exchange_cb_t cb,
274 void *cb_context)
276 int rc;
278 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
279 dev_name(&dev->dev), target_idx, skb->len);
281 device_lock(&dev->dev);
283 if (!device_is_registered(&dev->dev)) {
284 rc = -ENODEV;
285 kfree_skb(skb);
286 goto error;
289 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
291 error:
292 device_unlock(&dev->dev);
293 return rc;
297 * nfc_alloc_skb - allocate a skb for data exchange responses
299 * @size: size to allocate
300 * @gfp: gfp flags
302 struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
304 struct sk_buff *skb;
305 unsigned int total_size;
307 total_size = size + 1;
308 skb = alloc_skb(total_size, gfp);
310 if (skb)
311 skb_reserve(skb, 1);
313 return skb;
315 EXPORT_SYMBOL(nfc_alloc_skb);
318 * nfc_targets_found - inform that targets were found
320 * @dev: The nfc device that found the targets
321 * @targets: array of nfc targets found
322 * @ntargets: targets array size
324 * The device driver must call this function when one or many nfc targets
325 * are found. After calling this function, the device driver must stop
326 * polling for targets.
328 int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
329 int n_targets)
331 int i;
333 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
335 dev->polling = false;
337 for (i = 0; i < n_targets; i++)
338 targets[i].idx = dev->target_idx++;
340 spin_lock_bh(&dev->targets_lock);
342 dev->targets_generation++;
344 kfree(dev->targets);
345 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
346 GFP_ATOMIC);
348 if (!dev->targets) {
349 dev->n_targets = 0;
350 spin_unlock_bh(&dev->targets_lock);
351 return -ENOMEM;
354 dev->n_targets = n_targets;
355 spin_unlock_bh(&dev->targets_lock);
357 nfc_genl_targets_found(dev);
359 return 0;
361 EXPORT_SYMBOL(nfc_targets_found);
363 static void nfc_release(struct device *d)
365 struct nfc_dev *dev = to_nfc_dev(d);
367 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
369 nfc_genl_data_exit(&dev->genl_data);
370 kfree(dev->targets);
371 kfree(dev);
374 struct class nfc_class = {
375 .name = "nfc",
376 .dev_release = nfc_release,
378 EXPORT_SYMBOL(nfc_class);
380 static int match_idx(struct device *d, void *data)
382 struct nfc_dev *dev = to_nfc_dev(d);
383 unsigned *idx = data;
385 return dev->idx == *idx;
388 struct nfc_dev *nfc_get_device(unsigned idx)
390 struct device *d;
392 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
393 if (!d)
394 return NULL;
396 return to_nfc_dev(d);
400 * nfc_allocate_device - allocate a new nfc device
402 * @ops: device operations
403 * @supported_protocols: NFC protocols supported by the device
405 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
406 u32 supported_protocols,
407 int tx_headroom,
408 int tx_tailroom)
410 static atomic_t dev_no = ATOMIC_INIT(0);
411 struct nfc_dev *dev;
413 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
414 !ops->deactivate_target || !ops->data_exchange)
415 return NULL;
417 if (!supported_protocols)
418 return NULL;
420 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
421 if (!dev)
422 return NULL;
424 dev->dev.class = &nfc_class;
425 dev->idx = atomic_inc_return(&dev_no) - 1;
426 dev_set_name(&dev->dev, "nfc%d", dev->idx);
427 device_initialize(&dev->dev);
429 dev->ops = ops;
430 dev->supported_protocols = supported_protocols;
431 dev->tx_headroom = tx_headroom;
432 dev->tx_tailroom = tx_tailroom;
434 spin_lock_init(&dev->targets_lock);
435 nfc_genl_data_init(&dev->genl_data);
437 /* first generation must not be 0 */
438 dev->targets_generation = 1;
440 return dev;
442 EXPORT_SYMBOL(nfc_allocate_device);
445 * nfc_register_device - register a nfc device in the nfc subsystem
447 * @dev: The nfc device to register
449 int nfc_register_device(struct nfc_dev *dev)
451 int rc;
453 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
455 mutex_lock(&nfc_devlist_mutex);
456 nfc_devlist_generation++;
457 rc = device_add(&dev->dev);
458 mutex_unlock(&nfc_devlist_mutex);
460 if (rc < 0)
461 return rc;
463 rc = nfc_genl_device_added(dev);
464 if (rc)
465 pr_debug("The userspace won't be notified that the device %s was added\n",
466 dev_name(&dev->dev));
468 return 0;
470 EXPORT_SYMBOL(nfc_register_device);
473 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
475 * @dev: The nfc device to unregister
477 void nfc_unregister_device(struct nfc_dev *dev)
479 int rc;
481 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
483 mutex_lock(&nfc_devlist_mutex);
484 nfc_devlist_generation++;
486 /* lock to avoid unregistering a device while an operation
487 is in progress */
488 device_lock(&dev->dev);
489 device_del(&dev->dev);
490 device_unlock(&dev->dev);
492 mutex_unlock(&nfc_devlist_mutex);
494 rc = nfc_genl_device_removed(dev);
495 if (rc)
496 pr_debug("The userspace won't be notified that the device %s was removed\n",
497 dev_name(&dev->dev));
500 EXPORT_SYMBOL(nfc_unregister_device);
502 static int __init nfc_init(void)
504 int rc;
506 pr_info("NFC Core ver %s\n", VERSION);
508 rc = class_register(&nfc_class);
509 if (rc)
510 return rc;
512 rc = nfc_genl_init();
513 if (rc)
514 goto err_genl;
516 /* the first generation must not be 0 */
517 nfc_devlist_generation = 1;
519 rc = rawsock_init();
520 if (rc)
521 goto err_rawsock;
523 rc = af_nfc_init();
524 if (rc)
525 goto err_af_nfc;
527 return 0;
529 err_af_nfc:
530 rawsock_exit();
531 err_rawsock:
532 nfc_genl_exit();
533 err_genl:
534 class_unregister(&nfc_class);
535 return rc;
538 static void __exit nfc_exit(void)
540 af_nfc_exit();
541 rawsock_exit();
542 nfc_genl_exit();
543 class_unregister(&nfc_class);
546 subsys_initcall(nfc_init);
547 module_exit(nfc_exit);
549 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
550 MODULE_DESCRIPTION("NFC Core ver " VERSION);
551 MODULE_VERSION(VERSION);
552 MODULE_LICENSE("GPL");