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