blob: 785f1f20c7ba62d78b065d79bb23e4a0d48cef5e [file] [log] [blame]
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001/*
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
Samuel Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080025
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030026#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/slab.h>
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +010030#include <linux/nfc.h>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030031
32#include "nfc.h"
33
34#define VERSION "0.1"
35
36int nfc_devlist_generation;
37DEFINE_MUTEX(nfc_devlist_mutex);
38
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030039/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030040 * nfc_dev_up - turn on the NFC device
41 *
42 * @dev: The nfc device to be turned on
43 *
44 * The device remains up until the nfc_dev_down function is called.
45 */
46int nfc_dev_up(struct nfc_dev *dev)
47{
48 int rc = 0;
49
Joe Perches20c239c2011-11-29 11:37:33 -080050 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030051
52 device_lock(&dev->dev);
53
54 if (!device_is_registered(&dev->dev)) {
55 rc = -ENODEV;
56 goto error;
57 }
58
59 if (dev->dev_up) {
60 rc = -EALREADY;
61 goto error;
62 }
63
64 if (dev->ops->dev_up)
65 rc = dev->ops->dev_up(dev);
66
67 if (!rc)
68 dev->dev_up = true;
69
70error:
71 device_unlock(&dev->dev);
72 return rc;
73}
74
75/**
76 * nfc_dev_down - turn off the NFC device
77 *
78 * @dev: The nfc device to be turned off
79 */
80int nfc_dev_down(struct nfc_dev *dev)
81{
82 int rc = 0;
83
Joe Perches20c239c2011-11-29 11:37:33 -080084 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030085
86 device_lock(&dev->dev);
87
88 if (!device_is_registered(&dev->dev)) {
89 rc = -ENODEV;
90 goto error;
91 }
92
93 if (!dev->dev_up) {
94 rc = -EALREADY;
95 goto error;
96 }
97
98 if (dev->polling || dev->remote_activated) {
99 rc = -EBUSY;
100 goto error;
101 }
102
103 if (dev->ops->dev_down)
104 dev->ops->dev_down(dev);
105
106 dev->dev_up = false;
107
108error:
109 device_unlock(&dev->dev);
110 return rc;
111}
112
113/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300114 * nfc_start_poll - start polling for nfc targets
115 *
116 * @dev: The nfc device that must start polling
117 * @protocols: bitset of nfc protocols that must be used for polling
118 *
119 * The device remains polling for targets until a target is found or
120 * the nfc_stop_poll function is called.
121 */
122int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
123{
124 int rc;
125
Joe Perches20c239c2011-11-29 11:37:33 -0800126 pr_debug("dev_name=%s protocols=0x%x\n",
127 dev_name(&dev->dev), protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300128
129 if (!protocols)
130 return -EINVAL;
131
132 device_lock(&dev->dev);
133
134 if (!device_is_registered(&dev->dev)) {
135 rc = -ENODEV;
136 goto error;
137 }
138
139 if (dev->polling) {
140 rc = -EBUSY;
141 goto error;
142 }
143
144 rc = dev->ops->start_poll(dev, protocols);
145 if (!rc)
146 dev->polling = true;
147
148error:
149 device_unlock(&dev->dev);
150 return rc;
151}
152
153/**
154 * nfc_stop_poll - stop polling for nfc targets
155 *
156 * @dev: The nfc device that must stop polling
157 */
158int nfc_stop_poll(struct nfc_dev *dev)
159{
160 int rc = 0;
161
Joe Perches20c239c2011-11-29 11:37:33 -0800162 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300163
164 device_lock(&dev->dev);
165
166 if (!device_is_registered(&dev->dev)) {
167 rc = -ENODEV;
168 goto error;
169 }
170
171 if (!dev->polling) {
172 rc = -EINVAL;
173 goto error;
174 }
175
176 dev->ops->stop_poll(dev);
177 dev->polling = false;
178
179error:
180 device_unlock(&dev->dev);
181 return rc;
182}
183
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100184int nfc_dep_link_up(struct nfc_dev *dev, int target_index,
185 u8 comm_mode, u8 rf_mode)
186{
187 int rc = 0;
188
189 pr_debug("dev_name=%s comm:%d rf:%d\n",
190 dev_name(&dev->dev), comm_mode, rf_mode);
191
192 if (!dev->ops->dep_link_up)
193 return -EOPNOTSUPP;
194
195 device_lock(&dev->dev);
196
197 if (!device_is_registered(&dev->dev)) {
198 rc = -ENODEV;
199 goto error;
200 }
201
202 if (dev->dep_link_up == true) {
203 rc = -EALREADY;
204 goto error;
205 }
206
207 rc = dev->ops->dep_link_up(dev, target_index, comm_mode, rf_mode);
208
209error:
210 device_unlock(&dev->dev);
211 return rc;
212}
213
214int nfc_dep_link_down(struct nfc_dev *dev)
215{
216 int rc = 0;
217
218 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
219
220 if (!dev->ops->dep_link_down)
221 return -EOPNOTSUPP;
222
223 device_lock(&dev->dev);
224
225 if (!device_is_registered(&dev->dev)) {
226 rc = -ENODEV;
227 goto error;
228 }
229
230 if (dev->dep_link_up == false) {
231 rc = -EALREADY;
232 goto error;
233 }
234
235 if (dev->dep_rf_mode == NFC_RF_TARGET) {
236 rc = -EOPNOTSUPP;
237 goto error;
238 }
239
240 rc = dev->ops->dep_link_down(dev);
241 if (!rc) {
242 dev->dep_link_up = false;
243 nfc_genl_dep_link_down_event(dev);
244 }
245
246error:
247 device_unlock(&dev->dev);
248 return rc;
249}
250
251int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
252 u8 comm_mode, u8 rf_mode)
253{
254 dev->dep_link_up = true;
255 dev->dep_rf_mode = rf_mode;
256
257 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
258}
259EXPORT_SYMBOL(nfc_dep_link_is_up);
260
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300261/**
262 * nfc_activate_target - prepare the target for data exchange
263 *
264 * @dev: The nfc device that found the target
265 * @target_idx: index of the target that must be activated
266 * @protocol: nfc protocol that will be used for data exchange
267 */
268int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
269{
270 int rc;
271
Joe Perches20c239c2011-11-29 11:37:33 -0800272 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
273 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300274
275 device_lock(&dev->dev);
276
277 if (!device_is_registered(&dev->dev)) {
278 rc = -ENODEV;
279 goto error;
280 }
281
282 rc = dev->ops->activate_target(dev, target_idx, protocol);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300283 if (!rc)
284 dev->remote_activated = true;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300285
286error:
287 device_unlock(&dev->dev);
288 return rc;
289}
290
291/**
292 * nfc_deactivate_target - deactivate a nfc target
293 *
294 * @dev: The nfc device that found the target
295 * @target_idx: index of the target that must be deactivated
296 */
297int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
298{
299 int rc = 0;
300
Joe Perches20c239c2011-11-29 11:37:33 -0800301 pr_debug("dev_name=%s target_idx=%u\n",
302 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300303
304 device_lock(&dev->dev);
305
306 if (!device_is_registered(&dev->dev)) {
307 rc = -ENODEV;
308 goto error;
309 }
310
311 dev->ops->deactivate_target(dev, target_idx);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300312 dev->remote_activated = false;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300313
314error:
315 device_unlock(&dev->dev);
316 return rc;
317}
318
319/**
320 * nfc_data_exchange - transceive data
321 *
322 * @dev: The nfc device that found the target
323 * @target_idx: index of the target
324 * @skb: data to be sent
325 * @cb: callback called when the response is received
326 * @cb_context: parameter for the callback function
327 *
328 * The user must wait for the callback before calling this function again.
329 */
330int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
331 struct sk_buff *skb,
332 data_exchange_cb_t cb,
333 void *cb_context)
334{
335 int rc;
336
Joe Perches20c239c2011-11-29 11:37:33 -0800337 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
338 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300339
340 device_lock(&dev->dev);
341
342 if (!device_is_registered(&dev->dev)) {
343 rc = -ENODEV;
344 kfree_skb(skb);
345 goto error;
346 }
347
348 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
349
350error:
351 device_unlock(&dev->dev);
352 return rc;
353}
354
355/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100356 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300357 *
358 * @size: size to allocate
359 * @gfp: gfp flags
360 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100361struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
362 unsigned int flags, unsigned int size,
363 unsigned int *err)
364{
365 struct sk_buff *skb;
366 unsigned int total_size;
367
368 total_size = size +
369 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
370
371 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
372 if (skb)
373 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
374
375 return skb;
376}
377
378/**
379 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
380 *
381 * @size: size to allocate
382 * @gfp: gfp flags
383 */
384struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300385{
386 struct sk_buff *skb;
387 unsigned int total_size;
388
389 total_size = size + 1;
390 skb = alloc_skb(total_size, gfp);
391
392 if (skb)
393 skb_reserve(skb, 1);
394
395 return skb;
396}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100397EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300398
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300399/**
400 * nfc_targets_found - inform that targets were found
401 *
402 * @dev: The nfc device that found the targets
403 * @targets: array of nfc targets found
404 * @ntargets: targets array size
405 *
406 * The device driver must call this function when one or many nfc targets
407 * are found. After calling this function, the device driver must stop
408 * polling for targets.
409 */
410int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
411 int n_targets)
412{
413 int i;
414
Joe Perches20c239c2011-11-29 11:37:33 -0800415 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300416
417 dev->polling = false;
418
419 for (i = 0; i < n_targets; i++)
420 targets[i].idx = dev->target_idx++;
421
422 spin_lock_bh(&dev->targets_lock);
423
424 dev->targets_generation++;
425
426 kfree(dev->targets);
427 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
428 GFP_ATOMIC);
429
430 if (!dev->targets) {
431 dev->n_targets = 0;
432 spin_unlock_bh(&dev->targets_lock);
433 return -ENOMEM;
434 }
435
436 dev->n_targets = n_targets;
437 spin_unlock_bh(&dev->targets_lock);
438
439 nfc_genl_targets_found(dev);
440
441 return 0;
442}
443EXPORT_SYMBOL(nfc_targets_found);
444
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300445static void nfc_release(struct device *d)
446{
447 struct nfc_dev *dev = to_nfc_dev(d);
448
Joe Perches20c239c2011-11-29 11:37:33 -0800449 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300450
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300451 nfc_genl_data_exit(&dev->genl_data);
452 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300453 kfree(dev);
454}
455
456struct class nfc_class = {
457 .name = "nfc",
458 .dev_release = nfc_release,
459};
460EXPORT_SYMBOL(nfc_class);
461
462static int match_idx(struct device *d, void *data)
463{
464 struct nfc_dev *dev = to_nfc_dev(d);
465 unsigned *idx = data;
466
467 return dev->idx == *idx;
468}
469
470struct nfc_dev *nfc_get_device(unsigned idx)
471{
472 struct device *d;
473
474 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
475 if (!d)
476 return NULL;
477
478 return to_nfc_dev(d);
479}
480
481/**
482 * nfc_allocate_device - allocate a new nfc device
483 *
484 * @ops: device operations
485 * @supported_protocols: NFC protocols supported by the device
486 */
487struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortize8753042011-08-19 15:47:11 +0200488 u32 supported_protocols,
489 int tx_headroom,
490 int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300491{
492 static atomic_t dev_no = ATOMIC_INIT(0);
493 struct nfc_dev *dev;
494
495 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
496 !ops->deactivate_target || !ops->data_exchange)
497 return NULL;
498
499 if (!supported_protocols)
500 return NULL;
501
502 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
503 if (!dev)
504 return NULL;
505
506 dev->dev.class = &nfc_class;
507 dev->idx = atomic_inc_return(&dev_no) - 1;
508 dev_set_name(&dev->dev, "nfc%d", dev->idx);
509 device_initialize(&dev->dev);
510
511 dev->ops = ops;
512 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200513 dev->tx_headroom = tx_headroom;
514 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300515
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300516 spin_lock_init(&dev->targets_lock);
517 nfc_genl_data_init(&dev->genl_data);
518
519 /* first generation must not be 0 */
520 dev->targets_generation = 1;
521
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300522 return dev;
523}
524EXPORT_SYMBOL(nfc_allocate_device);
525
526/**
527 * nfc_register_device - register a nfc device in the nfc subsystem
528 *
529 * @dev: The nfc device to register
530 */
531int nfc_register_device(struct nfc_dev *dev)
532{
533 int rc;
534
Joe Perches20c239c2011-11-29 11:37:33 -0800535 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300536
537 mutex_lock(&nfc_devlist_mutex);
538 nfc_devlist_generation++;
539 rc = device_add(&dev->dev);
540 mutex_unlock(&nfc_devlist_mutex);
541
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300542 if (rc < 0)
543 return rc;
544
545 rc = nfc_genl_device_added(dev);
546 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800547 pr_debug("The userspace won't be notified that the device %s was added\n",
548 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300549
550 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300551}
552EXPORT_SYMBOL(nfc_register_device);
553
554/**
555 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
556 *
557 * @dev: The nfc device to unregister
558 */
559void nfc_unregister_device(struct nfc_dev *dev)
560{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300561 int rc;
562
Joe Perches20c239c2011-11-29 11:37:33 -0800563 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300564
565 mutex_lock(&nfc_devlist_mutex);
566 nfc_devlist_generation++;
567
568 /* lock to avoid unregistering a device while an operation
569 is in progress */
570 device_lock(&dev->dev);
571 device_del(&dev->dev);
572 device_unlock(&dev->dev);
573
574 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300575
576 rc = nfc_genl_device_removed(dev);
577 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800578 pr_debug("The userspace won't be notified that the device %s was removed\n",
579 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300580
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300581}
582EXPORT_SYMBOL(nfc_unregister_device);
583
584static int __init nfc_init(void)
585{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300586 int rc;
587
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800588 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300589
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300590 rc = class_register(&nfc_class);
591 if (rc)
592 return rc;
593
594 rc = nfc_genl_init();
595 if (rc)
596 goto err_genl;
597
598 /* the first generation must not be 0 */
599 nfc_devlist_generation = 1;
600
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300601 rc = rawsock_init();
602 if (rc)
603 goto err_rawsock;
604
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300605 rc = af_nfc_init();
606 if (rc)
607 goto err_af_nfc;
608
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300609 return 0;
610
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300611err_af_nfc:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300612 rawsock_exit();
613err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300614 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300615err_genl:
616 class_unregister(&nfc_class);
617 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300618}
619
620static void __exit nfc_exit(void)
621{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300622 af_nfc_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300623 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300624 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300625 class_unregister(&nfc_class);
626}
627
628subsys_initcall(nfc_init);
629module_exit(nfc_exit);
630
631MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
632MODULE_DESCRIPTION("NFC Core ver " VERSION);
633MODULE_VERSION(VERSION);
634MODULE_LICENSE("GPL");