NFC: Extend and fix the internal secure element API
[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>
be055b2f 30#include <linux/rfkill.h>
7c7cd3bf 31#include <linux/nfc.h>
3e256b8f 32
5df16cad
SO
33#include <net/genetlink.h>
34
3e256b8f
LRV
35#include "nfc.h"
36
37#define VERSION "0.1"
38
c8d56ae7
EL
39#define NFC_CHECK_PRES_FREQ_MS 2000
40
3e256b8f
LRV
41int nfc_devlist_generation;
42DEFINE_MUTEX(nfc_devlist_mutex);
43
7eda8b8e
SO
44/* NFC device ID bitmap */
45static DEFINE_IDA(nfc_index_ida);
46
9674da87
EL
47int nfc_fw_upload(struct nfc_dev *dev, const char *firmware_name)
48{
49 int rc = 0;
50
51 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
52
53 device_lock(&dev->dev);
54
55 if (!device_is_registered(&dev->dev)) {
56 rc = -ENODEV;
57 goto error;
58 }
59
60 if (dev->dev_up) {
61 rc = -EBUSY;
62 goto error;
63 }
64
65 if (!dev->ops->fw_upload) {
66 rc = -EOPNOTSUPP;
67 goto error;
68 }
69
70 dev->fw_upload_in_progress = true;
71 rc = dev->ops->fw_upload(dev, firmware_name);
72 if (rc)
73 dev->fw_upload_in_progress = false;
74
75error:
76 device_unlock(&dev->dev);
77 return rc;
78}
79
80int nfc_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
81{
82 dev->fw_upload_in_progress = false;
83
84 return nfc_genl_fw_upload_done(dev, firmware_name);
85}
86EXPORT_SYMBOL(nfc_fw_upload_done);
87
8b3fe7b5
IE
88/**
89 * nfc_dev_up - turn on the NFC device
90 *
91 * @dev: The nfc device to be turned on
92 *
93 * The device remains up until the nfc_dev_down function is called.
94 */
95int nfc_dev_up(struct nfc_dev *dev)
96{
97 int rc = 0;
98
20c239c1 99 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
100
101 device_lock(&dev->dev);
102
be055b2f
SO
103 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
104 rc = -ERFKILL;
105 goto error;
106 }
107
8b3fe7b5
IE
108 if (!device_is_registered(&dev->dev)) {
109 rc = -ENODEV;
110 goto error;
111 }
112
9674da87
EL
113 if (dev->fw_upload_in_progress) {
114 rc = -EBUSY;
115 goto error;
116 }
117
8b3fe7b5
IE
118 if (dev->dev_up) {
119 rc = -EALREADY;
120 goto error;
121 }
122
123 if (dev->ops->dev_up)
124 rc = dev->ops->dev_up(dev);
125
126 if (!rc)
127 dev->dev_up = true;
128
0a946301
SO
129 /* We have to enable the device before discovering SEs */
130 if (dev->ops->discover_se) {
131 rc = dev->ops->discover_se(dev);
132 if (!rc)
133 pr_warn("SE discovery failed\n");
134 }
135
8b3fe7b5
IE
136error:
137 device_unlock(&dev->dev);
138 return rc;
139}
140
141/**
142 * nfc_dev_down - turn off the NFC device
143 *
144 * @dev: The nfc device to be turned off
145 */
146int nfc_dev_down(struct nfc_dev *dev)
147{
148 int rc = 0;
149
20c239c1 150 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
8b3fe7b5
IE
151
152 device_lock(&dev->dev);
153
154 if (!device_is_registered(&dev->dev)) {
155 rc = -ENODEV;
156 goto error;
157 }
158
159 if (!dev->dev_up) {
160 rc = -EALREADY;
161 goto error;
162 }
163
90099433 164 if (dev->polling || dev->active_target) {
8b3fe7b5
IE
165 rc = -EBUSY;
166 goto error;
167 }
168
169 if (dev->ops->dev_down)
170 dev->ops->dev_down(dev);
171
172 dev->dev_up = false;
173
174error:
175 device_unlock(&dev->dev);
176 return rc;
177}
178
be055b2f
SO
179static int nfc_rfkill_set_block(void *data, bool blocked)
180{
181 struct nfc_dev *dev = data;
182
183 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
184
185 if (!blocked)
186 return 0;
187
188 nfc_dev_down(dev);
189
190 return 0;
191}
192
193static const struct rfkill_ops nfc_rfkill_ops = {
194 .set_block = nfc_rfkill_set_block,
195};
196
3e256b8f
LRV
197/**
198 * nfc_start_poll - start polling for nfc targets
199 *
200 * @dev: The nfc device that must start polling
201 * @protocols: bitset of nfc protocols that must be used for polling
202 *
203 * The device remains polling for targets until a target is found or
204 * the nfc_stop_poll function is called.
205 */
fe7c5800 206int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
3e256b8f
LRV
207{
208 int rc;
209
fe7c5800
SO
210 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
211 dev_name(&dev->dev), im_protocols, tm_protocols);
3e256b8f 212
fe7c5800 213 if (!im_protocols && !tm_protocols)
3e256b8f
LRV
214 return -EINVAL;
215
216 device_lock(&dev->dev);
217
218 if (!device_is_registered(&dev->dev)) {
219 rc = -ENODEV;
220 goto error;
221 }
222
7757dc8a
SO
223 if (!dev->dev_up) {
224 rc = -ENODEV;
225 goto error;
226 }
227
3e256b8f
LRV
228 if (dev->polling) {
229 rc = -EBUSY;
230 goto error;
231 }
232
fe7c5800 233 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
f212ad5e 234 if (!rc) {
3e256b8f 235 dev->polling = true;
f212ad5e
SO
236 dev->rf_mode = NFC_RF_NONE;
237 }
3e256b8f
LRV
238
239error:
240 device_unlock(&dev->dev);
241 return rc;
242}
243
244/**
245 * nfc_stop_poll - stop polling for nfc targets
246 *
247 * @dev: The nfc device that must stop polling
248 */
249int nfc_stop_poll(struct nfc_dev *dev)
250{
251 int rc = 0;
252
20c239c1 253 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
254
255 device_lock(&dev->dev);
256
257 if (!device_is_registered(&dev->dev)) {
258 rc = -ENODEV;
259 goto error;
260 }
261
262 if (!dev->polling) {
263 rc = -EINVAL;
264 goto error;
265 }
266
267 dev->ops->stop_poll(dev);
268 dev->polling = false;
5bcf099c 269 dev->rf_mode = NFC_RF_NONE;
3e256b8f
LRV
270
271error:
272 device_unlock(&dev->dev);
273 return rc;
274}
275
90099433
EL
276static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
277{
278 int i;
279
280 if (dev->n_targets == 0)
281 return NULL;
282
0f450772 283 for (i = 0; i < dev->n_targets; i++) {
90099433
EL
284 if (dev->targets[i].idx == target_idx)
285 return &dev->targets[i];
286 }
287
288 return NULL;
289}
290
47807d3d 291int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
1ed28f61
SO
292{
293 int rc = 0;
47807d3d
SO
294 u8 *gb;
295 size_t gb_len;
90099433 296 struct nfc_target *target;
1ed28f61 297
47807d3d 298 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
1ed28f61
SO
299
300 if (!dev->ops->dep_link_up)
301 return -EOPNOTSUPP;
302
303 device_lock(&dev->dev);
304
305 if (!device_is_registered(&dev->dev)) {
306 rc = -ENODEV;
307 goto error;
308 }
309
310 if (dev->dep_link_up == true) {
311 rc = -EALREADY;
312 goto error;
313 }
314
47807d3d
SO
315 gb = nfc_llcp_general_bytes(dev, &gb_len);
316 if (gb_len > NFC_MAX_GT_LEN) {
317 rc = -EINVAL;
318 goto error;
319 }
320
90099433
EL
321 target = nfc_find_target(dev, target_index);
322 if (target == NULL) {
323 rc = -ENOTCONN;
324 goto error;
325 }
326
327 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
f212ad5e 328 if (!rc) {
90099433 329 dev->active_target = target;
f212ad5e
SO
330 dev->rf_mode = NFC_RF_INITIATOR;
331 }
1ed28f61
SO
332
333error:
334 device_unlock(&dev->dev);
335 return rc;
336}
337
338int nfc_dep_link_down(struct nfc_dev *dev)
339{
340 int rc = 0;
341
342 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
343
344 if (!dev->ops->dep_link_down)
345 return -EOPNOTSUPP;
346
347 device_lock(&dev->dev);
348
349 if (!device_is_registered(&dev->dev)) {
350 rc = -ENODEV;
351 goto error;
352 }
353
354 if (dev->dep_link_up == false) {
355 rc = -EALREADY;
356 goto error;
357 }
358
1ed28f61
SO
359 rc = dev->ops->dep_link_down(dev);
360 if (!rc) {
361 dev->dep_link_up = false;
90099433 362 dev->active_target = NULL;
5bcf099c 363 dev->rf_mode = NFC_RF_NONE;
d646960f 364 nfc_llcp_mac_is_down(dev);
1ed28f61
SO
365 nfc_genl_dep_link_down_event(dev);
366 }
367
368error:
369 device_unlock(&dev->dev);
5bcf099c 370
1ed28f61
SO
371 return rc;
372}
373
374int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
0a40acb2 375 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
376{
377 dev->dep_link_up = true;
1ed28f61 378
d646960f
SO
379 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
380
1ed28f61
SO
381 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
382}
383EXPORT_SYMBOL(nfc_dep_link_is_up);
384
3e256b8f
LRV
385/**
386 * nfc_activate_target - prepare the target for data exchange
387 *
388 * @dev: The nfc device that found the target
389 * @target_idx: index of the target that must be activated
390 * @protocol: nfc protocol that will be used for data exchange
391 */
392int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
393{
394 int rc;
90099433 395 struct nfc_target *target;
3e256b8f 396
20c239c1
JP
397 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
398 dev_name(&dev->dev), target_idx, protocol);
3e256b8f
LRV
399
400 device_lock(&dev->dev);
401
402 if (!device_is_registered(&dev->dev)) {
403 rc = -ENODEV;
404 goto error;
405 }
406
90099433
EL
407 if (dev->active_target) {
408 rc = -EBUSY;
409 goto error;
410 }
411
412 target = nfc_find_target(dev, target_idx);
413 if (target == NULL) {
414 rc = -ENOTCONN;
415 goto error;
416 }
417
418 rc = dev->ops->activate_target(dev, target, protocol);
c8d56ae7 419 if (!rc) {
90099433 420 dev->active_target = target;
f212ad5e 421 dev->rf_mode = NFC_RF_INITIATOR;
3e256b8f 422
f0c91038 423 if (dev->ops->check_presence && !dev->shutting_down)
c8d56ae7
EL
424 mod_timer(&dev->check_pres_timer, jiffies +
425 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
426 }
3e256b8f
LRV
427
428error:
429 device_unlock(&dev->dev);
430 return rc;
431}
432
433/**
434 * nfc_deactivate_target - deactivate a nfc target
435 *
436 * @dev: The nfc device that found the target
437 * @target_idx: index of the target that must be deactivated
438 */
439int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
440{
441 int rc = 0;
442
20c239c1
JP
443 pr_debug("dev_name=%s target_idx=%u\n",
444 dev_name(&dev->dev), target_idx);
3e256b8f
LRV
445
446 device_lock(&dev->dev);
447
448 if (!device_is_registered(&dev->dev)) {
449 rc = -ENODEV;
450 goto error;
451 }
452
90099433
EL
453 if (dev->active_target == NULL) {
454 rc = -ENOTCONN;
455 goto error;
456 }
457
458 if (dev->active_target->idx != target_idx) {
459 rc = -ENOTCONN;
460 goto error;
461 }
462
c8d56ae7
EL
463 if (dev->ops->check_presence)
464 del_timer_sync(&dev->check_pres_timer);
465
90099433
EL
466 dev->ops->deactivate_target(dev, dev->active_target);
467 dev->active_target = NULL;
3e256b8f
LRV
468
469error:
470 device_unlock(&dev->dev);
471 return rc;
472}
473
474/**
475 * nfc_data_exchange - transceive data
476 *
477 * @dev: The nfc device that found the target
478 * @target_idx: index of the target
479 * @skb: data to be sent
480 * @cb: callback called when the response is received
481 * @cb_context: parameter for the callback function
482 *
483 * The user must wait for the callback before calling this function again.
484 */
0a40acb2
SO
485int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
486 data_exchange_cb_t cb, void *cb_context)
3e256b8f
LRV
487{
488 int rc;
489
20c239c1
JP
490 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
491 dev_name(&dev->dev), target_idx, skb->len);
3e256b8f
LRV
492
493 device_lock(&dev->dev);
494
495 if (!device_is_registered(&dev->dev)) {
496 rc = -ENODEV;
497 kfree_skb(skb);
498 goto error;
499 }
500
be9ae4ce
SO
501 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
502 if (dev->active_target->idx != target_idx) {
503 rc = -EADDRNOTAVAIL;
504 kfree_skb(skb);
505 goto error;
506 }
144612ca 507
be9ae4ce
SO
508 if (dev->ops->check_presence)
509 del_timer_sync(&dev->check_pres_timer);
510
511 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
512 cb_context);
513
f0c91038 514 if (!rc && dev->ops->check_presence && !dev->shutting_down)
be9ae4ce
SO
515 mod_timer(&dev->check_pres_timer, jiffies +
516 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
517 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
518 rc = dev->ops->tm_send(dev, skb);
519 } else {
520 rc = -ENOTCONN;
144612ca
EL
521 kfree_skb(skb);
522 goto error;
523 }
524
c8d56ae7 525
3e256b8f
LRV
526error:
527 device_unlock(&dev->dev);
528 return rc;
529}
530
541d920b
SO
531int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
532{
0a40acb2 533 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
541d920b
SO
534
535 if (gb_len > NFC_MAX_GT_LEN)
536 return -EINVAL;
537
d646960f 538 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
541d920b
SO
539}
540EXPORT_SYMBOL(nfc_set_remote_general_bytes);
541
ab73b751
SO
542u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
543{
544 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
545
546 return nfc_llcp_general_bytes(dev, gb_len);
547}
548EXPORT_SYMBOL(nfc_get_local_general_bytes);
549
73167ced
SO
550int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
551{
552 /* Only LLCP target mode for now */
553 if (dev->dep_link_up == false) {
554 kfree_skb(skb);
555 return -ENOLINK;
556 }
557
558 return nfc_llcp_data_received(dev, skb);
559}
560EXPORT_SYMBOL(nfc_tm_data_received);
561
fc40a8c1
SO
562int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
563 u8 *gb, size_t gb_len)
564{
565 int rc;
566
567 device_lock(&dev->dev);
568
569 dev->polling = false;
570
571 if (gb != NULL) {
572 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
573 if (rc < 0)
574 goto out;
575 }
576
f212ad5e
SO
577 dev->rf_mode = NFC_RF_TARGET;
578
fc40a8c1
SO
579 if (protocol == NFC_PROTO_NFC_DEP_MASK)
580 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
581
582 rc = nfc_genl_tm_activated(dev, protocol);
583
584out:
585 device_unlock(&dev->dev);
586
587 return rc;
588}
589EXPORT_SYMBOL(nfc_tm_activated);
590
591int nfc_tm_deactivated(struct nfc_dev *dev)
592{
593 dev->dep_link_up = false;
5bcf099c 594 dev->rf_mode = NFC_RF_NONE;
fc40a8c1
SO
595
596 return nfc_genl_tm_deactivated(dev);
597}
598EXPORT_SYMBOL(nfc_tm_deactivated);
599
3e256b8f 600/**
7c7cd3bf 601 * nfc_alloc_send_skb - allocate a skb for data exchange responses
3e256b8f
LRV
602 *
603 * @size: size to allocate
604 * @gfp: gfp flags
605 */
7c7cd3bf 606struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
0a40acb2
SO
607 unsigned int flags, unsigned int size,
608 unsigned int *err)
7c7cd3bf
SO
609{
610 struct sk_buff *skb;
611 unsigned int total_size;
612
613 total_size = size +
614 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
615
616 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
617 if (skb)
618 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
619
620 return skb;
621}
622
623/**
624 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
625 *
626 * @size: size to allocate
627 * @gfp: gfp flags
628 */
629struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
3e256b8f
LRV
630{
631 struct sk_buff *skb;
632 unsigned int total_size;
633
634 total_size = size + 1;
635 skb = alloc_skb(total_size, gfp);
636
637 if (skb)
638 skb_reserve(skb, 1);
639
640 return skb;
641}
7c7cd3bf 642EXPORT_SYMBOL(nfc_alloc_recv_skb);
3e256b8f 643
4d12b8b1
LRV
644/**
645 * nfc_targets_found - inform that targets were found
646 *
647 * @dev: The nfc device that found the targets
648 * @targets: array of nfc targets found
649 * @ntargets: targets array size
650 *
651 * The device driver must call this function when one or many nfc targets
652 * are found. After calling this function, the device driver must stop
653 * polling for targets.
d94f9c55
EL
654 * NOTE: This function can be called with targets=NULL and n_targets=0 to
655 * notify a driver error, meaning that the polling operation cannot complete.
d4ccb132
EL
656 * IMPORTANT: this function must not be called from an atomic context.
657 * In addition, it must also not be called from a context that would prevent
658 * the NFC Core to call other nfc ops entry point concurrently.
4d12b8b1 659 */
0a40acb2
SO
660int nfc_targets_found(struct nfc_dev *dev,
661 struct nfc_target *targets, int n_targets)
4d12b8b1 662{
c4fbb651
SO
663 int i;
664
20c239c1 665 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
4d12b8b1 666
c4fbb651 667 for (i = 0; i < n_targets; i++)
01ae0eea 668 targets[i].idx = dev->target_next_idx++;
c4fbb651 669
d4ccb132 670 device_lock(&dev->dev);
4d12b8b1 671
8668fdd6
EL
672 if (dev->polling == false) {
673 device_unlock(&dev->dev);
674 return 0;
675 }
676
677 dev->polling = false;
678
4d12b8b1
LRV
679 dev->targets_generation++;
680
681 kfree(dev->targets);
d94f9c55 682 dev->targets = NULL;
4d12b8b1 683
d94f9c55
EL
684 if (targets) {
685 dev->targets = kmemdup(targets,
686 n_targets * sizeof(struct nfc_target),
687 GFP_ATOMIC);
688
689 if (!dev->targets) {
690 dev->n_targets = 0;
691 device_unlock(&dev->dev);
692 return -ENOMEM;
693 }
4d12b8b1
LRV
694 }
695
696 dev->n_targets = n_targets;
d4ccb132 697 device_unlock(&dev->dev);
4d12b8b1
LRV
698
699 nfc_genl_targets_found(dev);
700
701 return 0;
702}
703EXPORT_SYMBOL(nfc_targets_found);
704
d4ccb132
EL
705/**
706 * nfc_target_lost - inform that an activated target went out of field
707 *
708 * @dev: The nfc device that had the activated target in field
709 * @target_idx: the nfc index of the target
710 *
711 * The device driver must call this function when the activated target
712 * goes out of the field.
713 * IMPORTANT: this function must not be called from an atomic context.
714 * In addition, it must also not be called from a context that would prevent
715 * the NFC Core to call other nfc ops entry point concurrently.
716 */
e1da0efa
EL
717int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
718{
719 struct nfc_target *tg;
720 int i;
721
722 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
723
d4ccb132 724 device_lock(&dev->dev);
e1da0efa
EL
725
726 for (i = 0; i < dev->n_targets; i++) {
727 tg = &dev->targets[i];
728 if (tg->idx == target_idx)
729 break;
730 }
731
732 if (i == dev->n_targets) {
d4ccb132 733 device_unlock(&dev->dev);
e1da0efa
EL
734 return -EINVAL;
735 }
736
737 dev->targets_generation++;
738 dev->n_targets--;
90099433 739 dev->active_target = NULL;
e1da0efa
EL
740
741 if (dev->n_targets) {
742 memcpy(&dev->targets[i], &dev->targets[i + 1],
743 (dev->n_targets - i) * sizeof(struct nfc_target));
744 } else {
745 kfree(dev->targets);
746 dev->targets = NULL;
747 }
748
d4ccb132 749 device_unlock(&dev->dev);
e1da0efa
EL
750
751 nfc_genl_target_lost(dev, target_idx);
752
753 return 0;
754}
755EXPORT_SYMBOL(nfc_target_lost);
756
9eb334ac 757inline void nfc_driver_failure(struct nfc_dev *dev, int err)
456411ca 758{
9eb334ac 759 nfc_targets_found(dev, NULL, 0);
456411ca
EL
760}
761EXPORT_SYMBOL(nfc_driver_failure);
762
3e256b8f
LRV
763static void nfc_release(struct device *d)
764{
765 struct nfc_dev *dev = to_nfc_dev(d);
766
20c239c1 767 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 768
4d12b8b1
LRV
769 nfc_genl_data_exit(&dev->genl_data);
770 kfree(dev->targets);
3e256b8f
LRV
771 kfree(dev);
772}
773
c8d56ae7
EL
774static void nfc_check_pres_work(struct work_struct *work)
775{
776 struct nfc_dev *dev = container_of(work, struct nfc_dev,
777 check_pres_work);
778 int rc;
779
780 device_lock(&dev->dev);
781
90099433
EL
782 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
783 rc = dev->ops->check_presence(dev, dev->active_target);
632c016a
EL
784 if (rc == -EOPNOTSUPP)
785 goto exit;
f0c91038 786 if (rc) {
d4ccb132
EL
787 u32 active_target_idx = dev->active_target->idx;
788 device_unlock(&dev->dev);
789 nfc_target_lost(dev, active_target_idx);
790 return;
c8d56ae7 791 }
f0c91038
EL
792
793 if (!dev->shutting_down)
794 mod_timer(&dev->check_pres_timer, jiffies +
795 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
c8d56ae7
EL
796 }
797
632c016a 798exit:
c8d56ae7
EL
799 device_unlock(&dev->dev);
800}
801
802static void nfc_check_pres_timeout(unsigned long data)
803{
804 struct nfc_dev *dev = (struct nfc_dev *)data;
805
916082b0 806 schedule_work(&dev->check_pres_work);
c8d56ae7
EL
807}
808
3e256b8f
LRV
809struct class nfc_class = {
810 .name = "nfc",
811 .dev_release = nfc_release,
812};
813EXPORT_SYMBOL(nfc_class);
814
9f3b795a 815static int match_idx(struct device *d, const void *data)
3e256b8f
LRV
816{
817 struct nfc_dev *dev = to_nfc_dev(d);
9f3b795a 818 const unsigned int *idx = data;
3e256b8f
LRV
819
820 return dev->idx == *idx;
821}
822
95c96174 823struct nfc_dev *nfc_get_device(unsigned int idx)
3e256b8f
LRV
824{
825 struct device *d;
826
827 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
828 if (!d)
829 return NULL;
830
831 return to_nfc_dev(d);
832}
833
834/**
835 * nfc_allocate_device - allocate a new nfc device
836 *
837 * @ops: device operations
838 * @supported_protocols: NFC protocols supported by the device
839 */
840struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
0a40acb2
SO
841 u32 supported_protocols,
842 int tx_headroom, int tx_tailroom)
3e256b8f 843{
3e256b8f
LRV
844 struct nfc_dev *dev;
845
846 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
be9ae4ce 847 !ops->deactivate_target || !ops->im_transceive)
3e256b8f
LRV
848 return NULL;
849
850 if (!supported_protocols)
851 return NULL;
852
853 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
854 if (!dev)
855 return NULL;
856
3e256b8f
LRV
857 dev->ops = ops;
858 dev->supported_protocols = supported_protocols;
390a1bd8 859 dev->active_se = NFC_SE_NONE;
e8753043
SO
860 dev->tx_headroom = tx_headroom;
861 dev->tx_tailroom = tx_tailroom;
3e256b8f 862
4d12b8b1
LRV
863 nfc_genl_data_init(&dev->genl_data);
864
5bcf099c 865 dev->rf_mode = NFC_RF_NONE;
d4ccb132 866
4d12b8b1
LRV
867 /* first generation must not be 0 */
868 dev->targets_generation = 1;
869
c8d56ae7 870 if (ops->check_presence) {
c8d56ae7
EL
871 init_timer(&dev->check_pres_timer);
872 dev->check_pres_timer.data = (unsigned long)dev;
873 dev->check_pres_timer.function = nfc_check_pres_timeout;
874
875 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
c8d56ae7
EL
876 }
877
3e256b8f
LRV
878 return dev;
879}
880EXPORT_SYMBOL(nfc_allocate_device);
881
882/**
883 * nfc_register_device - register a nfc device in the nfc subsystem
884 *
885 * @dev: The nfc device to register
886 */
887int nfc_register_device(struct nfc_dev *dev)
888{
889 int rc;
890
20c239c1 891 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 892
7eda8b8e
SO
893 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
894 if (dev->idx < 0)
895 return dev->idx;
896
897 dev->dev.class = &nfc_class;
898 dev_set_name(&dev->dev, "nfc%d", dev->idx);
899 device_initialize(&dev->dev);
900
3e256b8f
LRV
901 mutex_lock(&nfc_devlist_mutex);
902 nfc_devlist_generation++;
903 rc = device_add(&dev->dev);
904 mutex_unlock(&nfc_devlist_mutex);
905
4d12b8b1
LRV
906 if (rc < 0)
907 return rc;
908
d646960f
SO
909 rc = nfc_llcp_register_device(dev);
910 if (rc)
911 pr_err("Could not register llcp device\n");
912
4d12b8b1
LRV
913 rc = nfc_genl_device_added(dev);
914 if (rc)
20c239c1
JP
915 pr_debug("The userspace won't be notified that the device %s was added\n",
916 dev_name(&dev->dev));
4d12b8b1 917
be055b2f
SO
918 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
919 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
920 if (dev->rfkill) {
921 if (rfkill_register(dev->rfkill) < 0) {
922 rfkill_destroy(dev->rfkill);
923 dev->rfkill = NULL;
924 }
925 }
926
4d12b8b1 927 return 0;
3e256b8f
LRV
928}
929EXPORT_SYMBOL(nfc_register_device);
930
931/**
932 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
933 *
934 * @dev: The nfc device to unregister
935 */
936void nfc_unregister_device(struct nfc_dev *dev)
937{
7eda8b8e 938 int rc, id;
4d12b8b1 939
20c239c1 940 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 941
7eda8b8e
SO
942 id = dev->idx;
943
be055b2f
SO
944 if (dev->rfkill) {
945 rfkill_unregister(dev->rfkill);
946 rfkill_destroy(dev->rfkill);
947 }
948
f0c91038
EL
949 if (dev->ops->check_presence) {
950 device_lock(&dev->dev);
951 dev->shutting_down = true;
952 device_unlock(&dev->dev);
953 del_timer_sync(&dev->check_pres_timer);
954 cancel_work_sync(&dev->check_pres_work);
955 }
3e256b8f 956
f0c91038
EL
957 rc = nfc_genl_device_removed(dev);
958 if (rc)
959 pr_debug("The userspace won't be notified that the device %s "
960 "was removed\n", dev_name(&dev->dev));
4d12b8b1 961
d646960f
SO
962 nfc_llcp_unregister_device(dev);
963
f0c91038
EL
964 mutex_lock(&nfc_devlist_mutex);
965 nfc_devlist_generation++;
966 device_del(&dev->dev);
967 mutex_unlock(&nfc_devlist_mutex);
4d12b8b1 968
7eda8b8e 969 ida_simple_remove(&nfc_index_ida, id);
3e256b8f
LRV
970}
971EXPORT_SYMBOL(nfc_unregister_device);
972
973static int __init nfc_init(void)
974{
4d12b8b1
LRV
975 int rc;
976
ed1e0ad8 977 pr_info("NFC Core ver %s\n", VERSION);
3e256b8f 978
4d12b8b1
LRV
979 rc = class_register(&nfc_class);
980 if (rc)
981 return rc;
982
983 rc = nfc_genl_init();
984 if (rc)
985 goto err_genl;
986
987 /* the first generation must not be 0 */
988 nfc_devlist_generation = 1;
989
23b7869c
LRV
990 rc = rawsock_init();
991 if (rc)
992 goto err_rawsock;
993
d646960f
SO
994 rc = nfc_llcp_init();
995 if (rc)
996 goto err_llcp_sock;
997
c7fe3b52
AAJ
998 rc = af_nfc_init();
999 if (rc)
1000 goto err_af_nfc;
1001
4d12b8b1
LRV
1002 return 0;
1003
c7fe3b52 1004err_af_nfc:
d646960f
SO
1005 nfc_llcp_exit();
1006err_llcp_sock:
23b7869c
LRV
1007 rawsock_exit();
1008err_rawsock:
c7fe3b52 1009 nfc_genl_exit();
4d12b8b1
LRV
1010err_genl:
1011 class_unregister(&nfc_class);
1012 return rc;
3e256b8f
LRV
1013}
1014
1015static void __exit nfc_exit(void)
1016{
c7fe3b52 1017 af_nfc_exit();
d646960f 1018 nfc_llcp_exit();
23b7869c 1019 rawsock_exit();
4d12b8b1 1020 nfc_genl_exit();
3e256b8f
LRV
1021 class_unregister(&nfc_class);
1022}
1023
1024subsys_initcall(nfc_init);
1025module_exit(nfc_exit);
1026
1027MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1028MODULE_DESCRIPTION("NFC Core ver " VERSION);
1029MODULE_VERSION(VERSION);
1030MODULE_LICENSE("GPL");
1155bb61 1031MODULE_ALIAS_NETPROTO(PF_NFC);
5df16cad 1032MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);
This page took 0.219599 seconds and 5 git commands to generate.