nfc: Convert nfc_dbg to pr_debug
[firefly-linux-kernel-4.4.55.git] / net / nfc / core.c
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
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26 #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
35 int nfc_devlist_generation;
36 DEFINE_MUTEX(nfc_devlist_mutex);
37
38 int 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 }
55 EXPORT_SYMBOL(nfc_printk);
56
57 /**
58  * 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  */
64 int nfc_dev_up(struct nfc_dev *dev)
65 {
66         int rc = 0;
67
68         pr_debug("dev_name=%s\n", 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
88 error:
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  */
98 int nfc_dev_down(struct nfc_dev *dev)
99 {
100         int rc = 0;
101
102         pr_debug("dev_name=%s\n", 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
126 error:
127         device_unlock(&dev->dev);
128         return rc;
129 }
130
131 /**
132  * 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  */
140 int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
141 {
142         int rc;
143
144         pr_debug("dev_name=%s protocols=0x%x\n",
145                  dev_name(&dev->dev), protocols);
146
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
166 error:
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  */
176 int nfc_stop_poll(struct nfc_dev *dev)
177 {
178         int rc = 0;
179
180         pr_debug("dev_name=%s\n", dev_name(&dev->dev));
181
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
197 error:
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  */
209 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
210 {
211         int rc;
212
213         pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
214                  dev_name(&dev->dev), target_idx, protocol);
215
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);
224         if (!rc)
225                 dev->remote_activated = true;
226
227 error:
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  */
238 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
239 {
240         int rc = 0;
241
242         pr_debug("dev_name=%s target_idx=%u\n",
243                  dev_name(&dev->dev), target_idx);
244
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);
253         dev->remote_activated = false;
254
255 error:
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  */
271 int 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
278         pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
279                  dev_name(&dev->dev), target_idx, skb->len);
280
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
291 error:
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  */
302 struct 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 }
315 EXPORT_SYMBOL(nfc_alloc_skb);
316
317 /**
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  */
328 int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets,
329                                                         int n_targets)
330 {
331         int i;
332
333         pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
334
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 }
361 EXPORT_SYMBOL(nfc_targets_found);
362
363 static void nfc_release(struct device *d)
364 {
365         struct nfc_dev *dev = to_nfc_dev(d);
366
367         pr_debug("dev_name=%s\n", dev_name(&dev->dev));
368
369         nfc_genl_data_exit(&dev->genl_data);
370         kfree(dev->targets);
371         kfree(dev);
372 }
373
374 struct class nfc_class = {
375         .name = "nfc",
376         .dev_release = nfc_release,
377 };
378 EXPORT_SYMBOL(nfc_class);
379
380 static 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
388 struct 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  */
405 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
406                                         u32 supported_protocols,
407                                         int tx_headroom,
408                                         int tx_tailroom)
409 {
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;
431         dev->tx_headroom = tx_headroom;
432         dev->tx_tailroom = tx_tailroom;
433
434         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
440         return dev;
441 }
442 EXPORT_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  */
449 int nfc_register_device(struct nfc_dev *dev)
450 {
451         int rc;
452
453         pr_debug("dev_name=%s\n", dev_name(&dev->dev));
454
455         mutex_lock(&nfc_devlist_mutex);
456         nfc_devlist_generation++;
457         rc = device_add(&dev->dev);
458         mutex_unlock(&nfc_devlist_mutex);
459
460         if (rc < 0)
461                 return rc;
462
463         rc = nfc_genl_device_added(dev);
464         if (rc)
465                 pr_debug("The userspace won't be notified that the device %s was added\n",
466                          dev_name(&dev->dev));
467
468         return 0;
469 }
470 EXPORT_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  */
477 void nfc_unregister_device(struct nfc_dev *dev)
478 {
479         int rc;
480
481         pr_debug("dev_name=%s\n", dev_name(&dev->dev));
482
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);
493
494         rc = nfc_genl_device_removed(dev);
495         if (rc)
496                 pr_debug("The userspace won't be notified that the device %s was removed\n",
497                          dev_name(&dev->dev));
498
499 }
500 EXPORT_SYMBOL(nfc_unregister_device);
501
502 static int __init nfc_init(void)
503 {
504         int rc;
505
506         pr_info("NFC Core ver %s\n", VERSION);
507
508         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
519         rc = rawsock_init();
520         if (rc)
521                 goto err_rawsock;
522
523         rc = af_nfc_init();
524         if (rc)
525                 goto err_af_nfc;
526
527         return 0;
528
529 err_af_nfc:
530         rawsock_exit();
531 err_rawsock:
532         nfc_genl_exit();
533 err_genl:
534         class_unregister(&nfc_class);
535         return rc;
536 }
537
538 static void __exit nfc_exit(void)
539 {
540         af_nfc_exit();
541         rawsock_exit();
542         nfc_genl_exit();
543         class_unregister(&nfc_class);
544 }
545
546 subsys_initcall(nfc_init);
547 module_exit(nfc_exit);
548
549 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
550 MODULE_DESCRIPTION("NFC Core ver " VERSION);
551 MODULE_VERSION(VERSION);
552 MODULE_LICENSE("GPL");