blob: 03e45714730bbc544c824f6530cc29d6f71448ea [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
68 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
69
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
102 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
103
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
144 nfc_dbg("dev_name=%s protocols=0x%x", dev_name(&dev->dev), protocols);
145
146 if (!protocols)
147 return -EINVAL;
148
149 device_lock(&dev->dev);
150
151 if (!device_is_registered(&dev->dev)) {
152 rc = -ENODEV;
153 goto error;
154 }
155
156 if (dev->polling) {
157 rc = -EBUSY;
158 goto error;
159 }
160
161 rc = dev->ops->start_poll(dev, protocols);
162 if (!rc)
163 dev->polling = true;
164
165error:
166 device_unlock(&dev->dev);
167 return rc;
168}
169
170/**
171 * nfc_stop_poll - stop polling for nfc targets
172 *
173 * @dev: The nfc device that must stop polling
174 */
175int nfc_stop_poll(struct nfc_dev *dev)
176{
177 int rc = 0;
178
179 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
180
181 device_lock(&dev->dev);
182
183 if (!device_is_registered(&dev->dev)) {
184 rc = -ENODEV;
185 goto error;
186 }
187
188 if (!dev->polling) {
189 rc = -EINVAL;
190 goto error;
191 }
192
193 dev->ops->stop_poll(dev);
194 dev->polling = false;
195
196error:
197 device_unlock(&dev->dev);
198 return rc;
199}
200
201/**
202 * nfc_activate_target - prepare the target for data exchange
203 *
204 * @dev: The nfc device that found the target
205 * @target_idx: index of the target that must be activated
206 * @protocol: nfc protocol that will be used for data exchange
207 */
208int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
209{
210 int rc;
211
212 nfc_dbg("dev_name=%s target_idx=%u protocol=%u", dev_name(&dev->dev),
213 target_idx, protocol);
214
215 device_lock(&dev->dev);
216
217 if (!device_is_registered(&dev->dev)) {
218 rc = -ENODEV;
219 goto error;
220 }
221
222 rc = dev->ops->activate_target(dev, target_idx, protocol);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300223 if (!rc)
224 dev->remote_activated = true;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300225
226error:
227 device_unlock(&dev->dev);
228 return rc;
229}
230
231/**
232 * nfc_deactivate_target - deactivate a nfc target
233 *
234 * @dev: The nfc device that found the target
235 * @target_idx: index of the target that must be deactivated
236 */
237int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
238{
239 int rc = 0;
240
241 nfc_dbg("dev_name=%s target_idx=%u", dev_name(&dev->dev), target_idx);
242
243 device_lock(&dev->dev);
244
245 if (!device_is_registered(&dev->dev)) {
246 rc = -ENODEV;
247 goto error;
248 }
249
250 dev->ops->deactivate_target(dev, target_idx);
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300251 dev->remote_activated = false;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300252
253error:
254 device_unlock(&dev->dev);
255 return rc;
256}
257
258/**
259 * nfc_data_exchange - transceive data
260 *
261 * @dev: The nfc device that found the target
262 * @target_idx: index of the target
263 * @skb: data to be sent
264 * @cb: callback called when the response is received
265 * @cb_context: parameter for the callback function
266 *
267 * The user must wait for the callback before calling this function again.
268 */
269int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx,
270 struct sk_buff *skb,
271 data_exchange_cb_t cb,
272 void *cb_context)
273{
274 int rc;
275
276 nfc_dbg("dev_name=%s target_idx=%u skb->len=%u", dev_name(&dev->dev),
277 target_idx, skb->len);
278
279 device_lock(&dev->dev);
280
281 if (!device_is_registered(&dev->dev)) {
282 rc = -ENODEV;
283 kfree_skb(skb);
284 goto error;
285 }
286
287 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
288
289error:
290 device_unlock(&dev->dev);
291 return rc;
292}
293
294/**
295 * nfc_alloc_skb - allocate a skb for data exchange responses
296 *
297 * @size: size to allocate
298 * @gfp: gfp flags
299 */
300struct sk_buff *nfc_alloc_skb(unsigned int size, gfp_t gfp)
301{
302 struct sk_buff *skb;
303 unsigned int total_size;
304
305 total_size = size + 1;
306 skb = alloc_skb(total_size, gfp);
307
308 if (skb)
309 skb_reserve(skb, 1);
310
311 return skb;
312}
313EXPORT_SYMBOL(nfc_alloc_skb);
314
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300315/**
316 * nfc_targets_found - inform that targets were found
317 *
318 * @dev: The nfc device that found the targets
319 * @targets: array of nfc targets found
320 * @ntargets: targets array size
321 *
322 * The device driver must call this function when one or many nfc targets
323 * are found. After calling this function, the device driver must stop
324 * polling for targets.
325 */
326int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
327 int n_targets)
328{
329 int i;
330
331 nfc_dbg("dev_name=%s n_targets=%d", dev_name(&dev->dev), n_targets);
332
333 dev->polling = false;
334
335 for (i = 0; i < n_targets; i++)
336 targets[i].idx = dev->target_idx++;
337
338 spin_lock_bh(&dev->targets_lock);
339
340 dev->targets_generation++;
341
342 kfree(dev->targets);
343 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
344 GFP_ATOMIC);
345
346 if (!dev->targets) {
347 dev->n_targets = 0;
348 spin_unlock_bh(&dev->targets_lock);
349 return -ENOMEM;
350 }
351
352 dev->n_targets = n_targets;
353 spin_unlock_bh(&dev->targets_lock);
354
355 nfc_genl_targets_found(dev);
356
357 return 0;
358}
359EXPORT_SYMBOL(nfc_targets_found);
360
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300361static void nfc_release(struct device *d)
362{
363 struct nfc_dev *dev = to_nfc_dev(d);
364
365 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
366
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300367 nfc_genl_data_exit(&dev->genl_data);
368 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300369 kfree(dev);
370}
371
372struct class nfc_class = {
373 .name = "nfc",
374 .dev_release = nfc_release,
375};
376EXPORT_SYMBOL(nfc_class);
377
378static int match_idx(struct device *d, void *data)
379{
380 struct nfc_dev *dev = to_nfc_dev(d);
381 unsigned *idx = data;
382
383 return dev->idx == *idx;
384}
385
386struct nfc_dev *nfc_get_device(unsigned idx)
387{
388 struct device *d;
389
390 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
391 if (!d)
392 return NULL;
393
394 return to_nfc_dev(d);
395}
396
397/**
398 * nfc_allocate_device - allocate a new nfc device
399 *
400 * @ops: device operations
401 * @supported_protocols: NFC protocols supported by the device
402 */
403struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortize8753042011-08-19 15:47:11 +0200404 u32 supported_protocols,
405 int tx_headroom,
406 int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300407{
408 static atomic_t dev_no = ATOMIC_INIT(0);
409 struct nfc_dev *dev;
410
411 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
412 !ops->deactivate_target || !ops->data_exchange)
413 return NULL;
414
415 if (!supported_protocols)
416 return NULL;
417
418 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
419 if (!dev)
420 return NULL;
421
422 dev->dev.class = &nfc_class;
423 dev->idx = atomic_inc_return(&dev_no) - 1;
424 dev_set_name(&dev->dev, "nfc%d", dev->idx);
425 device_initialize(&dev->dev);
426
427 dev->ops = ops;
428 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200429 dev->tx_headroom = tx_headroom;
430 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300431
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300432 spin_lock_init(&dev->targets_lock);
433 nfc_genl_data_init(&dev->genl_data);
434
435 /* first generation must not be 0 */
436 dev->targets_generation = 1;
437
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300438 return dev;
439}
440EXPORT_SYMBOL(nfc_allocate_device);
441
442/**
443 * nfc_register_device - register a nfc device in the nfc subsystem
444 *
445 * @dev: The nfc device to register
446 */
447int nfc_register_device(struct nfc_dev *dev)
448{
449 int rc;
450
451 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
452
453 mutex_lock(&nfc_devlist_mutex);
454 nfc_devlist_generation++;
455 rc = device_add(&dev->dev);
456 mutex_unlock(&nfc_devlist_mutex);
457
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300458 if (rc < 0)
459 return rc;
460
461 rc = nfc_genl_device_added(dev);
462 if (rc)
463 nfc_dbg("The userspace won't be notified that the device %s was"
464 " added", dev_name(&dev->dev));
465
466
467 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300468}
469EXPORT_SYMBOL(nfc_register_device);
470
471/**
472 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
473 *
474 * @dev: The nfc device to unregister
475 */
476void nfc_unregister_device(struct nfc_dev *dev)
477{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300478 int rc;
479
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300480 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
481
482 mutex_lock(&nfc_devlist_mutex);
483 nfc_devlist_generation++;
484
485 /* lock to avoid unregistering a device while an operation
486 is in progress */
487 device_lock(&dev->dev);
488 device_del(&dev->dev);
489 device_unlock(&dev->dev);
490
491 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300492
493 rc = nfc_genl_device_removed(dev);
494 if (rc)
495 nfc_dbg("The userspace won't be notified that the device %s"
496 " was removed", dev_name(&dev->dev));
497
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300498}
499EXPORT_SYMBOL(nfc_unregister_device);
500
501static int __init nfc_init(void)
502{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300503 int rc;
504
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800505 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300506
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300507 rc = class_register(&nfc_class);
508 if (rc)
509 return rc;
510
511 rc = nfc_genl_init();
512 if (rc)
513 goto err_genl;
514
515 /* the first generation must not be 0 */
516 nfc_devlist_generation = 1;
517
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300518 rc = rawsock_init();
519 if (rc)
520 goto err_rawsock;
521
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300522 rc = af_nfc_init();
523 if (rc)
524 goto err_af_nfc;
525
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300526 return 0;
527
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300528err_af_nfc:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300529 rawsock_exit();
530err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300531 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300532err_genl:
533 class_unregister(&nfc_class);
534 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300535}
536
537static void __exit nfc_exit(void)
538{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300539 af_nfc_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300540 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300541 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300542 class_unregister(&nfc_class);
543}
544
545subsys_initcall(nfc_init);
546module_exit(nfc_exit);
547
548MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
549MODULE_DESCRIPTION("NFC Core ver " VERSION);
550MODULE_VERSION(VERSION);
551MODULE_LICENSE("GPL");