2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
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.
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.
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.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
35 int nfc_devlist_generation
;
36 DEFINE_MUTEX(nfc_devlist_mutex
);
38 int nfc_printk(const char *level
, const char *format
, ...)
44 va_start(args
, format
);
49 r
= printk("%sNFC: %pV\n", level
, &vaf
);
55 EXPORT_SYMBOL(nfc_printk
);
58 * nfc_dev_up - turn on the NFC device
60 * @dev: The nfc device to be turned on
62 * The device remains up until the nfc_dev_down function is called.
64 int nfc_dev_up(struct nfc_dev
*dev
)
68 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
70 device_lock(&dev
->dev
);
72 if (!device_is_registered(&dev
->dev
)) {
83 rc
= dev
->ops
->dev_up(dev
);
89 device_unlock(&dev
->dev
);
94 * nfc_dev_down - turn off the NFC device
96 * @dev: The nfc device to be turned off
98 int nfc_dev_down(struct nfc_dev
*dev
)
102 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
104 device_lock(&dev
->dev
);
106 if (!device_is_registered(&dev
->dev
)) {
116 if (dev
->polling
|| dev
->remote_activated
) {
121 if (dev
->ops
->dev_down
)
122 dev
->ops
->dev_down(dev
);
127 device_unlock(&dev
->dev
);
132 * nfc_start_poll - start polling for nfc targets
134 * @dev: The nfc device that must start polling
135 * @protocols: bitset of nfc protocols that must be used for polling
137 * The device remains polling for targets until a target is found or
138 * the nfc_stop_poll function is called.
140 int nfc_start_poll(struct nfc_dev
*dev
, u32 protocols
)
144 pr_debug("dev_name=%s protocols=0x%x\n",
145 dev_name(&dev
->dev
), protocols
);
150 device_lock(&dev
->dev
);
152 if (!device_is_registered(&dev
->dev
)) {
162 rc
= dev
->ops
->start_poll(dev
, protocols
);
167 device_unlock(&dev
->dev
);
172 * nfc_stop_poll - stop polling for nfc targets
174 * @dev: The nfc device that must stop polling
176 int nfc_stop_poll(struct nfc_dev
*dev
)
180 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
182 device_lock(&dev
->dev
);
184 if (!device_is_registered(&dev
->dev
)) {
194 dev
->ops
->stop_poll(dev
);
195 dev
->polling
= false;
198 device_unlock(&dev
->dev
);
203 * nfc_activate_target - prepare the target for data exchange
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
209 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
213 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
214 dev_name(&dev
->dev
), target_idx
, protocol
);
216 device_lock(&dev
->dev
);
218 if (!device_is_registered(&dev
->dev
)) {
223 rc
= dev
->ops
->activate_target(dev
, target_idx
, protocol
);
225 dev
->remote_activated
= true;
228 device_unlock(&dev
->dev
);
233 * nfc_deactivate_target - deactivate a nfc target
235 * @dev: The nfc device that found the target
236 * @target_idx: index of the target that must be deactivated
238 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
242 pr_debug("dev_name=%s target_idx=%u\n",
243 dev_name(&dev
->dev
), target_idx
);
245 device_lock(&dev
->dev
);
247 if (!device_is_registered(&dev
->dev
)) {
252 dev
->ops
->deactivate_target(dev
, target_idx
);
253 dev
->remote_activated
= false;
256 device_unlock(&dev
->dev
);
261 * nfc_data_exchange - transceive data
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
269 * The user must wait for the callback before calling this function again.
271 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
,
273 data_exchange_cb_t cb
,
278 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
279 dev_name(&dev
->dev
), target_idx
, skb
->len
);
281 device_lock(&dev
->dev
);
283 if (!device_is_registered(&dev
->dev
)) {
289 rc
= dev
->ops
->data_exchange(dev
, target_idx
, skb
, cb
, cb_context
);
292 device_unlock(&dev
->dev
);
297 * nfc_alloc_skb - allocate a skb for data exchange responses
299 * @size: size to allocate
302 struct sk_buff
*nfc_alloc_skb(unsigned int size
, gfp_t gfp
)
305 unsigned int total_size
;
307 total_size
= size
+ 1;
308 skb
= alloc_skb(total_size
, gfp
);
315 EXPORT_SYMBOL(nfc_alloc_skb
);
318 * nfc_targets_found - inform that targets were found
320 * @dev: The nfc device that found the targets
321 * @targets: array of nfc targets found
322 * @ntargets: targets array size
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.
328 int nfc_targets_found(struct nfc_dev
*dev
, struct nfc_target
*targets
,
333 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
335 dev
->polling
= false;
337 for (i
= 0; i
< n_targets
; i
++)
338 targets
[i
].idx
= dev
->target_idx
++;
340 spin_lock_bh(&dev
->targets_lock
);
342 dev
->targets_generation
++;
345 dev
->targets
= kmemdup(targets
, n_targets
* sizeof(struct nfc_target
),
350 spin_unlock_bh(&dev
->targets_lock
);
354 dev
->n_targets
= n_targets
;
355 spin_unlock_bh(&dev
->targets_lock
);
357 nfc_genl_targets_found(dev
);
361 EXPORT_SYMBOL(nfc_targets_found
);
363 static void nfc_release(struct device
*d
)
365 struct nfc_dev
*dev
= to_nfc_dev(d
);
367 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
369 nfc_genl_data_exit(&dev
->genl_data
);
374 struct class nfc_class
= {
376 .dev_release
= nfc_release
,
378 EXPORT_SYMBOL(nfc_class
);
380 static int match_idx(struct device
*d
, void *data
)
382 struct nfc_dev
*dev
= to_nfc_dev(d
);
383 unsigned *idx
= data
;
385 return dev
->idx
== *idx
;
388 struct nfc_dev
*nfc_get_device(unsigned idx
)
392 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
396 return to_nfc_dev(d
);
400 * nfc_allocate_device - allocate a new nfc device
402 * @ops: device operations
403 * @supported_protocols: NFC protocols supported by the device
405 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
406 u32 supported_protocols
,
410 static atomic_t dev_no
= ATOMIC_INIT(0);
413 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
414 !ops
->deactivate_target
|| !ops
->data_exchange
)
417 if (!supported_protocols
)
420 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
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
);
430 dev
->supported_protocols
= supported_protocols
;
431 dev
->tx_headroom
= tx_headroom
;
432 dev
->tx_tailroom
= tx_tailroom
;
434 spin_lock_init(&dev
->targets_lock
);
435 nfc_genl_data_init(&dev
->genl_data
);
437 /* first generation must not be 0 */
438 dev
->targets_generation
= 1;
442 EXPORT_SYMBOL(nfc_allocate_device
);
445 * nfc_register_device - register a nfc device in the nfc subsystem
447 * @dev: The nfc device to register
449 int nfc_register_device(struct nfc_dev
*dev
)
453 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
455 mutex_lock(&nfc_devlist_mutex
);
456 nfc_devlist_generation
++;
457 rc
= device_add(&dev
->dev
);
458 mutex_unlock(&nfc_devlist_mutex
);
463 rc
= nfc_genl_device_added(dev
);
465 pr_debug("The userspace won't be notified that the device %s was added\n",
466 dev_name(&dev
->dev
));
470 EXPORT_SYMBOL(nfc_register_device
);
473 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
475 * @dev: The nfc device to unregister
477 void nfc_unregister_device(struct nfc_dev
*dev
)
481 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
483 mutex_lock(&nfc_devlist_mutex
);
484 nfc_devlist_generation
++;
486 /* lock to avoid unregistering a device while an operation
488 device_lock(&dev
->dev
);
489 device_del(&dev
->dev
);
490 device_unlock(&dev
->dev
);
492 mutex_unlock(&nfc_devlist_mutex
);
494 rc
= nfc_genl_device_removed(dev
);
496 pr_debug("The userspace won't be notified that the device %s was removed\n",
497 dev_name(&dev
->dev
));
500 EXPORT_SYMBOL(nfc_unregister_device
);
502 static int __init
nfc_init(void)
506 pr_info("NFC Core ver %s\n", VERSION
);
508 rc
= class_register(&nfc_class
);
512 rc
= nfc_genl_init();
516 /* the first generation must not be 0 */
517 nfc_devlist_generation
= 1;
534 class_unregister(&nfc_class
);
538 static void __exit
nfc_exit(void)
543 class_unregister(&nfc_class
);
546 subsys_initcall(nfc_init
);
547 module_exit(nfc_exit
);
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");