projects/linux·blob master · refs

1272 lines · 25.0 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 *
5 * Authors:
6 * Lauro Ramos Venancio <[email protected]>
7 * Aloisio Almeida Jr <[email protected]>
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
11
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/rfkill.h>
17#include <linux/nfc.h>
18
19#include <net/genetlink.h>
20
21#include "nfc.h"
22
23#define VERSION "0.1"
24
25#define NFC_CHECK_PRES_FREQ_MS 2000
26
27int nfc_devlist_generation;
28DEFINE_MUTEX(nfc_devlist_mutex);
29
30/* NFC device ID bitmap */
31static DEFINE_IDA(nfc_index_ida);
32
33int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
34{
35 int rc = 0;
36
37 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
38
39 device_lock(&dev->dev);
40
41 if (dev->shutting_down) {
42 rc = -ENODEV;
43 goto error;
44 }
45
46 if (dev->dev_up) {
47 rc = -EBUSY;
48 goto error;
49 }
50
51 if (!dev->ops->fw_download) {
52 rc = -EOPNOTSUPP;
53 goto error;
54 }
55
56 dev->fw_download_in_progress = true;
57 rc = dev->ops->fw_download(dev, firmware_name);
58 if (rc)
59 dev->fw_download_in_progress = false;
60
61error:
62 device_unlock(&dev->dev);
63 return rc;
64}
65
66/**
67 * nfc_fw_download_done - inform that a firmware download was completed
68 *
69 * @dev: The nfc device to which firmware was downloaded
70 * @firmware_name: The firmware filename
71 * @result: The positive value of a standard errno value
72 */
73int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
74 u32 result)
75{
76 dev->fw_download_in_progress = false;
77
78 return nfc_genl_fw_download_done(dev, firmware_name, result);
79}
80EXPORT_SYMBOL(nfc_fw_download_done);
81
82/**
83 * nfc_dev_up - turn on the NFC device
84 *
85 * @dev: The nfc device to be turned on
86 *
87 * The device remains up until the nfc_dev_down function is called.
88 */
89int nfc_dev_up(struct nfc_dev *dev)
90{
91 int rc = 0;
92
93 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
94
95 device_lock(&dev->dev);
96
97 if (dev->shutting_down) {
98 rc = -ENODEV;
99 goto error;
100 }
101
102 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
103 rc = -ERFKILL;
104 goto error;
105 }
106
107 if (dev->fw_download_in_progress) {
108 rc = -EBUSY;
109 goto error;
110 }
111
112 if (dev->dev_up) {
113 rc = -EALREADY;
114 goto error;
115 }
116
117 if (dev->ops->dev_up)
118 rc = dev->ops->dev_up(dev);
119
120 if (!rc)
121 dev->dev_up = true;
122
123 /* We have to enable the device before discovering SEs */
124 if (dev->ops->discover_se && dev->ops->discover_se(dev))
125 pr_err("SE discovery failed\n");
126
127error:
128 device_unlock(&dev->dev);
129 return rc;
130}
131
132/**
133 * nfc_dev_down - turn off the NFC device
134 *
135 * @dev: The nfc device to be turned off
136 */
137int nfc_dev_down(struct nfc_dev *dev)
138{
139 int rc = 0;
140
141 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
142
143 device_lock(&dev->dev);
144
145 if (dev->shutting_down) {
146 rc = -ENODEV;
147 goto error;
148 }
149
150 if (!dev->dev_up) {
151 rc = -EALREADY;
152 goto error;
153 }
154
155 if (dev->polling || dev->active_target) {
156 rc = -EBUSY;
157 goto error;
158 }
159
160 if (dev->ops->dev_down)
161 dev->ops->dev_down(dev);
162
163 dev->dev_up = false;
164
165error:
166 device_unlock(&dev->dev);
167 return rc;
168}
169
170static int nfc_rfkill_set_block(void *data, bool blocked)
171{
172 struct nfc_dev *dev = data;
173
174 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
175
176 if (!blocked)
177 return 0;
178
179 nfc_dev_down(dev);
180
181 return 0;
182}
183
184static const struct rfkill_ops nfc_rfkill_ops = {
185 .set_block = nfc_rfkill_set_block,
186};
187
188/**
189 * nfc_start_poll - start polling for nfc targets
190 *
191 * @dev: The nfc device that must start polling
192 * @im_protocols: bitset of nfc initiator protocols to be used for polling
193 * @tm_protocols: bitset of nfc transport protocols to be used for polling
194 *
195 * The device remains polling for targets until a target is found or
196 * the nfc_stop_poll function is called.
197 */
198int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
199{
200 int rc;
201
202 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
203 dev_name(&dev->dev), im_protocols, tm_protocols);
204
205 if (!im_protocols && !tm_protocols)
206 return -EINVAL;
207
208 device_lock(&dev->dev);
209
210 if (dev->shutting_down) {
211 rc = -ENODEV;
212 goto error;
213 }
214
215 if (!dev->dev_up) {
216 rc = -ENODEV;
217 goto error;
218 }
219
220 if (dev->polling) {
221 rc = -EBUSY;
222 goto error;
223 }
224
225 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
226 if (!rc) {
227 dev->polling = true;
228 dev->rf_mode = NFC_RF_NONE;
229 }
230
231error:
232 device_unlock(&dev->dev);
233 return rc;
234}
235
236/**
237 * nfc_stop_poll - stop polling for nfc targets
238 *
239 * @dev: The nfc device that must stop polling
240 */
241int nfc_stop_poll(struct nfc_dev *dev)
242{
243 int rc = 0;
244
245 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
246
247 device_lock(&dev->dev);
248
249 if (dev->shutting_down) {
250 rc = -ENODEV;
251 goto error;
252 }
253
254 if (!dev->polling) {
255 rc = -EINVAL;
256 goto error;
257 }
258
259 dev->ops->stop_poll(dev);
260 dev->polling = false;
261 dev->rf_mode = NFC_RF_NONE;
262
263error:
264 device_unlock(&dev->dev);
265 return rc;
266}
267
268static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
269{
270 int i;
271
272 for (i = 0; i < dev->n_targets; i++) {
273 if (dev->targets[i].idx == target_idx)
274 return &dev->targets[i];
275 }
276
277 return NULL;
278}
279
280int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
281{
282 struct nfc_target *target;
283 u8 gb[NFC_MAX_GT_LEN];
284 size_t gb_len = 0;
285 int rc = 0;
286
287 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
288
289 if (!dev->ops->dep_link_up)
290 return -EOPNOTSUPP;
291
292 device_lock(&dev->dev);
293
294 if (dev->shutting_down) {
295 rc = -ENODEV;
296 goto error;
297 }
298
299 if (dev->dep_link_up == true) {
300 rc = -EALREADY;
301 goto error;
302 }
303
304 nfc_get_local_general_bytes(dev, gb, sizeof(gb), &gb_len);
305 if (gb_len > NFC_MAX_GT_LEN) {
306 rc = -EINVAL;
307 goto error;
308 }
309
310 target = nfc_find_target(dev, target_index);
311 if (target == NULL) {
312 rc = -ENOTCONN;
313 goto error;
314 }
315
316 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
317 if (!rc) {
318 dev->active_target = target;
319 dev->rf_mode = NFC_RF_INITIATOR;
320 }
321
322error:
323 device_unlock(&dev->dev);
324 return rc;
325}
326
327int nfc_dep_link_down(struct nfc_dev *dev)
328{
329 int rc = 0;
330
331 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
332
333 if (!dev->ops->dep_link_down)
334 return -EOPNOTSUPP;
335
336 device_lock(&dev->dev);
337
338 if (dev->shutting_down) {
339 rc = -ENODEV;
340 goto error;
341 }
342
343 if (dev->dep_link_up == false) {
344 rc = -EALREADY;
345 goto error;
346 }
347
348 rc = dev->ops->dep_link_down(dev);
349 if (!rc) {
350 dev->dep_link_up = false;
351 dev->active_target = NULL;
352 dev->rf_mode = NFC_RF_NONE;
353 nfc_llcp_mac_is_down(dev);
354 nfc_genl_dep_link_down_event(dev);
355 }
356
357error:
358 device_unlock(&dev->dev);
359
360 return rc;
361}
362
363int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
364 u8 comm_mode, u8 rf_mode)
365{
366 dev->dep_link_up = true;
367
368 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
369 struct nfc_target *target;
370
371 target = nfc_find_target(dev, target_idx);
372 if (target == NULL)
373 return -ENOTCONN;
374
375 dev->active_target = target;
376 }
377
378 dev->polling = false;
379 dev->rf_mode = rf_mode;
380
381 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
382
383 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
384}
385EXPORT_SYMBOL(nfc_dep_link_is_up);
386
387/**
388 * nfc_activate_target - prepare the target for data exchange
389 *
390 * @dev: The nfc device that found the target
391 * @target_idx: index of the target that must be activated
392 * @protocol: nfc protocol that will be used for data exchange
393 */
394int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
395{
396 int rc;
397 struct nfc_target *target;
398
399 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
400 dev_name(&dev->dev), target_idx, protocol);
401
402 device_lock(&dev->dev);
403
404 if (dev->shutting_down) {
405 rc = -ENODEV;
406 goto error;
407 }
408
409 if (dev->active_target) {
410 rc = -EBUSY;
411 goto error;
412 }
413
414 target = nfc_find_target(dev, target_idx);
415 if (target == NULL) {
416 rc = -ENOTCONN;
417 goto error;
418 }
419
420 rc = dev->ops->activate_target(dev, target, protocol);
421 if (!rc) {
422 dev->active_target = target;
423 dev->rf_mode = NFC_RF_INITIATOR;
424
425 if (dev->ops->check_presence && !dev->shutting_down)
426 mod_timer(&dev->check_pres_timer, jiffies +
427 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
428 }
429
430error:
431 device_unlock(&dev->dev);
432 return rc;
433}
434
435/**
436 * nfc_deactivate_target - deactivate a nfc target
437 *
438 * @dev: The nfc device that found the target
439 * @target_idx: index of the target that must be deactivated
440 * @mode: idle or sleep?
441 */
442int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
443{
444 int rc = 0;
445
446 pr_debug("dev_name=%s target_idx=%u\n",
447 dev_name(&dev->dev), target_idx);
448
449 device_lock(&dev->dev);
450
451 if (dev->shutting_down) {
452 rc = -ENODEV;
453 goto error;
454 }
455
456 if (dev->active_target == NULL) {
457 rc = -ENOTCONN;
458 goto error;
459 }
460
461 if (dev->active_target->idx != target_idx) {
462 rc = -ENOTCONN;
463 goto error;
464 }
465
466 if (dev->ops->check_presence)
467 timer_delete_sync(&dev->check_pres_timer);
468
469 dev->ops->deactivate_target(dev, dev->active_target, mode);
470 dev->active_target = NULL;
471
472error:
473 device_unlock(&dev->dev);
474 return rc;
475}
476
477/**
478 * nfc_data_exchange - transceive data
479 *
480 * @dev: The nfc device that found the target
481 * @target_idx: index of the target
482 * @skb: data to be sent
483 * @cb: callback called when the response is received
484 * @cb_context: parameter for the callback function
485 *
486 * The user must wait for the callback before calling this function again.
487 */
488int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
489 data_exchange_cb_t cb, void *cb_context)
490{
491 int rc;
492
493 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
494 dev_name(&dev->dev), target_idx, skb->len);
495
496 device_lock(&dev->dev);
497
498 if (dev->shutting_down) {
499 rc = -ENODEV;
500 kfree_skb(skb);
501 goto error;
502 }
503
504 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
505 if (dev->active_target->idx != target_idx) {
506 rc = -EADDRNOTAVAIL;
507 kfree_skb(skb);
508 goto error;
509 }
510
511 if (dev->ops->check_presence)
512 timer_delete_sync(&dev->check_pres_timer);
513
514 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
515 cb_context);
516
517 if (!rc && dev->ops->check_presence && !dev->shutting_down)
518 mod_timer(&dev->check_pres_timer, jiffies +
519 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
520 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
521 rc = dev->ops->tm_send(dev, skb);
522 } else {
523 rc = -ENOTCONN;
524 kfree_skb(skb);
525 goto error;
526 }
527
528
529error:
530 device_unlock(&dev->dev);
531 return rc;
532}
533
534struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
535{
536 struct nfc_se *se;
537
538 list_for_each_entry(se, &dev->secure_elements, list)
539 if (se->idx == se_idx)
540 return se;
541
542 return NULL;
543}
544EXPORT_SYMBOL(nfc_find_se);
545
546int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
547{
548 struct nfc_se *se;
549 int rc;
550
551 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
552
553 device_lock(&dev->dev);
554
555 if (dev->shutting_down) {
556 rc = -ENODEV;
557 goto error;
558 }
559
560 if (!dev->dev_up) {
561 rc = -ENODEV;
562 goto error;
563 }
564
565 if (dev->polling) {
566 rc = -EBUSY;
567 goto error;
568 }
569
570 if (!dev->ops->enable_se || !dev->ops->disable_se) {
571 rc = -EOPNOTSUPP;
572 goto error;
573 }
574
575 se = nfc_find_se(dev, se_idx);
576 if (!se) {
577 rc = -EINVAL;
578 goto error;
579 }
580
581 if (se->state == NFC_SE_ENABLED) {
582 rc = -EALREADY;
583 goto error;
584 }
585
586 rc = dev->ops->enable_se(dev, se_idx);
587 if (rc >= 0)
588 se->state = NFC_SE_ENABLED;
589
590error:
591 device_unlock(&dev->dev);
592 return rc;
593}
594
595int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
596{
597 struct nfc_se *se;
598 int rc;
599
600 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
601
602 device_lock(&dev->dev);
603
604 if (dev->shutting_down) {
605 rc = -ENODEV;
606 goto error;
607 }
608
609 if (!dev->dev_up) {
610 rc = -ENODEV;
611 goto error;
612 }
613
614 if (!dev->ops->enable_se || !dev->ops->disable_se) {
615 rc = -EOPNOTSUPP;
616 goto error;
617 }
618
619 se = nfc_find_se(dev, se_idx);
620 if (!se) {
621 rc = -EINVAL;
622 goto error;
623 }
624
625 if (se->state == NFC_SE_DISABLED) {
626 rc = -EALREADY;
627 goto error;
628 }
629
630 rc = dev->ops->disable_se(dev, se_idx);
631 if (rc >= 0)
632 se->state = NFC_SE_DISABLED;
633
634error:
635 device_unlock(&dev->dev);
636 return rc;
637}
638
639int nfc_set_remote_general_bytes(struct nfc_dev *dev, const u8 *gb, u8 gb_len)
640{
641 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
642
643 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
644}
645EXPORT_SYMBOL(nfc_set_remote_general_bytes);
646
647u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, u8 *out_gb,
648 size_t gb_max_len, size_t *gb_len)
649{
650 return nfc_llcp_general_bytes(dev, out_gb, gb_max_len, gb_len);
651}
652EXPORT_SYMBOL(nfc_get_local_general_bytes);
653
654int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
655{
656 /* Only LLCP target mode for now */
657 if (dev->dep_link_up == false) {
658 kfree_skb(skb);
659 return -ENOLINK;
660 }
661
662 return nfc_llcp_data_received(dev, skb);
663}
664EXPORT_SYMBOL(nfc_tm_data_received);
665
666int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
667 const u8 *gb, size_t gb_len)
668{
669 int rc;
670
671 device_lock(&dev->dev);
672
673 dev->polling = false;
674
675 if (gb != NULL) {
676 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
677 if (rc < 0)
678 goto out;
679 }
680
681 dev->rf_mode = NFC_RF_TARGET;
682
683 if (protocol == NFC_PROTO_NFC_DEP_MASK)
684 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
685
686 rc = nfc_genl_tm_activated(dev, protocol);
687
688out:
689 device_unlock(&dev->dev);
690
691 return rc;
692}
693EXPORT_SYMBOL(nfc_tm_activated);
694
695int nfc_tm_deactivated(struct nfc_dev *dev)
696{
697 dev->dep_link_up = false;
698 dev->rf_mode = NFC_RF_NONE;
699
700 return nfc_genl_tm_deactivated(dev);
701}
702EXPORT_SYMBOL(nfc_tm_deactivated);
703
704/**
705 * nfc_alloc_send_skb - allocate a skb for data exchange responses
706 *
707 * @dev: device sending the response
708 * @sk: socket sending the response
709 * @flags: MSG_DONTWAIT flag
710 * @size: size to allocate
711 * @err: pointer to memory to store the error code
712 */
713struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
714 unsigned int flags, unsigned int size,
715 unsigned int *err)
716{
717 struct sk_buff *skb;
718 unsigned int total_size;
719
720 total_size = size +
721 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
722
723 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
724 if (skb)
725 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
726
727 return skb;
728}
729
730/**
731 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
732 *
733 * @size: size to allocate
734 * @gfp: gfp flags
735 */
736struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
737{
738 struct sk_buff *skb;
739 unsigned int total_size;
740
741 total_size = size + 1;
742 skb = alloc_skb(total_size, gfp);
743
744 if (skb)
745 skb_reserve(skb, 1);
746
747 return skb;
748}
749EXPORT_SYMBOL(nfc_alloc_recv_skb);
750
751/**
752 * nfc_targets_found - inform that targets were found
753 *
754 * @dev: The nfc device that found the targets
755 * @targets: array of nfc targets found
756 * @n_targets: targets array size
757 *
758 * The device driver must call this function when one or many nfc targets
759 * are found. After calling this function, the device driver must stop
760 * polling for targets.
761 * NOTE: This function can be called with targets=NULL and n_targets=0 to
762 * notify a driver error, meaning that the polling operation cannot complete.
763 * IMPORTANT: this function must not be called from an atomic context.
764 * In addition, it must also not be called from a context that would prevent
765 * the NFC Core to call other nfc ops entry point concurrently.
766 */
767int nfc_targets_found(struct nfc_dev *dev,
768 struct nfc_target *targets, int n_targets)
769{
770 int i;
771
772 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
773
774 for (i = 0; i < n_targets; i++)
775 targets[i].idx = dev->target_next_idx++;
776
777 device_lock(&dev->dev);
778
779 if (dev->polling == false) {
780 device_unlock(&dev->dev);
781 return 0;
782 }
783
784 dev->polling = false;
785
786 dev->targets_generation++;
787
788 kfree(dev->targets);
789 dev->targets = NULL;
790
791 if (targets) {
792 dev->targets = kmemdup(targets,
793 n_targets * sizeof(struct nfc_target),
794 GFP_ATOMIC);
795
796 if (!dev->targets) {
797 dev->n_targets = 0;
798 device_unlock(&dev->dev);
799 return -ENOMEM;
800 }
801 }
802
803 dev->n_targets = n_targets;
804 device_unlock(&dev->dev);
805
806 nfc_genl_targets_found(dev);
807
808 return 0;
809}
810EXPORT_SYMBOL(nfc_targets_found);
811
812/**
813 * nfc_target_lost - inform that an activated target went out of field
814 *
815 * @dev: The nfc device that had the activated target in field
816 * @target_idx: the nfc index of the target
817 *
818 * The device driver must call this function when the activated target
819 * goes out of the field.
820 * IMPORTANT: this function must not be called from an atomic context.
821 * In addition, it must also not be called from a context that would prevent
822 * the NFC Core to call other nfc ops entry point concurrently.
823 */
824int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
825{
826 const struct nfc_target *tg;
827 int i;
828
829 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
830
831 device_lock(&dev->dev);
832
833 for (i = 0; i < dev->n_targets; i++) {
834 tg = &dev->targets[i];
835 if (tg->idx == target_idx)
836 break;
837 }
838
839 if (i == dev->n_targets) {
840 device_unlock(&dev->dev);
841 return -EINVAL;
842 }
843
844 dev->targets_generation++;
845 dev->n_targets--;
846 dev->active_target = NULL;
847
848 if (dev->n_targets) {
849 memcpy(&dev->targets[i], &dev->targets[i + 1],
850 (dev->n_targets - i) * sizeof(struct nfc_target));
851 } else {
852 kfree(dev->targets);
853 dev->targets = NULL;
854 }
855
856 device_unlock(&dev->dev);
857
858 nfc_genl_target_lost(dev, target_idx);
859
860 return 0;
861}
862EXPORT_SYMBOL(nfc_target_lost);
863
864inline void nfc_driver_failure(struct nfc_dev *dev, int err)
865{
866 nfc_targets_found(dev, NULL, 0);
867}
868EXPORT_SYMBOL(nfc_driver_failure);
869
870int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
871{
872 struct nfc_se *se;
873 int rc;
874
875 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
876
877 se = nfc_find_se(dev, se_idx);
878 if (se)
879 return -EALREADY;
880
881 se = kzalloc_obj(struct nfc_se);
882 if (!se)
883 return -ENOMEM;
884
885 se->idx = se_idx;
886 se->type = type;
887 se->state = NFC_SE_DISABLED;
888 INIT_LIST_HEAD(&se->list);
889
890 list_add(&se->list, &dev->secure_elements);
891
892 rc = nfc_genl_se_added(dev, se_idx, type);
893 if (rc < 0) {
894 list_del(&se->list);
895 kfree(se);
896
897 return rc;
898 }
899
900 return 0;
901}
902EXPORT_SYMBOL(nfc_add_se);
903
904int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
905{
906 struct nfc_se *se, *n;
907 int rc;
908
909 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
910
911 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
912 if (se->idx == se_idx) {
913 rc = nfc_genl_se_removed(dev, se_idx);
914 if (rc < 0)
915 return rc;
916
917 list_del(&se->list);
918 kfree(se);
919
920 return 0;
921 }
922
923 return -EINVAL;
924}
925EXPORT_SYMBOL(nfc_remove_se);
926
927int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
928 struct nfc_evt_transaction *evt_transaction)
929{
930 int rc;
931
932 pr_debug("transaction: %x\n", se_idx);
933
934 device_lock(&dev->dev);
935
936 if (!evt_transaction) {
937 rc = -EPROTO;
938 goto out;
939 }
940
941 rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
942out:
943 device_unlock(&dev->dev);
944 return rc;
945}
946EXPORT_SYMBOL(nfc_se_transaction);
947
948int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
949{
950 int rc;
951
952 pr_debug("connectivity: %x\n", se_idx);
953
954 device_lock(&dev->dev);
955 rc = nfc_genl_se_connectivity(dev, se_idx);
956 device_unlock(&dev->dev);
957 return rc;
958}
959EXPORT_SYMBOL(nfc_se_connectivity);
960
961static void nfc_release(struct device *d)
962{
963 struct nfc_dev *dev = to_nfc_dev(d);
964 struct nfc_se *se, *n;
965
966 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
967
968 nfc_genl_data_exit(&dev->genl_data);
969 kfree(dev->targets);
970
971 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
972 nfc_genl_se_removed(dev, se->idx);
973 list_del(&se->list);
974 kfree(se);
975 }
976
977 ida_free(&nfc_index_ida, dev->idx);
978
979 kfree(dev);
980}
981
982static void nfc_check_pres_work(struct work_struct *work)
983{
984 struct nfc_dev *dev = container_of(work, struct nfc_dev,
985 check_pres_work);
986 int rc;
987
988 device_lock(&dev->dev);
989
990 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
991 rc = dev->ops->check_presence(dev, dev->active_target);
992 if (rc == -EOPNOTSUPP)
993 goto exit;
994 if (rc) {
995 u32 active_target_idx = dev->active_target->idx;
996 device_unlock(&dev->dev);
997 nfc_target_lost(dev, active_target_idx);
998 return;
999 }
1000
1001 if (!dev->shutting_down)
1002 mod_timer(&dev->check_pres_timer, jiffies +
1003 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
1004 }
1005
1006exit:
1007 device_unlock(&dev->dev);
1008}
1009
1010static void nfc_check_pres_timeout(struct timer_list *t)
1011{
1012 struct nfc_dev *dev = timer_container_of(dev, t, check_pres_timer);
1013
1014 schedule_work(&dev->check_pres_work);
1015}
1016
1017const struct class nfc_class = {
1018 .name = "nfc",
1019 .dev_release = nfc_release,
1020};
1021EXPORT_SYMBOL(nfc_class);
1022
1023static int match_idx(struct device *d, const void *data)
1024{
1025 struct nfc_dev *dev = to_nfc_dev(d);
1026 const unsigned int *idx = data;
1027
1028 return dev->idx == *idx;
1029}
1030
1031struct nfc_dev *nfc_get_device(unsigned int idx)
1032{
1033 struct device *d;
1034
1035 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1036 if (!d)
1037 return NULL;
1038
1039 return to_nfc_dev(d);
1040}
1041
1042/**
1043 * nfc_allocate_device - allocate a new nfc device
1044 *
1045 * @ops: device operations
1046 * @supported_protocols: NFC protocols supported by the device
1047 * @tx_headroom: reserved space at beginning of skb
1048 * @tx_tailroom: reserved space at end of skb
1049 */
1050struct nfc_dev *nfc_allocate_device(const struct nfc_ops *ops,
1051 u32 supported_protocols,
1052 int tx_headroom, int tx_tailroom)
1053{
1054 struct nfc_dev *dev;
1055 int rc;
1056
1057 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1058 !ops->deactivate_target || !ops->im_transceive)
1059 return NULL;
1060
1061 if (!supported_protocols)
1062 return NULL;
1063
1064 dev = kzalloc_obj(struct nfc_dev);
1065 if (!dev)
1066 return NULL;
1067
1068 rc = ida_alloc(&nfc_index_ida, GFP_KERNEL);
1069 if (rc < 0)
1070 goto err_free_dev;
1071 dev->idx = rc;
1072
1073 dev->dev.class = &nfc_class;
1074 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1075 device_initialize(&dev->dev);
1076
1077 dev->ops = ops;
1078 dev->supported_protocols = supported_protocols;
1079 dev->tx_headroom = tx_headroom;
1080 dev->tx_tailroom = tx_tailroom;
1081 INIT_LIST_HEAD(&dev->secure_elements);
1082
1083 nfc_genl_data_init(&dev->genl_data);
1084
1085 dev->rf_mode = NFC_RF_NONE;
1086
1087 /* first generation must not be 0 */
1088 dev->targets_generation = 1;
1089
1090 if (ops->check_presence) {
1091 timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
1092 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1093 }
1094
1095 return dev;
1096
1097err_free_dev:
1098 kfree(dev);
1099
1100 return NULL;
1101}
1102EXPORT_SYMBOL(nfc_allocate_device);
1103
1104/**
1105 * nfc_register_device - register a nfc device in the nfc subsystem
1106 *
1107 * @dev: The nfc device to register
1108 */
1109int nfc_register_device(struct nfc_dev *dev)
1110{
1111 int rc;
1112
1113 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1114
1115 mutex_lock(&nfc_devlist_mutex);
1116 nfc_devlist_generation++;
1117 rc = device_add(&dev->dev);
1118 mutex_unlock(&nfc_devlist_mutex);
1119
1120 if (rc < 0)
1121 return rc;
1122
1123 rc = nfc_llcp_register_device(dev);
1124 if (rc)
1125 pr_err("Could not register llcp device\n");
1126
1127 device_lock(&dev->dev);
1128 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1129 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1130 if (dev->rfkill) {
1131 if (rfkill_register(dev->rfkill) < 0) {
1132 rfkill_destroy(dev->rfkill);
1133 dev->rfkill = NULL;
1134 }
1135 }
1136 dev->shutting_down = false;
1137 device_unlock(&dev->dev);
1138
1139 rc = nfc_genl_device_added(dev);
1140 if (rc)
1141 pr_debug("The userspace won't be notified that the device %s was added\n",
1142 dev_name(&dev->dev));
1143
1144 return 0;
1145}
1146EXPORT_SYMBOL(nfc_register_device);
1147
1148/**
1149 * nfc_unregister_rfkill - unregister a nfc device in the rfkill subsystem
1150 *
1151 * @dev: The nfc device to unregister
1152 */
1153void nfc_unregister_rfkill(struct nfc_dev *dev)
1154{
1155 struct rfkill *rfk = NULL;
1156 int rc;
1157
1158 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1159
1160 rc = nfc_genl_device_removed(dev);
1161 if (rc)
1162 pr_debug("The userspace won't be notified that the device %s "
1163 "was removed\n", dev_name(&dev->dev));
1164
1165 device_lock(&dev->dev);
1166 if (dev->rfkill) {
1167 rfk = dev->rfkill;
1168 dev->rfkill = NULL;
1169 }
1170 dev->shutting_down = true;
1171 device_unlock(&dev->dev);
1172
1173 if (rfk) {
1174 rfkill_unregister(rfk);
1175 rfkill_destroy(rfk);
1176 }
1177}
1178EXPORT_SYMBOL(nfc_unregister_rfkill);
1179
1180/**
1181 * nfc_remove_device - remove a nfc device in the nfc subsystem
1182 *
1183 * @dev: The nfc device to remove
1184 */
1185void nfc_remove_device(struct nfc_dev *dev)
1186{
1187 if (dev->ops->check_presence) {
1188 timer_delete_sync(&dev->check_pres_timer);
1189 cancel_work_sync(&dev->check_pres_work);
1190 }
1191
1192 nfc_llcp_unregister_device(dev);
1193
1194 mutex_lock(&nfc_devlist_mutex);
1195 nfc_devlist_generation++;
1196 device_del(&dev->dev);
1197 mutex_unlock(&nfc_devlist_mutex);
1198}
1199EXPORT_SYMBOL(nfc_remove_device);
1200
1201/**
1202 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1203 *
1204 * @dev: The nfc device to unregister
1205 */
1206void nfc_unregister_device(struct nfc_dev *dev)
1207{
1208 nfc_unregister_rfkill(dev);
1209 nfc_remove_device(dev);
1210}
1211EXPORT_SYMBOL(nfc_unregister_device);
1212
1213static int __init nfc_init(void)
1214{
1215 int rc;
1216
1217 pr_info("NFC Core ver %s\n", VERSION);
1218
1219 rc = class_register(&nfc_class);
1220 if (rc)
1221 return rc;
1222
1223 rc = nfc_genl_init();
1224 if (rc)
1225 goto err_genl;
1226
1227 /* the first generation must not be 0 */
1228 nfc_devlist_generation = 1;
1229
1230 rc = rawsock_init();
1231 if (rc)
1232 goto err_rawsock;
1233
1234 rc = nfc_llcp_init();
1235 if (rc)
1236 goto err_llcp_sock;
1237
1238 rc = af_nfc_init();
1239 if (rc)
1240 goto err_af_nfc;
1241
1242 return 0;
1243
1244err_af_nfc:
1245 nfc_llcp_exit();
1246err_llcp_sock:
1247 rawsock_exit();
1248err_rawsock:
1249 nfc_genl_exit();
1250err_genl:
1251 class_unregister(&nfc_class);
1252 return rc;
1253}
1254
1255static void __exit nfc_exit(void)
1256{
1257 af_nfc_exit();
1258 nfc_llcp_exit();
1259 rawsock_exit();
1260 nfc_genl_exit();
1261 class_unregister(&nfc_class);
1262}
1263
1264subsys_initcall(nfc_init);
1265module_exit(nfc_exit);
1266
1267MODULE_AUTHOR("Lauro Ramos Venancio <[email protected]>");
1268MODULE_DESCRIPTION("NFC Core ver " VERSION);
1269MODULE_VERSION(VERSION);
1270MODULE_LICENSE("GPL");
1271MODULE_ALIAS_NETPROTO(PF_NFC);
1272MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);