blob: 2a838b099d82e580a253187030f63a3d756d2915 [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>
30
31#include "nfc.h"
32
33#define VERSION "0.1"
34
35int nfc_devlist_generation;
36DEFINE_MUTEX(nfc_devlist_mutex);
37
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030038/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030039 * nfc_dev_up - turn on the NFC device
40 *
41 * @dev: The nfc device to be turned on
42 *
43 * The device remains up until the nfc_dev_down function is called.
44 */
45int nfc_dev_up(struct nfc_dev *dev)
46{
47 int rc = 0;
48
Joe Perches20c239c2011-11-29 11:37:33 -080049 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030050
51 device_lock(&dev->dev);
52
53 if (!device_is_registered(&dev->dev)) {
54 rc = -ENODEV;
55 goto error;
56 }
57
58 if (dev->dev_up) {
59 rc = -EALREADY;
60 goto error;
61 }
62
63 if (dev->ops->dev_up)
64 rc = dev->ops->dev_up(dev);
65
66 if (!rc)
67 dev->dev_up = true;
68
69error:
70 device_unlock(&dev->dev);
71 return rc;
72}
73
74/**
75 * nfc_dev_down - turn off the NFC device
76 *
77 * @dev: The nfc device to be turned off
78 */
79int nfc_dev_down(struct nfc_dev *dev)
80{
81 int rc = 0;
82
Joe Perches20c239c2011-11-29 11:37:33 -080083 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030084
85 device_lock(&dev->dev);
86
87 if (!device_is_registered(&dev->dev)) {
88 rc = -ENODEV;
89 goto error;
90 }
91
92 if (!dev->dev_up) {
93 rc = -EALREADY;
94 goto error;
95 }
96
97 if (dev->polling || dev->remote_activated) {
98 rc = -EBUSY;
99 goto error;
100 }
101
102 if (dev->ops->dev_down)
103 dev->ops->dev_down(dev);
104
105 dev->dev_up = false;
106
107error:
108 device_unlock(&dev->dev);
109 return rc;
110}
111
112/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300113 * nfc_start_poll - start polling for nfc targets
114 *
115 * @dev: The nfc device that must start polling
116 * @protocols: bitset of nfc protocols that must be used for polling
117 *
118 * The device remains polling for targets until a target is found or
119 * the nfc_stop_poll function is called.
120 */
121int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
122{
123 int rc;
124
Joe Perches20c239c2011-11-29 11:37:33 -0800125 pr_debug("dev_name=%s protocols=0x%x\n",
126 dev_name(&dev->dev), protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300127
128 if (!protocols)
129 return -EINVAL;
130
131 device_lock(&dev->dev);
132
133 if (!device_is_registered(&dev->dev)) {
134 rc = -ENODEV;
135 goto error;
136 }
137
138 if (dev->polling) {
139 rc = -EBUSY;
140 goto error;
141 }
142
143 rc = dev->ops->start_poll(dev, protocols);
144 if (!rc)
145 dev->polling = true;
146
147error:
148 device_unlock(&dev->dev);
149 return rc;
150}
151
152/**
153 * nfc_stop_poll - stop polling for nfc targets
154 *
155 * @dev: The nfc device that must stop polling
156 */
157int nfc_stop_poll(struct nfc_dev *dev)
158{
159 int rc = 0;
160
Joe Perches20c239c2011-11-29 11:37:33 -0800161 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300162
163 device_lock(&dev->dev);
164
165 if (!device_is_registered(&dev->dev)) {
166 rc = -ENODEV;
167 goto error;
168 }
169
170 if (!dev->polling) {
171 rc = -EINVAL;
172 goto error;
173 }
174
175 dev->ops->stop_poll(dev);
176 dev->polling = false;
177
178error:
179 device_unlock(&dev->dev);
180 return rc;
181}
182
183/**
184 * nfc_activate_target - prepare the target for data exchange
185 *
186 * @dev: The nfc device that found the target
187 * @target_idx: index of the target that must be activated
188 * @protocol: nfc protocol that will be used for data exchange
189 */
190int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
191{
192 int rc;
193
Joe Perches20c239c2011-11-29 11:37:33 -0800194 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
195 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300196
197 device_lock(&dev->dev);
198
199 if (!device_is_registered(&dev->dev)) {
200 rc = -ENODEV;
201 goto error;
202 }
203
204 rc = dev->ops->activate_target(dev, target_idx, protocol);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300205 if (!rc)
206 dev->remote_activated = true;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300207
208error:
209 device_unlock(&dev->dev);
210 return rc;
211}
212
213/**
214 * nfc_deactivate_target - deactivate a nfc target
215 *
216 * @dev: The nfc device that found the target
217 * @target_idx: index of the target that must be deactivated
218 */
219int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
220{
221 int rc = 0;
222
Joe Perches20c239c2011-11-29 11:37:33 -0800223 pr_debug("dev_name=%s target_idx=%u\n",
224 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300225
226 device_lock(&dev->dev);
227
228 if (!device_is_registered(&dev->dev)) {
229 rc = -ENODEV;
230 goto error;
231 }
232
233 dev->ops->deactivate_target(dev, target_idx);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300234 dev->remote_activated = false;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300235
236error:
237 device_unlock(&dev->dev);
238 return rc;
239}
240
241/**
242 * nfc_data_exchange - transceive data
243 *
244 * @dev: The nfc device that found the target
245 * @target_idx: index of the target
246 * @skb: data to be sent
247 * @cb: callback called when the response is received
248 * @cb_context: parameter for the callback function
249 *
250 * The user must wait for the callback before calling this function again.
251 */
252int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
253 struct sk_buff *skb,
254 data_exchange_cb_t cb,
255 void *cb_context)
256{
257 int rc;
258
Joe Perches20c239c2011-11-29 11:37:33 -0800259 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
260 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300261
262 device_lock(&dev->dev);
263
264 if (!device_is_registered(&dev->dev)) {
265 rc = -ENODEV;
266 kfree_skb(skb);
267 goto error;
268 }
269
270 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
271
272error:
273 device_unlock(&dev->dev);
274 return rc;
275}
276
277/**
278 * nfc_alloc_skb - allocate a skb for data exchange responses
279 *
280 * @size: size to allocate
281 * @gfp: gfp flags
282 */
283struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
284{
285 struct sk_buff *skb;
286 unsigned int total_size;
287
288 total_size = size + 1;
289 skb = alloc_skb(total_size, gfp);
290
291 if (skb)
292 skb_reserve(skb, 1);
293
294 return skb;
295}
296EXPORT_SYMBOL(nfc_alloc_skb);
297
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300298/**
299 * nfc_targets_found - inform that targets were found
300 *
301 * @dev: The nfc device that found the targets
302 * @targets: array of nfc targets found
303 * @ntargets: targets array size
304 *
305 * The device driver must call this function when one or many nfc targets
306 * are found. After calling this function, the device driver must stop
307 * polling for targets.
308 */
309int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
310 int n_targets)
311{
312 int i;
313
Joe Perches20c239c2011-11-29 11:37:33 -0800314 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300315
316 dev->polling = false;
317
318 for (i = 0; i < n_targets; i++)
319 targets[i].idx = dev->target_idx++;
320
321 spin_lock_bh(&dev->targets_lock);
322
323 dev->targets_generation++;
324
325 kfree(dev->targets);
326 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
327 GFP_ATOMIC);
328
329 if (!dev->targets) {
330 dev->n_targets = 0;
331 spin_unlock_bh(&dev->targets_lock);
332 return -ENOMEM;
333 }
334
335 dev->n_targets = n_targets;
336 spin_unlock_bh(&dev->targets_lock);
337
338 nfc_genl_targets_found(dev);
339
340 return 0;
341}
342EXPORT_SYMBOL(nfc_targets_found);
343
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300344static void nfc_release(struct device *d)
345{
346 struct nfc_dev *dev = to_nfc_dev(d);
347
Joe Perches20c239c2011-11-29 11:37:33 -0800348 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300349
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300350 nfc_genl_data_exit(&dev->genl_data);
351 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300352 kfree(dev);
353}
354
355struct class nfc_class = {
356 .name = "nfc",
357 .dev_release = nfc_release,
358};
359EXPORT_SYMBOL(nfc_class);
360
361static int match_idx(struct device *d, void *data)
362{
363 struct nfc_dev *dev = to_nfc_dev(d);
364 unsigned *idx = data;
365
366 return dev->idx == *idx;
367}
368
369struct nfc_dev *nfc_get_device(unsigned idx)
370{
371 struct device *d;
372
373 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
374 if (!d)
375 return NULL;
376
377 return to_nfc_dev(d);
378}
379
380/**
381 * nfc_allocate_device - allocate a new nfc device
382 *
383 * @ops: device operations
384 * @supported_protocols: NFC protocols supported by the device
385 */
386struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortize8753042011-08-19 15:47:11 +0200387 u32 supported_protocols,
388 int tx_headroom,
389 int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300390{
391 static atomic_t dev_no = ATOMIC_INIT(0);
392 struct nfc_dev *dev;
393
394 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
395 !ops->deactivate_target || !ops->data_exchange)
396 return NULL;
397
398 if (!supported_protocols)
399 return NULL;
400
401 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
402 if (!dev)
403 return NULL;
404
405 dev->dev.class = &nfc_class;
406 dev->idx = atomic_inc_return(&dev_no) - 1;
407 dev_set_name(&dev->dev, "nfc%d", dev->idx);
408 device_initialize(&dev->dev);
409
410 dev->ops = ops;
411 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200412 dev->tx_headroom = tx_headroom;
413 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300414
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300415 spin_lock_init(&dev->targets_lock);
416 nfc_genl_data_init(&dev->genl_data);
417
418 /* first generation must not be 0 */
419 dev->targets_generation = 1;
420
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300421 return dev;
422}
423EXPORT_SYMBOL(nfc_allocate_device);
424
425/**
426 * nfc_register_device - register a nfc device in the nfc subsystem
427 *
428 * @dev: The nfc device to register
429 */
430int nfc_register_device(struct nfc_dev *dev)
431{
432 int rc;
433
Joe Perches20c239c2011-11-29 11:37:33 -0800434 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300435
436 mutex_lock(&nfc_devlist_mutex);
437 nfc_devlist_generation++;
438 rc = device_add(&dev->dev);
439 mutex_unlock(&nfc_devlist_mutex);
440
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300441 if (rc < 0)
442 return rc;
443
444 rc = nfc_genl_device_added(dev);
445 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800446 pr_debug("The userspace won't be notified that the device %s was added\n",
447 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300448
449 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300450}
451EXPORT_SYMBOL(nfc_register_device);
452
453/**
454 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
455 *
456 * @dev: The nfc device to unregister
457 */
458void nfc_unregister_device(struct nfc_dev *dev)
459{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300460 int rc;
461
Joe Perches20c239c2011-11-29 11:37:33 -0800462 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300463
464 mutex_lock(&nfc_devlist_mutex);
465 nfc_devlist_generation++;
466
467 /* lock to avoid unregistering a device while an operation
468 is in progress */
469 device_lock(&dev->dev);
470 device_del(&dev->dev);
471 device_unlock(&dev->dev);
472
473 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300474
475 rc = nfc_genl_device_removed(dev);
476 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800477 pr_debug("The userspace won't be notified that the device %s was removed\n",
478 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300479
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300480}
481EXPORT_SYMBOL(nfc_unregister_device);
482
483static int __init nfc_init(void)
484{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300485 int rc;
486
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800487 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300488
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300489 rc = class_register(&nfc_class);
490 if (rc)
491 return rc;
492
493 rc = nfc_genl_init();
494 if (rc)
495 goto err_genl;
496
497 /* the first generation must not be 0 */
498 nfc_devlist_generation = 1;
499
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300500 rc = rawsock_init();
501 if (rc)
502 goto err_rawsock;
503
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300504 rc = af_nfc_init();
505 if (rc)
506 goto err_af_nfc;
507
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300508 return 0;
509
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300510err_af_nfc:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300511 rawsock_exit();
512err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300513 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300514err_genl:
515 class_unregister(&nfc_class);
516 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300517}
518
519static void __exit nfc_exit(void)
520{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300521 af_nfc_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300522 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300523 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300524 class_unregister(&nfc_class);
525}
526
527subsys_initcall(nfc_init);
528module_exit(nfc_exit);
529
530MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
531MODULE_DESCRIPTION("NFC Core ver " VERSION);
532MODULE_VERSION(VERSION);
533MODULE_LICENSE("GPL");