spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / net / nfc / core.c
blob3ddf6e698df0569c2f8aafbb19cecd4fa15b9aa4
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 ": %s: " fmt, __func__
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/nfc.h>
32 #include "nfc.h"
34 #define VERSION "0.1"
36 int nfc_devlist_generation;
37 DEFINE_MUTEX(nfc_devlist_mutex);
39 /**
40 * nfc_dev_up - turn on the NFC device
42 * @dev: The nfc device to be turned on
44 * The device remains up until the nfc_dev_down function is called.
46 int nfc_dev_up(struct nfc_dev *dev)
48 int rc = 0;
50 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
52 device_lock(&dev->dev);
54 if (!device_is_registered(&dev->dev)) {
55 rc = -ENODEV;
56 goto error;
59 if (dev->dev_up) {
60 rc = -EALREADY;
61 goto error;
64 if (dev->ops->dev_up)
65 rc = dev->ops->dev_up(dev);
67 if (!rc)
68 dev->dev_up = true;
70 error:
71 device_unlock(&dev->dev);
72 return rc;
75 /**
76 * nfc_dev_down - turn off the NFC device
78 * @dev: The nfc device to be turned off
80 int nfc_dev_down(struct nfc_dev *dev)
82 int rc = 0;
84 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
86 device_lock(&dev->dev);
88 if (!device_is_registered(&dev->dev)) {
89 rc = -ENODEV;
90 goto error;
93 if (!dev->dev_up) {
94 rc = -EALREADY;
95 goto error;
98 if (dev->polling || dev->remote_activated) {
99 rc = -EBUSY;
100 goto error;
103 if (dev->ops->dev_down)
104 dev->ops->dev_down(dev);
106 dev->dev_up = false;
108 error:
109 device_unlock(&dev->dev);
110 return rc;
114 * nfc_start_poll - start polling for nfc targets
116 * @dev: The nfc device that must start polling
117 * @protocols: bitset of nfc protocols that must be used for polling
119 * The device remains polling for targets until a target is found or
120 * the nfc_stop_poll function is called.
122 int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
124 int rc;
126 pr_debug("dev_name=%s protocols=0x%x\n",
127 dev_name(&dev->dev), protocols);
129 if (!protocols)
130 return -EINVAL;
132 device_lock(&dev->dev);
134 if (!device_is_registered(&dev->dev)) {
135 rc = -ENODEV;
136 goto error;
139 if (dev->polling) {
140 rc = -EBUSY;
141 goto error;
144 rc = dev->ops->start_poll(dev, protocols);
145 if (!rc)
146 dev->polling = true;
148 error:
149 device_unlock(&dev->dev);
150 return rc;
154 * nfc_stop_poll - stop polling for nfc targets
156 * @dev: The nfc device that must stop polling
158 int nfc_stop_poll(struct nfc_dev *dev)
160 int rc = 0;
162 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
164 device_lock(&dev->dev);
166 if (!device_is_registered(&dev->dev)) {
167 rc = -ENODEV;
168 goto error;
171 if (!dev->polling) {
172 rc = -EINVAL;
173 goto error;
176 dev->ops->stop_poll(dev);
177 dev->polling = false;
179 error:
180 device_unlock(&dev->dev);
181 return rc;
184 int nfc_dep_link_up(struct nfc_dev *dev, int target_index,
185 u8 comm_mode, u8 rf_mode)
187 int rc = 0;
189 pr_debug("dev_name=%s comm:%d rf:%d\n",
190 dev_name(&dev->dev), comm_mode, rf_mode);
192 if (!dev->ops->dep_link_up)
193 return -EOPNOTSUPP;
195 device_lock(&dev->dev);
197 if (!device_is_registered(&dev->dev)) {
198 rc = -ENODEV;
199 goto error;
202 if (dev->dep_link_up == true) {
203 rc = -EALREADY;
204 goto error;
207 rc = dev->ops->dep_link_up(dev, target_index, comm_mode, rf_mode);
209 error:
210 device_unlock(&dev->dev);
211 return rc;
214 int nfc_dep_link_down(struct nfc_dev *dev)
216 int rc = 0;
218 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
220 if (!dev->ops->dep_link_down)
221 return -EOPNOTSUPP;
223 device_lock(&dev->dev);
225 if (!device_is_registered(&dev->dev)) {
226 rc = -ENODEV;
227 goto error;
230 if (dev->dep_link_up == false) {
231 rc = -EALREADY;
232 goto error;
235 if (dev->dep_rf_mode == NFC_RF_TARGET) {
236 rc = -EOPNOTSUPP;
237 goto error;
240 rc = dev->ops->dep_link_down(dev);
241 if (!rc) {
242 dev->dep_link_up = false;
243 nfc_llcp_mac_is_down(dev);
244 nfc_genl_dep_link_down_event(dev);
247 error:
248 device_unlock(&dev->dev);
249 return rc;
252 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
253 u8 comm_mode, u8 rf_mode)
255 dev->dep_link_up = true;
256 dev->dep_rf_mode = rf_mode;
258 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
260 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
262 EXPORT_SYMBOL(nfc_dep_link_is_up);
265 * nfc_activate_target - prepare the target for data exchange
267 * @dev: The nfc device that found the target
268 * @target_idx: index of the target that must be activated
269 * @protocol: nfc protocol that will be used for data exchange
271 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
273 int rc;
275 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
276 dev_name(&dev->dev), target_idx, protocol);
278 device_lock(&dev->dev);
280 if (!device_is_registered(&dev->dev)) {
281 rc = -ENODEV;
282 goto error;
285 rc = dev->ops->activate_target(dev, target_idx, protocol);
286 if (!rc)
287 dev->remote_activated = true;
289 error:
290 device_unlock(&dev->dev);
291 return rc;
295 * nfc_deactivate_target - deactivate a nfc target
297 * @dev: The nfc device that found the target
298 * @target_idx: index of the target that must be deactivated
300 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
302 int rc = 0;
304 pr_debug("dev_name=%s target_idx=%u\n",
305 dev_name(&dev->dev), target_idx);
307 device_lock(&dev->dev);
309 if (!device_is_registered(&dev->dev)) {
310 rc = -ENODEV;
311 goto error;
314 dev->ops->deactivate_target(dev, target_idx);
315 dev->remote_activated = false;
317 error:
318 device_unlock(&dev->dev);
319 return rc;
323 * nfc_data_exchange - transceive data
325 * @dev: The nfc device that found the target
326 * @target_idx: index of the target
327 * @skb: data to be sent
328 * @cb: callback called when the response is received
329 * @cb_context: parameter for the callback function
331 * The user must wait for the callback before calling this function again.
333 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
334 struct sk_buff *skb,
335 data_exchange_cb_t cb,
336 void *cb_context)
338 int rc;
340 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
341 dev_name(&dev->dev), target_idx, skb->len);
343 device_lock(&dev->dev);
345 if (!device_is_registered(&dev->dev)) {
346 rc = -ENODEV;
347 kfree_skb(skb);
348 goto error;
351 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
353 error:
354 device_unlock(&dev->dev);
355 return rc;
358 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
360 pr_debug("dev_name=%s gb_len=%d\n",
361 dev_name(&dev->dev), gb_len);
363 if (gb_len > NFC_MAX_GT_LEN)
364 return -EINVAL;
366 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
368 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
370 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, u8 *gt_len)
372 return nfc_llcp_general_bytes(dev, gt_len);
374 EXPORT_SYMBOL(nfc_get_local_general_bytes);
377 * nfc_alloc_send_skb - allocate a skb for data exchange responses
379 * @size: size to allocate
380 * @gfp: gfp flags
382 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
383 unsigned int flags, unsigned int size,
384 unsigned int *err)
386 struct sk_buff *skb;
387 unsigned int total_size;
389 total_size = size +
390 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
392 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
393 if (skb)
394 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
396 return skb;
400 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
402 * @size: size to allocate
403 * @gfp: gfp flags
405 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
407 struct sk_buff *skb;
408 unsigned int total_size;
410 total_size = size + 1;
411 skb = alloc_skb(total_size, gfp);
413 if (skb)
414 skb_reserve(skb, 1);
416 return skb;
418 EXPORT_SYMBOL(nfc_alloc_recv_skb);
421 * nfc_targets_found - inform that targets were found
423 * @dev: The nfc device that found the targets
424 * @targets: array of nfc targets found
425 * @ntargets: targets array size
427 * The device driver must call this function when one or many nfc targets
428 * are found. After calling this function, the device driver must stop
429 * polling for targets.
431 int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
432 int n_targets)
434 int i;
436 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
438 dev->polling = false;
440 for (i = 0; i < n_targets; i++)
441 targets[i].idx = dev->target_idx++;
443 spin_lock_bh(&dev->targets_lock);
445 dev->targets_generation++;
447 kfree(dev->targets);
448 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
449 GFP_ATOMIC);
451 if (!dev->targets) {
452 dev->n_targets = 0;
453 spin_unlock_bh(&dev->targets_lock);
454 return -ENOMEM;
457 dev->n_targets = n_targets;
458 spin_unlock_bh(&dev->targets_lock);
460 nfc_genl_targets_found(dev);
462 return 0;
464 EXPORT_SYMBOL(nfc_targets_found);
466 static void nfc_release(struct device *d)
468 struct nfc_dev *dev = to_nfc_dev(d);
470 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
472 nfc_genl_data_exit(&dev->genl_data);
473 kfree(dev->targets);
474 kfree(dev);
477 struct class nfc_class = {
478 .name = "nfc",
479 .dev_release = nfc_release,
481 EXPORT_SYMBOL(nfc_class);
483 static int match_idx(struct device *d, void *data)
485 struct nfc_dev *dev = to_nfc_dev(d);
486 unsigned *idx = data;
488 return dev->idx == *idx;
491 struct nfc_dev *nfc_get_device(unsigned idx)
493 struct device *d;
495 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
496 if (!d)
497 return NULL;
499 return to_nfc_dev(d);
503 * nfc_allocate_device - allocate a new nfc device
505 * @ops: device operations
506 * @supported_protocols: NFC protocols supported by the device
508 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
509 u32 supported_protocols,
510 int tx_headroom,
511 int tx_tailroom)
513 static atomic_t dev_no = ATOMIC_INIT(0);
514 struct nfc_dev *dev;
516 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
517 !ops->deactivate_target || !ops->data_exchange)
518 return NULL;
520 if (!supported_protocols)
521 return NULL;
523 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
524 if (!dev)
525 return NULL;
527 dev->dev.class = &nfc_class;
528 dev->idx = atomic_inc_return(&dev_no) - 1;
529 dev_set_name(&dev->dev, "nfc%d", dev->idx);
530 device_initialize(&dev->dev);
532 dev->ops = ops;
533 dev->supported_protocols = supported_protocols;
534 dev->tx_headroom = tx_headroom;
535 dev->tx_tailroom = tx_tailroom;
537 spin_lock_init(&dev->targets_lock);
538 nfc_genl_data_init(&dev->genl_data);
540 /* first generation must not be 0 */
541 dev->targets_generation = 1;
543 return dev;
545 EXPORT_SYMBOL(nfc_allocate_device);
548 * nfc_register_device - register a nfc device in the nfc subsystem
550 * @dev: The nfc device to register
552 int nfc_register_device(struct nfc_dev *dev)
554 int rc;
556 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
558 mutex_lock(&nfc_devlist_mutex);
559 nfc_devlist_generation++;
560 rc = device_add(&dev->dev);
561 mutex_unlock(&nfc_devlist_mutex);
563 if (rc < 0)
564 return rc;
566 rc = nfc_llcp_register_device(dev);
567 if (rc)
568 pr_err("Could not register llcp device\n");
570 rc = nfc_genl_device_added(dev);
571 if (rc)
572 pr_debug("The userspace won't be notified that the device %s was added\n",
573 dev_name(&dev->dev));
575 return 0;
577 EXPORT_SYMBOL(nfc_register_device);
580 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
582 * @dev: The nfc device to unregister
584 void nfc_unregister_device(struct nfc_dev *dev)
586 int rc;
588 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
590 mutex_lock(&nfc_devlist_mutex);
591 nfc_devlist_generation++;
593 /* lock to avoid unregistering a device while an operation
594 is in progress */
595 device_lock(&dev->dev);
596 device_del(&dev->dev);
597 device_unlock(&dev->dev);
599 mutex_unlock(&nfc_devlist_mutex);
601 nfc_llcp_unregister_device(dev);
603 rc = nfc_genl_device_removed(dev);
604 if (rc)
605 pr_debug("The userspace won't be notified that the device %s was removed\n",
606 dev_name(&dev->dev));
609 EXPORT_SYMBOL(nfc_unregister_device);
611 static int __init nfc_init(void)
613 int rc;
615 pr_info("NFC Core ver %s\n", VERSION);
617 rc = class_register(&nfc_class);
618 if (rc)
619 return rc;
621 rc = nfc_genl_init();
622 if (rc)
623 goto err_genl;
625 /* the first generation must not be 0 */
626 nfc_devlist_generation = 1;
628 rc = rawsock_init();
629 if (rc)
630 goto err_rawsock;
632 rc = nfc_llcp_init();
633 if (rc)
634 goto err_llcp_sock;
636 rc = af_nfc_init();
637 if (rc)
638 goto err_af_nfc;
640 return 0;
642 err_af_nfc:
643 nfc_llcp_exit();
644 err_llcp_sock:
645 rawsock_exit();
646 err_rawsock:
647 nfc_genl_exit();
648 err_genl:
649 class_unregister(&nfc_class);
650 return rc;
653 static void __exit nfc_exit(void)
655 af_nfc_exit();
656 nfc_llcp_exit();
657 rawsock_exit();
658 nfc_genl_exit();
659 class_unregister(&nfc_class);
662 subsys_initcall(nfc_init);
663 module_exit(nfc_exit);
665 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
666 MODULE_DESCRIPTION("NFC Core ver " VERSION);
667 MODULE_VERSION(VERSION);
668 MODULE_LICENSE("GPL");