NFC: Add dev_up and dev_down control operations
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / nfc / core.c
blob47e02c1b8c02eea81a67f4432e6d45311ba6bd73
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 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
29 #include "nfc.h"
31 #define VERSION "0.1"
33 int nfc_devlist_generation;
34 DEFINE_MUTEX(nfc_devlist_mutex);
36 int nfc_printk(const char *level, const char *format, ...)
38 struct va_format vaf;
39 va_list args;
40 int r;
42 va_start(args, format);
44 vaf.fmt = format;
45 vaf.va = &args;
47 r = printk("%sNFC: %pV\n", level, &vaf);
49 va_end(args);
51 return r;
53 EXPORT_SYMBOL(nfc_printk);
55 /**
56 * nfc_dev_up - turn on the NFC device
58 * @dev: The nfc device to be turned on
60 * The device remains up until the nfc_dev_down function is called.
62 int nfc_dev_up(struct nfc_dev *dev)
64 int rc = 0;
66 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
68 device_lock(&dev->dev);
70 if (!device_is_registered(&dev->dev)) {
71 rc = -ENODEV;
72 goto error;
75 if (dev->dev_up) {
76 rc = -EALREADY;
77 goto error;
80 if (dev->ops->dev_up)
81 rc = dev->ops->dev_up(dev);
83 if (!rc)
84 dev->dev_up = true;
86 error:
87 device_unlock(&dev->dev);
88 return rc;
91 /**
92 * nfc_dev_down - turn off the NFC device
94 * @dev: The nfc device to be turned off
96 int nfc_dev_down(struct nfc_dev *dev)
98 int rc = 0;
100 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
102 device_lock(&dev->dev);
104 if (!device_is_registered(&dev->dev)) {
105 rc = -ENODEV;
106 goto error;
109 if (!dev->dev_up) {
110 rc = -EALREADY;
111 goto error;
114 if (dev->polling || dev->remote_activated) {
115 rc = -EBUSY;
116 goto error;
119 if (dev->ops->dev_down)
120 dev->ops->dev_down(dev);
122 dev->dev_up = false;
124 error:
125 device_unlock(&dev->dev);
126 return rc;
130 * nfc_start_poll - start polling for nfc targets
132 * @dev: The nfc device that must start polling
133 * @protocols: bitset of nfc protocols that must be used for polling
135 * The device remains polling for targets until a target is found or
136 * the nfc_stop_poll function is called.
138 int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
140 int rc;
142 nfc_dbg("dev_name=%s protocols=0x%x", dev_name(&dev->dev), protocols);
144 if (!protocols)
145 return -EINVAL;
147 device_lock(&dev->dev);
149 if (!device_is_registered(&dev->dev)) {
150 rc = -ENODEV;
151 goto error;
154 if (dev->polling) {
155 rc = -EBUSY;
156 goto error;
159 rc = dev->ops->start_poll(dev, protocols);
160 if (!rc)
161 dev->polling = true;
163 error:
164 device_unlock(&dev->dev);
165 return rc;
169 * nfc_stop_poll - stop polling for nfc targets
171 * @dev: The nfc device that must stop polling
173 int nfc_stop_poll(struct nfc_dev *dev)
175 int rc = 0;
177 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
179 device_lock(&dev->dev);
181 if (!device_is_registered(&dev->dev)) {
182 rc = -ENODEV;
183 goto error;
186 if (!dev->polling) {
187 rc = -EINVAL;
188 goto error;
191 dev->ops->stop_poll(dev);
192 dev->polling = false;
194 error:
195 device_unlock(&dev->dev);
196 return rc;
200 * nfc_activate_target - prepare the target for data exchange
202 * @dev: The nfc device that found the target
203 * @target_idx: index of the target that must be activated
204 * @protocol: nfc protocol that will be used for data exchange
206 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
208 int rc;
210 nfc_dbg("dev_name=%s target_idx=%u protocol=%u", dev_name(&dev->dev),
211 target_idx, protocol);
213 device_lock(&dev->dev);
215 if (!device_is_registered(&dev->dev)) {
216 rc = -ENODEV;
217 goto error;
220 rc = dev->ops->activate_target(dev, target_idx, protocol);
221 if (!rc)
222 dev->remote_activated = true;
224 error:
225 device_unlock(&dev->dev);
226 return rc;
230 * nfc_deactivate_target - deactivate a nfc target
232 * @dev: The nfc device that found the target
233 * @target_idx: index of the target that must be deactivated
235 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
237 int rc = 0;
239 nfc_dbg("dev_name=%s target_idx=%u", dev_name(&dev->dev), target_idx);
241 device_lock(&dev->dev);
243 if (!device_is_registered(&dev->dev)) {
244 rc = -ENODEV;
245 goto error;
248 dev->ops->deactivate_target(dev, target_idx);
249 dev->remote_activated = false;
251 error:
252 device_unlock(&dev->dev);
253 return rc;
257 * nfc_data_exchange - transceive data
259 * @dev: The nfc device that found the target
260 * @target_idx: index of the target
261 * @skb: data to be sent
262 * @cb: callback called when the response is received
263 * @cb_context: parameter for the callback function
265 * The user must wait for the callback before calling this function again.
267 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
268 struct sk_buff *skb,
269 data_exchange_cb_t cb,
270 void *cb_context)
272 int rc;
274 nfc_dbg("dev_name=%s target_idx=%u skb->len=%u", dev_name(&dev->dev),
275 target_idx, skb->len);
277 device_lock(&dev->dev);
279 if (!device_is_registered(&dev->dev)) {
280 rc = -ENODEV;
281 kfree_skb(skb);
282 goto error;
285 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
287 error:
288 device_unlock(&dev->dev);
289 return rc;
293 * nfc_alloc_skb - allocate a skb for data exchange responses
295 * @size: size to allocate
296 * @gfp: gfp flags
298 struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
300 struct sk_buff *skb;
301 unsigned int total_size;
303 total_size = size + 1;
304 skb = alloc_skb(total_size, gfp);
306 if (skb)
307 skb_reserve(skb, 1);
309 return skb;
311 EXPORT_SYMBOL(nfc_alloc_skb);
314 * nfc_targets_found - inform that targets were found
316 * @dev: The nfc device that found the targets
317 * @targets: array of nfc targets found
318 * @ntargets: targets array size
320 * The device driver must call this function when one or many nfc targets
321 * are found. After calling this function, the device driver must stop
322 * polling for targets.
324 int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
325 int n_targets)
327 int i;
329 nfc_dbg("dev_name=%s n_targets=%d", dev_name(&dev->dev), n_targets);
331 dev->polling = false;
333 for (i = 0; i < n_targets; i++)
334 targets[i].idx = dev->target_idx++;
336 spin_lock_bh(&dev->targets_lock);
338 dev->targets_generation++;
340 kfree(dev->targets);
341 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
342 GFP_ATOMIC);
344 if (!dev->targets) {
345 dev->n_targets = 0;
346 spin_unlock_bh(&dev->targets_lock);
347 return -ENOMEM;
350 dev->n_targets = n_targets;
351 spin_unlock_bh(&dev->targets_lock);
353 nfc_genl_targets_found(dev);
355 return 0;
357 EXPORT_SYMBOL(nfc_targets_found);
359 static void nfc_release(struct device *d)
361 struct nfc_dev *dev = to_nfc_dev(d);
363 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
365 nfc_genl_data_exit(&dev->genl_data);
366 kfree(dev->targets);
367 kfree(dev);
370 struct class nfc_class = {
371 .name = "nfc",
372 .dev_release = nfc_release,
374 EXPORT_SYMBOL(nfc_class);
376 static int match_idx(struct device *d, void *data)
378 struct nfc_dev *dev = to_nfc_dev(d);
379 unsigned *idx = data;
381 return dev->idx == *idx;
384 struct nfc_dev *nfc_get_device(unsigned idx)
386 struct device *d;
388 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
389 if (!d)
390 return NULL;
392 return to_nfc_dev(d);
396 * nfc_allocate_device - allocate a new nfc device
398 * @ops: device operations
399 * @supported_protocols: NFC protocols supported by the device
401 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
402 u32 supported_protocols,
403 int tx_headroom,
404 int tx_tailroom)
406 static atomic_t dev_no = ATOMIC_INIT(0);
407 struct nfc_dev *dev;
409 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
410 !ops->deactivate_target || !ops->data_exchange)
411 return NULL;
413 if (!supported_protocols)
414 return NULL;
416 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
417 if (!dev)
418 return NULL;
420 dev->dev.class = &nfc_class;
421 dev->idx = atomic_inc_return(&dev_no) - 1;
422 dev_set_name(&dev->dev, "nfc%d", dev->idx);
423 device_initialize(&dev->dev);
425 dev->ops = ops;
426 dev->supported_protocols = supported_protocols;
427 dev->tx_headroom = tx_headroom;
428 dev->tx_tailroom = tx_tailroom;
430 spin_lock_init(&dev->targets_lock);
431 nfc_genl_data_init(&dev->genl_data);
433 /* first generation must not be 0 */
434 dev->targets_generation = 1;
436 return dev;
438 EXPORT_SYMBOL(nfc_allocate_device);
441 * nfc_register_device - register a nfc device in the nfc subsystem
443 * @dev: The nfc device to register
445 int nfc_register_device(struct nfc_dev *dev)
447 int rc;
449 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
451 mutex_lock(&nfc_devlist_mutex);
452 nfc_devlist_generation++;
453 rc = device_add(&dev->dev);
454 mutex_unlock(&nfc_devlist_mutex);
456 if (rc < 0)
457 return rc;
459 rc = nfc_genl_device_added(dev);
460 if (rc)
461 nfc_dbg("The userspace won't be notified that the device %s was"
462 " added", dev_name(&dev->dev));
465 return 0;
467 EXPORT_SYMBOL(nfc_register_device);
470 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
472 * @dev: The nfc device to unregister
474 void nfc_unregister_device(struct nfc_dev *dev)
476 int rc;
478 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
480 mutex_lock(&nfc_devlist_mutex);
481 nfc_devlist_generation++;
483 /* lock to avoid unregistering a device while an operation
484 is in progress */
485 device_lock(&dev->dev);
486 device_del(&dev->dev);
487 device_unlock(&dev->dev);
489 mutex_unlock(&nfc_devlist_mutex);
491 rc = nfc_genl_device_removed(dev);
492 if (rc)
493 nfc_dbg("The userspace won't be notified that the device %s"
494 " was removed", dev_name(&dev->dev));
497 EXPORT_SYMBOL(nfc_unregister_device);
499 static int __init nfc_init(void)
501 int rc;
503 nfc_info("NFC Core ver %s", VERSION);
505 rc = class_register(&nfc_class);
506 if (rc)
507 return rc;
509 rc = nfc_genl_init();
510 if (rc)
511 goto err_genl;
513 /* the first generation must not be 0 */
514 nfc_devlist_generation = 1;
516 rc = rawsock_init();
517 if (rc)
518 goto err_rawsock;
520 rc = af_nfc_init();
521 if (rc)
522 goto err_af_nfc;
524 return 0;
526 err_af_nfc:
527 rawsock_exit();
528 err_rawsock:
529 nfc_genl_exit();
530 err_genl:
531 class_unregister(&nfc_class);
532 return rc;
535 static void __exit nfc_exit(void)
537 af_nfc_exit();
538 rawsock_exit();
539 nfc_genl_exit();
540 class_unregister(&nfc_class);
543 subsys_initcall(nfc_init);
544 module_exit(nfc_exit);
546 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
547 MODULE_DESCRIPTION("NFC Core ver " VERSION);
548 MODULE_VERSION(VERSION);
549 MODULE_LICENSE("GPL");