OSDN Git Service

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