NFC: The core part should generate the target index
[deliverable/linux.git] / net / nfc / core.c
CommitLineData
3e256b8f
LRV
1/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
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.
12 *
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.
17 *
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.
22 */
23
52858b51 24#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
ed1e0ad8 25
3e256b8f
LRV
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/slab.h>
7c7cd3bf 30#include <linux/nfc.h>
3e256b8f
LRV
31
32#include "nfc.h"
33
34#define VERSION "0.1"
35
36int nfc_devlist_generation;
37DEFINE_MUTEX(nfc_devlist_mutex);
38
8b3fe7b5
IE
39/**
40 * nfc_dev_up - turn on the NFC device
41 *
42 * @dev: The nfc device to be turned on
43 *
44 * The device remains up until the nfc_dev_down function is called.
45 */
46int nfc_dev_up(struct nfc_dev *dev)
47{
48 int rc = 0;
49
20c239c1 50 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
51
52 device_lock(&dev->dev);
53
54 if (!device_is_registered(&dev->dev)) {
55 rc = -ENODEV;
56 goto error;
57 }
58
59 if (dev->dev_up) {
60 rc = -EALREADY;
61 goto error;
62 }
63
64 if (dev->ops->dev_up)
65 rc = dev->ops->dev_up(dev);
66
67 if (!rc)
68 dev->dev_up = true;
69
70error:
71 device_unlock(&dev->dev);
72 return rc;
73}
74
75/**
76 * nfc_dev_down - turn off the NFC device
77 *
78 * @dev: The nfc device to be turned off
79 */
80int nfc_dev_down(struct nfc_dev *dev)
81{
82 int rc = 0;
83
20c239c1 84 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
85
86 device_lock(&dev->dev);
87
88 if (!device_is_registered(&dev->dev)) {
89 rc = -ENODEV;
90 goto error;
91 }
92
93 if (!dev->dev_up) {
94 rc = -EALREADY;
95 goto error;
96 }
97
98 if (dev->polling || dev->remote_activated) {
99 rc = -EBUSY;
100 goto error;
101 }
102
103 if (dev->ops->dev_down)
104 dev->ops->dev_down(dev);
105
106 dev->dev_up = false;
107
108error:
109 device_unlock(&dev->dev);
110 return rc;
111}
112
3e256b8f
LRV
113/**
114 * nfc_start_poll - start polling for nfc targets
115 *
116 * @dev: The nfc device that must start polling
117 * @protocols: bitset of nfc protocols that must be used for polling
118 *
119 * The device remains polling for targets until a target is found or
120 * the nfc_stop_poll function is called.
121 */
122int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
123{
124 int rc;
125
20c239c1
JP
126 pr_debug("dev_name=%s protocols=0x%x\n",
127 dev_name(&dev->dev), protocols);
3e256b8f
LRV
128
129 if (!protocols)
130 return -EINVAL;
131
132 device_lock(&dev->dev);
133
134 if (!device_is_registered(&dev->dev)) {
135 rc = -ENODEV;
136 goto error;
137 }
138
139 if (dev->polling) {
140 rc = -EBUSY;
141 goto error;
142 }
143
144 rc = dev->ops->start_poll(dev, protocols);
145 if (!rc)
146 dev->polling = true;
147
148error:
149 device_unlock(&dev->dev);
150 return rc;
151}
152
153/**
154 * nfc_stop_poll - stop polling for nfc targets
155 *
156 * @dev: The nfc device that must stop polling
157 */
158int nfc_stop_poll(struct nfc_dev *dev)
159{
160 int rc = 0;
161
20c239c1 162 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
163
164 device_lock(&dev->dev);
165
166 if (!device_is_registered(&dev->dev)) {
167 rc = -ENODEV;
168 goto error;
169 }
170
171 if (!dev->polling) {
172 rc = -EINVAL;
173 goto error;
174 }
175
176 dev->ops->stop_poll(dev);
177 dev->polling = false;
178
179error:
180 device_unlock(&dev->dev);
181 return rc;
182}
183
47807d3d 184int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
1ed28f61
SO
185{
186 int rc = 0;
47807d3d
SO
187 u8 *gb;
188 size_t gb_len;
1ed28f61 189
47807d3d 190 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
1ed28f61
SO
191
192 if (!dev->ops->dep_link_up)
193 return -EOPNOTSUPP;
194
195 device_lock(&dev->dev);
196
197 if (!device_is_registered(&dev->dev)) {
198 rc = -ENODEV;
199 goto error;
200 }
201
202 if (dev->dep_link_up == true) {
203 rc = -EALREADY;
204 goto error;
205 }
206
47807d3d
SO
207 gb = nfc_llcp_general_bytes(dev, &gb_len);
208 if (gb_len > NFC_MAX_GT_LEN) {
209 rc = -EINVAL;
210 goto error;
211 }
212
213 rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len);
1ed28f61
SO
214
215error:
216 device_unlock(&dev->dev);
217 return rc;
218}
219
220int nfc_dep_link_down(struct nfc_dev *dev)
221{
222 int rc = 0;
223
224 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
225
226 if (!dev->ops->dep_link_down)
227 return -EOPNOTSUPP;
228
229 device_lock(&dev->dev);
230
231 if (!device_is_registered(&dev->dev)) {
232 rc = -ENODEV;
233 goto error;
234 }
235
236 if (dev->dep_link_up == false) {
237 rc = -EALREADY;
238 goto error;
239 }
240
241 if (dev->dep_rf_mode == NFC_RF_TARGET) {
242 rc = -EOPNOTSUPP;
243 goto error;
244 }
245
246 rc = dev->ops->dep_link_down(dev);
247 if (!rc) {
248 dev->dep_link_up = false;
d646960f 249 nfc_llcp_mac_is_down(dev);
1ed28f61
SO
250 nfc_genl_dep_link_down_event(dev);
251 }
252
253error:
254 device_unlock(&dev->dev);
255 return rc;
256}
257
258int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
0a40acb2 259 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
260{
261 dev->dep_link_up = true;
262 dev->dep_rf_mode = rf_mode;
263
d646960f
SO
264 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
265
1ed28f61
SO
266 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
267}
268EXPORT_SYMBOL(nfc_dep_link_is_up);
269
3e256b8f
LRV
270/**
271 * nfc_activate_target - prepare the target for data exchange
272 *
273 * @dev: The nfc device that found the target
274 * @target_idx: index of the target that must be activated
275 * @protocol: nfc protocol that will be used for data exchange
276 */
277int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
278{
279 int rc;
280
20c239c1
JP
281 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
282 dev_name(&dev->dev), target_idx, protocol);
3e256b8f
LRV
283
284 device_lock(&dev->dev);
285
286 if (!device_is_registered(&dev->dev)) {
287 rc = -ENODEV;
288 goto error;
289 }
290
291 rc = dev->ops->activate_target(dev, target_idx, protocol);
8b3fe7b5
IE
292 if (!rc)
293 dev->remote_activated = true;
3e256b8f
LRV
294
295error:
296 device_unlock(&dev->dev);
297 return rc;
298}
299
300/**
301 * nfc_deactivate_target - deactivate a nfc target
302 *
303 * @dev: The nfc device that found the target
304 * @target_idx: index of the target that must be deactivated
305 */
306int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
307{
308 int rc = 0;
309
20c239c1
JP
310 pr_debug("dev_name=%s target_idx=%u\n",
311 dev_name(&dev->dev), target_idx);
3e256b8f
LRV
312
313 device_lock(&dev->dev);
314
315 if (!device_is_registered(&dev->dev)) {
316 rc = -ENODEV;
317 goto error;
318 }
319
320 dev->ops->deactivate_target(dev, target_idx);
8b3fe7b5 321 dev->remote_activated = false;
3e256b8f
LRV
322
323error:
324 device_unlock(&dev->dev);
325 return rc;
326}
327
328/**
329 * nfc_data_exchange - transceive data
330 *
331 * @dev: The nfc device that found the target
332 * @target_idx: index of the target
333 * @skb: data to be sent
334 * @cb: callback called when the response is received
335 * @cb_context: parameter for the callback function
336 *
337 * The user must wait for the callback before calling this function again.
338 */
0a40acb2
SO
339int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
340 data_exchange_cb_t cb, void *cb_context)
3e256b8f
LRV
341{
342 int rc;
343
20c239c1
JP
344 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
345 dev_name(&dev->dev), target_idx, skb->len);
3e256b8f
LRV
346
347 device_lock(&dev->dev);
348
349 if (!device_is_registered(&dev->dev)) {
350 rc = -ENODEV;
351 kfree_skb(skb);
352 goto error;
353 }
354
355 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
356
357error:
358 device_unlock(&dev->dev);
359 return rc;
360}
361
541d920b
SO
362int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
363{
0a40acb2 364 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
541d920b
SO
365
366 if (gb_len > NFC_MAX_GT_LEN)
367 return -EINVAL;
368
d646960f 369 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
541d920b
SO
370}
371EXPORT_SYMBOL(nfc_set_remote_general_bytes);
372
3e256b8f 373/**
7c7cd3bf 374 * nfc_alloc_send_skb - allocate a skb for data exchange responses
3e256b8f
LRV
375 *
376 * @size: size to allocate
377 * @gfp: gfp flags
378 */
7c7cd3bf 379struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
0a40acb2
SO
380 unsigned int flags, unsigned int size,
381 unsigned int *err)
7c7cd3bf
SO
382{
383 struct sk_buff *skb;
384 unsigned int total_size;
385
386 total_size = size +
387 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
388
389 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
390 if (skb)
391 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
392
393 return skb;
394}
395
396/**
397 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
398 *
399 * @size: size to allocate
400 * @gfp: gfp flags
401 */
402struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
3e256b8f
LRV
403{
404 struct sk_buff *skb;
405 unsigned int total_size;
406
407 total_size = size + 1;
408 skb = alloc_skb(total_size, gfp);
409
410 if (skb)
411 skb_reserve(skb, 1);
412
413 return skb;
414}
7c7cd3bf 415EXPORT_SYMBOL(nfc_alloc_recv_skb);
3e256b8f 416
4d12b8b1
LRV
417/**
418 * nfc_targets_found - inform that targets were found
419 *
420 * @dev: The nfc device that found the targets
421 * @targets: array of nfc targets found
422 * @ntargets: targets array size
423 *
424 * The device driver must call this function when one or many nfc targets
425 * are found. After calling this function, the device driver must stop
426 * polling for targets.
427 */
0a40acb2
SO
428int nfc_targets_found(struct nfc_dev *dev,
429 struct nfc_target *targets, int n_targets)
4d12b8b1 430{
c4fbb651
SO
431 int i;
432
20c239c1 433 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
4d12b8b1
LRV
434
435 dev->polling = false;
436
c4fbb651
SO
437 for (i = 0; i < n_targets; i++)
438 targets[i].idx = dev->target_idx++;
439
4d12b8b1
LRV
440 spin_lock_bh(&dev->targets_lock);
441
442 dev->targets_generation++;
443
444 kfree(dev->targets);
445 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
0a40acb2 446 GFP_ATOMIC);
4d12b8b1
LRV
447
448 if (!dev->targets) {
449 dev->n_targets = 0;
450 spin_unlock_bh(&dev->targets_lock);
451 return -ENOMEM;
452 }
453
454 dev->n_targets = n_targets;
455 spin_unlock_bh(&dev->targets_lock);
456
457 nfc_genl_targets_found(dev);
458
459 return 0;
460}
461EXPORT_SYMBOL(nfc_targets_found);
462
e1da0efa
EL
463int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
464{
465 struct nfc_target *tg;
466 int i;
467
468 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
469
470 spin_lock_bh(&dev->targets_lock);
471
472 for (i = 0; i < dev->n_targets; i++) {
473 tg = &dev->targets[i];
474 if (tg->idx == target_idx)
475 break;
476 }
477
478 if (i == dev->n_targets) {
479 spin_unlock_bh(&dev->targets_lock);
480 return -EINVAL;
481 }
482
483 dev->targets_generation++;
484 dev->n_targets--;
485
486 if (dev->n_targets) {
487 memcpy(&dev->targets[i], &dev->targets[i + 1],
488 (dev->n_targets - i) * sizeof(struct nfc_target));
489 } else {
490 kfree(dev->targets);
491 dev->targets = NULL;
492 }
493
494 spin_unlock_bh(&dev->targets_lock);
495
496 nfc_genl_target_lost(dev, target_idx);
497
498 return 0;
499}
500EXPORT_SYMBOL(nfc_target_lost);
501
3e256b8f
LRV
502static void nfc_release(struct device *d)
503{
504 struct nfc_dev *dev = to_nfc_dev(d);
505
20c239c1 506 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 507
4d12b8b1
LRV
508 nfc_genl_data_exit(&dev->genl_data);
509 kfree(dev->targets);
3e256b8f
LRV
510 kfree(dev);
511}
512
513struct class nfc_class = {
514 .name = "nfc",
515 .dev_release = nfc_release,
516};
517EXPORT_SYMBOL(nfc_class);
518
519static int match_idx(struct device *d, void *data)
520{
521 struct nfc_dev *dev = to_nfc_dev(d);
522 unsigned *idx = data;
523
524 return dev->idx == *idx;
525}
526
527struct nfc_dev *nfc_get_device(unsigned idx)
528{
529 struct device *d;
530
531 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
532 if (!d)
533 return NULL;
534
535 return to_nfc_dev(d);
536}
537
538/**
539 * nfc_allocate_device - allocate a new nfc device
540 *
541 * @ops: device operations
542 * @supported_protocols: NFC protocols supported by the device
543 */
544struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
0a40acb2
SO
545 u32 supported_protocols,
546 int tx_headroom, int tx_tailroom)
3e256b8f
LRV
547{
548 static atomic_t dev_no = ATOMIC_INIT(0);
549 struct nfc_dev *dev;
550
551 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
0a40acb2 552 !ops->deactivate_target || !ops->data_exchange)
3e256b8f
LRV
553 return NULL;
554
555 if (!supported_protocols)
556 return NULL;
557
558 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
559 if (!dev)
560 return NULL;
561
562 dev->dev.class = &nfc_class;
563 dev->idx = atomic_inc_return(&dev_no) - 1;
564 dev_set_name(&dev->dev, "nfc%d", dev->idx);
565 device_initialize(&dev->dev);
566
567 dev->ops = ops;
568 dev->supported_protocols = supported_protocols;
e8753043
SO
569 dev->tx_headroom = tx_headroom;
570 dev->tx_tailroom = tx_tailroom;
3e256b8f 571
4d12b8b1
LRV
572 spin_lock_init(&dev->targets_lock);
573 nfc_genl_data_init(&dev->genl_data);
574
575 /* first generation must not be 0 */
576 dev->targets_generation = 1;
577
3e256b8f
LRV
578 return dev;
579}
580EXPORT_SYMBOL(nfc_allocate_device);
581
582/**
583 * nfc_register_device - register a nfc device in the nfc subsystem
584 *
585 * @dev: The nfc device to register
586 */
587int nfc_register_device(struct nfc_dev *dev)
588{
589 int rc;
590
20c239c1 591 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
592
593 mutex_lock(&nfc_devlist_mutex);
594 nfc_devlist_generation++;
595 rc = device_add(&dev->dev);
596 mutex_unlock(&nfc_devlist_mutex);
597
4d12b8b1
LRV
598 if (rc < 0)
599 return rc;
600
d646960f
SO
601 rc = nfc_llcp_register_device(dev);
602 if (rc)
603 pr_err("Could not register llcp device\n");
604
4d12b8b1
LRV
605 rc = nfc_genl_device_added(dev);
606 if (rc)
20c239c1
JP
607 pr_debug("The userspace won't be notified that the device %s was added\n",
608 dev_name(&dev->dev));
4d12b8b1
LRV
609
610 return 0;
3e256b8f
LRV
611}
612EXPORT_SYMBOL(nfc_register_device);
613
614/**
615 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
616 *
617 * @dev: The nfc device to unregister
618 */
619void nfc_unregister_device(struct nfc_dev *dev)
620{
4d12b8b1
LRV
621 int rc;
622
20c239c1 623 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
624
625 mutex_lock(&nfc_devlist_mutex);
626 nfc_devlist_generation++;
627
628 /* lock to avoid unregistering a device while an operation
629 is in progress */
630 device_lock(&dev->dev);
631 device_del(&dev->dev);
632 device_unlock(&dev->dev);
633
634 mutex_unlock(&nfc_devlist_mutex);
4d12b8b1 635
d646960f
SO
636 nfc_llcp_unregister_device(dev);
637
4d12b8b1
LRV
638 rc = nfc_genl_device_removed(dev);
639 if (rc)
20c239c1
JP
640 pr_debug("The userspace won't be notified that the device %s was removed\n",
641 dev_name(&dev->dev));
4d12b8b1 642
3e256b8f
LRV
643}
644EXPORT_SYMBOL(nfc_unregister_device);
645
646static int __init nfc_init(void)
647{
4d12b8b1
LRV
648 int rc;
649
ed1e0ad8 650 pr_info("NFC Core ver %s\n", VERSION);
3e256b8f 651
4d12b8b1
LRV
652 rc = class_register(&nfc_class);
653 if (rc)
654 return rc;
655
656 rc = nfc_genl_init();
657 if (rc)
658 goto err_genl;
659
660 /* the first generation must not be 0 */
661 nfc_devlist_generation = 1;
662
23b7869c
LRV
663 rc = rawsock_init();
664 if (rc)
665 goto err_rawsock;
666
d646960f
SO
667 rc = nfc_llcp_init();
668 if (rc)
669 goto err_llcp_sock;
670
c7fe3b52
AAJ
671 rc = af_nfc_init();
672 if (rc)
673 goto err_af_nfc;
674
4d12b8b1
LRV
675 return 0;
676
c7fe3b52 677err_af_nfc:
d646960f
SO
678 nfc_llcp_exit();
679err_llcp_sock:
23b7869c
LRV
680 rawsock_exit();
681err_rawsock:
c7fe3b52 682 nfc_genl_exit();
4d12b8b1
LRV
683err_genl:
684 class_unregister(&nfc_class);
685 return rc;
3e256b8f
LRV
686}
687
688static void __exit nfc_exit(void)
689{
c7fe3b52 690 af_nfc_exit();
d646960f 691 nfc_llcp_exit();
23b7869c 692 rawsock_exit();
4d12b8b1 693 nfc_genl_exit();
3e256b8f
LRV
694 class_unregister(&nfc_class);
695}
696
697subsys_initcall(nfc_init);
698module_exit(nfc_exit);
699
700MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
701MODULE_DESCRIPTION("NFC Core ver " VERSION);
702MODULE_VERSION(VERSION);
703MODULE_LICENSE("GPL");
This page took 0.09954 seconds and 5 git commands to generate.