NFC: Add modules alias for NFC sockets
[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);
f212ad5e 147 if (!rc) {
3e256b8f 148 dev->polling = true;
f212ad5e
SO
149 dev->rf_mode = NFC_RF_NONE;
150 }
3e256b8f
LRV
151
152error:
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 */
162int nfc_stop_poll(struct nfc_dev *dev)
163{
164 int rc = 0;
165
20c239c1 166 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
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
183error:
184 device_unlock(&dev->dev);
185 return rc;
186}
187
90099433
EL
188static 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
47807d3d 203int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
1ed28f61
SO
204{
205 int rc = 0;
47807d3d
SO
206 u8 *gb;
207 size_t gb_len;
90099433 208 struct nfc_target *target;
1ed28f61 209
47807d3d 210 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
1ed28f61
SO
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
47807d3d
SO
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
90099433
EL
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);
f212ad5e 240 if (!rc) {
90099433 241 dev->active_target = target;
f212ad5e
SO
242 dev->rf_mode = NFC_RF_INITIATOR;
243 }
1ed28f61
SO
244
245error:
246 device_unlock(&dev->dev);
247 return rc;
248}
249
250int 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
1ed28f61
SO
271 rc = dev->ops->dep_link_down(dev);
272 if (!rc) {
273 dev->dep_link_up = false;
90099433 274 dev->active_target = NULL;
d646960f 275 nfc_llcp_mac_is_down(dev);
1ed28f61
SO
276 nfc_genl_dep_link_down_event(dev);
277 }
278
279error:
280 device_unlock(&dev->dev);
281 return rc;
282}
283
284int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
0a40acb2 285 u8 comm_mode, u8 rf_mode)
1ed28f61
SO
286{
287 dev->dep_link_up = true;
1ed28f61 288
d646960f
SO
289 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
290
1ed28f61
SO
291 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
292}
293EXPORT_SYMBOL(nfc_dep_link_is_up);
294
3e256b8f
LRV
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 */
302int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
303{
304 int rc;
90099433 305 struct nfc_target *target;
3e256b8f 306
20c239c1
JP
307 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
308 dev_name(&dev->dev), target_idx, protocol);
3e256b8f
LRV
309
310 device_lock(&dev->dev);
311
312 if (!device_is_registered(&dev->dev)) {
313 rc = -ENODEV;
314 goto error;
315 }
316
90099433
EL
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);
c8d56ae7 329 if (!rc) {
90099433 330 dev->active_target = target;
f212ad5e 331 dev->rf_mode = NFC_RF_INITIATOR;
3e256b8f 332
c8d56ae7
EL
333 if (dev->ops->check_presence)
334 mod_timer(&dev->check_pres_timer, jiffies +
335 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
336 }
3e256b8f
LRV
337
338error:
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 */
349int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
350{
351 int rc = 0;
352
20c239c1
JP
353 pr_debug("dev_name=%s target_idx=%u\n",
354 dev_name(&dev->dev), target_idx);
3e256b8f
LRV
355
356 device_lock(&dev->dev);
357
358 if (!device_is_registered(&dev->dev)) {
359 rc = -ENODEV;
360 goto error;
361 }
362
90099433
EL
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
c8d56ae7
EL
373 if (dev->ops->check_presence)
374 del_timer_sync(&dev->check_pres_timer);
375
90099433
EL
376 dev->ops->deactivate_target(dev, dev->active_target);
377 dev->active_target = NULL;
3e256b8f
LRV
378
379error:
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 */
0a40acb2
SO
395int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
396 data_exchange_cb_t cb, void *cb_context)
3e256b8f
LRV
397{
398 int rc;
399
20c239c1
JP
400 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
401 dev_name(&dev->dev), target_idx, skb->len);
3e256b8f
LRV
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
be9ae4ce
SO
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 }
144612ca 417
be9ae4ce
SO
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;
144612ca
EL
431 kfree_skb(skb);
432 goto error;
433 }
434
c8d56ae7 435
3e256b8f
LRV
436error:
437 device_unlock(&dev->dev);
438 return rc;
439}
440
541d920b
SO
441int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
442{
0a40acb2 443 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
541d920b
SO
444
445 if (gb_len > NFC_MAX_GT_LEN)
446 return -EINVAL;
447
d646960f 448 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
541d920b
SO
449}
450EXPORT_SYMBOL(nfc_set_remote_general_bytes);
451
ab73b751
SO
452u8 *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}
458EXPORT_SYMBOL(nfc_get_local_general_bytes);
459
73167ced
SO
460int 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}
470EXPORT_SYMBOL(nfc_tm_data_received);
471
fc40a8c1
SO
472int 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
f212ad5e
SO
487 dev->rf_mode = NFC_RF_TARGET;
488
fc40a8c1
SO
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
494out:
495 device_unlock(&dev->dev);
496
497 return rc;
498}
499EXPORT_SYMBOL(nfc_tm_activated);
500
501int nfc_tm_deactivated(struct nfc_dev *dev)
502{
503 dev->dep_link_up = false;
504
505 return nfc_genl_tm_deactivated(dev);
506}
507EXPORT_SYMBOL(nfc_tm_deactivated);
508
3e256b8f 509/**
7c7cd3bf 510 * nfc_alloc_send_skb - allocate a skb for data exchange responses
3e256b8f
LRV
511 *
512 * @size: size to allocate
513 * @gfp: gfp flags
514 */
7c7cd3bf 515struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
0a40acb2
SO
516 unsigned int flags, unsigned int size,
517 unsigned int *err)
7c7cd3bf
SO
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 */
538struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
3e256b8f
LRV
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}
7c7cd3bf 551EXPORT_SYMBOL(nfc_alloc_recv_skb);
3e256b8f 552
4d12b8b1
LRV
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.
d94f9c55
EL
563 * NOTE: This function can be called with targets=NULL and n_targets=0 to
564 * notify a driver error, meaning that the polling operation cannot complete.
d4ccb132
EL
565 * IMPORTANT: this function must not be called from an atomic context.
566 * In addition, it must also not be called from a context that would prevent
567 * the NFC Core to call other nfc ops entry point concurrently.
4d12b8b1 568 */
0a40acb2
SO
569int nfc_targets_found(struct nfc_dev *dev,
570 struct nfc_target *targets, int n_targets)
4d12b8b1 571{
c4fbb651
SO
572 int i;
573
20c239c1 574 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
4d12b8b1 575
c4fbb651 576 for (i = 0; i < n_targets; i++)
01ae0eea 577 targets[i].idx = dev->target_next_idx++;
c4fbb651 578
d4ccb132 579 device_lock(&dev->dev);
4d12b8b1 580
8668fdd6
EL
581 if (dev->polling == false) {
582 device_unlock(&dev->dev);
583 return 0;
584 }
585
586 dev->polling = false;
587
4d12b8b1
LRV
588 dev->targets_generation++;
589
590 kfree(dev->targets);
d94f9c55 591 dev->targets = NULL;
4d12b8b1 592
d94f9c55
EL
593 if (targets) {
594 dev->targets = kmemdup(targets,
595 n_targets * sizeof(struct nfc_target),
596 GFP_ATOMIC);
597
598 if (!dev->targets) {
599 dev->n_targets = 0;
600 device_unlock(&dev->dev);
601 return -ENOMEM;
602 }
4d12b8b1
LRV
603 }
604
605 dev->n_targets = n_targets;
d4ccb132 606 device_unlock(&dev->dev);
4d12b8b1
LRV
607
608 nfc_genl_targets_found(dev);
609
610 return 0;
611}
612EXPORT_SYMBOL(nfc_targets_found);
613
d4ccb132
EL
614/**
615 * nfc_target_lost - inform that an activated target went out of field
616 *
617 * @dev: The nfc device that had the activated target in field
618 * @target_idx: the nfc index of the target
619 *
620 * The device driver must call this function when the activated target
621 * goes out of the field.
622 * IMPORTANT: this function must not be called from an atomic context.
623 * In addition, it must also not be called from a context that would prevent
624 * the NFC Core to call other nfc ops entry point concurrently.
625 */
e1da0efa
EL
626int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
627{
628 struct nfc_target *tg;
629 int i;
630
631 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
632
d4ccb132 633 device_lock(&dev->dev);
e1da0efa
EL
634
635 for (i = 0; i < dev->n_targets; i++) {
636 tg = &dev->targets[i];
637 if (tg->idx == target_idx)
638 break;
639 }
640
641 if (i == dev->n_targets) {
d4ccb132 642 device_unlock(&dev->dev);
e1da0efa
EL
643 return -EINVAL;
644 }
645
646 dev->targets_generation++;
647 dev->n_targets--;
90099433 648 dev->active_target = NULL;
e1da0efa
EL
649
650 if (dev->n_targets) {
651 memcpy(&dev->targets[i], &dev->targets[i + 1],
652 (dev->n_targets - i) * sizeof(struct nfc_target));
653 } else {
654 kfree(dev->targets);
655 dev->targets = NULL;
656 }
657
d4ccb132 658 device_unlock(&dev->dev);
e1da0efa
EL
659
660 nfc_genl_target_lost(dev, target_idx);
661
662 return 0;
663}
664EXPORT_SYMBOL(nfc_target_lost);
665
9eb334ac 666inline void nfc_driver_failure(struct nfc_dev *dev, int err)
456411ca 667{
9eb334ac 668 nfc_targets_found(dev, NULL, 0);
456411ca
EL
669}
670EXPORT_SYMBOL(nfc_driver_failure);
671
3e256b8f
LRV
672static void nfc_release(struct device *d)
673{
674 struct nfc_dev *dev = to_nfc_dev(d);
675
20c239c1 676 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f 677
c8d56ae7
EL
678 if (dev->ops->check_presence) {
679 del_timer_sync(&dev->check_pres_timer);
680 destroy_workqueue(dev->check_pres_wq);
681 }
682
4d12b8b1
LRV
683 nfc_genl_data_exit(&dev->genl_data);
684 kfree(dev->targets);
3e256b8f
LRV
685 kfree(dev);
686}
687
c8d56ae7
EL
688static void nfc_check_pres_work(struct work_struct *work)
689{
690 struct nfc_dev *dev = container_of(work, struct nfc_dev,
691 check_pres_work);
692 int rc;
693
694 device_lock(&dev->dev);
695
90099433
EL
696 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
697 rc = dev->ops->check_presence(dev, dev->active_target);
c8d56ae7
EL
698 if (!rc) {
699 mod_timer(&dev->check_pres_timer, jiffies +
700 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
701 } else {
d4ccb132
EL
702 u32 active_target_idx = dev->active_target->idx;
703 device_unlock(&dev->dev);
704 nfc_target_lost(dev, active_target_idx);
705 return;
c8d56ae7
EL
706 }
707 }
708
709 device_unlock(&dev->dev);
710}
711
712static void nfc_check_pres_timeout(unsigned long data)
713{
714 struct nfc_dev *dev = (struct nfc_dev *)data;
715
716 queue_work(dev->check_pres_wq, &dev->check_pres_work);
717}
718
3e256b8f
LRV
719struct class nfc_class = {
720 .name = "nfc",
721 .dev_release = nfc_release,
722};
723EXPORT_SYMBOL(nfc_class);
724
725static int match_idx(struct device *d, void *data)
726{
727 struct nfc_dev *dev = to_nfc_dev(d);
95c96174 728 unsigned int *idx = data;
3e256b8f
LRV
729
730 return dev->idx == *idx;
731}
732
95c96174 733struct nfc_dev *nfc_get_device(unsigned int idx)
3e256b8f
LRV
734{
735 struct device *d;
736
737 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
738 if (!d)
739 return NULL;
740
741 return to_nfc_dev(d);
742}
743
744/**
745 * nfc_allocate_device - allocate a new nfc device
746 *
747 * @ops: device operations
748 * @supported_protocols: NFC protocols supported by the device
749 */
750struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
0a40acb2
SO
751 u32 supported_protocols,
752 int tx_headroom, int tx_tailroom)
3e256b8f
LRV
753{
754 static atomic_t dev_no = ATOMIC_INIT(0);
755 struct nfc_dev *dev;
756
757 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
be9ae4ce 758 !ops->deactivate_target || !ops->im_transceive)
3e256b8f
LRV
759 return NULL;
760
761 if (!supported_protocols)
762 return NULL;
763
764 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
765 if (!dev)
766 return NULL;
767
768 dev->dev.class = &nfc_class;
769 dev->idx = atomic_inc_return(&dev_no) - 1;
770 dev_set_name(&dev->dev, "nfc%d", dev->idx);
771 device_initialize(&dev->dev);
772
773 dev->ops = ops;
774 dev->supported_protocols = supported_protocols;
e8753043
SO
775 dev->tx_headroom = tx_headroom;
776 dev->tx_tailroom = tx_tailroom;
3e256b8f 777
4d12b8b1
LRV
778 nfc_genl_data_init(&dev->genl_data);
779
d4ccb132 780
4d12b8b1
LRV
781 /* first generation must not be 0 */
782 dev->targets_generation = 1;
783
c8d56ae7
EL
784 if (ops->check_presence) {
785 char name[32];
786 init_timer(&dev->check_pres_timer);
787 dev->check_pres_timer.data = (unsigned long)dev;
788 dev->check_pres_timer.function = nfc_check_pres_timeout;
789
790 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
791 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
792 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
793 WQ_UNBOUND |
794 WQ_MEM_RECLAIM, 1);
795 if (dev->check_pres_wq == NULL) {
796 kfree(dev);
797 return NULL;
798 }
799 }
800
3e256b8f
LRV
801 return dev;
802}
803EXPORT_SYMBOL(nfc_allocate_device);
804
805/**
806 * nfc_register_device - register a nfc device in the nfc subsystem
807 *
808 * @dev: The nfc device to register
809 */
810int nfc_register_device(struct nfc_dev *dev)
811{
812 int rc;
813
20c239c1 814 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
815
816 mutex_lock(&nfc_devlist_mutex);
817 nfc_devlist_generation++;
818 rc = device_add(&dev->dev);
819 mutex_unlock(&nfc_devlist_mutex);
820
4d12b8b1
LRV
821 if (rc < 0)
822 return rc;
823
d646960f
SO
824 rc = nfc_llcp_register_device(dev);
825 if (rc)
826 pr_err("Could not register llcp device\n");
827
4d12b8b1
LRV
828 rc = nfc_genl_device_added(dev);
829 if (rc)
20c239c1
JP
830 pr_debug("The userspace won't be notified that the device %s was added\n",
831 dev_name(&dev->dev));
4d12b8b1
LRV
832
833 return 0;
3e256b8f
LRV
834}
835EXPORT_SYMBOL(nfc_register_device);
836
837/**
838 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
839 *
840 * @dev: The nfc device to unregister
841 */
842void nfc_unregister_device(struct nfc_dev *dev)
843{
4d12b8b1
LRV
844 int rc;
845
20c239c1 846 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
3e256b8f
LRV
847
848 mutex_lock(&nfc_devlist_mutex);
849 nfc_devlist_generation++;
850
851 /* lock to avoid unregistering a device while an operation
852 is in progress */
853 device_lock(&dev->dev);
854 device_del(&dev->dev);
855 device_unlock(&dev->dev);
856
857 mutex_unlock(&nfc_devlist_mutex);
4d12b8b1 858
d646960f
SO
859 nfc_llcp_unregister_device(dev);
860
4d12b8b1
LRV
861 rc = nfc_genl_device_removed(dev);
862 if (rc)
20c239c1
JP
863 pr_debug("The userspace won't be notified that the device %s was removed\n",
864 dev_name(&dev->dev));
4d12b8b1 865
3e256b8f
LRV
866}
867EXPORT_SYMBOL(nfc_unregister_device);
868
869static int __init nfc_init(void)
870{
4d12b8b1
LRV
871 int rc;
872
ed1e0ad8 873 pr_info("NFC Core ver %s\n", VERSION);
3e256b8f 874
4d12b8b1
LRV
875 rc = class_register(&nfc_class);
876 if (rc)
877 return rc;
878
879 rc = nfc_genl_init();
880 if (rc)
881 goto err_genl;
882
883 /* the first generation must not be 0 */
884 nfc_devlist_generation = 1;
885
23b7869c
LRV
886 rc = rawsock_init();
887 if (rc)
888 goto err_rawsock;
889
d646960f
SO
890 rc = nfc_llcp_init();
891 if (rc)
892 goto err_llcp_sock;
893
c7fe3b52
AAJ
894 rc = af_nfc_init();
895 if (rc)
896 goto err_af_nfc;
897
4d12b8b1
LRV
898 return 0;
899
c7fe3b52 900err_af_nfc:
d646960f
SO
901 nfc_llcp_exit();
902err_llcp_sock:
23b7869c
LRV
903 rawsock_exit();
904err_rawsock:
c7fe3b52 905 nfc_genl_exit();
4d12b8b1
LRV
906err_genl:
907 class_unregister(&nfc_class);
908 return rc;
3e256b8f
LRV
909}
910
911static void __exit nfc_exit(void)
912{
c7fe3b52 913 af_nfc_exit();
d646960f 914 nfc_llcp_exit();
23b7869c 915 rawsock_exit();
4d12b8b1 916 nfc_genl_exit();
3e256b8f
LRV
917 class_unregister(&nfc_class);
918}
919
920subsys_initcall(nfc_init);
921module_exit(nfc_exit);
922
923MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
924MODULE_DESCRIPTION("NFC Core ver " VERSION);
925MODULE_VERSION(VERSION);
926MODULE_LICENSE("GPL");
1155bb61 927MODULE_ALIAS_NETPROTO(PF_NFC);
This page took 0.13171 seconds and 5 git commands to generate.