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