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