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