blob: c922adb9e651cd35e468b50611f9fd6d5169bf08 [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
Joe Perchesed1e0ad2011-11-29 11:37:32 -080024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
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
38int nfc_printk(const char *level, const char *format, ...)
39{
40 struct va_format vaf;
41 va_list args;
42 int r;
43
44 va_start(args, format);
45
46 vaf.fmt = format;
47 vaf.va = &args;
48
49 r = printk("%sNFC: %pV\n", level, &vaf);
50
51 va_end(args);
52
53 return r;
54}
55EXPORT_SYMBOL(nfc_printk);
56
57/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030058 * nfc_dev_up - turn on the NFC device
59 *
60 * @dev: The nfc device to be turned on
61 *
62 * The device remains up until the nfc_dev_down function is called.
63 */
64int nfc_dev_up(struct nfc_dev *dev)
65{
66 int rc = 0;
67
Joe Perches20c239c2011-11-29 11:37:33 -080068 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030069
70 device_lock(&dev->dev);
71
72 if (!device_is_registered(&dev->dev)) {
73 rc = -ENODEV;
74 goto error;
75 }
76
77 if (dev->dev_up) {
78 rc = -EALREADY;
79 goto error;
80 }
81
82 if (dev->ops->dev_up)
83 rc = dev->ops->dev_up(dev);
84
85 if (!rc)
86 dev->dev_up = true;
87
88error:
89 device_unlock(&dev->dev);
90 return rc;
91}
92
93/**
94 * nfc_dev_down - turn off the NFC device
95 *
96 * @dev: The nfc device to be turned off
97 */
98int nfc_dev_down(struct nfc_dev *dev)
99{
100 int rc = 0;
101
Joe Perches20c239c2011-11-29 11:37:33 -0800102 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300103
104 device_lock(&dev->dev);
105
106 if (!device_is_registered(&dev->dev)) {
107 rc = -ENODEV;
108 goto error;
109 }
110
111 if (!dev->dev_up) {
112 rc = -EALREADY;
113 goto error;
114 }
115
116 if (dev->polling || dev->remote_activated) {
117 rc = -EBUSY;
118 goto error;
119 }
120
121 if (dev->ops->dev_down)
122 dev->ops->dev_down(dev);
123
124 dev->dev_up = false;
125
126error:
127 device_unlock(&dev->dev);
128 return rc;
129}
130
131/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300132 * nfc_start_poll - start polling for nfc targets
133 *
134 * @dev: The nfc device that must start polling
135 * @protocols: bitset of nfc protocols that must be used for polling
136 *
137 * The device remains polling for targets until a target is found or
138 * the nfc_stop_poll function is called.
139 */
140int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
141{
142 int rc;
143
Joe Perches20c239c2011-11-29 11:37:33 -0800144 pr_debug("dev_name=%s protocols=0x%x\n",
145 dev_name(&dev->dev), protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300146
147 if (!protocols)
148 return -EINVAL;
149
150 device_lock(&dev->dev);
151
152 if (!device_is_registered(&dev->dev)) {
153 rc = -ENODEV;
154 goto error;
155 }
156
157 if (dev->polling) {
158 rc = -EBUSY;
159 goto error;
160 }
161
162 rc = dev->ops->start_poll(dev, protocols);
163 if (!rc)
164 dev->polling = true;
165
166error:
167 device_unlock(&dev->dev);
168 return rc;
169}
170
171/**
172 * nfc_stop_poll - stop polling for nfc targets
173 *
174 * @dev: The nfc device that must stop polling
175 */
176int nfc_stop_poll(struct nfc_dev *dev)
177{
178 int rc = 0;
179
Joe Perches20c239c2011-11-29 11:37:33 -0800180 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300181
182 device_lock(&dev->dev);
183
184 if (!device_is_registered(&dev->dev)) {
185 rc = -ENODEV;
186 goto error;
187 }
188
189 if (!dev->polling) {
190 rc = -EINVAL;
191 goto error;
192 }
193
194 dev->ops->stop_poll(dev);
195 dev->polling = false;
196
197error:
198 device_unlock(&dev->dev);
199 return rc;
200}
201
202/**
203 * nfc_activate_target - prepare the target for data exchange
204 *
205 * @dev: The nfc device that found the target
206 * @target_idx: index of the target that must be activated
207 * @protocol: nfc protocol that will be used for data exchange
208 */
209int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
210{
211 int rc;
212
Joe Perches20c239c2011-11-29 11:37:33 -0800213 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
214 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300215
216 device_lock(&dev->dev);
217
218 if (!device_is_registered(&dev->dev)) {
219 rc = -ENODEV;
220 goto error;
221 }
222
223 rc = dev->ops->activate_target(dev, target_idx, protocol);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300224 if (!rc)
225 dev->remote_activated = true;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300226
227error:
228 device_unlock(&dev->dev);
229 return rc;
230}
231
232/**
233 * nfc_deactivate_target - deactivate a nfc target
234 *
235 * @dev: The nfc device that found the target
236 * @target_idx: index of the target that must be deactivated
237 */
238int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
239{
240 int rc = 0;
241
Joe Perches20c239c2011-11-29 11:37:33 -0800242 pr_debug("dev_name=%s target_idx=%u\n",
243 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300244
245 device_lock(&dev->dev);
246
247 if (!device_is_registered(&dev->dev)) {
248 rc = -ENODEV;
249 goto error;
250 }
251
252 dev->ops->deactivate_target(dev, target_idx);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300253 dev->remote_activated = false;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300254
255error:
256 device_unlock(&dev->dev);
257 return rc;
258}
259
260/**
261 * nfc_data_exchange - transceive data
262 *
263 * @dev: The nfc device that found the target
264 * @target_idx: index of the target
265 * @skb: data to be sent
266 * @cb: callback called when the response is received
267 * @cb_context: parameter for the callback function
268 *
269 * The user must wait for the callback before calling this function again.
270 */
271int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
272 struct sk_buff *skb,
273 data_exchange_cb_t cb,
274 void *cb_context)
275{
276 int rc;
277
Joe Perches20c239c2011-11-29 11:37:33 -0800278 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
279 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300280
281 device_lock(&dev->dev);
282
283 if (!device_is_registered(&dev->dev)) {
284 rc = -ENODEV;
285 kfree_skb(skb);
286 goto error;
287 }
288
289 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
290
291error:
292 device_unlock(&dev->dev);
293 return rc;
294}
295
296/**
297 * nfc_alloc_skb - allocate a skb for data exchange responses
298 *
299 * @size: size to allocate
300 * @gfp: gfp flags
301 */
302struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
303{
304 struct sk_buff *skb;
305 unsigned int total_size;
306
307 total_size = size + 1;
308 skb = alloc_skb(total_size, gfp);
309
310 if (skb)
311 skb_reserve(skb, 1);
312
313 return skb;
314}
315EXPORT_SYMBOL(nfc_alloc_skb);
316
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300317/**
318 * nfc_targets_found - inform that targets were found
319 *
320 * @dev: The nfc device that found the targets
321 * @targets: array of nfc targets found
322 * @ntargets: targets array size
323 *
324 * The device driver must call this function when one or many nfc targets
325 * are found. After calling this function, the device driver must stop
326 * polling for targets.
327 */
328int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
329 int n_targets)
330{
331 int i;
332
Joe Perches20c239c2011-11-29 11:37:33 -0800333 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300334
335 dev->polling = false;
336
337 for (i = 0; i < n_targets; i++)
338 targets[i].idx = dev->target_idx++;
339
340 spin_lock_bh(&dev->targets_lock);
341
342 dev->targets_generation++;
343
344 kfree(dev->targets);
345 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
346 GFP_ATOMIC);
347
348 if (!dev->targets) {
349 dev->n_targets = 0;
350 spin_unlock_bh(&dev->targets_lock);
351 return -ENOMEM;
352 }
353
354 dev->n_targets = n_targets;
355 spin_unlock_bh(&dev->targets_lock);
356
357 nfc_genl_targets_found(dev);
358
359 return 0;
360}
361EXPORT_SYMBOL(nfc_targets_found);
362
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300363static void nfc_release(struct device *d)
364{
365 struct nfc_dev *dev = to_nfc_dev(d);
366
Joe Perches20c239c2011-11-29 11:37:33 -0800367 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300368
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300369 nfc_genl_data_exit(&dev->genl_data);
370 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300371 kfree(dev);
372}
373
374struct class nfc_class = {
375 .name = "nfc",
376 .dev_release = nfc_release,
377};
378EXPORT_SYMBOL(nfc_class);
379
380static int match_idx(struct device *d, void *data)
381{
382 struct nfc_dev *dev = to_nfc_dev(d);
383 unsigned *idx = data;
384
385 return dev->idx == *idx;
386}
387
388struct nfc_dev *nfc_get_device(unsigned idx)
389{
390 struct device *d;
391
392 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
393 if (!d)
394 return NULL;
395
396 return to_nfc_dev(d);
397}
398
399/**
400 * nfc_allocate_device - allocate a new nfc device
401 *
402 * @ops: device operations
403 * @supported_protocols: NFC protocols supported by the device
404 */
405struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortize8753042011-08-19 15:47:11 +0200406 u32 supported_protocols,
407 int tx_headroom,
408 int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300409{
410 static atomic_t dev_no = ATOMIC_INIT(0);
411 struct nfc_dev *dev;
412
413 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
414 !ops->deactivate_target || !ops->data_exchange)
415 return NULL;
416
417 if (!supported_protocols)
418 return NULL;
419
420 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
421 if (!dev)
422 return NULL;
423
424 dev->dev.class = &nfc_class;
425 dev->idx = atomic_inc_return(&dev_no) - 1;
426 dev_set_name(&dev->dev, "nfc%d", dev->idx);
427 device_initialize(&dev->dev);
428
429 dev->ops = ops;
430 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200431 dev->tx_headroom = tx_headroom;
432 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300433
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300434 spin_lock_init(&dev->targets_lock);
435 nfc_genl_data_init(&dev->genl_data);
436
437 /* first generation must not be 0 */
438 dev->targets_generation = 1;
439
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300440 return dev;
441}
442EXPORT_SYMBOL(nfc_allocate_device);
443
444/**
445 * nfc_register_device - register a nfc device in the nfc subsystem
446 *
447 * @dev: The nfc device to register
448 */
449int nfc_register_device(struct nfc_dev *dev)
450{
451 int rc;
452
Joe Perches20c239c2011-11-29 11:37:33 -0800453 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300454
455 mutex_lock(&nfc_devlist_mutex);
456 nfc_devlist_generation++;
457 rc = device_add(&dev->dev);
458 mutex_unlock(&nfc_devlist_mutex);
459
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300460 if (rc < 0)
461 return rc;
462
463 rc = nfc_genl_device_added(dev);
464 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800465 pr_debug("The userspace won't be notified that the device %s was added\n",
466 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300467
468 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300469}
470EXPORT_SYMBOL(nfc_register_device);
471
472/**
473 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
474 *
475 * @dev: The nfc device to unregister
476 */
477void nfc_unregister_device(struct nfc_dev *dev)
478{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300479 int rc;
480
Joe Perches20c239c2011-11-29 11:37:33 -0800481 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300482
483 mutex_lock(&nfc_devlist_mutex);
484 nfc_devlist_generation++;
485
486 /* lock to avoid unregistering a device while an operation
487 is in progress */
488 device_lock(&dev->dev);
489 device_del(&dev->dev);
490 device_unlock(&dev->dev);
491
492 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300493
494 rc = nfc_genl_device_removed(dev);
495 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800496 pr_debug("The userspace won't be notified that the device %s was removed\n",
497 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300498
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300499}
500EXPORT_SYMBOL(nfc_unregister_device);
501
502static int __init nfc_init(void)
503{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300504 int rc;
505
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800506 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300507
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300508 rc = class_register(&nfc_class);
509 if (rc)
510 return rc;
511
512 rc = nfc_genl_init();
513 if (rc)
514 goto err_genl;
515
516 /* the first generation must not be 0 */
517 nfc_devlist_generation = 1;
518
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300519 rc = rawsock_init();
520 if (rc)
521 goto err_rawsock;
522
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300523 rc = af_nfc_init();
524 if (rc)
525 goto err_af_nfc;
526
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300527 return 0;
528
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300529err_af_nfc:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300530 rawsock_exit();
531err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300532 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300533err_genl:
534 class_unregister(&nfc_class);
535 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300536}
537
538static void __exit nfc_exit(void)
539{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300540 af_nfc_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300541 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300542 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300543 class_unregister(&nfc_class);
544}
545
546subsys_initcall(nfc_init);
547module_exit(nfc_exit);
548
549MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
550MODULE_DESCRIPTION("NFC Core ver " VERSION);
551MODULE_VERSION(VERSION);
552MODULE_LICENSE("GPL");