2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
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>
33 int nfc_devlist_generation;
34 DEFINE_MUTEX(nfc_devlist_mutex);
36 int nfc_printk(const char *level, const char *format, ...)
42 va_start(args, format);
47 r = printk("%sNFC: %pV\n", level, &vaf);
53 EXPORT_SYMBOL(nfc_printk);
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)
66 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
68 device_lock(&dev->dev);
70 if (!device_is_registered(&dev->dev)) {
81 rc = dev->ops->dev_up(dev);
87 device_unlock(&dev->dev);
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)
100 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
102 device_lock(&dev->dev);
104 if (!device_is_registered(&dev->dev)) {
114 if (dev->polling || dev->remote_activated) {
119 if (dev->ops->dev_down)
120 dev->ops->dev_down(dev);
125 device_unlock(&dev->dev);
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)
142 nfc_dbg("dev_name=%s protocols=0x%x", dev_name(&dev->dev), protocols);
147 device_lock(&dev->dev);
149 if (!device_is_registered(&dev->dev)) {
159 rc = dev->ops->start_poll(dev, protocols);
164 device_unlock(&dev->dev);
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)
177 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
179 device_lock(&dev->dev);
181 if (!device_is_registered(&dev->dev)) {
191 dev->ops->stop_poll(dev);
192 dev->polling = false;
195 device_unlock(&dev->dev);
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)
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)) {
220 rc = dev->ops->activate_target(dev, target_idx, protocol);
222 dev->remote_activated = true;
225 device_unlock(&dev->dev);
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)
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)) {
248 dev->ops->deactivate_target(dev, target_idx);
249 dev->remote_activated = false;
252 device_unlock(&dev->dev);
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,
269 data_exchange_cb_t cb,
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)) {
285 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
288 device_unlock(&dev->dev);
293 * nfc_alloc_skb - allocate a skb for data exchange responses
295 * @size: size to allocate
298 struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
301 unsigned int total_size;
303 total_size = size + 1;
304 skb = alloc_skb(total_size, gfp);
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,
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++;
341 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
346 spin_unlock_bh(&dev->targets_lock);
350 dev->n_targets = n_targets;
351 spin_unlock_bh(&dev->targets_lock);
353 nfc_genl_targets_found(dev);
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);
370 struct class nfc_class = {
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)
388 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
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,
406 static atomic_t dev_no = ATOMIC_INIT(0);
409 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
410 !ops->deactivate_target || !ops->data_exchange)
413 if (!supported_protocols)
416 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
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);
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;
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)
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);
459 rc = nfc_genl_device_added(dev);
461 nfc_dbg("The userspace won't be notified that the device %s was"
462 " added", dev_name(&dev->dev));
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)
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
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);
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)
503 nfc_info("NFC Core ver %s", VERSION);
505 rc = class_register(&nfc_class);
509 rc = nfc_genl_init();
513 /* the first generation must not be 0 */
514 nfc_devlist_generation = 1;
531 class_unregister(&nfc_class);
535 static void __exit nfc_exit(void)
540 class_unregister(&nfc_class);
543 subsys_initcall(nfc_init);
544 module_exit(nfc_exit);
547 MODULE_DESCRIPTION("NFC Core ver " VERSION);
548 MODULE_VERSION(VERSION);
549 MODULE_LICENSE("GPL");